| Title: | R Interface to Python Tools for Computing Higher-Order U-Statistics |
|---|---|
| Description: | Provides an R interface to the Python package 'u-stats' <https://pypi.org/project/u-stats/> for efficient computation of higher-order U-statistics using Einstein summation notation, implementing the methods of Chen, Zhang, and Liu (2025) <doi:10.48550/arXiv.2508.12627>. The package automatically converts R objects to 'NumPy' or 'PyTorch' tensors via 'reticulate' and supports GPU acceleration when 'PyTorch' with 'CUDA' is available. Python dependencies are declared via 'reticulate' and can be installed automatically on first use. Designed for large-scale statistical estimation where numerical stability and performance are critical. |
| Authors: | Xingyu Chen [aut, cre], Ruiqi Zhang [aut], Lin Liu [aut] |
| Maintainer: | Xingyu Chen <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.5 |
| Built: | 2026-06-19 09:26:41 UTC |
| Source: | https://github.com/cxy0714/u-statistics-r |
Reports whether Python and required modules for ustat() are
available, including the detected PyTorch version and whether CUDA
(GPU acceleration) can be used.
check_ustats_setup()check_ustats_setup()
Note that with reticulate (>= 1.41), calling this function may initialize Python and trigger the automatic, one-time provisioning of the declared Python dependencies if no Python environment is configured yet (this can involve a sizeable download the first time).
Invisibly returns TRUE if environment is ready
## Not run: check_ustats_setup() ## End(Not run)## Not run: check_ustats_setup() ## End(Not run)
Installs and configures the Python environment required to run
ustat(), including u_stats, numpy, and
torch.
setup_ustats( method = c("auto", "virtualenv", "conda", "system"), envname = "r-ustats", gpu = FALSE, restart = FALSE, persist = FALSE )setup_ustats( method = c("auto", "virtualenv", "conda", "system"), envname = "r-ustats", gpu = FALSE, restart = FALSE, persist = FALSE )
method |
Installation method for Python:
|
envname |
Name of the virtualenv/conda environment (default: |
gpu |
Logical; if |
restart |
Logical; whether to restart the R session after setup |
persist |
Logical; if |
Most users do not need to call this function. With
reticulate (>= 1.41), the Python dependencies declared by this
package are provisioned automatically in a cached environment the first
time Python is used (e.g. on the first call to ustat()). Call
setup_ustats() only if you prefer a persistent, dedicated
environment, or if you want to control how PyTorch is installed (see
the gpu argument).
Note: PyTorch is strongly recommended. The NumPy backend is slower and may be numerically less stable for higher-order U-statistics.
Invisibly returns TRUE if setup completed and the
environment verifies, FALSE otherwise.
## Not run: setup_ustats() # CPU-only PyTorch (small, default) setup_ustats(gpu = TRUE) # default PyPI PyTorch (CUDA on Linux) setup_ustats(method = "conda", envname = "ustats-env") ## End(Not run)## Not run: setup_ustats() # CPU-only PyTorch (small, default) setup_ustats(gpu = TRUE) # default PyPI PyTorch (CUDA on Linux) setup_ustats(method = "conda", envname = "ustats-env") ## End(Not run)
Computes a higher-order U-statistic from precomputed kernel tensors using
the Python package u_stats. This function serves as an R interface
and handles automatic data conversion via reticulate.
ustat( tensors, expression, backend = c("torch", "numpy"), average = TRUE, dtype = NULL )ustat( tensors, expression, backend = c("torch", "numpy"), average = TRUE, dtype = NULL )
tensors |
A list of numeric vectors, matrices, or arrays representing kernel evaluations. All tensors must have compatible dimensions. |
expression |
Either a character string in Einstein notation or a list of numeric vectors of length 1 or 2 describing index structure. |
backend |
Character string specifying the computation backend:
|
average |
Logical; if |
dtype |
Optional character string specifying numeric precision for
tensors converted from R. Must be one of
|
The U-statistic structure can be specified using either:
An Einstein summation string (e.g. "ab,bc->"), or
A nested list of index vectors (e.g. list(c(1,2), c(2,3)))
This function requires a working Python environment with the
u_stats package installed. With reticulate (>= 1.41) the
required Python packages are provisioned automatically the first time
Python is used, so no manual setup is needed in most cases. To create a
persistent environment instead (or to choose between the CPU-only and
CUDA builds of PyTorch), use setup_ustats(); use
check_ustats_setup() to verify the configuration.
R numeric objects are converted to NumPy arrays using the selected precision. If Python tensors (e.g., Torch tensors) are supplied directly, they are passed through unchanged.
A numeric scalar containing the computed U-statistic.
## Not run: setup_ustats() v1 <- runif(100) H1 <- matrix(runif(100), 10, 10) H2 <- matrix(runif(100), 10, 10) ustat(list(H1, H2), "ab,bc->") ustat(list(H1, H2), "ab,bc->", dtype = "float32") ustat(list(H1, H2), "ab,bc->", dtype = NULL) # auto precision ## End(Not run)## Not run: setup_ustats() v1 <- runif(100) H1 <- matrix(runif(100), 10, 10) H2 <- matrix(runif(100), 10, 10) ustat(list(H1, H2), "ab,bc->") ustat(list(H1, H2), "ab,bc->", dtype = "float32") ustat(list(H1, H2), "ab,bc->", dtype = NULL) # auto precision ## End(Not run)