dcov_large: Distance covariance with optional blockwise computation

View source: R/dcov_large.R

dcov_largeR Documentation

Distance covariance with optional blockwise computation

Description

Computes distance covariance between x and y. If block_size = NULL, the standard full pairwise-distance computation is used. If block_size is a positive integer, a blockwise algorithm is used that avoids allocating the full n \times n distance matrices.

Usage

dcov_large(x, y, block_size = NULL)

dcov2_large(x, y, block_size = NULL)

Arguments

x

A numeric vector or numeric matrix. Rows are observations.

y

A numeric vector or numeric matrix. Rows are observations.

block_size

Either NULL or one positive integer. If NULL, the standard full computation is used, except in the univariate case where dccpp is used. If positive, the blockwise computation is used with that block size.

Details

dcov_large() returns the distance covariance and thus behaves like dcov. dcov2_large() returns the corresponding squared distance covariance.

Vectors are treated as one-column matrices.

The functions compute the biased V-statistic version of distance covariance based on pairwise Euclidean distances between the rows of x and y.

If block_size = NULL, all pairwise distances are computed in the usual way. In the special case where both x and y are univariate, the optimized dccpp implementation is used instead. This implementation has computational complexity of order O(n \log n), making it preferable to the blockwise approach in the univariate setting.

If block_size is a positive integer, the same quantity is computed block by block. The observations are split into consecutive blocks of size at most block_size, and pairwise distances are accumulated over all block pairs. This reduces memory usage because only distances for one block pair are stored at a time rather than the full distance matrices.

The block size does not need to divide the sample size. If n is not a multiple of block_size, the final block simply contains the remaining observations and is processed in the same way as the other blocks.

The blockwise result is intended to agree with the standard computation up to numerical differences due to floating-point arithmetic. It mainly reduces memory usage; the computational complexity remains of order O(n^2).

Smaller block sizes reduce peak memory usage but may increase computation time due to additional block overhead. Larger block sizes are closer to the standard full computation in memory behavior.

Value

For dcov_large(), a numeric scalar giving the distance covariance.

For dcov2_large(), a numeric scalar giving the squared distance covariance.

References

Székely, G. J., Rizzo, M. L., and Bakirov, N. K. (2007). Measuring and testing dependence by correlation of distances. Annals of Statistics, 35(6), 2769–2794. doi:10.1214/009053607000000505.

Székely, G. J. and Rizzo, M. L. (2009). Brownian distance covariance. Annals of Applied Statistics, 3(4), 1236–1265. doi:10.1214/09-AOAS312.

Chaudhuri, A. and Hu, W. (2019). A fast algorithm for computing distance correlation. Computational Statistics & Data Analysis, 135, 15–24. doi:10.1016/j.csda.2019.01.016.

Examples

n <- 100
x <- matrix(rnorm(n * 3), n, 3)
y <- matrix(rnorm(n * 2), n, 2)

v1 <- dcov2_large(x, y)
v2 <- dcov2_large(x, y, block_size = 32L)

v1
v2
all.equal(v1, v2, tolerance = 1e-10)

d1 <- dcov_large(x, y)
d2 <- dcov_large(x, y, block_size = 32L)

d1
d2
all.equal(d1, d2, tolerance = 1e-10)

# Compare with energy::dcov()
if (requireNamespace("energy", quietly = TRUE)) {
 
 d_energy <- energy::dcov(x, y)
 d_large  <- dcov_large(x, y)
 
 d_energy
 d_large
 
 all.equal(d_energy, d_large, tolerance = 1e-10)
}


dcorBSS documentation built on Nov. 10, 2026, 5:07 p.m.