Skip to content

Model does no contain samples when using vb with init function #1163

@haansn08

Description

@haansn08

I'm trying to estimate a large correlation matrix (dimensions about 100x100), which makes Stan fail to find initial values, I think because filling all entries with random values from (-2, 2) doesn't give a valid correlation matrix, even if you try 100 times. My idea for a workaround was to choose the initial values using the R package randcorr. However it seems that vb doesn't like having a custom init function and returns a model without any samples. Here is a minimal example to reproduce the problem:

library(rstan)
library(randcorr)

model_code <- '
data {
 array[100] vector[100] y;
}
parameters {
 cholesky_factor_corr[100] y_corr_chol;
}
model {
 y_corr_chol ~ lkj_corr_cholesky(4);

 vector[100] zero = rep_vector(0, 100);
 y ~ multi_normal_cholesky(zero, y_corr_chol);
}
'

mod <- stan_model(model_code = model_code)

initf <- function() {
	list(
		y_corr_chol = t(chol(randcorr(100)))
	)
}

fit <- vb(
	mod,
	cores=1, chains=1,
	init = initf,
	data = list(
		y = matrix(rnorm(100 * 100), 100, 100)
	)
)
> fit
Stan model 'anon_model' does not contain samples.

> sessionInfo()
R version 4.5.1 (2025-06-13)
Platform: x86_64-pc-linux-gnu
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.12.0
LAPACK: /usr/lib/liblapack.so.3.12.0  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

time zone: Europe/Vienna
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] randcorr_1.0        rstan_2.32.7        StanHeaders_2.32.10

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5         cli_3.6.5           rlang_1.1.6
 [4] processx_3.8.6      RcppParallel_5.1.10 glue_1.8.0
 [7] gridExtra_2.3       ps_1.9.1            pkgbuild_1.4.7
[10] stats4_4.5.1        scales_1.4.0        grid_4.5.1
[13] tibble_3.2.1        lifecycle_1.0.4     QuickJSR_1.7.0
[16] inline_0.3.21       compiler_4.5.1      codetools_0.2-20
[19] RColorBrewer_1.1-3  Rcpp_1.0.14         pkgconfig_2.0.3
[22] farver_2.1.2        R6_2.6.1            pillar_1.10.2
[25] parallel_4.5.1      callr_3.7.6         magrittr_2.0.3
[28] loo_2.8.0           tools_4.5.1         gtable_0.3.6
[31] matrixStats_1.5.0   ggplot2_3.5.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions