-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSALC.py
More file actions
158 lines (128 loc) · 4.66 KB
/
SALC.py
File metadata and controls
158 lines (128 loc) · 4.66 KB
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
# Running example of the application script. Define a model and simulate a small number of counts
# in the spectrum, define the energy ranges for counting counts, run simple MCMC with Metropolis to get the
# confidence on parameters
from ui import *
import sherpa.astro.ui as ui
from mhtest import *
execfile("funcs.py")
# needs to use cash or cstat for this analysis
set_stat("cash")
# define a model expression
model = "xsphabs.abs1*powlaw1d.p1"
parnames=np.array(['abs1.nh','p1.gamma','p1.ampl'])
arf1=unpack_arf("core1.arf")
rmf1=unpack_rmf("core1.rmf")
set_model(1, model)
abs1.nh = 0.1
p1.gamma = 2.0
# define the number of counts in the simulation and exposure time
expos=100
counts0=300
fake_pha(1,arf1,rmf1,exposure=expos)
p1.ampl = counts0/calc_data_sum()
fake_pha(1,arf1,rmf1,exposure=expos)
ranges= np.array([[.3,.9],[.9,2.5],[2.5,8.0]])
counts = np.zeros( ranges.shape[0] )
lamb = np.zeros( ranges.shape[0] )
lklhd = np.zeros( ranges.shape[0] )
for i in range(ranges.shape[0]):
notice( ranges[i,0] , ranges[i,1])
counts[i] = calc_data_sum(ranges[i,0] , ranges[i,1] )
lamb[i] = calc_model_sum(ranges[i,0] , ranges[i,1] )
lklhd[i] = np.exp(-.5*calc_stat() )
notice()
def log_sum( counts, ranges):
for i in range(ranges.shape[0]):
notice( ranges[i,0] , ranges[i,1])
lamb[i] = calc_model_sum(ranges[i,0] , ranges[i,1] )
lklhd[i] = counts[i] * np.log(lamb[i]) - lamb[i]
notice()
return np.sum(lklhd)
def z(counts,ranges,outfile='z.txt'):
L = 30
out = np.zeros( (L,L) )
for i in range(1,L,1):
for j in range(1,L,1):
if j==1:
print str(i)
abs1.nh = i*0.0333+0.001
p1.gamma = j*0.1
out[i,j] = log_sum(counts, ranges)
write_draws(out, outfile)
def grid(ranges,outfile='grid.txt'):
n = np.array([0.001,0.01,0.025,0.05,0.075,0.1,0.125,0.25,0.5,1])
g = np.array([0.1,0.25,0.5,0.75,1,1.5,2,3])
out = np.zeros( (g.size*n.size,4) )
lamb = np.zeros( ranges.shape[0] )
for i in range(g.size):
for j in range(n.size):
if j==1:
print str(i)
abs1.nh = n[j]
p1.gamma = g[i]
for k in range(ranges.shape[0]):
lamb[k] = calc_model_sum(ranges[k,0], ranges[k,1])
cS = np.log10(lamb[0]/lamb[1])
cH = np.log10(lamb[1]/lamb[2])
out[(n.size)*i+j] = np.hstack( (g[i],n[j],cS,cH) )
write_draws(out, outfile)
# Setup for running metropolis, initial parameters and scales for mvn based on covariance
# Note covar - may fail, change to the diagonal if this happens
#start = np.array([.1,.1,.1])
k = ranges.shape[0]-1
notice(ranges[0,0],ranges[k,1])
fit()
covar()
#start = np.array([0.1,1.5,0.0001]) # initial parameters in original code set by hand
start = get_fit_results().parvals
#sigma = np.diag( np.array([.0001,.0001,.0001]) ) # fixed scales in original
#sigma = np.diag(np.array(get_covar_results().parmaxes)**2) # diagonal matrix, no correlation
sigma = get_covar_results().extra_output
def metropolis(counts, ranges, parnames, start, sigma, outfile='out.txt', num_iter=1000 ):
iterations = np.zeros( (num_iter+1, parnames.size) )
statistics = np.zeros( (num_iter+1,1) )
current=np.copy(start)
_set_par_vals(parnames, current)
statistics[0] = log_sum( counts, ranges)
statistics[0] += -0.01*current[1] - np.log(current[2])
iterations[0] = np.copy(current)
zero_vec = np.zeros(parnames.size)
for i in range(1,num_iter+1,1):
if np.mod(i,100)==1:
print "draw "+str(i)
#print "current", current
current = np.copy(iterations[i-1])
while True:
try:
#if ((sigma==sigma_m).all()):
proposal = iterations[i-1] + np.random.multivariate_normal(zero_vec, sigma)
_set_par_vals(parnames, proposal)
break
except Exception:
pass
stat_temp = log_sum( counts, ranges)
stat_temp += -0.01*proposal[1] - np.log(proposal[2])
alpha = np.exp( stat_temp - statistics[i-1])
u = np.random.uniform(0,1,1)
if u <= alpha:
iterations[i]=np.copy(proposal)
statistics[i]=np.copy(stat_temp)
else:
iterations[i]=np.copy(iterations[i-1])
statistics[i]=np.copy(statistics[i-1])
burnin = int(np.round(num_iter*0.2)+1)
result = np.hstack( (statistics[np.arange(burnin,num_iter+1)],iterations[np.arange(burnin,num_iter+1)]) )
analyze_draws( result, parnames, 2, dict=False, verbose=True, means=True)
write_draws( iterations, outfile)
return result
draws = metropolis( counts, ranges, parnames, start, sigma, outfile="out.txt", num_iter=10000 )
# plot to investigate the performance of the simulations in terms of the MCMC runs, plot_trace()
# and parameter distributions - plot_pdf(), plot_cdf()
plot_trace(draws[:,2])
plot_pdf(draws[:,2], bins=100)
plot_cdf(draws[:,2])
print get_cdf_plot()
#means ={}
#for i,par in enumerate(parnames):
# means[par] = np.round(np.mean( draws[:,i+1]) , 3)
#means