Specifying prior parameter in data block not possible

Hello,

I specified in the data block the mean for the prior distribution of the likelihood parameter. I expected this to work since it is only a real number. When running the model I get the message: “Warning no priors for parameter mu specified”. However, when specifying the sigma for the likelihood distribution in the data block this is not an issue.


data{
 real x;
 real y; 
 real mu_mean;
 real sigma;
}
parameters{
real mu; 
}
model {
// priors
 mu ~ normal(mu_mean, 1);
// likelihood
 y ~ normal(mu,  sigma); 
} 

Is there a particular reason why this is not an option in Stan or how to solve this?

Best,
María

what interface are you using, and what version of Stan?

I did a cut-and-paste on the above program into file foo.stan and then compiled and ran it on CmdStan 2.28.1 with the following data file:

{
    "x" : 1.0,
    "y" : 7.0,
    "mu_mean" : 5.0,
    "sigma" : 2.0
}

it all just worked.

~/.cmdstan/cmdstan-2.28.1> ./foo sample data file=foo.data.json
method = sample (Default)
  sample
    num_samples = 1000 (Default)
    num_warmup = 1000 (Default)
    save_warmup = 0 (Default)
    thin = 1 (Default)
    adapt
      engaged = 1 (Default)
      gamma = 0.050000000000000003 (Default)
      delta = 0.80000000000000004 (Default)
      kappa = 0.75 (Default)
      t0 = 10 (Default)
      init_buffer = 75 (Default)
      term_buffer = 50 (Default)
      window = 25 (Default)
    algorithm = hmc (Default)
      hmc
        engine = nuts (Default)
          nuts
            max_depth = 10 (Default)
        metric = diag_e (Default)
        metric_file =  (Default)
        stepsize = 1 (Default)
        stepsize_jitter = 0 (Default)
    num_chains = 1 (Default)
id = 1 (Default)
data
  file = foo.data.json
init = 2 (Default)
random
  seed = 1686177967 (Default)
output
  file = output.csv (Default)
  diagnostic_file =  (Default)
  refresh = 100 (Default)
  sig_figs = -1 (Default)
  profile_file = profile.csv (Default)
num_threads = 1 (Default)


Gradient evaluation took 8e-06 seconds
1000 transitions using 10 leapfrog steps per transition would take 0.08 seconds.
Adjust your expectations accordingly!


Iteration:    1 / 2000 [  0%]  (Warmup)
Iteration:  100 / 2000 [  5%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  300 / 2000 [ 15%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  500 / 2000 [ 25%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  700 / 2000 [ 35%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration:  900 / 2000 [ 45%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1100 / 2000 [ 55%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1300 / 2000 [ 65%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1500 / 2000 [ 75%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1700 / 2000 [ 85%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 1900 / 2000 [ 95%]  (Sampling)
Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 0.004 seconds (Warm-up)
               0.011 seconds (Sampling)
               0.015 seconds (Total)

~/.cmdstan/cmdstan-2.28.1> bin/stansummary output.csv 
Inference for Stan model: foo_model
1 chains: each with iter=(1000); warmup=(0); thin=(1); 1000 iterations saved.

Warmup took 0.0040 seconds
Sampling took 0.011 seconds

                Mean     MCSE   StdDev    5%   50%   95%    N_Eff  N_Eff/s    R_hat

lp__           -0.89    0.038     0.73  -2.4 -0.59 -0.40      382    34721     1.00
accept_stat__   0.94  2.8e-03  8.9e-02  0.75  0.98   1.0  1.0e+03  9.2e+04  1.0e+00
stepsize__      0.94      nan  6.0e-15  0.94  0.94  0.94      nan      nan      nan
treedepth__      1.4  1.6e-02  4.9e-01   1.0   1.0   2.0  1.0e+03  9.1e+04  1.0e+00
n_leapfrog__     2.9  5.8e-02  1.8e+00   1.0   3.0   7.0  9.4e+02  8.5e+04  1.0e+00
divergent__     0.00      nan  0.0e+00  0.00  0.00  0.00      nan      nan      nan
energy__         1.4  5.1e-02  1.0e+00  0.46   1.0   3.5  4.2e+02  3.8e+04  1.0e+00

mu               5.4    0.042     0.88   4.0   5.4   6.9      442    40159      1.0

Samples were drawn using hmc with nuts.
For each parameter, N_Eff is a crude measure of effective sample size,
and R_hat is the potential scale reduction factor on split chains (at 
convergence, R_hat=1).

Hi, thanks for replying. I am using pystan v3.3.0 and ran the model in pycharm and a jupyternotebook, both of them gave me the same error. Could this be the problem ?

what version of Stan is pystan using?

I’m assuming the latest version, Stan 2.26.1, since it was recently installed but how can this be checked?

Based on the documentation, it seems like

import httpstan
print(httpstan.stan.version())

should print the current Stan version for you

Using 4.5.0 stan (had to run httpstan.version) and pystan 3.2

I upgraded to 3.3.0 for pystan and 4.6.1 for stan and still get “Warning: The parameter mu_mean has no priors.” (the example provided above is a minimal working example, three parameters are actually used with three means tried to be specified in the data block and after called in the priors, the warning message appears for all of them).

that’s a pystan thing. there is no 4.5.0 Stan. it sounds like pystan is running the Stan compiler’s pedantic mode checks by default.

if this is annoying, try CmdStanPy: CmdStanPy 1.0.0rc3

Ok, thanks for your reply and advice!