Last active
April 16, 2017 16:29
-
-
Save bbbales2/2f68c4a9111284c0df8ad2f56505b495 to your computer and use it in GitHub Desktop.
Log normal sampling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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