-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorner Plot
171 lines (138 loc) · 4.78 KB
/
Corner Plot
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
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import math
import emcee
import corner
data = pd.read_csv('http://astroweb.cwru.edu/SPARC/SPARC_Lelli2016c.mrt', header=97, skipinitialspace=True,
delimiter=' ',
names=['Name', 'T', 'D', 'e_D', 'f_D', 'Inc', 'e_Inc', 'L[3.6]', 'e_L[3.6]',
'Reff', 'SBeff', 'Rdisk', 'SBdisk', 'MHI', 'RHI',
'vflat', 'e_vflat', 'Q', 'Ref'])
data2 = pd.read_csv('http://astroweb.cwru.edu/SPARC/MassModels_Lelli2016c.mrt', header=24, skipinitialspace=True,
delimiter=' ',
names=['ID', 'D', 'R', 'vobs', 'e_vobs', 'vgas', 'vdisk', 'vbul', 'SBdisk', 'SBbul', 'X'])
#--------------
#There are 2 things to change in this code-
#1) name
#2) model
#--------------
# USER INPUT
name = 'NGC5005' #Name of the galaxy you want fit for
model = 'Burkert' #pISO, Burkert, NFW are the options
#--------------
Name = data['Name']
gal = (Name == name)
data = data[gal]
i = np.array(data['Inc'])
e_i = np.array(data['e_Inc'])
e_D = np.array(data['e_D'])
ID = data2['ID']
gal1 = (ID == name)
data2 = data2[gal1]
vobs = np.array(data2['vobs'])
e_vobs = np.array(data2['e_vobs'])
R1 = np.array(data2['R'])
vdisk = np.array(data2['vdisk'])
vgas = np.array(data2['vgas'])
vbul = np.array(data2['vbul'])
D = np.array(data['D'])
def logn(x, mu, sig):
y = stats.lognorm.pdf(x, s=sig, loc = mu, scale = np.exp(mu))
return y
def gaus(x, mu, sig):
y = stats.norm.pdf(x, loc=mu, scale=sig)
return y
def pISO(v200, c200, d):
ho=73
rs = (v200*100)/(c200*ho)
R = R1*(d/D)
x = R/rs
vdm = (v200**2)*((1-(np.arctan(x)/x))/(1-(np.arctan(c200)/c200)))
return np.sqrt(np.absolute(vdm))
def burk(v200, c200, d):
ho = 73
rs = (v200*100)/(c200*ho)
R = R1*(d/D)
x = R/rs
vdm = (v200**2)*((c200/x)*((0.5*np.log(1+x**2))+np.log(1+x)-np.arctan(x))/
((0.5*np.log(1+c200**2))+np.log(1+c200)-np.arctan(c200)))
return np.sqrt(np.absolute(vdm))
def NFW(v200, c200, d):
ho = 73
rs = (v200*100)/(c200*ho)
R = R1*(d/D)
x = R/rs
vdm2 = (v200**2)*((c200*(np.log(1+x)-(x/(1+x))))/(x*(np.log(1+c200)-(c200/(1+c200)))))
return np.sqrt(np.absolute(vdm2))
def logpos(theta):
v200, c200, G, d, I = theta
if v200<10 or v200>500 or c200<0 or c200>100 or G<0 or d<0 or I<0 or I>90:
return -np.inf
gdisk = G
gbul = 1.4*gdisk
#Priors on our parameters
Dpr = np.log(gaus(d, D, e_D))
Ipr = np.log(gaus(I, i, e_i))
pgd = np.log(logn(gdisk, 0.5, 0.1))
pgb = np.log(logn(gbul, 0.7, 0.1))
#Likelihood
if model=='pISO':
vdm = pISO(v200, c200, d)
elif model=='Burkert':
vdm = burk(v200, c200, d)
elif model == 'NFW':
vdm = NFW(v200, c200, d)
vdisk1 = vdisk*(np.sqrt(d/D))
vbul1 = vbul*(np.sqrt(d/D))
vgas1 = vgas*(np.sqrt(d/D))
vobs1 = vobs*(math.sin(math.radians(i))/math.sin(math.radians(I)))
e_vobs1 = e_vobs*(math.sin(math.radians(i))/math.sin(math.radians(I)))
dy = vobs1-(np.sqrt(np.square(vdm)+(gdisk*np.square(vdisk1))+(gbul*np.square(vbul1))+np.square(vgas1)))
chi2 = np.sum(np.square(dy/e_vobs1))
ll = -0.5*chi2
return Dpr + Ipr + pgd + pgb + ll
ndim = 5
nwalkers = 20
nburn = 5000
nsteps = 25000
np.random.seed(0)
gue = np.zeros((nwalkers, ndim))
gue[:, 0] = np.random.normal(85, 10, nwalkers)
gue[:, 1] = np.random.normal(7.5, 2, nwalkers)
gue[:, 2] = np.random.normal(0.5, 0.1, nwalkers)
gue[:, 3] = np.random.normal(D, 0.5, nwalkers)
gue[:, 4] = np.random.normal(i, 3, nwalkers)
sample = emcee.EnsembleSampler(nwalkers, ndim, logpos)
sample.run_mcmc(gue, nsteps, progress=True)
print("Mean acceptance fraction: {0:.3f}".format(np.mean(sample.acceptance_fraction)))
flat_samples = sample.get_chain(discard=nburn, flat=True)
theta = np.mean(flat_samples[:,0:5], axis=0)
print(theta)
#-----Plotting-----
v200, c200, G, d, I = theta
R = R1*(d/D)
gdisk = G
gbul = 1.4*gdisk
vdisk1 = vdisk*(np.sqrt(d/D))
vbul1 = vbul*(np.sqrt(d/D))
vgas1 = vgas*(np.sqrt(d/D))
vobs1 = vobs*(math.sin(math.radians(i))/math.sin(math.radians(I)))
e_vobs1 = e_vobs*(math.sin(math.radians(i))/math.sin(math.radians(I)))
if model=='pISO':
vdm = pISO(v200, c200, d)
elif model=='Burkert':
vdm = burk(v200, c200, d)
elif model == 'NFW':
vdm = NFW(v200, c200, d)
vtot = (np.sqrt(np.square(vdm)+(gdisk*np.square(vdisk1))+(gbul*np.square(vbul1))+np.square(vgas1)))
plt.plot(R, vtot, 'r')
plt.plot(R, vdisk1, 'b--')
plt.plot(R, vbul1, 'purple')
plt.plot(R, vgas1, 'g:')
plt.plot(R, vdm, 'k-.')
plt.errorbar(R, vobs1, e_vobs1, fmt='ko-')
plt.title(name + ' ' + model + ' Fit')
corner.corner(flat_samples, labels=['v200', 'c200', 'G', 'd', 'I'], bins = 50)
#-----------------------