| OTProblem | R Documentation |
User-facing constructor for an R6 OTProblem object.
OTProblem(measure_1, measure_2, ...)
measure_1 |
An object of class Measure |
measure_2 |
An object of class Measure |
... |
Not used at this time |
An R6 class for creating optimal transport problems with two Measure objects.
Use OTProblem() to construct an object of class
OTProblem. The component objects must be of class Measure.
The process of solving an OT problem involves three steps:
(1) setting up the problem by creating Measure objects and combining them into an OTProblem object,
(2) choosing the hyperparameters for the problem, and
(3) solving the problem by minimizing the objective function.
The first step is done by creating Measure objects and
then combining them into an OTProblem object using
the $add(), $subtract(), $multiply(), and $divide() methods.
The second step is done by calling the $setup_arguments() method on the OTProblem object.
The third step is done by calling the $solve() method on the OTProblem object.
An R6 object of class OTProblem.
devicethe torch::torch_device() of the data.
dtypethe torch::torch_dtype of the data.
selected_deltathe delta value selected after choose_hyperparameters
selected_lambdathe lambda value selected after choose_hyperparameters
polished_weightsFloat64 source weights of the selected fit when
it was polished, otherwise NULL.
lossPrints the current value of the objective. Only available after the solve method has been run
penaltyReturns a list of the lambda and delta penalities that will be iterated through. To set these values, use the setup_arguments function.
OTProblem$add()adds o2 to the OTProblem
OTProblem$add(o2)
o2A number or object of class OTProblem
# example code
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2)
ot2 <- OTProblem(m3, m2)
print(ot1)
print(ot2)
ot1$add(ot2)
print(ot1)
print(ot2)
}
OTProblem$subtract()subtracts o2 from OTProblem
OTProblem$subtract(o2)
o2A number or object of class OTProblem
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2)
ot2 <- OTProblem(m3, m2)
print(ot1)
print(ot2)
ot1$subtract(ot2)
print(ot1)
print(ot2)
}
OTProblem$multiply()multiplies OTProblem by o2
OTProblem$multiply(o2)
o2A number or object of class OTProblem
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2)
ot2 <- OTProblem(m3, m2)
print(ot1)
print(ot2)
ot1$multiply(ot2)
print(ot1)
print(ot2)
}
OTProblem$divide()divides OTProblem by agument o2
OTProblem$divide(o2)
o2A number or object of class OTProblem
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2)
ot2 <- OTProblem(m3, m2)
print(ot1)
print(ot2)
ot1$divide(ot2)
print(ot1)
print(ot2)
}
OTProblem$print()prints the OT problem object
OTProblem$print(...)
...Not used at this time
OTProblem$new()Constructor method
OTProblem$new(measure_1, measure_2)
measure_1An object of class Measure
measure_2An object of class Measure
...Not used at this time
An R6 object of class OTProblem
OTProblem$setup_arguments()Sets up the OT problems for the OTProblem object. This should be run before choose_hyperparameters and solve.
OTProblem$setup_arguments(
lambda,
delta,
grid.length = 7L,
cost.function = NULL,
p = 2,
cost.online = "auto",
debias = TRUE,
diameter = NULL,
ot_niter = 1000L,
ot_tol = 0.001,
polish = c("none", "auto"),
lambda.selection.data = NULL,
lambda.selector.control = list()
)
lambdaNumeric penalty candidates in solver cost units, or
"auto" to select one positive value before fitting using
pooled binary-label prediction. If omitted, a default grid is used.
deltaThe constraint paramters to try for the balance function problems, if any.
grid.lengthThe number of hyperparameters to try if not provided
cost.functionThe cost function for the data. Can be any function that takes arguments x1, x2, p. Defaults to the Euclidean distance.
pThe power to raise the cost matrix by. Default is 2
cost.onlineShould online costs be used? Default is "auto" but "tensorized" stores the cost matrix in memory while "online" will calculate it on the fly.
debiasShould debiased a debiased OTProblem be used? Defaults to TRUE
diameterDiameter of the cost function.
ot_niterNumber of iterations to run the solver
ot_tolSinkhorn tolerance: iterations stop once the largest change
in the potentials is below ot_tol * lambda.
polishPrimal weight refinement, "auto" or "none"; see cotOptions().
lambda.selection.dataOptional list containing pooled covariates x,
binary labels z, and optional unique unit.id. Covariates must have
the solver's feature order and scaling. For a single two-sample term,
labels can instead be inferred from measure membership; absent unit IDs,
this assumes independent samples. In a general OT problem these labels
represent sample membership, not necessarily causal treatment. The
caller chooses the pooling and labels for the intended comparison.
Shared IDs require explicit pooled data, and supplied IDs must be
unique. Identical covariate rows are not deduplicated. Composite
objectives require explicit data. RBF selection requires fixed
supports, uniform original masses, a conditionally negative definite cost, and two
observations per class.
lambda.selector.controlSelection controls; set details = TRUE
to retain the adaptive evaluation trace.
returns the object invisibly
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y, adapt = "weights",
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
ot <- OTProblem(m1, m2)
ot$setup_arguments(lambda = 1000)
}
OTProblem$solve()Solve the OTProblem at each parameter value. Must run setup_arguments first.
OTProblem$solve(
niter = 500L,
tol = 1e-05,
optimizer = c("torch", "frank-wolfe"),
torch_optim = torch::optim_lbfgs,
torch_scheduler = torch::lr_reduce_on_plateau,
torch_args = NULL,
osqp_args = NULL,
quick.balance.function = TRUE
)
niterThe nubmer of iterations to run solver at each combination of hyperparameter values
tolThe tolerance for convergence
optimizerThe optimizer to use. One of "torch" or "frank-wolfe"
torch_optimThe torch_optimizer to use. Default is torch::optim_lbfgs
torch_schedulerThe torch::lr_scheduler to use. Default is torch::lr_reduce_on_plateau
torch_argsArguments passed to the torch optimizer and scheduler
osqp_argsArguments passed to osqp::osqpSettings() if appropriate
quick.balance.functionShould osqp::osqp() be used to select balance function constraints (delta) or not. Default true.
returns the object invisibly
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y, adapt = "weights",
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
ot <- OTProblem(m1, m2)
ot$setup_arguments(lambda = 1000)
ot$solve(niter = 1, torch_optim = torch::optim_rmsprop)
}
OTProblem$rbf_path_eligibility()Internal: inspect numerical eligibility for ATE coordination.
OTProblem$rbf_path_eligibility()
A lambda grid and eligibility vector.
OTProblem$rbf_grid()Internal: the lambda grid the "auto" selector would score.
OTProblem$rbf_grid()
The input lambda grid, or NULL if "auto" cannot score it.
OTProblem$rbf_shared_scores()Internal: share a prepared RBF context and score cache.
OTProblem$rbf_shared_scores()
The prepared context and cache.
OTProblem$choose_hyperparameters()Selects the hyperparameter values through a bootstrap algorithm
OTProblem$choose_hyperparameters(
n_boot_lambda = 100L,
n_boot_delta = 1000L,
lambda_bootstrap = Inf,
lambda.selector = c("auto", "bootstrap"),
lambda.selector.control = list(),
lambda.selection.data = NULL,
.rbf.eligible = NULL,
.rbf.shared = NULL
)
n_boot_lambdaThe number of bootstrap iterations to run when selecting lambda
n_boot_deltaThe number of bootstrap iterations to run when selecting delta
lambda_bootstrapThe penalty parameter to use when selecting lambda. Higher numbers run faster.
lambda.selectorLambda selection rule to apply across the
fitted lambda grid. "auto" (default) directly scores each eligible
finite, positive supplied lambda using centered kernel-ridge
leave-one-out prediction of the pooled binary labels. Ties favor the
larger lambda. When that rule cannot be used for the problem, "auto"
uses "bootstrap", the bootstrap selector of earlier versions.
The kernel is the solver's Gibbs kernel exp(-C/lambda)
(Gaussian RBF for p = 2).
lambda.selector.controlOptional list of controls for the selected lambda selector.
lambda.selection.dataOptional replacement list of pooled x,
binary z, and optional unique unit.id, in the solver's feature
representation. If omitted, reuse the setup snapshot.
.rbf.eligible, .rbf.sharedInternal arguments for coordinating ATE arms.
returns the object invisibly
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y, adapt = "weights",
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
ot <- OTProblem(m1, m2)
ot$setup_arguments(lambda = c(1,1000))
ot$solve(niter = 1, torch_optim = torch::optim_rmsprop)
ot$choose_hyperparameters(n_boot_lambda = 2, n_boot_delta = 10, lambda_bootstrap = 100)
# Fixed-support two-sample membership can be inferred. For overlapping
# supports or composite objectives, supply pooled x, binary z, and
# optional unique unit.id through lambda.selection.data at setup.
x0 <- matrix(c(-2, -1, 0, 1), ncol = 1)
x1 <- matrix(c(0, 1, 2, 3), ncol = 1)
rbf <- OTProblem(Measure(x0, adapt = "weights"), Measure(x1))
rbf$setup_arguments(lambda = c(.1, 1, 10))
rbf$solve(niter = 2)
rbf$choose_hyperparameters(lambda.selector = "auto")
rbf$info()$hyperparam.metrics$lambda.details
}
OTProblem$info()Provides diagnostics after solve and choose_hyperparameter methods have been run.
OTProblem$info()
a list with slots
loss the final loss values
iterations The number of iterations run for each combination of parameters
balance.function.differences The final differences in the balance functions
hyperparam.metrics A list of the bootstrap evaluation for delta and lambda values
polish Per-lambda and per-delta polish status and iterations.
Eligible fixed-point fits report the same refinement or skip statuses
as eligible primal fits; none means polishing was disabled.
converged whether the Frank–Wolfe gap is below atol + tol * D.
reason Per-lambda and per-delta outcome message: ok, niter,
gap above tolerance, gap unavailable, polish error: <msg>, or
error: <msg>.
if (torch::torch_is_installed()) {
ot$info()
}
OTProblem$clone()The objects of this class are cloneable with this method.
OTProblem$clone(deep = FALSE)
deepWhether to make a deep clone.
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y, adapt = "weights",
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
ot2 <- OTProblem(m3, m2,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# you can add or subtract OTProblem objects into
# a new OTProblem
ot <- 0.5 * ot1 + 0.5 * ot2
print(ot)
# Then you choose the hyperparameters
ot$setup_arguments(lambda = 1000)
# then you can solve the objective function
ot$solve(niter = 1, torch_optim = torch::optim_rmsprop)
}
## ------------------------------------------------
## Method `OTProblem$add()`
## ------------------------------------------------
# example code
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2)
ot2 <- OTProblem(m3, m2)
print(ot1)
print(ot2)
ot1$add(ot2)
print(ot1)
print(ot2)
}
## ------------------------------------------------
## Method `OTProblem$subtract()`
## ------------------------------------------------
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2)
ot2 <- OTProblem(m3, m2)
print(ot1)
print(ot2)
ot1$subtract(ot2)
print(ot1)
print(ot2)
}
## ------------------------------------------------
## Method `OTProblem$multiply()`
## ------------------------------------------------
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2)
ot2 <- OTProblem(m3, m2)
print(ot1)
print(ot2)
ot1$multiply(ot2)
print(ot1)
print(ot2)
}
## ------------------------------------------------
## Method `OTProblem$divide()`
## ------------------------------------------------
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
z <- matrix(3,102, 10)
m3 <- Measure(x = z,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
# setup OT problems
ot1 <- OTProblem(m1, m2)
ot2 <- OTProblem(m3, m2)
print(ot1)
print(ot2)
ot1$divide(ot2)
print(ot1)
print(ot2)
}
## ------------------------------------------------
## Method `OTProblem$setup_arguments()`
## ------------------------------------------------
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y, adapt = "weights",
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
ot <- OTProblem(m1, m2)
ot$setup_arguments(lambda = 1000)
}
## ------------------------------------------------
## Method `OTProblem$solve()`
## ------------------------------------------------
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y, adapt = "weights",
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
ot <- OTProblem(m1, m2)
ot$setup_arguments(lambda = 1000)
ot$solve(niter = 1, torch_optim = torch::optim_rmsprop)
}
## ------------------------------------------------
## Method `OTProblem$choose_hyperparameters()`
## ------------------------------------------------
if (torch::torch_is_installed()) {
# setup measures
x <- matrix(1, 100, 10)
m1 <- Measure(x = x,
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
y <- matrix(2, 100, 10)
m2 <- Measure(x = y, adapt = "weights",
device = torch::torch_device("cpu"),
dtype = torch::torch_double())
ot <- OTProblem(m1, m2)
ot$setup_arguments(lambda = c(1,1000))
ot$solve(niter = 1, torch_optim = torch::optim_rmsprop)
ot$choose_hyperparameters(n_boot_lambda = 2, n_boot_delta = 10, lambda_bootstrap = 100)
# Fixed-support two-sample membership can be inferred. For overlapping
# supports or composite objectives, supply pooled x, binary z, and
# optional unique unit.id through lambda.selection.data at setup.
x0 <- matrix(c(-2, -1, 0, 1), ncol = 1)
x1 <- matrix(c(0, 1, 2, 3), ncol = 1)
rbf <- OTProblem(Measure(x0, adapt = "weights"), Measure(x1))
rbf$setup_arguments(lambda = c(.1, 1, 10))
rbf$solve(niter = 2)
rbf$choose_hyperparameters(lambda.selector = "auto")
rbf$info()$hyperparam.metrics$lambda.details
}
## ------------------------------------------------
## Method `OTProblem$info()`
## ------------------------------------------------
if (torch::torch_is_installed()) {
ot$info()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.