Skip to content

Commit 97fe36d

Browse files
authored
Merge pull request #30 from cosomelet/main
marciab000
2 parents f12ff9a + 0aa5538 commit 97fe36d

File tree

6 files changed

+199
-74
lines changed

6 files changed

+199
-74
lines changed

Data/Pantheon_old/lcparam_full.txt

+18-18
Original file line numberDiff line numberDiff line change
@@ -1028,21 +1028,21 @@
10281028
1.800000000000000044e+00 1.799601000000000006e+00 2.620110000000000028e+01 1.893198849999999993e+02 6.227817900000000151e+01
10291029
2.259999999999999787e+00 2.259600999999999971e+00 2.687699999999999889e+01 1.891565860000000043e+02 6.230917399999999873e+01
10301030
1.913999999999999924e+00 1.914828999999999892e+00 2.616090000000000160e+01 3.444306600000000174e+01 -5.256643000000000399e+00
1031-
1.300000000000000044e+00 1.301201999999999970e+00 2.552015000000000100e+01 0.000000000000000000e+00 0.000000000000000000e+00
1032-
1.340000000000000080e+00 1.341202000000000005e+00 2.546519999999999939e+01 0.000000000000000000e+00 0.000000000000000000e+00
1033-
1.020000000000000018e+00 1.021201999999999943e+00 2.488234999999999886e+01 0.000000000000000000e+00 0.000000000000000000e+00
1034-
7.349999999999999867e-01 7.362020000000000231e-01 2.370605000000000118e+01 0.000000000000000000e+00 0.000000000000000000e+00
1035-
1.120000000000000107e+00 1.121202000000000032e+00 2.506390000000000029e+01 0.000000000000000000e+00 0.000000000000000000e+00
1036-
1.229999999999999982e+00 1.231201999999999908e+00 2.528614999999999924e+01 0.000000000000000000e+00 0.000000000000000000e+00
1037-
1.229999999999999982e+00 1.231201999999999908e+00 2.549849999999999994e+01 0.000000000000000000e+00 0.000000000000000000e+00
1038-
8.539999999999999813e-01 8.552020000000000177e-01 2.431279999999999930e+01 0.000000000000000000e+00 0.000000000000000000e+00
1039-
1.370000000000000107e+00 1.371202000000000032e+00 2.551589999999999847e+01 0.000000000000000000e+00 0.000000000000000000e+00
1040-
9.751999999999999558e-01 9.764019999999999921e-01 2.470609999999999928e+01 0.000000000000000000e+00 0.000000000000000000e+00
1041-
9.699999999999999734e-01 9.712020000000000097e-01 2.511544999999999916e+01 0.000000000000000000e+00 0.000000000000000000e+00
1042-
7.399999999999999911e-01 7.412020000000000275e-01 2.384459999999999980e+01 0.000000000000000000e+00 0.000000000000000000e+00
1043-
1.389999999999999902e+00 1.391202000000000050e+00 2.536305000000000121e+01 0.000000000000000000e+00 0.000000000000000000e+00
1044-
1.304999999999999938e+00 1.306202000000000085e+00 2.526419999999999888e+01 0.000000000000000000e+00 0.000000000000000000e+00
1045-
9.350000000000000533e-01 9.362019999999999786e-01 2.426660000000000039e+01 0.000000000000000000e+00 0.000000000000000000e+00
1046-
1.014000000000000012e+00 1.015201999999999938e+00 2.485444999999999993e+01 0.000000000000000000e+00 0.000000000000000000e+00
1047-
1.314999999999999947e+00 1.316202000000000094e+00 2.554234999999999900e+01 0.000000000000000000e+00 0.000000000000000000e+00
1048-
1.092000000000000082e+00 1.093202000000000007e+00 2.463834999999999908e+01 0.000000000000000000e+00 0.000000000000000000e+00
1031+
1.300000000000000044e+00 1.301201999999999970e+00 2.552015000000000100e+01 5.315630000000000166e+01 -2.777959999999999852e+01
1032+
1.340000000000000080e+00 1.341202000000000005e+00 2.546519999999999939e+01 1.892880000000000109e+02 6.219140000000000157e+01
1033+
1.020000000000000018e+00 1.021201999999999943e+00 2.488234999999999886e+01 1.893369999999999891e+02 6.222820000000000107e+01
1034+
7.349999999999999867e-01 7.362020000000000231e-01 2.370605000000000118e+01 5.309308300000000003e+01 -2.774080000000000013e+01
1035+
1.120000000000000107e+00 1.121202000000000032e+00 2.506390000000000029e+01 1.890579999999999927e+02 6.220210000000000150e+01
1036+
1.229999999999999982e+00 1.231201999999999908e+00 2.528614999999999924e+01 1.890954999999999870e+02 6.230644399999999905e+01
1037+
1.229999999999999982e+00 1.231201999999999908e+00 2.549849999999999994e+01 1.892359999999999900e+02 6.221479999999999677e+01
1038+
8.539999999999999813e-01 8.552020000000000177e-01 2.431279999999999930e+01 1.891450000000000102e+02 6.226359999999999673e+01
1039+
1.370000000000000107e+00 1.371202000000000032e+00 2.551589999999999847e+01 5.304180000000000206e+01 -2.783050000000000068e+01
1040+
9.751999999999999558e-01 9.764019999999999921e-01 2.470609999999999928e+01 5.310560000000000258e+01 -2.775080000000000169e+01
1041+
9.699999999999999734e-01 9.712020000000000097e-01 2.511544999999999916e+01 1.895374999999999943e+02 6.231312199999999990e+01
1042+
7.399999999999999911e-01 7.412020000000000275e-01 2.384459999999999980e+01 5.307562500000000227e+01 -2.773626399999999848e+01
1043+
1.389999999999999902e+00 1.391202000000000050e+00 2.536305000000000121e+01 1.892249999999999943e+02 6.213969999999999771e+01
1044+
1.304999999999999938e+00 1.306202000000000085e+00 2.526419999999999888e+01 5.310329999999999728e+01 -2.777159999999999940e+01
1045+
9.350000000000000533e-01 9.362019999999999786e-01 2.426660000000000039e+01 1.893710000000000093e+02 6.219109999999999872e+01
1046+
1.014000000000000012e+00 1.015201999999999938e+00 2.485444999999999993e+01 3.542530000000000001e+01 -3.364799999999999791e+00
1047+
1.314999999999999947e+00 1.316202000000000094e+00 2.554234999999999900e+01 3.544369999999999976e+01 -3.382299999999999862e+00
1048+
1.092000000000000082e+00 1.093202000000000007e+00 2.463834999999999908e+01 1.873568960000000061e+02 1.849053000000000058e+00

