-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_iso.py
50 lines (34 loc) · 1.73 KB
/
sample_iso.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import matplotlib.pyplot as plt
import numpy as np
import theano.tensor as tt
import pymc3 as pm
import corner
from astropy.io import ascii
deg = np.pi/180.0
# load the dataset
data = ascii.read("data/iso_sample.ecsv", format="ecsv")
N_systems = len(data)
# instantiate a PyMC3 model class
# instantiate a PyMC3 model class
with pm.Model() as model:
mu = pm.Normal("mu", mu=0.0, sd=2.0)
tau = pm.HalfNormal("tau", sd=4.0)
tau_off = pm.Deterministic("tau_off", tau + 0.5)
v = pm.LogitNormal("v", mu=mu, tau=tau_off, shape=N_systems)
theta = pm.Deterministic("theta", v * 180.)
cos_theta = tt.cos(v * np.pi)
sin_theta = tt.sin(v * np.pi)
# Enforce the geometrical prior on i_disk, as before
# Testval tells the chain to start in the center of the posterior.
cos_i_disk = pm.Uniform("cosIdisk", lower=-1.0, upper=1.0, shape=N_systems, testval=data["cos_i_disk"].data)
sin_i_disk = tt.sqrt(1.0 - cos_i_disk**2)
# This is the azimuthal orientation of the binary vector on the circle some theta away
phi = pm.Uniform("phi", lower=-np.pi/2.0, upper=np.pi/2.0, shape=N_systems)
cos_i_star = pm.Deterministic("cos_i_star", -sin_i_disk * sin_theta * tt.sin(phi) + cos_i_disk * cos_theta)
# Finally, we define the likelihood by conditioning on the observations using a Normal
obs_disk = pm.Normal("obs_disk", mu=cos_i_disk, sd=data["cos_i_disk_err"].data, observed=data["cos_i_disk"].data)
obs_star = pm.Normal("obs_star", mu=cos_i_star, sd=data["cos_i_star_err"].data, observed=data["cos_i_star"].data)
# sample the model!
with model:
trace = pm.sample(draws=10000, tune=40000, chains=2, nuts_kwargs={"target_accept":0.95})
pm.backends.ndarray.save_trace(trace, directory="iso", overwrite=True)