| Title: | Boosting and Bayesian Additive Trees for Two-Sample Comparison |
| Version: | 0.4.0 |
| Description: | Implements boosting and Bayesian additive tree methods for two-sample comparison through density ratio estimation, following Awaya, Xu, and Ma (2025) <doi:10.48550/arXiv.2508.03059>. The package supports both point estimation and uncertainty quantification via Bayesian additive tree ensembles. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| Depends: | R (≥ 4.0.0) |
| LinkingTo: | Rcpp, RcppArmadillo |
| Imports: | graphics, Rcpp, stats |
| Suggests: | ggplot2, knitr, mvtnorm, rmarkdown, scales |
| SystemRequirements: | C++17 |
| VignetteBuilder: | knitr |
| URL: | https://github.com/nawaya040/BATTS/tree/cran/dev, https://arxiv.org/abs/2508.03059 |
| BugReports: | https://github.com/nawaya040/BATTS/issues |
| NeedsCompilation: | yes |
| Packaged: | 2026-10-28 11:13:06 UTC; naway |
| Author: | Naoki Awaya [aut, cre, cph], Yuliang Xu [aut, cph], Li Ma [aut, cph] |
| Maintainer: | Naoki Awaya <nawaya@waseda.jp> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-13 22:50:20 UTC |
Boosting and Bayesian Additive Trees for Two-Sample Comparison
Description
Estimate the density ratio of two samples using boosting or generalized Bayesian additive trees.
Details
-
batts_boost()fits by boosting, with settings inboost_control(). -
batts_bart()draws from the generalized posterior, with settings inbart_control(). -
predict(),fitted()anddensity_ratio()evaluate a fit; see batts-predict.print(),summary()and plot() describe it.
Author(s)
Maintainer: Naoki Awaya nawaya@waseda.jp [copyright holder]
Authors:
Yuliang Xu [copyright holder]
Li Ma [copyright holder]
References
Awaya, N., Xu, Y., and Ma, L. (2025). Two-sample comparison through additive tree models for density ratios. arXiv:2508.03059. doi:10.48550/arXiv.2508.03059.
See Also
Useful links:
Report bugs at https://github.com/nawaya040/BATTS/issues
Deprecated functions in BATTS
Description
These functions still work and give the same results as before, but warn once per session. Use the replacements below.
Details
| Deprecated | Replacement |
boots() | batts_boost() with boost_control() |
batts() | batts_bart() with bart_control() |
eval_balance_weight() | predict() (type = "weight" for the same scale) or density_ratio()
|
Arguments of boots() and batts() that moved into the control functions:
| Old argument | New setting |
data, group_labels | x, group |
max_resol | depth (boosting) |
n_min_obs_per_node | min_obs_per_node (boosting) |
size_burnin | burnin |
size_backfitting | draws |
omega_prior_parameters | omega_prior |
tree_priors | tree_prior |
output_BART_ensembles | keep_draws
|
n_ratio_per_node accepted only its default and has no replacement. The
batts() arguments max_resol, n_bins, learn_rate, use_gradient
and n_min_obs_per_node have no effect on a BART fit and have no
replacement in bart_control().
eval_balance_weight(fit, points) returns the balancing weight
w = sqrt(p/q); predict(fit, points) returns the log-density ratio
2 * log(w) by default.
Settings for Bayesian additive tree fits
Description
Collect the settings of batts_bart(). The defaults are the settings used
in the simulations of Awaya, Xu, and Ma (2025): 200 trees, 2000 burn-in
iterations, 1000 saved draws, lambda_0 = 5 and margin 0.1. Each fit starts
from unsplit trees.
Usage
bart_control(
num_trees = 200,
burnin = 2000,
draws = 1000,
thin = 1,
prob_moves = c(1/3, 1/3, 1/3),
lambda_0 = 5,
omega_prior = c(1, 1),
tree_prior = c(0.95, 2),
margin_scale = 0.1,
keep_draws = FALSE
)
Arguments
num_trees |
Number of trees in the ensemble. |
burnin |
Number of discarded backfitting iterations. |
draws |
Number of saved posterior draws. |
thin |
Number of backfitting sweeps per iteration, including burn-in iterations. |
prob_moves |
Probabilities of the GROW, PRUNE and CHANGE moves: three positive values summing to one. |
lambda_0 |
Leaf-prior scale per tree; the ensemble scale
|
omega_prior |
Shape and rate of the Gamma prior on the loss temperature. |
tree_prior |
Two tree-depth prior parameters: the split probability at
depth d is |
margin_scale |
Fraction of each training range added at both ends of the fitted domain. |
keep_draws |
Logical; save the posterior forests so that the fit can be
evaluated at new points with |
Value
An object of class bart_control.
See Also
Examples
bart_control(num_trees = 100, burnin = 300, draws = 301, keep_draws = TRUE)
Bayesian additive trees for a two-sample density ratio (deprecated)
Description
Deprecated: use batts_bart() with bart_control(); see
BATTS-deprecated for the argument names. batts() still works and returns
the same result for the same settings, but warns once per session.
Usage
batts(
data,
group_labels,
num_trees = 200,
max_resol = 0,
learn_rate = 0.01,
n_bins = 100,
n_min_obs_per_node = 1,
n_ratio_per_node = 1e-100,
margin_scale = 0.1,
use_gradient = FALSE,
size_burnin = 2000,
size_backfitting = 1000,
thin = 1,
prob_moves = c(1/3, 1/3, 1/3),
lambda_0 = 5,
omega_prior_parameters = c(1, 1),
tree_priors = c(0.95, 2),
output_BART_ensembles = FALSE,
quiet = FALSE
)
Arguments
data |
Numeric matrix with observations in rows and variables in columns.
Values must be finite. With nonnegative |
group_labels |
Numeric vector of zeros and ones, one per row of |
num_trees |
Number of trees in the Bayesian ensemble. |
max_resol |
Maximum boosting tree depth. For |
learn_rate |
Boosting shrinkage factor; use a positive value. |
n_bins |
Number of bins used to construct the boosting cut grid.
For root-initialized |
n_min_obs_per_node |
Minimum count from each group in a boosting child. |
n_ratio_per_node |
Reserved argument. Only the default |
margin_scale |
Nonnegative fraction of each training range added at both ends of the fitted domain. A negative value uses the fixed unit box and requires that the supplied data already lie in that box. |
use_gradient |
Logical; use gradient boosting when TRUE and
forward-stagewise boosting when FALSE. Initialization in |
size_burnin |
Nonnegative integer number of discarded backfitting
iterations (default 2000). |
size_backfitting |
Positive integer number of saved draws (default 1000). |
thin |
Positive integer giving the number of backfitting sweeps per iteration, including burn-in iterations. |
prob_moves |
Probabilities of GROW, PRUNE, and CHANGE, in that order; supply three finite positive values summing to one (tolerance 1e-12). |
lambda_0 |
Positive leaf-prior scale per tree; ensemble scale is
|
omega_prior_parameters |
Shape and rate of the Gamma prior on the loss temperature, as a positive length-two vector. |
tree_priors |
Two tree-depth prior parameters: split probability at
depth d is |
output_BART_ensembles |
Logical; save posterior forests for subsequent evaluation on new points. Training-point draws are returned in either case. |
quiet |
Logical; suppress progress messages. |
Details
Draw generalized Bayesian balancing weights w = sqrt(p/q) by backfitting.
The defaults (200 trees, 2000 burn-in iterations, 1000 saved draws,
lambda_0 = 5 and margin 0.1) are the settings of the paper's simulations.
Each fit starts with unsplit trees (max_resol = 0). Use multiple
chains and convergence diagnostics for substantive applications. A short
successful run does not establish convergence or frequentist coverage.
Sparse overlap and complete separation can produce prior-sensitive
estimates and slow mixing. The returned intervals are pointwise;
zero inclusion does not establish equality of distributions.
Value
An object of class batts_bart, inheriting batts_fit. It is a
list containing balance_weight_BART_data, an observations-by-draws
matrix; forest_list, saved forests when requested; omega_store,
temperature draws; and lambda_store, fixed leaf-scale values. Also includes
tree_list, c, data_info, and Omega for initialization and evaluation.
See Also
boots(), eval_balance_weight(), batts-methods
Examples
set.seed(2)
x <- matrix(c(rnorm(20), rnorm(20, 0.5)), ncol = 1)
g <- rep(0:1, each = 20)
# Small API demonstration; these settings are not a convergence recommendation.
fit <- batts(x, g, num_trees = 4, size_burnin = 5,
size_backfitting = 10, output_BART_ensembles = TRUE, quiet = TRUE)
dim(fit$balance_weight_BART_data)
Methods for BATTS fits
Description
Display and summary methods for fits from batts_boost() and
batts_bart().
Usage
## S3 method for class 'batts_boost'
print(x, ...)
## S3 method for class 'batts_bart'
print(x, ...)
## S3 method for class 'batts_fit'
summary(object, ...)
## S3 method for class 'summary.batts_fit'
print(x, ...)
## S3 method for class 'batts_fit'
nobs(object, ...)
Arguments
x, object |
A |
... |
Not used. |
Value
print returns its argument invisibly. summary returns an object
of class summary.batts_fit. nobs returns the number of training
observations.
Examples
set.seed(1)
fit <- batts_boost(rnorm(50), y = rnorm(50, 0.5),
control = boost_control(num_trees_max = 50, depth = 2),
quiet = TRUE)
fit
summary(fit)
nobs(fit)
Plot a BATTS fit
Description
Diagnostic and summary plots drawn with base graphics.
Usage
## S3 method for class 'batts_boost'
plot(x, which = c("cv", "fit"), ...)
## S3 method for class 'batts_bart'
plot(x, which = c("trace", "fit"), level = 0.95, ...)
Arguments
x |
A |
which |
The plot to draw. For a boosting fit the default is |
... |
Graphical parameters passed to |
level |
Credible level of the band for |
Details
For a boosting fit, which = "cv" plots the cross-validation loss, averaged
over folds, against the number of trees and marks the selected number;
which = "fit" plots the fitted log-density ratio log(p/q) at the
training points. For a Bayesian additive trees fit, which = "trace" plots
the saved draws of the loss temperature, and which = "fit" plots the
posterior mean of log(p/q) at the training points with a pointwise
credible band.
which = "fit" needs a fit with one variable. For more variables, evaluate
the fit with predict() and plot the result.
Plotting draws no random numbers.
Value
x, invisibly.
See Also
batts_boost(), batts_bart(), batts-predict
Examples
set.seed(1)
fit <- batts_boost(rnorm(100), y = rnorm(100, 0.5),
control = boost_control(num_trees_max = 100, depth = 2),
quiet = TRUE)
plot(fit)
plot(fit, which = "fit")
Predict balancing weights and density ratios
Description
Predict balancing weights and density ratios
Usage
## S3 method for class 'batts_boost'
predict(
object,
newdata,
type = c("log_ratio", "ratio", "weight"),
outside = c("error", "na", "clamp"),
...
)
## S3 method for class 'batts_bart'
predict(
object,
newdata,
type = c("log_ratio", "ratio", "weight"),
summary = c("mean", "median", "none"),
level = 0.95,
interval = FALSE,
outside = c("error", "na", "clamp"),
...
)
## S3 method for class 'batts_boost'
fitted(object, type = c("log_ratio", "ratio", "weight"), ...)
## S3 method for class 'batts_bart'
fitted(
object,
type = c("log_ratio", "ratio", "weight"),
summary = c("mean", "median", "none"),
...
)
Arguments
object |
A |
newdata |
Points at which to evaluate the fit: a numeric matrix or data
frame with the training variables, or a numeric vector (several points
for a fit with one variable, otherwise one point). When the fit recorded
column names and |
type |
|
outside |
What to do with points outside the fitted domain (the
training range widened by |
... |
Not used. A |
summary |
For a |
level |
Credible level used when |
interval |
For a |
Details
Prediction draws no random numbers.
Value
A numeric vector, or a matrix when summary = "none" or
interval = TRUE.
Examples
set.seed(1)
x <- rnorm(50)
y <- rnorm(50, 0.5)
fit_boost <- batts_boost(x, y = y,
control = boost_control(num_trees_max = 50, depth = 2),
quiet = TRUE)
predict(fit_boost, c(-1, 0, 1))
predict(fit_boost, c(-1, 0, 1), type = "ratio")
head(fitted(fit_boost))
predict(fit_boost, c(-10, 0), outside = "na")
# Small demonstration; these settings are not a convergence recommendation.
fit_bart <- batts_bart(x, y = y,
control = bart_control(num_trees = 10, burnin = 20,
draws = 21, keep_draws = TRUE),
quiet = TRUE)
predict(fit_bart, c(-1, 0, 1), interval = TRUE)
dim(predict(fit_bart, c(-1, 0, 1), summary = "none"))
Fit a two-sample density ratio by Bayesian additive trees
Description
Draw the balancing weight w = sqrt(p/q) from its generalized posterior,
where p is the density of the first group and q the density of the
second. The log-density ratio is 2 * log(w).
Usage
batts_bart(x, group = NULL, y = NULL, control = bart_control(), quiet = FALSE)
Arguments
x |
Observations in rows and variables in columns: a numeric matrix, a data frame with numeric columns, or a numeric vector for one variable. Values must be finite. |
group |
Two-valued labels, one per row of |
y |
Instead of |
control |
Settings created by |
quiet |
Logical; suppress progress messages. |
Details
Supply exactly one of group and y. Set the seed before
fitting.
Use several chains and convergence diagnostics for substantive applications; a short successful run does not establish convergence or frequentist coverage. Sparse overlap and complete separation can produce prior-sensitive estimates and slow mixing. The intervals are pointwise; that an interval contains zero does not establish equality of the distributions.
Value
An object of class batts_bart, inheriting batts_fit. Besides
the posterior draws at the training points, it records the call, the
settings, the group labels (group_levels, numerator first) and the
column names (variable_names). Use predict(), fitted() and
density_ratio() to evaluate it; evaluating it at new points requires
bart_control(keep_draws = TRUE). Use print(), summary() and
plot() to describe it.
See Also
bart_control(), batts_boost(), batts-predict
Examples
set.seed(2)
# Small demonstration; these settings are not a convergence recommendation.
fit <- batts_bart(rnorm(30), y = rnorm(30, 0.5),
control = bart_control(num_trees = 10, burnin = 20,
draws = 21, keep_draws = TRUE),
quiet = TRUE)
fit
predict(fit, c(-1, 0, 1), interval = TRUE)
Fit a two-sample density ratio by boosting
Description
Fit the balancing weight w = sqrt(p/q) by tree boosting, where p is the
density of the first group and q the density of the second. The
log-density ratio is 2 * log(w).
Usage
batts_boost(
x,
group = NULL,
y = NULL,
control = boost_control(),
quiet = FALSE
)
Arguments
x |
Observations in rows and variables in columns: a numeric matrix, a data frame with numeric columns, or a numeric vector for one variable. Values must be finite. |
group |
Two-valued labels, one per row of |
y |
Instead of |
control |
Settings created by |
quiet |
Logical; suppress progress messages. |
Details
Supply exactly one of group and y. Set the seed before
fitting; cross-validation folds and subsamples use the R random number
generator.
With subsampling, the splits, gradients and leaf values of each tree use its subsample, while the balancing weights of all observations are updated. Cross-validation losses are evaluated on the held-out folds.
Value
An object of class batts_boost, inheriting batts_fit. Besides
the fitted trees, it records the call, the settings, the group labels
(group_levels, numerator first) and the column names
(variable_names). Use predict(), fitted() and density_ratio() to
evaluate it, and print(), summary() and plot() to
describe it.
See Also
boost_control(), batts_bart(), batts-predict
Examples
set.seed(1)
p_sample <- rnorm(50)
q_sample <- rnorm(50, 0.5)
fit <- batts_boost(p_sample, y = q_sample,
control = boost_control(num_trees_max = 50, depth = 2),
quiet = TRUE)
fit
predict(fit, c(-1, 0, 1))
# The same data as one sample with group labels; the first level is p
x <- c(p_sample, q_sample)
g <- factor(rep(c("p", "q"), each = 50))
ctl <- boost_control(num_trees_max = 50, depth = 2)
set.seed(2); a <- batts_boost(x, g, control = ctl, quiet = TRUE)
set.seed(2); b <- batts_boost(p_sample, y = q_sample, control = ctl, quiet = TRUE)
identical(predict(a, 0), predict(b, 0))
Settings for boosting fits
Description
Collect the settings of batts_boost(). The defaults are the settings used
in the simulations of Awaya, Xu, and Ma (2025): gradient boosting with at
most 1000 trees selected by 5-fold cross-validation, depth 4, learning rate
0.01, 32 bins, margin 0.1 and per-tree subsampling of one half.
Usage
boost_control(
num_trees_max = 1000,
K_CV = 5,
depth = 4,
learn_rate = 0.01,
n_bins = 32,
margin_scale = 0.1,
subsample_fraction = 0.5,
min_obs_per_node = 1,
use_gradient = TRUE
)
Arguments
num_trees_max |
Maximum number of boosting trees. |
K_CV |
Number of group-stratified cross-validation folds used to select
the number of trees, or zero to fit |
depth |
Maximum depth of each tree. |
learn_rate |
Shrinkage factor of each boosting step. |
n_bins |
Number of bins used to construct the uniformly spaced candidate cut points, relative to the current node. |
margin_scale |
Fraction of each training range added at both ends of the fitted domain. A negative value uses the unit box and requires the data to lie in it. |
subsample_fraction |
Fraction of the training observations used to
construct each tree, in (0, 1]. Each tree uses
|
min_obs_per_node |
Minimum count from each group in a child node. Splits that would leave fewer are excluded; under complete separation this can prevent informative splits. |
use_gradient |
Logical; gradient boosting when |
Value
An object of class boost_control.
See Also
Examples
boost_control(depth = 2)
Boosting for a two-sample density ratio (deprecated)
Description
Deprecated: use batts_boost() with boost_control(); see
BATTS-deprecated for the argument names. boots() still works and returns
the same result for the same settings, but warns once per session.
Usage
boots(
data,
group_labels,
num_trees_max = 1000,
K_CV = 5,
max_resol = 4,
learn_rate = 0.01,
n_bins = 32,
n_min_obs_per_node = 1,
n_ratio_per_node = 1e-100,
margin_scale = 0.1,
use_gradient = TRUE,
quiet = FALSE,
subsample_fraction = 0.5
)
Arguments
data |
Numeric matrix with observations in rows and variables in columns.
Values must be finite. With nonnegative |
group_labels |
Numeric vector of zeros and ones, one per row of |
num_trees_max |
Maximum number of boosting trees. |
K_CV |
Number of group-stratified cross-validation folds, or zero to
fit |
max_resol |
Maximum boosting tree depth. For |
learn_rate |
Boosting shrinkage factor; use a positive value. |
n_bins |
Number of bins used to construct the boosting cut grid.
For root-initialized |
n_min_obs_per_node |
Minimum count from each group in a boosting child. |
n_ratio_per_node |
Reserved argument. Only the default |
margin_scale |
Nonnegative fraction of each training range added at both ends of the fitted domain. A negative value uses the fixed unit box and requires that the supplied data already lie in that box. |
use_gradient |
Logical; use gradient boosting when TRUE and
forward-stagewise boosting when FALSE. Initialization in |
quiet |
Logical; suppress progress messages. |
subsample_fraction |
Fraction of the training observations used to
construct each tree, in (0, 1]. With the default |
Details
Fit an additive tree estimate of the balancing weight w = sqrt(p/q).
The full log-density ratio is obtained as 2 * log(w).
The defaults (gradient boosting with at most 1000 trees selected by 5-fold CV, depth 4, learning rate 0.01, 32 bins, margin 0.1 and per-tree subsampling of one half) are the settings of the paper's simulations. Set the seed before fitting; CV folds and subsamples use the R random number generator.
Boosting uses uniformly spaced candidate cuts; Bayesian cut proposals
are uniform. Candidate cuts are relative to the current node. Splits requiring
children with too few observations from either group are excluded. Under
complete separation this restriction can prevent informative splits.
Forward-stagewise fits include a multiplicative normalization constant.
With subsampling, splits, gradients, and leaf values use the tree's
subsample, while the balancing weights of all observations are updated and
the forward-stagewise normalization uses the whole training set. CV losses
are evaluated on the held-out folds. Subsampling uses the R random number
generator; subsample_fraction = 1 does not.
Value
An object of class batts_boost, inheriting batts_fit. It is a
list containing balance_weight_boosting_data (training weights),
tree_list (fitted trees), c (normalization), data_info (training
domain and group sizes), and Omega (domain bounds). With CV, also
returns loss_CV_store (folds by candidate tree count).
See Also
batts(), eval_balance_weight(), batts-methods
Examples
set.seed(1)
x <- matrix(c(rnorm(30), rnorm(30, 0.5)), ncol = 1)
g <- rep(0:1, each = 30)
fit <- boots(x, g, num_trees_max = 5, quiet = TRUE)
head(2 * log(fit$balance_weight_boosting_data))
Density ratio of a fitted BATTS model
Description
The density ratio p/q with group 0 in the numerator, that is w^2 for the
fitted balancing weight w. Equivalent to
predict(object, newdata, type = "ratio").
Usage
density_ratio(object, newdata, ...)
## S3 method for class 'batts_fit'
density_ratio(object, newdata, ...)
Arguments
object |
A |
newdata |
Evaluation points; see batts-predict. If omitted, the training observations are used. |
... |
Passed to the |
Value
A numeric vector, or a matrix for a batts_bart fit with
summary = "none" or interval = TRUE.
See Also
Examples
set.seed(1)
fit <- batts_boost(rnorm(50), y = rnorm(50, 0.5),
control = boost_control(num_trees_max = 50, depth = 2),
quiet = TRUE)
density_ratio(fit, c(-1, 0, 1))
Evaluate fitted balancing weights (deprecated)
Description
Deprecated: use predict() (with type = "weight" for the same scale)
or density_ratio(). eval_balance_weight() still works and returns the
same result, but warns once per session.
Usage
eval_balance_weight(list_result, eval_points, is_Bayes = FALSE)
Arguments
list_result |
|
eval_points |
Finite numeric matrix with the same columns, in the same
order, as the training matrix. All points must lie inside |
is_Bayes |
Logical; additionally evaluate posterior forests. Requires
fitting with |
Details
Evaluate stored trees at points inside the fitted domain. Weights have scale
w = sqrt(p/q), with group 0 in the numerator; use 2 * log(w) for log ratios.
Prediction does not draw random numbers. With is_Bayes = TRUE,
the boosting component describes the initialization; the posterior draws
are in balancing_weight_BART. Missing posterior forests cause an error.
Value
A list with balancing_weight_boosting, a one-column numeric matrix,
and, when
is_Bayes = TRUE, balancing_weight_BART, an evaluation-points-by-draws
matrix. Both include the fitted normalization constant.
See Also
boots(), batts(), batts-predict, density_ratio()
Examples
set.seed(3)
x <- matrix(c(rnorm(20), rnorm(20, 0.5)), ncol = 1)
fit <- boots(x, rep(0:1, each = 20), num_trees_max = 5, quiet = TRUE)
pred <- eval_balance_weight(fit, x[1:3, , drop = FALSE])
pred$balancing_weight_boosting