stan-dev / stan-dev/rstan

Model does no contain samples when using vb with init function

Open
#1,163 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the RStan vb() entry point and reproduce the supplied minimal example with the custom initf function and 100x100 data. Trace why the returned fit contains no samples; done means the same invocation completes with samples available in fit.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.