-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitting_barsizes.py
424 lines (363 loc) · 12.5 KB
/
fitting_barsizes.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# code for performing and evaluating fits
import numpy as np
from scipy.optimize import curve_fit
import astrostat
# Functions for fits: returning one or array of values given input
# x (potentially multiple parts) and individual parameter values
# For use with scipy.optimize.curve_fit
def flin( x, a, b ):
"""Linear function of x
Parameters
----------
x : float or ndarray or list of float
x values (independent variable)
a, b : float
parameters for the model (intercept, slope)
Returns
-------
yy : ndarray of float
array of y values
"""
return a + b*x
def fbrokenlin( x, a1, b1, x_brk, b2 ):
"""Broken-linear function of x
Parameters
----------
x : float or ndarray or list of float
x values (independent variable)
a1, b1, x_brk, b2 : float
parameters for the model
Returns
-------
yy : ndarray of float
array of y values
The model is
y = a1 + b1*x for x < x_brk
y = a2 + b2*x for x > x_brk
Note that a2 is computed from the other parameters (it's not an independent
parameter, bcs. both equations have to be equal when x=x_brk)
"""
a2 = a1 + (b1 - b2)*x_brk
npts = len(x)
yy = []
for x_i in x:
if x_i < x_brk:
y_i = a1 + b1*x_i
else:
y_i = a2 + b2*x_i
yy.append(y_i)
return np.array(yy)
def fmulti_lin_brokenlin_old( X, a, b, a1, b1, x_brk, b2 ):
"""Composite function which add linear fit (a, b) to broken-linear
fit (rest of parameters)
*** THIS IS THE OLDER, INCORRECT VERSION ***
Parameters
----------
X : tuple of x1, x2
x1 : 1D numpy array of predictor using linear fit (e.g., log R_e)
x2 : 1D numpy array of predictor using broken-linear fit (e.g., log M_star)
a, b, a1, b1, x_brk, b2 : float
parameters for the model
Returns
-------
yy : ndarray of float
array of y values
The model is
y = a + b*x1 + a1 + b1*x2 for x < x_brk
y = a + b*x1 + a2 + b2*x2 for x > x_brk
Note that a2 is computed from the other parameters
"""
# unpack the two independent variables
# e.g., x1 = log(R_e), x2 = log(M_star)
x1,x2 = X
a2 = a1 + (b1 - b2)*x_brk
npts = len(x1)
yy = []
for i in range(npts):
x1_i = x1[i]
x2_i = x2[i]
if x2_i < x_brk:
y_i = a + b*x1_i + a1 + b1*x2_i
else:
y_i = a + b*x1_i + a2 + b2*x2_i
yy.append(y_i)
return np.array(yy)
def fmulti_lin_brokenlin( X, a1, b, b1, x_brk, b2 ):
"""Composite function which add linear fit (a, b) to broken-linear
fit (rest of parameters)
Parameters
----------
X : tuple of x1, x2
x1 : 1D numpy array of predictor using linear fit (e.g., log R_e)
x2 : 1D numpy array of predictor using broken-linear fit (e.g., log M_star)
a, b, a1, b1, x_brk, b2 : float
parameters for the model
Returns
-------
yy : ndarray of float
array of y values
The model is
y = a1 + b*x1 + b1*x2 for x < x_brk
y = a2 + b*x1 + b2*x2 for x > x_brk
Note that a2 is computed from the other parameters (it's not an independent
parameter, bcs. both equations have to be equal when x=x_brk)
"""
# unpack the two independent variables
# e.g., x1 = log(R_e), x2 = log(M_star)
x1,x2 = X
a2 = a1 + (b1 - b2)*x_brk
npts = len(x1)
yy = []
for i in range(npts):
x1_i = x1[i]
x2_i = x2[i]
if x2_i < x_brk:
y_i = a1 + b*x1_i + b1*x2_i
else:
y_i = a2 + b*x1_i + b2*x2_i
yy.append(y_i)
return np.array(yy)
def fmulti_binary( X, a, b, a1, b1, x_brk, b2 ):
"""Like fmulti_lin_brokenlin, but only computes the linear fit when the
value ofthe first data array is > 0
Parameters
----------
X : tuple of x1, x2
x1 : 1D numpy array of predictor using linear fit (e.g., log barsize)
For galaxies without bars, log barsize must = -inf
x2 : 1D numpy array of predictor using broken-linear fit (e.g., log M_star)
a, b, a1, b1, x_brk, b2 : float
parameters for the model
Returns
-------
yy : ndarray of float
array of y values
The model is
y = B*(a + b*x1) + a1 + b1*x2 for x < x_brk
y = B*(a + b*x1) + a2 + b2*x2 for x > x_brk
where B = 1 for barred galaxy (defined as x1 > 0) and 0 for unbarred
Note that a2 is computed from the other parameters
"""
# unpack the two independent variables
# e.g., x1 = log(barsize), x2 = log(M_star)
x1,x2 = X
a2 = a1 + (b1 - b2)*x_brk
npts = len(x1)
yy = []
for i in range(npts):
x1_i = x1[i]
x2_i = x2[i]
if x1_i > -90.0:
barTerm = a + b*x1_i
else:
barTerm = 0.0
if x2_i < x_brk:
y_i = barTerm + a1 + b1*x2_i
else:
y_i = barTerm + a2 + b2*x2_i
yy.append(y_i)
return np.array(yy)
def logLikelihood( x, y, errs, fitFn, params, debug=False ):
"""
Computes the log likelihood of the model described by fitFn, with
model parameters in params, given data described by x (independent
variable[s]) and y. Assumes chi^2 statistics.
Parameters
----------
x : ndarray of float (or tuple/list of two such arrays)
data -- independent variable values
y : ndarray of float
data -- dependent variable values
errs : ndarray of float
Gaussian sigma values describing uncertainties on y values
fitFn : function
function defining the model; will be called as fitFn(x, *params)
params : ndarray of float
parameter values for the model
Returns
-------
logLikelihood : float
= -0.5 * chi^2 from comparing computed model values with data
"""
y_model = fitFn(x, *params)
weights = 1.0 / errs**2
resid2 = (y - y_model)**2
chi2 = np.sum(weights * resid2)
if debug:
print("chi^2 = %g" % chi2)
return -0.5 * chi2
def PrintParams( params, prefix="", mode="linear" ):
"""
Pretty-printing of parameter values
Parameters
----------
params : ndarray of float
parameter values for the model
prefix : str
optional prefix to go at the head of printed output lines
mode : str
what type of model the parameters are for
one of ["linear", "broken-linear", "composite", or "binary"]
"""
if mode == "linear":
alpha, beta = params
print(prefix + "alpha, beta = [%g, %g]" % (alpha, beta))
elif mode == "broken-linear":
alpha1, beta1, x_break, beta2 = params
alpha2 = alpha1 + (beta1 - beta2)*x_break
print("alpha1, beta1, alpha2, beta2, x_break = ")
print("[%.3f, %.3f, %.3f, %.3f, %.3f]" % (alpha1, beta1, alpha2, beta2, x_break))
elif mode == "composite":
alpha1, beta, beta1, x_break, beta2 = params
alpha2 = alpha1 + (beta1 - beta2)*x_break
print("alpha1, beta, alpha2, beta1, beta2, x_break = ")
txt = "[%.3f, %.3f, %.3f, " % (alpha1, beta, alpha2)
txt += "%.3f, %.3f, %.3f]" % (beta1, beta2, x_break)
print(txt)
elif mode in "binary":
alpha, beta, alpha1, beta1, x_break, beta2 = params
alpha2 = alpha1 + (beta1 - beta2)*x_break
print("alpha, beta, alpha1, beta1, alpha2, beta2, x_break = ")
txt = "[%.3f, %.3f, %.3f, " % (alpha, beta, alpha1)
txt += "%.3f, %.3f, %.3f, %.3f]" % (beta1, alpha2, beta2, x_break)
print(txt)
def DoFit( x, y, errs, ii, p0, mode="linear", doPrint=True ):
"""
Fit model to data (x,y), returning best-fit parameters and AIC.
Parameters
----------
x : ndarray of float (or tuple/list of two such arrays)
data -- independent variable values
y : ndarray of float
data -- dependent variable values
errs : ndarray of float
Gaussian sigma values describing uncertainties on y values
ii : list of int
indices into x and y, specifying a particular subsample
(only x[ii] and y[ii] will be used for the fit)
p0 : ndarray of float
initialparameter values for the model
mode : str
specifies which model to fit to the data
One of ["linear", "broken-linear", "composite", "binary"]
doPrint : bool
If True, the best-fitting parameter values and the corresponding AIC
are printed at the end of the fit
Returns
-------
(p_bestfit, aic) : tuple of (ndarray of float, float)
p_bestfit = ndarray of best-fitting parameter values
aic = AIC (Akaike Information Criterion) for best-fit parameters
"""
if mode == "linear":
func = flin
xx = x[ii]
elif mode == "broken-linear":
func = fbrokenlin
xx = x[ii]
elif mode == "composite":
func = fmulti_lin_brokenlin
xx1,xx2 = x
xx1_sub = xx1[ii]
xx2_sub = xx2[ii]
xx = [xx1_sub,xx2_sub]
elif mode == "binary":
func = fmulti_binary
xx1,xx2 = x
xx1_sub = xx1[ii]
xx2_sub = xx2[ii]
xx = [xx1_sub,xx2_sub]
yy = y[ii]
if errs is not None:
ee = errs[ii]
nParams = len(p0)
pp, pcov = curve_fit(func, xx, yy, p0=p0, sigma=ee)
ll = logLikelihood(xx, yy, ee, func, pp)
aic = astrostat.AICc(ll, nParams, len(xx))
if doPrint:
PrintParams(pp, " ", mode)
print(" AIC = %g" % aic)
return (pp, aic)
def ParameterUncertainties( x, y, errs, ii, p0, mode="linear", nIterations=100 ):
"""
Estimate uncertainties for fitting model to data (x,y), using bootstrap
resampling.
Parameters
----------
x : ndarray of float (or tuple/list of two such arrays)
data -- independent variable values
y : ndarray of float
data -- dependent variable values
errs : ndarray of float
Gaussian sigma values describing uncertainties on y values
ii : list of int
indices into x and y, specifying a particular subsample
(only x[ii] and y[ii] will be used for the fit)
p0 : ndarray of float
initialparameter values for the model
mode : str
specifies which model to fit to the data
One of ["linear", "broken-linear", "composite", "binary"]
nIterations : int
Number of rounds of bootstrap resampling to do
Returns
-------
paramIntervals : list of (float, float)
list of (lower_limit,upper_limit) for parameters, where lower_limit
and upper_limit mark the confidence interval for the distribution
of a given parameter's bootstrap values.
This has the same number of tuples as the length of p0, with the
same ordering.
"""
if mode == "linear":
func = flin
xx = x[ii]
elif mode == "broken-linear":
func = fbrokenlin
xx = x[ii]
elif mode == "composite":
func = fmulti_lin_brokenlin
xx1,xx2 = x
xx1_sub = xx1[ii]
xx2_sub = xx2[ii]
xx = [xx1_sub,xx2_sub]
elif mode == "binary":
func = fmulti_binary
xx1,xx2 = x
xx1_sub = xx1[ii]
xx2_sub = xx2[ii]
xx = [xx1_sub,xx2_sub]
yy = y[ii]
if errs is not None:
ee = errs[ii]
nData = len(yy)
nParams = len(p0)
paramsArray = []
for i in range(nParams):
paramsArray.append([])
pp, pcov = curve_fit(func, xx, yy, p0=p0, sigma=ee)
indices = np.arange(0, nData)
nFailed = 0
for n in range(nIterations):
# generate bootstrap sample
try:
i_bootstrap = np.random.choice(indices, nData, replace=True)
if type(xx) in [tuple,list]:
xx_b = (xx[0][i_bootstrap], xx[1][i_bootstrap])
else:
xx_b = xx[i_bootstrap]
yy_b = yy[i_bootstrap]
sigma_b = ee[i_bootstrap]
pnew, pcov = curve_fit(func, xx_b, yy_b, p0=pp, sigma=sigma_b)
for i in range(nParams):
paramsArray[i].append(pnew[i])
except RuntimeError:
# couldn't get a proper fit, so let's discard this sample and try again
nFailed += 1
pass
paramIntervals = []
for i in range(nParams):
paramIntervals.append(astrostat.ConfidenceInterval(paramsArray[i]))
if nFailed > 0:
print("\tParameterUncertainties: %d failed iterations" % nFailed)
return paramIntervals