Skip to content

Instantly share code, notes, and snippets.

@bbbales2
Last active April 16, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbbales2/2f68c4a9111284c0df8ad2f56505b495 to your computer and use it in GitHub Desktop.
Save bbbales2/2f68c4a9111284c0df8ad2f56505b495 to your computer and use it in GitHub Desktop.
Log normal sampling
import pystan
import numpy
import matplotlib.pyplot as plt
y = numpy.random.lognormal(mean = 0.7, sigma = 1.0, size = 5)
# Both of these models produce the same results
model_code = """
data {
int<lower=1> N;
vector<lower=0.0>[N] y;
}
parameters {
real mu;
real<lower=0.0> sigma;
}
model {
y ~ lognormal(mu, sigma);
}
"""
sm1 = pystan.StanModel(model_code = model_code)
model_code = """
data {
int<lower=1> N;
vector<lower=0.0>[N] y;
}
parameters {
real mu;
real<lower=0.0> sigma;
}
model {
log(y) ~ normal(mu, sigma);
}
"""
sm2 = pystan.StanModel(model_code = model_code)
# These fits should be equivalent
fit1 = sm1.sampling(data = { 'N' : len(y), 'y' : y }, iter = 100000, warmup = 1000)
fit2 = sm2.sampling(data = { 'N' : len(y), 'y' : y }, iter = 100000, warmup = 1000)
print fit1
print fit2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment