Nothing
#A demonstration of functions from FastGP
library(FastGP)
library(mvtnorm)
library(MASS)
library(microbenchmark)
N <- 100
sigma <- 20 #variance parameter in the covariance function
phi <- 100 #decay parameter for the squared-exponential kernel
x_seq <- seq(N)
Sig <- as.matrix(sigma*exp(-as.matrix(dist(x_seq))^2/phi)) + .001*diag(N) #test covariance function
#compare output of FastGP with base, mvtnorm, MASS
max(abs(solve(Sig)-rcppeigen_invert_matrix(Sig)))
max(abs(solve(Sig)-tinv(Sig)))
abs(sum(log(det(Sig)))-tdet(Sig))
abs(det(Sig)-rcppeigen_get_det(Sig))
abs(rcpp_log_dmvnorm(S=Sig, mu=rep(0,N), x=rep(.1,N),istoep=TRUE)-dmvnorm(rep(.1,N),mean=rep(0,N),sigma = Sig,log=T))
abs(rcpp_log_dmvnorm(S=Sig, mu=rep(0,N), x=rep(.1,N),istoep=FALSE)-dmvnorm(rep(.1,N),mean=rep(0,N),sigma = Sig,log=T))
max(abs(as.matrix(dist(as.matrix(seq(1,N))))-rcpp_distance(matrix(seq(1,N),nrow=N),N,1)))
#some timing comparisons (Toeplitz advantage becomes bigger as N_bench increases)
#multivariate normal sampling
n_draws <- 1
N_bench <- 200
mu <- rep(0,N_bench)
x_seq <- seq(N_bench)
Sig_bench <- as.matrix(sigma*exp(-as.matrix(dist(x_seq))^2/phi)) + .001*diag(N_bench) #test covariance function
bm <- microbenchmark(
FastGP_Toep = FastGP::rcpp_rmvnorm_Toep(n = n_draws, S = Sig_bench, mu = mu),
FastGP = FastGP::rcpp_rmvnorm(n = n_draws, S = Sig_bench, mu = mu),
FastGP_stable = FastGP::rcpp_rmvnorm_stable(n = n_draws, S = Sig_bench, mu = mu),
mvtnorm_chol = mvtnorm::rmvnorm(n = n_draws, mean = mu, sigma = Sig_bench, method = "chol", checkSymmetry = FALSE),
mvtnorm_eigen = mvtnorm::rmvnorm(n = n_draws, mean = mu, sigma = Sig_bench, method = "eigen", checkSymmetry = FALSE),
MASS = MASS::mvrnorm(n = n_draws, mu = mu, Sigma = Sig_bench),
times = 25,
unit = "ms"
)
s <- summary(bm, unit = "ms")
s[order(s$median), ]
#multivariate normal log-density
bm <- microbenchmark(
FastGP_Toep = FastGP::rcpp_log_dmvnorm(S = Sig_bench,mu = mu,x = rep(.1, N_bench),istoep = TRUE),
FastGP = FastGP::rcpp_log_dmvnorm(S = Sig_bench, mu = mu, x = rep(.1, N_bench), istoep = FALSE),
mvtnorm = mvtnorm::dmvnorm(x = rep(.1, N_bench), mean = mu, sigma = Sig_bench, log = TRUE),
times = 25,
unit = "ms"
)
s <- summary(bm, unit = "ms")
s[order(s$median), ]
#test multivariate normal samples for a difficult covariance matrix
x <- seq(0, 0.04, length.out = 15)
Sigma_hard <- exp(-as.matrix(dist(x))^2)
#check condition number
kappa(Sigma_hard)
# mvtnorm: eigendecomposition for covariance matrix
max(abs(cov(rmvnorm(100000, sigma = Sigma_hard, method = "eigen")) - Sigma_hard)/Sigma_hard)
# mvtnorm: singular value decomposition for covariance matrix
max(abs(cov(rmvnorm(100000, sigma = Sigma_hard, method = "svd")) - Sigma_hard)/Sigma_hard)
# mvtnorm: Cholesky decomposition for covariance matrix
max(abs(cov(rmvnorm(100000, sigma = Sigma_hard, method = "chol")) - Sigma_hard)/Sigma_hard)
# MASS
max(abs(cov(mvrnorm(100000, mu = rep(0,15), Sigma = Sigma_hard)) - Sigma_hard)/Sigma_hard)
#rcpp_rmvnorm
max(abs(cov(rcpp_rmvnorm(100000, S = Sigma_hard, mu = rep(0,15))) - Sigma_hard)/Sigma_hard)
#rcpp_rmvnorm_stable
max(abs(cov(rcpp_rmvnorm_stable(10000, S = Sigma_hard,mu = rep(0,15))) - Sigma_hard)/Sigma_hard)
#Now a demo of elliptical slice sampling
#Relevant Parameters:
A <- 1 #amplitude of the sin function used for our signal
T <- 5 #period of the sin function used as our signal
#A function that creates a signal, which can be toggled for multiple shapes.
#In the present iteration we use a sin function with variable period.
signal <- function(t) {
return(A*sin(t/T))
}
#Generate a copy of the signal on the time scale of 1...N
t <- mvrnorm(n = 1, mu = rep(0,N), Sigma = Sig , tol = 1e-6)
s <- signal(seq(1:N)+t)+rnorm(N,sd=sqrt(.001))
#log-lik function
log_lik <- function(w,s){
return (rcpp_log_dmvnorm(S = .001*diag(1,N),mu=signal(seq(1:N)+w),s,istoep=TRUE))
}
mcmc_samples <- ess_Toep(log_lik, s, Sig, 1000, 250, N)
par(mfrow=c(1,1))
plot(colMeans(mcmc_samples), type="l")
points(t, type="l", lwd=3, col="red")
points(colMeans(mcmc_samples) + 2* apply(mcmc_samples, 2, sd), type="l", lty="dashed")
points(colMeans(mcmc_samples) - 2* apply(mcmc_samples, 2, sd), type="l", lty="dashed")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.