Measure: Measure

View source: R/cotOOP.R

MeasureR Documentation

Measure

Description

Constructor for an R6 Measure object.

Usage

cot_default_unit_id(x, unit.id = NULL)

Measure(
  x,
  weights = NULL,
  probability.measure = TRUE,
  adapt = c("none", "weights", "x"),
  balance.functions = NA_real_,
  target.values = NA_real_,
  unit.id = NULL,
  dtype = NULL,
  device = NULL
)

Arguments

x

The data points

unit.id

Optional unit identifiers. If NULL, non-automatic row names from x are used when available.

weights

The empirical measure. If NULL, assigns equal weight to each observation

probability.measure

Is the empirical measure a probability measure? Default is TRUE.

adapt

Should we try to adapt the data ("x"), the weights ("weights"), or neither ("none"). Default is "none".

balance.functions

A matrix of functions of the covariates to target for mean balance. If NULL and target.values are provided, will use the data in x.

target.values

The targets for the balance functions. Should be the same length as columns in balance.functions.

dtype

The torch_tensor dtype or NULL.

device

The device to have the data on. Should be result of torch::torch_device() or NULL. NULL uses the CPU.

Details

An R6 class for representing empirical measures (data + weights) with optional gradient-based adaptation via torch.

Use Measure() to construct a measure. The returned object supports active bindings like ⁠$weights⁠ and ⁠$x⁠, and methods like ⁠$detach()⁠. See below for defined methods and fields.

Value

Returns a Measure object

Examples

if(torch::torch_is_installed()) {
m <- Measure(x = matrix(0, 10, 2), adapt = "none",
             device = torch::torch_device("cpu"),
             dtype = torch::torch_double())
print(m)
m$x
m$x <- matrix(1,10,2) # must have same dimensions
m$x
m$weights
m$weights <- 1:10/sum(1:10)
m$weights

# with gradients
m <- Measure(x = matrix(0, 10, 2),
             adapt = "weights",
             device = torch::torch_device("cpu"),
             dtype = torch::torch_double())
m$requires_grad # TRUE
m$requires_grad <- "none" # turns off
m$requires_grad # FALSE
m$requires_grad <- "x"
m$requires_grad # TRUE
m <- Measure(matrix(0, 10, 2), adapt = "none",
             device = torch::torch_device("cpu"),
             dtype = torch::torch_double())
m$grad # NULL
m <- Measure(matrix(0, 10, 2), adapt = "weights",
             device = torch::torch_device("cpu"),
             dtype = torch::torch_double())
loss <- sum(m$weights * 1:10)
loss$backward()
m$grad
# note the weights gradient is on the log softmax scale
#and the first parameter is fixed for identifiability
m$grad <- rep(1,9)
m$grad
}

causalOT documentation built on Nov. 24, 2026, 1:06 a.m.