| Title: | Binary Expansion Testing |
|---|---|
| Description: | Nonparametric detection of nonuniformity and dependence with Binary Expansion Testing (BET). See Kai Zhang (2019) BET on Independence, Journal of the American Statistical Association, 114:528, 1620-1637, <DOI:10.1080/01621459.2018.1537921>, Kai Zhang, Wan Zhang, Zhigen Zhao, Wen Zhou. (2023). BEAUTY Powered BEAST, <doi:10.48550/arXiv.2103.00674> and Wan Zhang, Zhigen Zhao, Michael Baiocchi, Yao Li, Kai Zhang. (2023) SorBET: A Fast and Powerful Algorithm to Test Dependence of Variables, Technical report. |
| Authors: | Wan Zhang [aut, cre], Zhigen Zhao [aut], Michael Baiocchi [aut], Kai Zhang [aut] |
| Maintainer: | Wan Zhang <[email protected]> |
| License: | GPL |
| Version: | 0.6.0 |
| Built: | 2026-10-04 22:44:32 UTC |
| Source: | https://github.com/cran/BET |
BEAST (Binary Expansion Adaptive Symmetry Test) is used for nonparametric detection of nonuniformity or dependence.
BEAST( X, dep, subsample.percent = 1/2, B = 100, unif.margin = FALSE, lambda = NULL, index = list(c(1:ncol(X))), method = "p", num = NULL )BEAST( X, dep, subsample.percent = 1/2, B = 100, unif.margin = FALSE, lambda = NULL, index = list(c(1:ncol(X))), method = "p", num = NULL )
X |
a matrix to be tested. |
dep |
depth of the binary expansion for the |
subsample.percent |
Fraction of observations in each subsample. The effective size is the positive integer truncation of n * subsample.percent. |
B |
Number of subsamples. |
unif.margin |
logicals. If |
lambda |
tuning parameter for soft-thresholding, default to be |
index |
a list of indices. If provided, test the independence among two or more groups of variables. For example, |
method |
If |
num |
number of permutations if method == "p" (default to be 100), or simulations if method == "s" (supply an explicit positive value). |
Interaction |
the most frequent interaction among all subsamples. |
BEAST.Statistic |
BEAST statistic. |
Null.Distribution |
simulated null distribution. |
p.value |
simulated p-value. |
## Elapsed times 7.32 secs ## Measured in R 4.0.2, 32 bit, on a processor 3.3 GHz 6-Core Intel Core i5 under MacOS, 2024/9/6 ## Not run: x1 = runif(128) x2 = runif(128) y = sin(4*pi*(x1 + x2)) + 0.8*rnorm(128) ##test independence between (x1, x2) and y BEAST(cbind(x1, x2, y), 3, index = list(c(1,2), c(3))) ##test mutual independence among x1, x2 and y BEAST(cbind(x1, x2, y), 3, index = list(1, 2, 3)) ##test bivariate uniformity x1 = rbeta(128, 2, 4) x2 = rbeta(128, 2, 4) BEAST(cbind(x1, x2), 3) ##test multivariate uniformity x1 = rbeta(128, 2, 4) x2 = rbeta(128, 2, 4) x3 = rbeta(128, 2, 4) BEAST(cbind(x1, x2, x3), 3) ## End(Not run)## Elapsed times 7.32 secs ## Measured in R 4.0.2, 32 bit, on a processor 3.3 GHz 6-Core Intel Core i5 under MacOS, 2024/9/6 ## Not run: x1 = runif(128) x2 = runif(128) y = sin(4*pi*(x1 + x2)) + 0.8*rnorm(128) ##test independence between (x1, x2) and y BEAST(cbind(x1, x2, y), 3, index = list(c(1,2), c(3))) ##test mutual independence among x1, x2 and y BEAST(cbind(x1, x2, y), 3, index = list(1, 2, 3)) ##test bivariate uniformity x1 = rbeta(128, 2, 4) x2 = rbeta(128, 2, 4) BEAST(cbind(x1, x2), 3) ##test multivariate uniformity x1 = rbeta(128, 2, 4) x2 = rbeta(128, 2, 4) x3 = rbeta(128, 2, 4) BEAST(cbind(x1, x2, x3), 3) ## End(Not run)
Tests independence between two continuous variables using a stored calibration based on the joint Gaussian limit of full-sample and internally reranked subsample interactions.
BEAST.asymptotic(X, calibration, alpha = 0.05, seed = NULL) ## S3 method for class 'BEASTAsymptoticResult' print(x, ...)BEAST.asymptotic(X, calibration, alpha = 0.05, seed = NULL) ## S3 method for class 'BEASTAsymptoticResult' print(x, ...)
X |
Numeric matrix or all-numeric data frame with exactly two columns, finite observations, no missing values, and nonconstant margins. |
calibration |
An object from |
alpha |
Significance level strictly between zero and one. |
seed |
Optional integer seed in |
x |
A test result. |
... |
Reserved for print-method compatibility. |
The function calls BEAST(method="stat") with the tuning parameters
stored in calibration and compares with its
G0 draws.
The p-value is mean(calibration$G0 >= Scaled.Statistic). The critical
value is sort(calibration$G0)[ceiling((1-alpha)*G)], and rejection uses
Scaled.Statistic > critical.value. At atoms, this strict decision can
differ from comparing the empirical p-value with alpha.
The dataset must have two columns and the calibration's sample size. Marginal ties produce a warning; permutation calibration is recommended for tied data. A nonpositive critical value also requires permutation calibration and produces an error. A nonfinite BEAST statistic produces an error.
The same calibration can be reused across datasets with matching sample size
and tuning parameters, and at different significance levels. See
BEAST.asymptotic.calibrate for its construction.
An object of class BEASTAsymptoticResult, with BEAST.Statistic,
Scaled.Statistic, p.value, critical.value, logical
reject, alpha, Interaction from the BEAST statistic,
calibration configuration/method summary, tie indicator and optional seed.
BEAST.asymptotic.calibrate, BEAST
# Small counts for illustration; use larger H and G for inference. i <- seq_len(32) X1 <- cbind(i/33, i/33 + 0.08*sin(i)) X2 <- cbind(i/33, -i/33 + 0.08*cos(i)) cal <- BEAST.asymptotic.calibrate(32, dep=2, subsample.percent=1/2, B=2, lambda=0, H=12, G=64, seed=1) BEAST.asymptotic(X1, cal, seed=11) BEAST.asymptotic(X2, cal, seed=12)# Small counts for illustration; use larger H and G for inference. i <- seq_len(32) X1 <- cbind(i/33, i/33 + 0.08*sin(i)) X2 <- cbind(i/33, -i/33 + 0.08*cos(i)) cal <- BEAST.asymptotic.calibrate(32, dep=2, subsample.percent=1/2, B=2, lambda=0, H=12, G=64, seed=1) BEAST.asymptotic(X1, cal, seed=11) BEAST.asymptotic(X2, cal, seed=12)
Constructs a reusable joint Gaussian reference for BEAST testing independence between two continuous variables.
BEAST.asymptotic.calibrate(n, dep = 3, subsample.percent = 3/16, B = 128, lambda = NULL, H = 1000, G = 10000, seed = NULL, ncores = 1) ## S3 method for class 'BEASTAsymptoticCalibration' print(x, ...)BEAST.asymptotic.calibrate(n, dep = 3, subsample.percent = 3/16, B = 128, lambda = NULL, H = 1000, G = 10000, seed = NULL, ncores = 1) ## S3 method for class 'BEASTAsymptoticCalibration' print(x, ...)
n |
Integer sample size, at least two. |
dep |
Binary expansion depth, an integer from 1 through 5. |
subsample.percent |
Subsample fraction in |
B |
Positive integer number of subsamples, denoted by |
lambda |
Finite nonnegative soft threshold. |
H |
Number of independent null replications for covariance estimation; integer at least two. |
G |
Number of joint Gaussian draws; integer at least two. |
seed |
Optional integer seed in |
ncores |
Must equal 1 for serial calibration. |
x |
A calibration object. |
... |
Reserved for print-method compatibility. |
Calibration uses all cross interactions.
For each of H independent bivariate normal null samples, it computes
the full-sample interaction means and their average over B size-
subsamples drawn without replacement. Marginal ranks are recomputed within
each subsample. The covariance of this joint vector, scaled by
, is estimated and negative eigenvalues are set to zero.
Joint Gaussian draws are soft-thresholded at .
The BEAST limit map divides the inner product of the thresholded full and
subsample vectors by the norm of the full vector, returning zero when that
norm is zero. The resulting G0 draws are stored for testing at different
significance levels.
Both n and the effective must be divisible by
, with . A calibration can be saved with
saveRDS() and reused for bivariate datasets with the same sample size
and tuning parameters. For multivariate blocks, the covariance also depends
on within-block dependence, so this reusable calibration is bivariate only.
An explicit seed makes calibration reproducible and preserves the
caller's RNG state. With seed=NULL, a master seed is drawn from the
current RNG stream and recorded in the object. Calibration is serial.
An object of class BEASTAsymptoticCalibration, containing n,
dep, subsample.percent, effective r, B, actual
lambda, q, H, G, the covariance Gamma,
pre-clipping Gamma.raw, G0, nonzero fraction, minimum raw
eigenvalue, count of clipped eigenvalues, basis/configuration, RNG metadata,
package version, method identifier and bivariate continuous-margin scope.
# Small counts for illustration; use larger H and G for inference. cal <- BEAST.asymptotic.calibrate(32, dep = 2, subsample.percent = 1/2, B = 2, H = 8, G = 32, seed = 1) cal length(cal$G0)# Small counts for illustration; use larger H and G for inference. cal <- BEAST.asymptotic.calibrate(32, dep = 2, subsample.percent = 1/2, B = 2, H = 8, G = 32, seed = 1) cal length(cal$G0)
The BET package provides functions for nonparametric detection of nonuniformity and dependence with Binary Expansion Testing (BET).
MaxBET
symm
get.signs
cell.counts
bet.plot
MaxBETs
BEAST
Kai Zhang (2019) BET on Independence, Journal of the American Statistical Association, 114:528, 1620-1637, doi:10.1080/01621459.2018.1537921, Kai Zhang, Zhigen Zhao, and Wen Zhou (2021). BEAUTY Powered BEAST, <arXiv:2103.00674> and Wan Zhang, Zhigen Zhao, Michael Baiocchi, Yao Li, Kai Zhang. SorBET: A Fast and Powerful Algorithm to Test Dependence of Variables. Techinical report, 2023.
bet.plot shows the cross interaction of the strongest asymmetry, which the BET returns with the rejection of independence null.
This function only works for the test on two variables, that is, X can only have two columns.
There are nontrivial binary variables in the -field and of them are cross interactions, whose positive regions are in plotted in white and whose negative regions are plotted in blue.
bet.plot shows the cross interaction where the difference of number of observations in the positive and negative region is largest.
## S3 method for class 'plot' bet(X, dep, unif.margin = FALSE, cex=0.5, index = list(c(1:ncol(X))), ...)## S3 method for class 'plot' bet(X, dep, unif.margin = FALSE, cex=0.5, index = list(c(1:ncol(X))), ...)
X |
a matrix with two columns. |
dep |
depth of BET. |
unif.margin |
logicals. If |
cex |
number indicating the amount by which plotting text and symbols should be scaled relative to the default. |
index |
a list of indices. If provided, test the independence among two or more groups of variables. For example, |
... |
graphical parameters to plot |
v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) bet.plot(cbind(X1, X2), 3, index = list(1,2))v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) bet.plot(cbind(X1, X2), 3, index = list(1,2))
cell.counts returns the amount of data points in each cell getting from binary expansion.
cell.counts(X, dep, unif.margin = FALSE)cell.counts(X, dep, unif.margin = FALSE)
X |
a matrix to be tested. |
dep |
depth of the marginal binary expansions. |
unif.margin |
logicals. If |
The result is a dataframe with 2 rows and columns, where is the number of columns of X. The first column is the binary index, the second column is the amount of data points.
v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) cell.counts(cbind(X1, X2), 3)v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) cell.counts(cbind(X1, X2), 3)
get.signs returns all the signs of colors for each point under all interactions up to depth d in marginal binary expansions for the tests BET and BETs.
get.signs(X, dep, unif.margin = FALSE)get.signs(X, dep, unif.margin = FALSE)
X |
a matrix to be tested. |
dep |
depth of the marginal binary expansions. |
unif.margin |
logicals. If |
The result is a dataframe with rows and columns, where is the number of columns of X and is the number of rows of X. The values of or stand for the sign of color, while the marginal interactions return .
v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) get.signs(cbind(X1, X2), 3)v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) get.signs(cbind(X1, X2), 3)
MaxBET stands for Binary Expansion Testing. It is used for nonparametric detection of nonuniformity or dependence. It can be used to test whether a column vector is [0, 1]-uniformly distributed. It can also be used to detect dependence between columns of a matrix X, if X has more than one column.
MaxBET( X, dep, unif.margin = FALSE, asymptotic = TRUE, plot = FALSE, index = list(c(1:ncol(X))) )MaxBET( X, dep, unif.margin = FALSE, asymptotic = TRUE, plot = FALSE, index = list(c(1:ncol(X))) )
X |
a matrix to be tested. When |
dep |
depth of the binary expansion for the |
unif.margin |
logicals. If |
asymptotic |
logicals. If |
plot |
logicals. If |
index |
a list of indices. If provided, test the independence among two or more groups of variables. For example, |
MaxBET tests the independence or uniformity by considering the maximal magnitude of the symmetry statistics in the -field generated from marginal binary expansions at the depth d.
Interaction |
a dataframe with |
Extreme.Asymmetry |
the extreme asymmetry statistics. |
p.value.bonf |
p-value of the test with Bonferroni adjustment. |
z.statistic |
normal approximation of the test statistic. |
##test mutual independence v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) MaxBET(cbind(X1, X2), 3, asymptotic = FALSE, index = list(1,2)) ##test independence between (x1, x2) and y x1 = runif(128) x2 = runif(128) y = sin(4*pi*(x1 + x2)) + 0.4*rnorm(128) MaxBET(cbind(x1, x2, y), 3, index = list(c(1,2), c(3))) ##test uniformity x1 = rbeta(128, 2, 4) x2 = rbeta(128, 2, 4) x3 = rbeta(128, 2, 4) MaxBET(cbind(x1, x2, x3), 3)##test mutual independence v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) MaxBET(cbind(X1, X2), 3, asymptotic = FALSE, index = list(1,2)) ##test independence between (x1, x2) and y x1 = runif(128) x2 = runif(128) y = sin(4*pi*(x1 + x2)) + 0.4*rnorm(128) MaxBET(cbind(x1, x2, y), 3, index = list(c(1,2), c(3))) ##test uniformity x1 = rbeta(128, 2, 4) x2 = rbeta(128, 2, 4) x3 = rbeta(128, 2, 4) MaxBET(cbind(x1, x2, x3), 3)
MaxBETs is used for nonparametric dependence detection.
Extended from BET, for a chosen maximal depth d.max, MaxBETs does a sequential test up to d.max and avoids overlapping symmetry statistics in different depths,
for all . The adjustment is done by multiplying the number of interactions which are in the -field generated by marginal binary expansions at depth but not in that at depth .
MaxBETs( X, d.max = 4, unif.margin = FALSE, asymptotic = TRUE, plot = FALSE, index = list(c(1:ncol(X))) )MaxBETs( X, d.max = 4, unif.margin = FALSE, asymptotic = TRUE, plot = FALSE, index = list(c(1:ncol(X))) )
X |
a matrix to be tested. When |
d.max |
the maximal depth of the binary expansion for |
unif.margin |
logicals. If |
asymptotic |
logicals. If |
plot |
logicals. If |
index |
a list of indices. If provided, test the independence among two or more groups of variables, for example, |
bet.s.pvalue.bonf |
the overall p-value on the test. |
bet.s.index |
the interaction that the p-value is minimal. |
bet.s.zstatistic |
normal approximation of the test statistic. |
##test mutual independence v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) MaxBETs(cbind(X1, X2), 3, asymptotic = FALSE, index = list(1,2)) ##test independence between (x1, x2) and y x1 = runif(128) x2 = runif(128) y = sin(4*pi*(x1 + x2)) + 0.4*rnorm(128) MaxBETs(cbind(x1, x2, y), 3, index = list(c(1,2), c(3))) ##test uniformity x1 = rbeta(128, 2, 4) x2 = rbeta(128, 2, 4) x3 = rbeta(128, 2, 4) MaxBETs(cbind(x1, x2, x3), 3)##test mutual independence v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) MaxBETs(cbind(X1, X2), 3, asymptotic = FALSE, index = list(1,2)) ##test independence between (x1, x2) and y x1 = runif(128) x2 = runif(128) y = sin(4*pi*(x1 + x2)) + 0.4*rnorm(128) MaxBETs(cbind(x1, x2, y), 3, index = list(c(1,2), c(3))) ##test uniformity x1 = rbeta(128, 2, 4) x2 = rbeta(128, 2, 4) x3 = rbeta(128, 2, 4) MaxBETs(cbind(x1, x2, x3), 3)
This data set collects the galactic coordinates of the 256 brightest stars in the night sky (Perryman et al. 1997). We consider the longitude (x) and sine latitude (y) here.
data(star)data(star)
An object of class data.frame with 256 rows and 2 columns.
data(star) MaxBETs(cbind(star$x.raw, star$y.raw), asymptotic = FALSE, plot = TRUE, index = list(1,2))data(star) MaxBETs(cbind(star$x.raw, star$y.raw), asymptotic = FALSE, plot = TRUE, index = list(1,2))
symm returns all the symmetry statistics up to depth d in marginal binary expansions for the tests BET and BETs.
symm( X, dep, unif.margin = FALSE, print.sample.size = TRUE )symm( X, dep, unif.margin = FALSE, print.sample.size = TRUE )
X |
a matrix to be tested. |
dep |
depth of the marginal binary expansions. |
unif.margin |
logicals. If |
print.sample.size |
logicals. If |
The result is a dataframe with columns, where is the number of columns of X. The first column gives the binary index for all variables, the next columns displays all the interactions of respective variables, the last column of Statistics gives the respective symmetry statistic.
v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) symm(cbind(X1, X2), 3)v <- runif(128, -pi, pi) X1 <- cos(v) + 2.5 * rnorm(128, 0, 1/20) X2 <- sin(v) + 2.5 * rnorm(128, 0, 1/20) symm(cbind(X1, X2), 3)