Skip to content

Nested array prevent use of initialisation with previous fits #1069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wds15 opened this issue Mar 26, 2025 · 4 comments
Closed

Nested array prevent use of initialisation with previous fits #1069

wds15 opened this issue Mar 26, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@wds15
Copy link

wds15 commented Mar 26, 2025

Describe the bug
I like to initialise a new fit with the results from a previous run which fails for models with nested array structures. The issue is that the nested array structure is not carried forward to the initials correctly.

To Reproduce
Example model (referred to as bernoulli-array.stan):

data {
  int<lower=0> N;
  array[N] int<lower=0,upper=1> y;
}
parameters {
  array[1,1] real<lower=0,upper=1> theta;
}
model {
  theta[1,1] ~ beta(1,1);  // uniform prior on interval 0,1
  y ~ bernoulli(theta[1,1]);
}
ba <- cmdstanr::cmdstan_model("bernoulli-array.stan")

post <- ba$sample(list(N=20, y=rbinom(20, 1, 0.2)), refresh=250, init=0.2, seed = 4679453)

post2 <- ba$sample(list(N=20, y=rbinom(20, 1, 0.2)), refresh=250, init=post, seed = 4679453)

The second fit fails for me with:

post2 <- ba$sample(list(N=20, y=rbinom(20, 1, 0.2)), refresh=250, init=post, seed = 4679453)
> Merging chains in order to subset via 'draw'.
Running MCMC with 4 sequential chains...

Chain 1 Unrecoverable error evaluating the log probability at the initial value.
Chain 1 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=theta; dims declared=(1,1); dims found=() (in '/scratch/weberse2/tmp/RtmpZLNHUy/model-33207a2e2840a8.stan', line 6, column 2 to column 41)
Warning: Chain 1 finished unexpectedly!

Chain 2 Unrecoverable error evaluating the log probability at the initial value.
Chain 2 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=theta; dims declared=(1,1); dims found=() (in '/scratch/weberse2/tmp/RtmpZLNHUy/model-33207a2e2840a8.stan', line 6, column 2 to column 41)
Warning: Chain 2 finished unexpectedly!

Chain 3 Unrecoverable error evaluating the log probability at the initial value.
Chain 3 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=theta; dims declared=(1,1); dims found=() (in '/scratch/weberse2/tmp/RtmpZLNHUy/model-33207a2e2840a8.stan', line 6, column 2 to column 41)
Warning: Chain 3 finished unexpectedly!

Chain 4 Unrecoverable error evaluating the log probability at the initial value.
Chain 4 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=theta; dims declared=(1,1); dims found=() (in '/scratch/weberse2/tmp/RtmpZLNHUy/model-33207a2e2840a8.stan', line 6, column 2 to column 41)
Warning: Chain 4 finished unexpectedly!

Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Warning messages:
1: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
 
2: No chains finished successfully. Unable to retrieve the fit. 

Expected behavior
This should simply work.

Operating system
macOS Sequoia

CmdStanR version number
0.8.1

CmdStan: 2.34.1

Additional context
Add any other context about the problem here.

@wds15 wds15 added the bug Something isn't working label Mar 26, 2025
@jgabry
Copy link
Member

jgabry commented Mar 26, 2025

Have you tried installing the development version from GitHub? I'm pretty sure @SteveBronder already fixed this and I don't get any error running your example with the development version. That fix will be included in the next release, which will have to be soon in order to deal with #1066.

@wds15
Copy link
Author

wds15 commented Mar 28, 2025

Thanks for looking into this and running the example. You are right - with the development version this works just fine.

I need to make it a habit to check with the dev version again. Also releasing more often cmdstanr is something to consider? Obviously that's work, but how about releasing with every other Stan release or so?

Relying on the development version is not really an option for most things I do, which is why more frequent releases are much appreciated.

@wds15 wds15 closed this as completed Mar 28, 2025
@andrjohns
Copy link
Collaborator

Great to hear! We should definitely look at releasing more frequently.

@jgabry I'll cut a new release tomorrow if there aren't any other changes/fixes I'm forgetting about?

@jgabry
Copy link
Member

jgabry commented Mar 28, 2025

@andrjohns Sounds good, thanks! I think everything else (including my PR to remove the deprecations) can wait for the following release (hopefully 1.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants