-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsigma_nfw_mcmc.py
206 lines (154 loc) · 5.84 KB
/
dsigma_nfw_mcmc.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 18 17:18:03 2023
@author: Admin
"""
import emcee
import numpy as np
import math
import pandas as pd
from profiley.nfw import TNFW, NFW
import matplotlib.pyplot as plt
from colossus.halo import concentration, profile_nfw
from colossus.cosmology import cosmology
from astropy.table import Table
from scipy.interpolate import interp1d
from subhalo_profile import make_profile, make_NFW
bin='0609'
if bin=='0609':
lowlim=0.6
highlim=0.9
elif bin=='0306':
lowlim=0.3
highlim=0.6
elif bin=='0103':
lowlim=0.1
highlim=0.3
lenses = Table.read("./data/redmapper_mnc_allz.fits")
data_mask = (
(lenses["R"] >= lowlim)
& (lenses["R"] < highlim)
& (lenses["PMem"] > 0.8)
# & (lenses["zspec"] > -1)
)
lenses = lenses[data_mask]
z=np.mean(lenses['z_any'])
def subhalo_profile(r,mass,A):
# print(mass)
# print(tau)
# print(z)
# print(A)
mass=math.pow(10, mass)
concentration_model="duffy08"
c=concentration.concentration(
M=mass, mdef="200m", z=z, model=concentration_model
)
# c=4.67*(mass/math.pow(10, 14))**(-0.11)
# halo_profile = profile_nfw.NFWProfile(M=mass, c=c, z=z, mdef="200m")
halo_profile=NFW(mass, c, z)
eta=2
# tnfw = TNFW(mass, c, z, tau, eta)
# R = np.linspace(0.01, 1.5, 75)
# dSigma=np.squeeze(tnfw.projected_excess(r))/1000000
# dSigma=halo_profile.deltaSigma(r*1000)
dSigma= np.squeeze(halo_profile.projected_excess(r))/1000000
# dSigma=nfw.projected_excess(R)
halo_table = np.genfromtxt(f'{bin}(Mh70).txt', delimiter='\t', usecols=(0, 1), dtype=float)
halo_r=halo_table[:,0]/1000
halo_ds=halo_table[:,1]
f = interp1d(halo_r, halo_ds, kind='cubic')
halo_dSigma=f(r)*A
summed_halo=np.add(dSigma,halo_dSigma)/1000000
return summed_halo
def Gaussian(params, r, y, y_err):
mass, A = params
model_prediction = subhalo_profile(r, mass, A)
sigma2 = y_err**2
return -0.5 * np.sum((y - model_prediction)**2 / sigma2 + np.log(sigma2))
def log_prior(params):
mass, A = params
if mass > 10 and A > 0:
return 0.0
return -np.inf
def log_probability(params, r, y, y_err):
lp = log_prior(params)
if not np.isfinite(lp):
return -np.inf
return lp + Gaussian(params, r, y, y_err)
# Read the CSV file
# df = pd.read_csv(f'D:/GitHub/summer-research/output-roman(correct)/roman_esd_ShapePipe_redmapper_clusterDist{lowlim}_randomsTrue_1.csv')
df = pd.read_csv(f'D:/roman_esd_ShapePipe_redmapper_clusterDist{lowlim}_randomsTrue_1.csv')
# df=pd.read_csv(f'D:/GitHub/summer-research/output/{bin}.txt',
# delim_whitespace = True,
# names = ['rp','ds','ds_err'],
# comment = '#')
# Save the "ds" and "rp" columns as variables
ds = df['ds']
rp = df['rp']
ds_err=df['ds_err']
ndim = 2
nwalkers = 100
mass_state=np.random.uniform(12, 13.2, size=nwalkers)
A_state=np.random.uniform(0.5, 1, size=nwalkers)
initial_positions = np.vstack((mass_state, A_state)).T
def proposal_function(p0, random):
new_p0 = p0 + random.normal(0, 0.1, size=p0.shape)
return new_p0, 0.0 # The second value is the log probability, set to 0 for symmetric proposal
MH=[emcee.moves.MHMove(proposal_function)]
sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, args=(rp, ds, ds_err), moves=MH)
nsteps = 5000
sampler.run_mcmc(initial_positions, nsteps, progress=True)
samples = sampler.get_chain(discard=1000, flat=True) # Discard the first 100 steps as burn-in
best_fit_params = np.median(samples, axis=0)
param_uncertainties = np.std(samples, axis=0)
lens_mass, param_A =best_fit_params
print("Best-fit parameters:")
print("Mass:", lens_mass)
print("A:", param_A)
lens_z=z
print("Parameter uncertainties:")
print("Mass uncertainty:", param_uncertainties[0])
# print("Tau uncertainty:", param_uncertainties[1])
print("A uncertainty:", param_uncertainties[1])
fit = subhalo_profile(rp, lens_mass, param_A)
residual=np.subtract(fit,ds)
residual_sq=[x**2 for x in residual] #get square of residuals
chi2=np.sum(np.array(residual_sq)/np.array(ds)) #chi^2
r_full,ds_host,ds_sub=make_NFW(math.pow(10,lens_mass), lens_z, param_A ,distbin=bin, plot=False)
# r_full,ds_full=make_profile(1e12, 0.35, 35, 0.6, distbin=bin, plot=False)
# plt.plot(rp, ds, 'bo', label='Isaac Data')
# plt.plot(rp, fit, 'r-', label='interp Curve')
plt.plot(r_full,ds_host, label='halo', linestyle='--', color='orange')
plt.plot(r_full,ds_sub,label='subhalo', linestyle='--', color='green')
plt.plot(r_full, ds_host+ds_sub, 'k-', label='fitted Curve')
plt.errorbar(rp, ds, ds_err, fmt='o',label='dsigma Data', color='tab:blue')
plt.xlabel('R (Mpc)')
plt.ylabel('M/pc^2')
plt.grid()
# plt.ylim(-40,120)
plt.title(f'{bin} lens log(mass): {lens_mass:.2f}, Z: {lens_z:.2}, A: {param_A:.2}')
plt.legend()
plt.show()
plt.close()
import corner
# Get the samples (chain)
# Extract the parameter names (you can customize them as needed)
param_names = ['log(Mass)', 'A']
# Plot the corner plot to visualize the parameter space
fig = corner.corner(samples, labels=param_names, truths=best_fit_params, quantiles=[0.16, 0.5, 0.84], show_titles=True)
plt.show()
# Plot the trace plots to see the evolution of the walkers
fig, axes = plt.subplots(ndim, figsize=(10, 7), sharex=True)
labels = [f"{i}" for i in param_names]
for i in range(ndim):
ax = axes[i]
ax.plot(sampler.chain[:, :, i].T, "k", alpha=0.3)
ax.set_ylabel(labels[i])
ax.yaxis.set_label_coords(-0.1, 0.5)
axes[-1].set_xlabel("Step")
plt.show()
# chain_file_path = f"{bin}sampler_chain.txt"
# np.savetxt(chain_file_path, sampler.chain.reshape(-1, ndim), header=" ".join(param_names), fmt='%f')
# # Save samples to a text file
# samples_file_path = f"{bin}samples.txt"
# np.savetxt(samples_file_path, samples, header=" ".join(param_names), fmt='%f')