gqs() can't handle sample from model where params are 0-length containers
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 1.1k
- Forks
- 266
- Avg merge
- 2h 56m
- Merged PRs (30d)
- 1
Description
Summary:
When generating quantities from a model that contains the ? operator in the parameter section, gqs will complain that that given parameter does not exist.
It seems that ? is not implemented in gqs()
Description:
First described here: https://discourse.mc-stan.org/t/generated-quantities-for-0-dim-vectors-matrices/11514/5
Reproducible Steps:
Here’s a minimal example that illustrates my problem (I'm on the rstan development branch):
m <- stan_model(model_code =
data {
int<lower = 0, upper = 1> flag;
}
parameters {
real y;
vector[flag ? 1 : 0] phi;
}
model {
if ( flag == 1 ) {
phi ~ normal(10, 0.1);
y ~ normal(phi, 1);
} else if (flag == 0) {
y ~ normal(0, 1);
}
}')
f <- sampling(m, data = list( flag = 1 ))
f
## 4 chains, each with iter=2000; warmup=1000; thin=1;
## post-warmup draws per chain=1000, total post-warmup draws=4000.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## y 0.02 0.03 1.03 -1.99 -0.68 0.02 0.72 1.98 1175 1
## lp__ -0.53 0.02 0.75 -2.58 -0.68 -0.24 -0.06 0.00 1445 1
##
## Samples were drawn using NUTS(diag_e) at Tue Oct 22 10:11:18 2019.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
So far, so good: When flag = 0, phi is a null-dimensional vector and not included in the output – nor in the as.matrix(f)
gqs part:
First, model is compiled with vector[flag ? 1 : 0] phi; commented out. Flag will be always 0:
mc <-'
data {
int<lower = 0, upper = 1> flag;
}
parameters {
real y;
// vector[flag ? 1 : 0] phi;
}
generated quantities {
real y_rep;
y_rep = normal_rng(y, 1);
}'
m2 <- stan_model(model_code = mc)
f2 <- rstan::gqs(m2, draws = as.matrix(f), data = list( flag = 0))
## Inference for Stan model: 17e2d78e13de006222877a7663a21f94.
## 1 chains, each with iter=4000; warmup=0; thin=1;
## post-warmup draws per chain=4000, total post-warmup draws=4000.
##
## mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
## y_rep 0.02 0.04 1.46 -2.83 -0.96 0.02 1.03 2.87 1641 1
##
## Samples were drawn using at Tue Oct 22 10:11:25 2019.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at
## convergence, Rhat=1).
This works because phi was commented out by hand.
Now, vector[flag ? 1 : 0] phi; is left in model, flag is still flag = 0
mc <-'
data {
int<lower = 0, upper = 1> flag;
}
parameters {
real y;
vector[flag ? 1 : 0] phi;
}
generated quantities {
real y_rep;
y_rep = normal_rng(y, 1);
}
m2 <- stan_model(model_code = mc)`
Problem:
Now let’s call gqs(), with flag = 0
f2 <- rstan::gqs(m2, draws = as.matrix(f), data = list( flag = 0))
This returns:
Exception: Variable phi missing (in 'model49802b31ca9a_ea9a19bd60c8bdb46970b36a506c1c97' at line 7)
The summary indicates that nothing gets evaluated.
Also, it seems that the Exception always shows up when the ? operator is used, such as in
vector[flag ? 1 : 0] phi;
RStan Version:
rstan from development branch, version 2.19.9
R Version:
R version 3.6.1 (2019-07-05)
Operating System:
Ubuntu 18.04.3 LTS
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the failure through rstan::gqs() with the provided Stan model containing vector[flag ? 1 : 0] phi and flag = 0. Compare this with the commented-out-parameter case and trace how gqs() supplies draws for zero-length containers. Done means generated quantities run without the “Variable phi missing” exception while preserving the existing output behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100