cov_recursive | R Documentation |
These algorithms are used in kernel_adapt()
to simplify variance-covariance
recalculation at every step of the algorithm.
cov_recursive(
X_t,
Cov_t,
Mean_t_prev,
t.,
Mean_t = NULL,
eps = 0,
Sd = 1,
Ik = diag(ncol(Cov_t))
)
mean_recursive(X_t, Mean_t_prev, t.)
X_t |
Last value of the sample |
Cov_t |
Covariance in t |
t. |
Sample size up to |
Mean_t, Mean_t_prev |
Vectors of averages in time |
Sd, eps, Ik |
See |
The variance covariance algorithm was described in Haario, Saksman and Tamminen (2002).
Haario, H., Saksman, E., & Tamminen, J. (2001). An adaptive Metropolis algorithm. Bernoulli, 7(2), 223–242. https://projecteuclid.org/euclid.bj/1080222083
# Generating random data (only four points to see the difference)
set.seed(1231)
n <- 3
X <- matrix(rnorm(n*4), ncol = 4)
# These two should be equal
mean_recursive(
X_t = X[1,],
Mean_t_prev = colMeans(X[-1,]),
t. = n - 1
)
colMeans(X)
# These two should be equal
cov_recursive(
X_t = X[1, ],
Cov_t = cov(X[-1,]),
Mean_t = colMeans(X),
Mean_t_prev = colMeans(X[-1, ]),
t = n-1
)
cov(X)
# Speed example -------------------------------------------------------------
set.seed(13155511)
X <- matrix(rnorm(1e3*100), ncol = 100)
ans0 <- cov(X[-1,])
t0 <- system.time({
ans1 <- cov(X)
})
t1 <- system.time(ans2 <- cov_recursive(
X[1, ], ans0,
Mean_t = colMeans(X),
Mean_t_prev = colMeans(X[-1,]),
t. = 1e3 - 1
))
# Comparing accuracy and speed
range(ans1 - ans2)
t0/t1
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.