forked from mabitbol/sd_foregrounds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcmcscript.py
53 lines (42 loc) · 1.55 KB
/
mcmcscript.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
51
52
53
import numpy as np
import emcee
import spectral_distortions as sd
import foregrounds as fg
import fisher
#import sys
#from emcee.utils import MPIPool
import multiprocessing as mp
def lnprob(theta, x, y, yerr):
lp = lnprior(theta)
if not np.isfinite(lp):
return -np.inf
return lp + lnlike(theta, x, y, yerr)
def lnlike(theta, x, y, yerr):
mu_amp, y_tot, kT_yweight, DeltaT_amp = theta
model = sd.DeltaI_mu(x, mu_amp)+sd.DeltaI_reltSZ_2param_yweight(x, y_tot, kT_yweight)+\
sd.DeltaI_DeltaT(x, DeltaT_amp)
return -0.5 * (np.sum((y-model)**2. * yerr**-2.))
def lnprior(theta):
return 0.
def run():
pool = mp.Pool()
nargs = ['mu', 'y', 'kT', 'DeltaT']
fish = fisher.FisherEstimation(duration=12., bandpass=False)
fncs = [sd.DeltaI_mu, sd.DeltaI_reltSZ_2param_yweight, sd.DeltaI_DeltaT]
fish.set_signals(fncs)
x = fish.center_frequencies
noise = fish.noise / 1000.
y = sd.DeltaI_mu(x)+sd.DeltaI_reltSZ_2param_yweight(x)+sd.DeltaI_DeltaT(x)
yerr = noise*np.random.randn(len(x))
y += yerr
ndim, nwalkers = 4, 32
pos = [fish.p0*(1. + 1.e-1*np.random.randn(ndim)) for i in range(nwalkers)]
sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(x, y, noise), pool=pool)
sampler.run_mcmc(pos, 2000);
pool.close()
samples = sampler.chain[:, 500:, :].reshape((-1, ndim))
p_mc = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]), zip(*np.percentile(samples, [16, 50, 84], axis=0)))
for k in range(4):
print nargs[k], (p_mc[k][1]+p_mc[k][2])/2.
return
run()