Skip to contents

Accumulates a running mean vector and covariance matrix over one or more chunks of multivariate data, mirroring the C# RunningCovarianceMatrix class. The accumulator state travels in the return value, so a chunked run holds no C++ state.

Usage

running_covariance(x, state = NULL)

Arguments

x

a numeric matrix (or an object coercible to one via as.matrix()), observations in rows and variables in columns.

state

a corehydro_running_covariance object from a previous call, or NULL (the default) to start a fresh accumulator.

Value

a corehydro_running_covariance list with n, mean (length-ncol(x)), and the covariance, sample_covariance, sample_correlation, population_covariance, and population_correlation matrices (each ncol(x) by ncol(x)). covariance is unadjusted by sample size; sample_*/population_* are the N-1- and N-normalized variants. The C# accumulator seeds covariance at the identity matrix before the first push (a stability prior for its other consumer, adaptive MCMC), so every derived matrix carries a small diagonal-only bias that only fades as n grows – do not expect an exact match to stats::cov()/stats::cor() on the same data at small n.

Examples

x <- matrix(c(1, 2, 3, 4, 5, 2, 4, 5, 4, 5), ncol = 2)
running_covariance(x)
#> <corehydro_running_covariance> n = 5, 2 variable(s)