Skip to content

Instantly share code, notes, and snippets.

@andycasey
Created May 3, 2018 01:50
Show Gist options
  • Select an option

  • Save andycasey/2afc434de2975a3edd79ac2539bc6bf9 to your computer and use it in GitHub Desktop.

Select an option

Save andycasey/2afc434de2975a3edd79ac2539bc6bf9 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import pystan as stan
model = stan.StanModel(model_code="""
data {
real parallax;
real parallax_error;
}
parameters {
real<lower=0, upper=1000> d; // distance
}
model {
parallax ~ normal(1.0/d, parallax_error);
}
""")
data = dict(parallax=0.12, parallax_error=0.06) # both in milliarcseconds/yr
p_opt = model.optimizing(data=data)
samples = model.sampling(data=data, chains=2, init=[p_opt, p_opt], iter=100000)
d_samples = samples.extract()["d"]
fig, ax = plt.subplots()
ax.hist(d_samples, bins=np.linspace(0, 100, 50))
ax.set_xlim(0, 100)
ax.set_xlabel(r"$d$ $[{\rm kpc}]$")
ax.set_yticks([])
fig.tight_layout()
fig.savefig("distances_pdf.png", dpi=150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment