stan-dev / stan-dev/rstan

gqs() does not work with transformed parameters

Open
#714 5 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

Summary:

Standalone generated quantities via qgs() don't work with transformed parameters.

Description:

See this discourse topic: Using transformed parameters leads to error Wrong number of parameter values in draws from fitted model. Expecting 2 columns, found 3 columns. which, I guess, comes from stan/src/stan/services/sample/standalone_gqs.hpp.

Reproducible Steps:
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

sc <- "
transformed data{
  vector[10] y = [4.65, 6.02, 4.92, 4.77, 8.12, 6.93, 7.39, 7.6, 5.68, 2.14]';
}
parameters{
  real mu;
  real log_sigma;
}
transformed parameters{
  real<lower=0> sigma = exp(log_sigma);
}
model{
  y ~ normal(mu, sigma);
  mu ~ normal(0, 10);
  log_sigma ~ normal(0, 2.5);
}

"

sm <- stan_model(model_code = sc)

post <- sampling(sm)

sc_gq <- "
parameters{
  real mu;
  real log_sigma;
}
transformed parameters{
  real<lower=0> sigma = exp(log_sigma);
}
generated quantities{
  vector[10] y_rep;
  
  for (i in 1:10)
    y_rep[i] = normal_rng(mu, sigma);
}

"

sm_gq <- stan_model(model_code = sc_gq)

rep <- gqs(sm_gq, draws = as.matrix(post))

rep
Current Output:
Wrong number of parameter values in draws from fitted model.  Expecting 2 columns, found 3 columns.

Inference for Stan model: 9a372375694ef14366f13bf8c7378254.
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[1]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[2]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[3]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[4]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[5]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[6]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[7]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[8]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[9]     0     NaN  0    0   0   0   0     0   NaN  NaN
y_rep[10]    0     NaN  0    0   0   0   0     0   NaN  NaN

Samples were drawn using  at Tue Nov 05 15:31:10 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).
Expected Output:
Inference for Stan model: bdf3c0b7af993ed90b29cc81706a90ea.
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[1]  5.82    0.04 2.16 1.54 4.51 5.82 7.08 10.20  3487    1
y_rep[2]  5.83    0.03 2.14 1.54 4.51 5.85 7.13  9.99  3851    1
y_rep[3]  5.82    0.04 2.16 1.64 4.44 5.83 7.13 10.17  3743    1
y_rep[4]  5.75    0.04 2.19 1.36 4.48 5.75 7.10 10.09  3354    1
y_rep[5]  5.85    0.03 2.14 1.77 4.48 5.81 7.17 10.21  3879    1
y_rep[6]  5.82    0.03 2.14 1.52 4.52 5.83 7.15 10.07  3830    1
y_rep[7]  5.83    0.04 2.17 1.60 4.52 5.82 7.13 10.07  3509    1
y_rep[8]  5.86    0.03 2.08 1.64 4.52 5.85 7.17 10.09  3700    1
y_rep[9]  5.83    0.04 2.13 1.54 4.53 5.82 7.14 10.11  3362    1
y_rep[10] 5.77    0.03 2.11 1.72 4.47 5.76 7.08 10.01  3740    1

Samples were drawn using  at Tue Nov 05 15:34:33 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).
RStan Version:
> packageVersion("rstan")
[1] ‘2.19.9’
R Version:
> R.version.string
[1] "R version 3.6.1 (2019-07-05)"
Operating System:

Win7 Pro SP1

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

Reproduce the supplied R example with transformed parameters and standalone gqs(), then inspect stan/src/stan/services/sample/standalone_gqs.hpp at the linked validation logic. Trace how parameter and transformed-parameter columns are handled; done means the example runs without the column-count error and produces nonzero y_rep output.

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.