/
nr_numarray.py
401 lines (367 loc) · 11.1 KB
/
nr_numarray.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from math import *
import numarray as N
def fit(x, y, sig=None):
#Given a set of data points x, y,
#with individual standard deviations sig,
#fit them to a straight line y = a + bx by minimizing chi-sq.
#Returned are a, b and their respective probable uncertainties siga and sigb,
#the chi-square chi2, and the scatter sigdat.
#If mwt=0 on input, then the standard deviations are assumed to be unavailable:
#the normalization of chi2 is to unit standard deviation on all points.
if sig is None or N.all(sig == 1.0):
mwt=False
else:
mwt=True
sx=0.0
sy=0.0
st2=0.0
b=0.0
ndata = len(x)
if mwt:
#Accumulate sums ...
ss=0.0
for i in range(ndata):
#...with weights
wt=1.0/sig[i]**2
ss += wt
sx += x[i]*wt
sy += y[i]*wt
else:
for i in range(ndata):
#...or without weights.
sx += x[i]
sy += y[i]
ss=ndata
sxoss=sx/ss
if mwt:
for i in range(ndata):
t=(x[i]-sxoss)/sig[i]
st2 += t*t
b += t*y[i]/sig[i]
else:
for i in range(ndata):
t=x[i]-sxoss
st2 += t*t
b += t*y[i]
b /= st2
#Solve for a, b, siga, and sigb.
a=(sy-sx*b)/ss
siga=sqrt((1.0+sx*sx/(ss*st2))/ss)
sigb=sqrt(1.0/st2)
#Calculate chi2.
chi2=0.0
if mwt:
for i in range(ndata):
chi2 += ((y[i] - a - b*x[i]) / sig[i])**2
else:
#For unweighted data evaluate typical sig using chi2,
#and adjust the standard deviations.
for i in range(ndata):
chi2 += (y[i] - a - b*x[i])**2
sigdat=sqrt(chi2/(ndata-2))
siga *= sigdat
sigb *= sigdat
return a, b, siga, sigb, chi2
def fit_slope(x, y, sig, mwt, a=0.0, siga=0.0):
#Given a set of data points x, y,
#with individual standard deviations sig,
#fit them to a straight line y = a + bx by minimizing chi-sq,
#where the intercept of the line is fixed.
#Returned are b and its probable uncertainty sigb,
#the chi-square chi2, and the scatter sigdat.
#If mwt=0 on input, then the standard deviations are assumed to be unavailable:
#the normalization of chi2 is to unit standard deviation on all points.
sx=0.0
sy=0.0
sxx=0.0
sxy=0.0
b=0.0
ndata = len(x)
if mwt:
#Accumulate sums ...
ss = 0.0
for i in range(ndata):
#...with weights
wt=1.0/sig[i]**2
ss += wt
sx += x[i]*wt
sy += y[i]*wt
sxx += x[i]*x[i]*wt
sxy += x[i]*y[i]*wt
else:
for i in range(ndata):
#...or without weights.
sx += x[i]
sy += y[i]
sxx += x[i]*x[i]
sxy += x[i]*y[i]
ss=ndata
#Solve for b and sigb.
b = (sxy - a*sx) / sxx
sigb = sqrt(1.0 / sxx + (siga * sx / sxx)**2)
chi2=0.0
#Calculate chi2.
if mwt:
for i in range(ndata):
chi2 += ((y[i] - a - b*x[i]) / sig[i])**2
else:
#For unweighted data evaluate typical sig using chi2,
#and adjust the standard deviations.
for i in range(ndata):
chi2 += (y[i] - a - b*x[i])**2
sigdat=sqrt(chi2/(ndata-1))
sigb *= sigdat
return b, sigb, chi2
def fit_intercept(x, y, sig, mwt, b=0.0, sigb=0.0):
#Given a set of data points x, y,
#with individual standard deviations sig,
#fit them to a straight line y = a + bx by minimizing chi-sq,
#where the intercept of the line is fixed.
#Returned are a and its probable uncertainty siga,
#the chi-square chi2, and the scatter sigdat.
#If mwt=0 on input, then the standard deviations are assumed to be unavailable:
#the normalization of chi2 is to unit standard deviation on all points.
sx=0.0
sy=0.0
a=0.0
ndata = len(x)
if mwt:
#Accumulate sums ...
ss = 0.0
for i in range(ndata):
#...with weights
wt=1.0/sig[i]**2
ss += wt
sx += x[i]*wt
sy += y[i]*wt
else:
for i in range(ndata):
#...or without weights.
sx += x[i]
sy += y[i]
ss=ndata
#Solve for a and siga.
a = (sy - b*sx) / ss
siga = sqrt(1.0 / ss + (sigb * sx / ss)**2)
chi2=0.0
#Calculate chi2.
if mwt:
for i in range(ndata):
chi2 += ((y[i] - a - b*x[i]) / sig[i])**2
else:
#For unweighted data evaluate typical sig using chi2,
#and adjust the standard deviations.
for i in range(ndata):
chi2 += (y[i] - a - b*x[i])**2
sigdat=sqrt(chi2/(ndata-1))
siga *= sigdat
return a, siga, chi2
def fit_i(x, y, sig, int_scat=0.0):
#Given a set of data points x, y,
#with individual measurement error standard deviations sig,
#fit them to a straight line relation y = a + bx
#with intrinsic scatter int_scat by minimizing chi-sq.
#Returned are a, b and their respective probable uncertainties siga and sigb,
#the chi-square chi2, and the scatter sigdat.
#If mwt=0 on input, then the standard deviations are assumed to be unavailable:
#the normalization of chi2 is to unit standard deviation on all points.
sigtot = N.sqrt(sig**2 + int_scat**2)
return fit(x, y, sigtot)
def fit_slope_i(x, y, sig, int_scat=0.0, a=0.0, siga=0.0):
#Given a set of data points x, y,
#with individual standard deviations sig,
#fit them to a straight line y = a + bx
#with intrinsic scatter int_scat by minimizing chi-sq.
#where the intercept of the line is fixed.
#Returned are b and its probable uncertainty sigb,
#the chi-square chi2, and the scatter sigdat.
#If mwt=0 on input, then the standard deviations are assumed to be unavailable:
#the normalization of chi2 is to unit standard deviation on all points.
sigtot = N.sqrt(sig**2 + int_scat**2)
return fit_slope(x, y, sigtot, 1, a, siga)
def fit_intercept_i(x, y, sig, int_scat, b=0.0, sigb=0.0):
#Given a set of data points x, y,
#with individual standard deviations sig,
#fit them to a straight line y = a + bx by minimizing chi-sq,
#where the intercept of the line is fixed.
#Returned are a and its probable uncertainty siga,
#the chi-square chi2, and the scatter sigdat.
#If mwt=0 on input, then the standard deviations are assumed to be unavailable:
#the normalization of chi2 is to unit standard deviation on all points.
sigtot = N.sqrt(sig**2 + int_scat**2)
return fit_intercept(x, y, sigtot, 1, b, sigb)
def ttest(n1, ave1, var1, n2, ave2, var2):
df = n1+n2-2
svar = ((n1-1)*var1+(n2-1)*var2)/df
t = (ave1-ave2)/sqrt(svar*(1.0/n1+1.0/n2))
prob = betai(0.5*df,0.5,df/(df+t**2))
return t, prob
def tutest(n1, ave1, var1, n2, ave2, var2):
t = (ave1-ave2)/sqrt(var1/n1+var2/n2)
df = (var1/n1+var2/n2)**2/((var1/n1)**2/(n1-1)+(var2/n2)**2/(n2-1))
prob = betai(0.5*df,0.5,df/(df+t**2))
return t, prob
def ftest_samples(d1, d2):
m1 = mean(d1)
v1 = variance(d1, m=m1)
m2 = mean(d2)
v2 = variance(d2, m=m2)
if v1 > v2:
vr = v1/v2
df1 = len(d1)-1
df2 = len(d2)-1
else:
vr = v2/v1
df1 = len(d2)-1
df2 = len(d1)-1
prob = 2.0*betai(0.5*df2, 0.5*df1, df2/(df2+df1*vr))
if prob > 1.0: prob = 2.0 - prob
return prob
def ftest(n1, v1, n2, v2):
if v1 > v2:
vr = v1/v2
df1 = n1-1
df2 = n2-1
else:
vr = v2/v1
df1 = n2-1
df2 = n1-1
prob = 2.0*betai(0.5*df2, 0.5*df1, df2/(df2+df1*vr))
if prob > 1.0: prob = 2.0 - prob
return prob
def betai(a, b, x):
if (x < 0.0 or x > 1.0):
print "Bad x in routine betai"
if (x == 0.0 or x == 1.0):
bt=0.0
else:
bt = exp(gammln(a+b) - gammln(a) - gammln(b) + a*log(x) + b*log(1.0-x))
if (x < (a+1.0)/(a+b+2.0)):
return bt * betacf(a,b,x)/a
else:
return 1.0 - bt * betacf(b,a,1.0-x)/b
def betacf(a, b, x):
qab=a+b
qap=a+1.0
qam=a-1.0
c=1.0
d=1.0-qab*x/qap
FPMIN = 1.0e-30
EPS = 3.0e-7
if (abs(d) < FPMIN): d=FPMIN
d=1.0/d
h=d
for m in range(1, 101):
m2=2*m
aa=m*(b-m)*x/((qam+m2)*(a+m2))
d=1.0+aa*d
if (abs(d) < FPMIN): d=FPMIN
c=1.0+aa/c
if (abs(c) < FPMIN): c=FPMIN
d=1.0/d
h *= d*c
aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2))
d=1.0+aa*d
if (abs(d) < FPMIN): d=FPMIN
c=1.0+aa/c
if (abs(c) < FPMIN): c=FPMIN
d=1.0/d
delta=d*c
h *= delta
if (abs(delta-1.0) < EPS): break
if (m > 100): print "a or b too big, or MAXIT too small in betacf"
return h
def gammln(xx):
cof = [76.18009172947146,-86.50532032941677,
24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5]
y=x=xx
tmp=x+5.5
tmp -= (x+0.5)*log(tmp)
ser=1.000000000190015
for j in range(6):
y = y+1
ser += cof[j]/y
return -tmp+log(2.5066282746310005*ser/x)
def gammp(a, x):
if (x < 0.0 or a <= 0.0):
print "Invalid arguments in routine gammp"
if (x < (a+1.0)):
gamser, gln = gser(a,x)
return gamser
else:
gammcf, gln = gcf(a,x)
return 1.0-gammcf
def gser(a, x):
gln=gammln(a);
if (x <= 0.0):
if (x < 0.0):
print "x less than 0 in routine gser"
gamser=0.0
return
else:
ap=a
delta=sum=1.0/a
for n in range(1, 101):
ap +=1
delta *= x/ap
sum += delta
if (abs(delta) < abs(sum)*3.0e-7):
gamser=sum*exp(-x+a*log(x)-(gln))
break
if n>100: print "a too large, ITMAX too small in routine gser"
return gamser, gln
def gcf(a, x):
gln=gammln(a)
b=x+1.0-a
FPMIN = 1.0e-30
c=1.0/FPMIN
d=1.0/b
h=d
for i in range(1, 101):
an = -i*(i-a)
b += 2.0
d=an*d+b
if (abs(d) < FPMIN): d=FPMIN
c=b+an/c
if (abs(c) < FPMIN): c=FPMIN
d=1.0/d
delta=d*c
h *= delta
if (abs(delta-1.0) < 3.0e-7): break
if (i > 100): print "a too large, ITMAX too small in gcf"
gammcf=exp(-x+a*log(x)-(gln))*h;
return gammcf, gln
def erff(x):
e = gammp(0.5,x*x)
if x < 0.0: e = -e
return e
def erffc(x):
return 1.0 - erff(x)
def mean(x, s=None):
# calculates the weighted mean of x with errors s
if s is None:
return N.sum(x) / len(x)
else:
w = 1.0/s**2
sumw = N.sum(w)
sumx = N.sum(w*x)
return sumx/sumw
def variance(x, s=None, m=None):
# calculates the weighted variance of x with errors s and mean m
if m is None: m = mean(x, s)
if s is None:
sumdx2 = N.sum((x - m)**2)
return sumdx2/len(x)
else:
w = 1.0/s**2
sumw = N.sum(w)
sumdx2 = N.sum(w * (x - m)**2)
return sumdx2/sumw
def stderr(x, s, m):
# calculates the standard error of x with errors s about mean m
w = 1.0/s**2
sumw = N.sum(w)
sumw2 = N.sum(w*w)
sumdx2 = N.sum(w * (x - m)**2)
return sqrt(sumdx2/sumw2)