Multiply probability by a constant in Stan model

462 views Asked by At

I am working in PySTAN. Suppose my likelihood is: p1 * p2

where p1 ~ N(x, xerr)

and

p2 = 0.823 if t = 0 
       1   if t = 1

My model is:

model = """
data {
    int<lower=0> N; // number of points
    vector[N] x; 
    vector[N] xerr; 
    vector[N] cnd; // indicator: elements are 0 or 1
}

parameters {
    real alpha; // intercept
    real beta; // slope
}

model {
    vector[N] p2;

    // PRIORS
    alpha ~ normal(0,10);
    beta ~ normal(0,10);

    p1 ~ normal(x, xerr);

   for (i in 1:N) {
       if (cnd[i] == 1)
            p2[i] ~ 0.823;
       else if (cnd[i] == 0)
            p2[i] ~ 1;
    }
}
"""

However, I got an error!

All I am trying to do is to multiply the likelihood by a constant in case a certain condition was met. How can I do it in PySTAN?

Here is the error I got & traceback:

Traceback (most recent call last):
  File "/home/paula/Desktop/fakedata.py", line 91, in <module>
    fit = pystan.stan(model_code=fit_code, data=fit_data, iter=10, chains=1)
  File "/usr/local/lib/python2.7/dist-packages/pystan/api.py", line 373, in stan
    save_dso=save_dso, verbose=verbose)
  File "/usr/local/lib/python2.7/dist-packages/pystan/model.py", line 219, in __init__
obfuscate_model_name=obfuscate_model_name)
  File "/usr/local/lib/python2.7/dist-packages/pystan/api.py", line 129, in stanc
raise ValueError(error_msg)

ValueError: Failed to parse Stan model     'anon_model_04ef08903b48c28a7c3fee52b890cdaf'. Error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:


ERROR at line 43

 41:        for (i in 1:N) {
 42:        if (cnd[i] == 1)
 43:            p2[i] ~ 0.823
                           ^
 44:        else if (cnd[i] == 0)

PARSER EXPECTED: <distribution and parameters>
1

There are 1 answers

1
ariddell On

The function you're looking for is increment_log_prob(...).

Check out the Stan manual for details: http://mc-stan.org/manual.html

It's also worth noting that the best place to get help with Stan is currently the stan-users mailing list: http://mc-stan.org/groups.html