This vignette shows how to calculate sectional curvature from an rstan
object for the multivariate \(t\) distribution with 50 degrees of freedom in 100 dimensions.
We need these packages.
library("rstan")
library("curvature")
Stan code for \(t\) distribution.
model_code <- "
data {
int<lower=1> nu;
int<lower=1> K;
vector[K] mu;
cov_matrix[K] Sigma;
}
parameters {
vector[K] y;
}
model {
y ~ multi_student_t(nu, mu, Sigma);
}
"
model = stan_model(model_code = model_code)
Prepare parameters for simulations.
d = 100 # dimension of distribution
mu = rep(0,d) # mean
Sigma = diag(1,d) # covariance
nu = 50 # degrees of freedom
T0 = 1000 # burn-in
T = 100 # after burn-in
noOfSecs = 1000
data = list(nu = nu,
mu = mu,
Sigma = Sigma,
K = d)
fit = sampling(model,
data = data,
chains = 1,
iter = T+T0,
warmup = T0,
algorithm = "HMC",
control = list(metric = "unit_e"))
##
## SAMPLING FOR MODEL 'ef5e7059909d18d29ab9ddcad2812d10' NOW (CHAIN 1).
##
## Gradient evaluation took 0.000111 seconds
## 1000 transitions using 10 leapfrog steps per transition would take 1.11 seconds.
## Adjust your expectations accordingly!
##
##
## Iteration: 1 / 1100 [ 0%] (Warmup)
## Iteration: 110 / 1100 [ 10%] (Warmup)
## Iteration: 220 / 1100 [ 20%] (Warmup)
## Iteration: 330 / 1100 [ 30%] (Warmup)
## Iteration: 440 / 1100 [ 40%] (Warmup)
## Iteration: 550 / 1100 [ 50%] (Warmup)
## Iteration: 660 / 1100 [ 60%] (Warmup)
## Iteration: 770 / 1100 [ 70%] (Warmup)
## Iteration: 880 / 1100 [ 80%] (Warmup)
## Iteration: 990 / 1100 [ 90%] (Warmup)
## Iteration: 1001 / 1100 [ 91%] (Sampling)
## Iteration: 1100 / 1100 [100%] (Sampling)
##
## Elapsed Time: 0.744495 seconds (Warm-up)
## 0.078869 seconds (Sampling)
## 0.823364 seconds (Total)
res = curvature(fit = fit)
## computing gradients will take approx. 3 secs.
## computing Hessians will take approx. 148 secs.
plot(res)
summary(res)
## sectional curvature stats:
## # A tibble: 1 x 5
## mean median sd min max
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.0001536921 0.0001470814 2.604606e-05 9.870614e-05 0.0002172657
sessionInfo()
## R version 3.4.1 (2017-06-30)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Sierra 10.12.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] curvature_0.1.0 rstan_2.16.2 StanHeaders_2.16.0-1
## [4] ggplot2_2.2.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.12 knitr_1.17 magrittr_1.5
## [4] munsell_0.4.3 colorspace_1.3-2 rlang_0.1.2
## [7] stringr_1.2.0 plyr_1.8.4 tools_3.4.1
## [10] parallel_3.4.1 grid_3.4.1 gtable_0.2.0
## [13] htmltools_0.3.6 yaml_2.1.14 lazyeval_0.2.0
## [16] rprojroot_1.2 digest_0.6.12 tibble_1.3.4
## [19] numDeriv_2016.8-1 gridExtra_2.2.1 codetools_0.2-15
## [22] inline_0.3.14 evaluate_0.10.1 rmarkdown_1.6
## [25] labeling_0.3 stringi_1.1.5 compiler_3.4.1
## [28] scales_0.5.0 backports_1.1.0 stats4_3.4.1