Skip to content

Instantly share code, notes, and snippets.

@y-mitsui
Created April 2, 2017 04:00
Show Gist options
  • Select an option

  • Save y-mitsui/0ea7dd1e3ee7b50cbede700921e4d108 to your computer and use it in GitHub Desktop.

Select an option

Save y-mitsui/0ea7dd1e3ee7b50cbede700921e4d108 to your computer and use it in GitHub Desktop.
stan_ep1
import pystan
import numpy as np
stan_code = """
data {
int<lower=0> N;
real<lower=0> x[N];
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
for(n in 1:N) {
x[n] ~ normal(mu, sigma);
}
}
generated quantities {
real<lower=0, upper=1> mu_over;
real<lower=0, upper=1> mu_over2;
real es;
real<lower=0, upper=1> es_over;
mu_over <- step(mu - 2500);
mu_over2 <- step(mu - 3000);
es <- (mu - 2500) / sigma;
es_over <- step(es-0.8);
}
"""
sample = [3060, 2840, 1780, 3280, 3550, 2450, 2200, 3070, 2100, 4100,
3630, 3060, 3280, 1870, 2980, 3120, 2150, 3830, 4300, 1880]
stan_model = pystan.StanModel(model_code=stan_code)
print np.mean(sample)
op = stan_model.sampling(data=dict(x=sample, N=len(sample)), chains=1, iter=10000)
print op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment