-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3b_CornerTracePlots.py
95 lines (73 loc) · 3.24 KB
/
3b_CornerTracePlots.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# import required packages/modules
import corner
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
def traceandcorner(dataname, covname, priorname, nwalker, niter):
os.chdir("~/GaPP_27/Application/3_mcmcdgp_new/files/thetasample")
data = pd.read_csv("thetasample_" + dataname + "_" + covname + "_" + priorname + ".txt", sep=" ", header=None)
os.chdir("~/GaPP_27/Application/3_mcmcdgp_new/files/logl")
logl = pd.read_csv("logl_" + dataname + "_" + covname + "_" + priorname + ".txt", sep=" ", header=None)
# determine list of parameters for trace/corner plots
data.columns = {
"SquEx": ['sigma_f', 'l'],
"DoubleSquEx": ['sigma_{f1}', 'l_1', 'sigma_{f2}', 'l_2'],
"RatQuadratic": ['sigma_f', 'l', 'alpha'],
"Matern32": ['sigma_f', 'l'],
"Matern52": ['sigma_f', 'l'],
"Matern72": ['sigma_f', 'l'],
"Matern92": ['sigma_f', 'l'],
"Cauchy": ['sigma_f', 'l']
}.get(covname, "ERROR: invalid covariance function")
npar = data.shape[1] # number of parameters (= number of columns in thetasample)
# check nwalker * niter = number of rows of thetasample
if not (nwalker * niter == data.shape[0]):
ValueError("Warning: nwalker * niter not equal to number of rows of thetasample")
# check nwalker * niter = number of rows of thetasample
if not (nwalker * niter == logl.shape[0]):
ValueError("Warning: nwalker * niter not equal to number of rows of loglikelihood text file")
dataijavg = np.zeros((niter, npar))
loglijavg = np.zeros((niter, 1))
for iters in range(niter):
start = nwalker * iters
end = nwalker * iters + nwalker
# calculate average theta at each iteration
for par in range(npar):
dataijavg[iters, par] = np.mean(data.iloc[start:end, par])
# calculate average loglikelihood at each iteration
loglijavg[iters] = logl.iloc[iters]
plotname = "plot_" + dataname + "_" + covname + "_" + priorname
# trace plot (to loop over par = 0, 1, ...)
os.chdir("../trace")
for par in range(npar):
plt.figure()
plt.plot(dataijavg[:, par])
plt.xlabel("Iteration")
plt.ylabel(data.columns[par])
plt.savefig("trace_" + plotname + "_" + data.columns[par] + ".pdf")
plt.close()
# corner plot
os.chdir("../corner")
corner.corner(dataijavg, labels=data.columns)
plt.savefig("corner_" + plotname + ".pdf")
plt.close()
# loglikelihood plot
os.chdir("../logl")
plt.figure()
plt.plot(loglijavg)
plt.savefig("logl_" + plotname + ".pdf")
plt.close()
# go back to folder containing thetasample
os.chdir("../thetasample/")
# define data sources, covariance functions, prior
datas = ["CC", "CC+SN", "CC+SN+BAO"]
covnames = ["SquEx", "DoubleSquEx", "RatQuadratic", "Matern32", "Matern52", "Matern72", "Matern92", "Cauchy"]
priornames = ["NoPrior", "Riess", "TRGB", "H0LiCOW", "CM", "Planck", "DES"]
# loop over all datasets/cov. fn./priors
for i in range(len(datas)):
for j in range(len(covnames)):
for k in range(len(priornames)):
print
print(i, j, k)
traceandcorner(datas[i], covnames[j], priornames[k], nwalker=100, niter=100000)