forked from SgmAstro/DESI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cobaya_example.py
54 lines (40 loc) · 1.74 KB
/
cobaya_example.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
54
import numpy as np
import getdist.plots as gdplt
from cobaya.run import run
from scipy import stats
from getdist.mcsamples import MCSamplesFromCobaya
# Run me on interactive:
# srun -N 1 -n 1 python cobaya_test.py
def gauss_ring_logp(x, y, mean_radius=1, std=0.02):
"""
Defines a gaussian ring likelihood on cartesian coordinater,
around some ``mean_radius`` and with some ``std``.
"""
return stats.norm.logpdf(np.sqrt(x**2 + y**2), loc=mean_radius, scale=std)
def get_r(x, y):
return np.sqrt(x ** 2 + y ** 2)
def get_theta(x, y):
return np.arctan(y / x)
info = {"likelihood": {"ring": gauss_ring_logp}}
info["params"] = {
"x": {"prior": {"min": 0, "max": 2}, "ref": 0.5, "proposal": 0.01},
"y": {"prior": {"min": 0, "max": 2}, "ref": 0.5, "proposal": 0.01}}
info["params"]["r"] = {"derived": get_r}
info["params"]["theta"] = {"derived": get_theta,
"latex": r"\theta", "min": 0, "max": np.pi/2}
info["sampler"] = {"mcmc": {"Rminus1_stop": 0.001, "max_tries": 1000}}
updated_info, sampler = run(info, output='cobaya_test/test_chain')
'''
gdsamples = MCSamplesFromCobaya(updated_info, sampler.products()["sample"])
gdplot = gdplt.get_subplot_plotter(width_inch=5)
gdplot.triangle_plot(gdsamples, ["x", "y"], filled=True)
gdplot = gdplt.get_subplot_plotter(width_inch=5)
gdplot.plots_1d(gdsamples, ["r", "theta"], nx=2)
info["prior"] = {"x_eq_y_band":
lambda x, y: stats.norm.logpdf(x - y, loc=0, scale=0.3)}
updated_info_x_eq_y, sampler_x_eq_y = run(info)
gdsamples_x_eq_y = MCSamplesFromCobaya(
updated_info_x_eq_y, sampler_x_eq_y.products()["sample"])
gdplot = gdplt.get_subplot_plotter(width_inch=5)
gdplot.triangle_plot(gdsamples_x_eq_y, ["x", "y"], filled=True)
'''