Notebooks/Validation_cosmo.ipynb

+57-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,62 @@
2222
"import matplotlib.pyplot as plt"
2323
]
2424
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 38,
28+
"id": "6767992e",
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"name": "stdout",
33+
"output_type": "stream",
34+
"text": [
35+
"Frodo\n",
36+
"Koekemoer\n",
37+
"Patuxent\n",
38+
"Rakke\n",
39+
"SCP06C0\n"
40+
]
41+
}
42+
],
43+
"source": [
44+
"# To make the positions fo teh pantheon dataset \n",
45+
"\n",
46+
"\"\"\" More importantly I notice some of teh supernovae no longer exist in the Panhteon+ compilation I dont know why !!\n",
47+
"\"\"\"\n",
48+
"\n",
49+
"pant_old_names = np.loadtxt('../Data/Pantheon_old/lcparam_full_long.txt', dtype='str', usecols=0, skiprows=1)\n",
50+
"pant_old = np.loadtxt('../Data/Pantheon_old/lcparam_full.txt', )\n",
51+
"pant_new_names = np.loadtxt('../Data/Pantheon+/Pantheon+SH0ES.dat', skiprows=1, dtype='str', usecols=0 )\n",
52+
"pant_new = np.loadtxt('../Data/Pantheon+/Pantheon+SH0ES.dat', skiprows=1, usecols=(26, 27))\n",
53+
"# taken from github of Pantheon+\n",
54+
"others = np.array([[53.093083,-27.74080], [189.095500, 62.306444], [189.537500, 62.313122], [53.075625, -27.736264], [187.356896, 1.849053]]) \n",
55+
"\n",
56+
"j = 0\n",
57+
"for i in range(len(pant_old_names[1030:])):\n",
58+
" if pant_old_names[1030+i] in pant_new_names:\n",
59+
" \n",
60+
" index_new = np.where(pant_new_names == pant_old_names[1030+i])\n",
61+
" pant_old[1030+i, 3] = pant_new[index_new, 0]\n",
62+
" pant_old[1030+i, 4] = pant_new[index_new, 1]\n",
63+
" else:\n",
64+
" # print('Not found')\n",
65+
" print(pant_old_names[1030+i])\n",
66+
" pant_old[1030+i, 3] = others[j, 0]\n",
67+
" pant_old[1030+i, 4] = others[j, 1]\n",
68+
" j += 1\n",
69+
"\n",
70+
"# np.savetxt('../Data/Pantheon_old/lcparam_full_2.txt', pant_old)\n"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 36,
76+
"id": "5cc1d83f",
77+
"metadata": {},
78+
"outputs": [],
79+
"source": []
80+
},
2581
{
2682
"cell_type": "code",
2783
"execution_count": 3,
@@ -482,7 +538,7 @@
482538
"name": "python",
483539
"nbconvert_exporter": "python",
484540
"pygments_lexer": "ipython3",
485-
"version": "3.8.8"
541+
"version": "3.8.11"
486542
}
487543
},
488544
"nbformat": 4,

marcia/GPconfig.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
filename = 'GPconfig.ini'
1919

2020
# self scale implies all the tasks have the same length scale paramter.
21-
config['GENERAL'] = {'n_Tasks': n_Tasks, 'self_scale': True}
21+
config['GENERAL'] = {'n_Tasks': n_Tasks, 'self_scale': True, 'ind_scatter': False}
2222

2323
config['KERNEL'] = {'Interpolate': True, 'Method': 'cubic', 'n_points': 100 }
2424

@@ -39,6 +39,8 @@
3939
config.set(Task, 'l_max', '10.0')
4040
config.set(Task, 'sigma_f_min', '0.001')
4141
config.set(Task, 'sigma_f_max', '10.0')
42+
config.set(Task, 'sig_int_min', '0.001')
43+
config.set(Task, 'sig_int_max', '5.0')
4244

4345
# if necessary to include a intrinsic scatter or an offset to the covariance matrix
4446
config['INTRINSIC_SCATTER'] = {'sigma_int': True, 'offset': True}
@@ -72,6 +74,9 @@ def __init__(self, filename):
7274
# Set the self_scale parameter
7375
self.self_scale = config.getboolean('GENERAL', 'self_scale')
7476

77+
# Set the ind_scatter parameter
78+
self.ind_scatter = config.getboolean('GENERAL', 'ind_scatter')
79+
7580
# Set the interpolation parameters
7681
self.Interpolate = config.getboolean('KERNEL', 'Interpolate')
7782
self.Method = config.get('KERNEL', 'Method')
@@ -103,7 +108,10 @@ def __init__(self, filename):
103108
l_max = config.getfloat(Task, 'l_max')
104109
sigma_f_min = config.getfloat(Task, 'sigma_f_min')
105110
sigma_f_max = config.getfloat(Task, 'sigma_f_max')
111+
sig_int_min = config.getfloat(Task, 'sig_int_min')
112+
sig_int_max = config.getfloat(Task, 'sig_int_max')
113+
106114
# Create the GPparams class
107-
self.__dict__[Task] = {'model': model, 'nu': nu, 'l_min': l_min, 'l_max': l_max, 'sigma_f_min': sigma_f_min, 'sigma_f_max': sigma_f_max}
115+
self.__dict__[Task] = {'model': model, 'nu': nu, 'l_min': l_min, 'l_max': l_max, 'sigma_f_min': sigma_f_min, 'sigma_f_max': sigma_f_max, 'sig_int_min': sig_int_min, 'sig_int_max': sig_int_max}
108116

109117

marcia/database.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ def get_data(self,choose):
9090
return self.get_GRB()
9191
elif choose == 'SNE':
9292
return self.get_SNE()
93-
elif choose == 'QSA':
94-
return self.get_QSA()
93+
elif choose == 'QSO':
94+
return self.get_QSO()
95+
elif choose == 'QSO_full':
96+
return self.get_QSO_full()
9597
elif choose == 'Planck_TT':
9698
return self.get_CMB_planck_TT()
9799
elif choose == 'Planck_EE':
@@ -245,8 +247,8 @@ def get_CMB_planck_TE(self):
245247

246248
@load_data_once
247249
def get_data_pantheon_old(self):
248-
datapath = os.path.join(__datapath__, 'Pantheon_old','lcparam_select.txt')
249-
covpath = os.path.join(__datapath__, 'Pantheon_old','cov_select.txt')
250+
datapath = os.path.join(__datapath__, 'Pantheon_old','lcparam_full.txt')
251+
covpath = os.path.join(__datapath__, 'Pantheon_old','cov_full.txt')
250252
zcmb, zhel, mb, ra, dec = np.loadtxt(datapath).T
251253
cov = np.loadtxt(covpath)
252254
return zcmb, zhel, mb, cov, ra, dec

marcia/likelihood.py

+66-29
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def chisq_BAO_alam(self,theta):
5555

5656
return np.dot(dist- distance_theory, np.dot(np.linalg.inv(cov), dist - distance_theory))
5757

58-
def chisq_Pantheon_plus(self,theta):
58+
def chisq_Pantheon_plus(self,theta, residual = False):
5959
cmb_z, mb, covariance = self.db.get_pantheon_plus()
6060
helio_z = self.db.get_pantheon_plus(Zhel=True)
6161
distance_theory = self.theory.distance_modulus(theta, cmb_z, helio_z)
@@ -66,9 +66,12 @@ def chisq_Pantheon_plus(self,theta):
6666
icov = np.linalg.inv(covariance)
6767
self.inv_covariance['Pantheon_plus'] = icov
6868

69-
return np.dot(delta, np.dot(icov, delta))
69+
if residual:
70+
return delta, np.sqrt(np.diag(covariance))
71+
else:
72+
return np.dot(delta, np.dot(icov, delta))
7073

71-
def chisq_Pantheon_old(self,theta):
74+
def chisq_Pantheon_old(self,theta, residual = False):
7275
cmb_z, mb, covariance = self.db.get_pantheon_old()
7376
helio_z = self.db.get_pantheon_old(Zhel=True)
7477
distance_theory = self.theory.distance_modulus(theta, cmb_z, helio_z)
@@ -78,17 +81,22 @@ def chisq_Pantheon_old(self,theta):
7881
else:
7982
icov = np.linalg.inv(covariance)
8083
self.inv_covariance['Pantheon_old'] = icov
81-
82-
return np.dot(delta, np.dot(icov, delta))
84+
if residual:
85+
return delta, np.sqrt(np.diag(covariance))
86+
else:
87+
return np.dot(delta, np.dot(icov, delta))
8388

84-
def chisq_QSO_dm(self,theta):
89+
def chisq_QSO(self,theta, residual = False):
8590
p = self.params(theta)
8691
#pdb.set_trace()
8792
z,dm,cov = self.db.get_QSO()
88-
distance_theory = self.theory.distance_modulus(theta, z,z)
93+
distance_theory = self.theory.distance_modulus(theta, z,z) - p.M_b
8994
delta = dm - distance_theory
9095
var = np.diag(cov)
91-
return np.sum(delta**2./(var + p.qso_sigma**2))
96+
if residual:
97+
return delta, np.sqrt(var)
98+
else:
99+
return np.sum(delta**2./(var + p.qso_sigma**2))
92100

93101
def chisq_QSO_full(self,theta):
94102
p = self.params(theta)
@@ -147,13 +155,20 @@ def __init__(self, data, GP_filename=None, cosmo_filename=None):
147155
self.nmodel = self.nTasks
148156
self.self_scale = MTGP['self_scale']
149157
self.scatter = MTGP['sigma_int']
158+
self.offset = MTGP['offset']
159+
160+
# extra paramter per dataset to be added to the theta
161+
self.ind_scatter = MTGP['ind_scatter']
150162

151163
# To define the priors for the GP hyperparameters using the the GPconfig file
152164
self.priors = []
153165
for i in range(self.nTasks):
154166
i = i+1 # To start from 1
155167
self.priors.append([MTGP[f'Task_{i}']['sigma_f_min'], MTGP[f'Task_{i}']['sigma_f_max']])
156168
self.priors.append([MTGP[f'Task_{i}']['l_min'], MTGP[f'Task_{i}']['l_max']])
169+
if self.ind_scatter:
170+
self.priors.append([MTGP[f'Task_{i}']['sig_int_min'], MTGP[f'Task_{i}']['sig_int_max']])
171+
157172

158173
self.priors = np.array(self.priors)
159174

@@ -184,44 +199,68 @@ def set_theta(self, theta):
184199
# and possibly with intrinsic scatter at the end [..., sigma_int]
185200
# Final form is [[sigma_f_1, l_1], [sigma_f_2, l_2], ...]
186201

187-
if self.self_scale == False and len(theta) != 2*self.nTasks and self.scatter == False:
188-
raise ValueError('The number of hyperparameters does not match the number of tasks')
189-
elif self.self_scale == False and len(theta) != 2*self.nTasks+1 and self.scatter == True:
190-
pass
202+
203+
if self.self_scale == False:
204+
params_GP = []
205+
if len(theta)>=2*self.nTasks:
206+
for i in range(self.nTasks):
207+
params_GP.append([theta[2*i], theta[2*i+1]])
208+
else:
209+
raise ValueError(f'The number of hyperparameters does not match the number of tasks, there must be atleast {2*self.nTasks} hyperparameters')
210+
211+
elif self.self_scale == True:
212+
params_GP =[]
213+
if len(theta)>=self.nTasks+1:
214+
for i in range(self.nTasks):
215+
params_GP.append([theta[i], theta[self.nTasks]])
216+
else:
217+
raise ValueError(f'The number of hyperparameters does not match the number of tasks, there must be atleast {self.nTasks+1} hyperparameters')
218+
219+
220+
if self.ind_scatter == True and len(theta)>=self.nTasks+2:
221+
params_scatter = theta[-self.nTasks:]
191222
else:
192-
# rearrange the theta values into a list of lists
193-
params = []
194-
for i in range(self.nTasks):
195-
params.append([theta[2*i], theta[2*i+1]])
223+
params_scatter = [self.scatter]*self.nTasks
196224

197-
return params
225+
return params_GP, params_scatter
198226

199227
def logPrior(self, theta):
200228
# To set the priors for the GP hyperparameters
201229
for (lower, upper), value in zip(self.priors, theta):
202230
if not lower < value < upper:
203-
# print(value, lower, upper)
204-
# sys.exit()
205231
return -np.inf
206232
return 0.0
207233

208-
def logLike(self,theta):
209-
chi2 = self.chisq(theta)
234+
def logLike(self,theta_GP, theta_scatter):
235+
chi2 = self.chisq(theta_GP, theta_scatter)
210236
return -0.5*chi2
211237

212238
def logProb(self, theta):
239+
213240
lp = self.logPrior(theta)
214-
theta = self.set_theta(theta)
241+
theta_GP, theta_scatter = self.set_theta(theta)
215242
if not np.isfinite(lp):
216243
return -np.inf
217-
return lp + self.logLike(theta)
244+
return lp + self.logLike(theta_GP, theta_scatter)
218245

219-
def chisq(self,theta):
246+
def chisq(self, theta_GP, theta_scatter):
220247
# We define the GP likelihood here
221248
chi2 = 0
249+
250+
# To set the scatter covariance matrix which is a diagonal matrix of size len of ntasks
251+
scatter_matrix = self.offset*np.eye(len(self.D_covmat))
252+
scatter_diag = []
253+
for i in range(self.nTasks):
254+
# create arrays of length of the data for each task
255+
scatter_diag = np.append(scatter_diag, len(np.array(self.d.x[f'x{i}']))*[theta_scatter[i]**2.])
256+
257+
int_scatter_covmat = scatter_matrix + np.diag(scatter_diag)
258+
222259
# To call the GP class to get the covariance matrix
223-
GP_covmat = self.GP_kernels.Cov_Mat(theta)
224-
Total_covmat = GP_covmat + self.D_covmat + 1e-6*np.eye(len(self.D_covmat))
260+
GP_covmat = self.GP_kernels.Cov_Mat(theta_GP)
261+
262+
# To calculate the total covariance matrix
263+
Total_covmat = GP_covmat + self.D_covmat + int_scatter_covmat
225264

226265
# To perform cholesky decomposition
227266
L_inv = np.linalg.inv(Total_covmat)
@@ -235,6 +274,4 @@ def chisq(self,theta):
235274
y_inv = np.dot(L_inv, y)
236275
chi2 = np.dot(y, y_inv.T) + log_det - offset
237276

238-
return chi2
239-
240-
277+
return chi2

0 commit comments

Comments
 (0)