Learning Pystan and vectorizing models

Operating System: OS X 10.11.6
Python Version: 3.6 Anaconda
Interface Version:
Compiler/Toolkit:

Hello,

I am in the process of moving our geomicrobiology models into Pystan. I am beginner with Pystan and Bayesian modelling. I am working on a model to compare two groups. Bob Carpenter helped in another thread to clean it up. But I am having troubles understanding the code.

My original mode was:

Dictionary containing all data to be passed to STAN

compare_groups = {‘y1’: y1,
‘y2’: y2,
‘n1’: n1,‘n2’: n2}

DO NOT USE - old model string.

model_string = “”"
data {
int n1;
int n2;
vector[n1] y1;
vector[n2] y2;
}

parameters { //
real<lower=-3, upper=3> mu1; //
real<lower=-3, upper=3> mu2; //
real<lower=0> sigma1;
real<lower=0> sigma2;
}

model {
mu1 ~ normal(0, 10);
mu2 ~ normal(0, 10);
sigma1 ~ cauchy(0, 5);
sigma2 ~ cauchy(0, 5);
y1 ~ normal(mu1, sigma1);
y2 ~ normal(mu2, sigma2);
}

generated quantities {
}
"""

Bob suggested that this was better:

Corrected by Bob Carpenter

model_string = “”“
data {
int N[2];
vector[N[1]] y1;
vector[N[2]] y2;
}
parameters {
vector[2] mu;
vector<lower=0>[2] sigma;
}
model {
mu ~ normal(0, 10);
sigma ~ cauchy(0, 5);
y1 ~ normal(mu[1], sigma[1]);
y2 ~ normal(mu[2], sigma[2]);
}
”""

However when I run the model I get the following error:
RuntimeError: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=N; dims declared=(2); dims found=()

I am guessing I am not declaring N correctly in the dictionary that feeds into the model?

Yeah, it looks like he suggested passing in your different Ns as a list.

So for your original model you had something like:

data = { "n1" : 5, "n2" : 7, ... }

For your new model you should have something like:

data = { "N" : [5, 7], ... }

You’re probably getting the error because you’re passing in a single integer for N.

Hope that helps!

1 Like

Perfect! @bbbales2 . That solved it. I was goofing up the python syntax.

ara