Pystan log-probability confusing!

For example, if the model is

parameters {
    real<lower=0.0, upper=20> A;
    real <lower=0, upper=10> B;

you would call

upar = fit.unconstrain_pars({"A": a, "B": b})
lp = fit.log_prob(upar, adjust_transform=False)
1 Like

The only problem now is that it calculates lp at each point, i.e. a or b can be just one number, not a list! Is there a way of giving all the sampled points in pars? If not, it’s better to calculate lp by hand.

Error for giving the fit.extract() as pars in fit.unconstrain_pars(pars):

RuntimeError: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=A; dims declared=(); dims found=(2136)

If you want to compute logprob for each posterior draw it’s probably easier to do it in the generated quantities or transformed parameters block.

fit.extract() gives a dictionary that contains all the draws so doing it in python would look like this, I guess

pars = fit.extract()
lps = numpy.zeros(4000) # or however many draws you have
for i in range(4000):
  upar = fit.unconstrain_pars({p: v[i] for p, v in pars.items()})
  lps[i] = fit.log_prob(upar, adjust_transform=False)```
1 Like

Okay. So basically upar = fit.unconstrain_pars() does not take more than one number per parameter?

It takes however many are declare in the parameters block so e.g. if you have vector[5] it takes five. extract() gives many draws so the arrays have one dimension more than what is declared in the model.