-
Notifications
You must be signed in to change notification settings - Fork 10
/
linearfit_BCES.py
252 lines (220 loc) · 8.57 KB
/
linearfit_BCES.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 - Francesco de Gasperin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Bivariate, Correlated Errors and intrinsic Scatter (BCES)
translated from the FORTRAN code by Christina Bird and Matthew Bershady
(Akritas & Bershady, 1996)
Do linear regression including error on both variables and intrinsic scatter.
"""
import scipy
def bces(x1, x2, x1err=None, x2err=None, cerr=None, nsim=5000, model='yx', \
bootstrap=5000, verbose='normal', full_output=True):
"""
Bivariate, Correlated Errors and intrinsic Scatter (BCES)
translated from the FORTRAN code by Christina Bird and Matthew Bershady
(Akritas & Bershady, 1996)
Linear regression in the presence of heteroscedastic errors on both
variables and intrinsic scatter
Parameters
----------
x1 : array of floats
Independent variable, or observable
x2 : array of floats
Dependent variable
x1err : array of floats (optional)
Uncertainties on the independent variable
x2err : array of floats (optional)
Uncertainties on the dependent variable
cerr : array of floats (optional)
Covariances of the uncertainties in the dependent and
independent variables
nsim : int (default 1000)
Number of bootstrap samples for uncertainties on best-fit
parameters
model : {'yx', 'xy', 'bi', 'orth'}
BCES model with which to calculate regression. See Notes
below for details.
bootstrap : False or int (default False)
get the errors from bootstrap resampling instead of the
analytical prescription? if bootstrap is an int, it is the
number of bootstrap resamplings
verbose : str (default 'normal')
Verbose level. Options are {'quiet', 'normal', 'debug'}
full_output : bool (default True)
If True, return also the covariance between the normalization
and slope of the regression.
Returns
-------
a : tuple of length 2
Best-fit normalization and its uncertainty (a, da)
b : tuple of length 2
Best-fit slope and its uncertainty (b, db)
Optional outputs
----------------
cov : 2x2 array of floats
covariance between a and b. Returned if full_output is set to
True.
Notes
-----
If verbose is normal or debug, the results from all the BCES models will
be printed (still, only the one selected in *model* will be returned).
the *model* parameter:
-'yx' stands for BCES(Y|X)
-'xy' stands for BCES(X|Y)
-'bi' stands for BCES Bisector
-'orth' stands for BCES Orthogonal
"""
def _bess(npts, x1, x2, x1err, x2err, cerr):
"""
Do the entire regression calculation for 4 slopes:
OLS(Y|X), OLS(X|Y), bisector, orthogonal
"""
# calculate sigma's for datapoints using length of confidence intervals
sig11var = sum(x1err ** 2) / npts
sig22var = sum(x2err ** 2) / npts
sig12var = sum(cerr) / npts
# calculate means and variances
x1av = scipy.average(x1)
x1var = scipy.std(x1) ** 2
x2av = scipy.average(x2)
x2var = scipy.std(x2) ** 2
covar_x1x2 = sum((x1 - x1av) * (x2 - x2av)) / npts
# compute the regression slopes for OLS(X2|X1), OLS(X1|X2),
# bisector and orthogonal
b = scipy.zeros(4)
b[0] = (covar_x1x2 - sig12var) / (x1var - sig11var)
b[1] = (x2var - sig22var) / (covar_x1x2 - sig12var)
b[2] = (b[0] * b[1] - 1 + scipy.sqrt((1 + b[0] ** 2) * \
(1 + b[1] ** 2))) / (b[0] + b[1])
b[3] = 0.5 * ((b[1] - 1 / b[0]) + scipy.sign(covar_x1x2) * \
scipy.sqrt(4 + (b[1] - 1 / b[0]) ** 2))
# compute intercepts for above 4 cases:
a = x2av - b * x1av
# set up variables to calculate standard deviations of slope and intercept
xi = []
xi.append(((x1 - x1av) * (x2 - b[0] * x1 - a[0]) + b[0] * x1err ** 2) / \
(x1var - sig11var))
xi.append(((x2 - x2av) * (x2 - b[1] * x1 - a[1]) + x2err ** 2) / \
covar_x1x2)
xi.append((xi[0] * (1 + b[1] ** 2) + xi[1] * (1 + b[0] ** 2)) / \
((b[0] + b[1]) * scipy.sqrt((1 + b[0] ** 2) * (1 + b[1] ** 2))))
xi.append((xi[0] / b[0] ** 2 + xi[1]) * b[3] / \
scipy.sqrt(4 + (b[1] - 1 / b[0]) ** 2))
zeta = []
for i in range(4):
zeta.append(x2 - b[i] * x1 - x1av * xi[i])
# calculate variance for all a and b
bvar = scipy.zeros(4)
avar = scipy.zeros(4)
for i in range(4):
bvar[i] = scipy.std(xi[i]) ** 2 / npts
avar[i] = scipy.std(zeta[i]) ** 2 / npts
return a, b, avar, bvar, xi, zeta
def _bootspbec(npts, x, y, xerr, yerr, cerr):
"""
Bootstrap samples
"""
j = scipy.random.randint(npts, size = npts)
xboot = x[j]
xerrboot = xerr[j]
yboot = y[j]
yerrboot = yerr[j]
cerrboot = cerr[j]
return xboot, yboot, xerrboot, yerrboot, cerrboot
# ---- Main routine starts here ---- #
models = [['yx', 'xy', 'bi', 'orth'],
['BCES(Y|X)', 'BCES(X|Y)', 'BCES Bisector', 'BCES Orthogonal']]
# which to return?
j = models[0].index(model)
npts = len(x1)
# are the errors defined?
if x1err is None:
x1err = scipy.zeros(npts)
if x2err is None:
x2err = scipy.zeros(npts)
if cerr is None:
from scipy import random
cerr = scipy.zeros(npts)
#cerr = scipy.cov(x1err, x2err)[1][0] * scipy.ones(npts)
if verbose == 'debug':
print('x1 =', x1)
print('x1err =', x1err)
print('x2 =', x2)
print('x2err =', x2err)
print('cerr =', cerr)
print('\n ** Returning values for', models[1][j], '**')
if bootstrap is not False:
print(' with errors from %d bootstrap resamplings' %bootstrap)
print('')
# calculate nominal fits
bessresults = _bess(npts, x1, x2, x1err, x2err, cerr)
(a, b, avar, bvar, xi, zeta) = bessresults
# covariance between normalization and slope
if full_output:
covar_ab = scipy.cov(xi[j], zeta[j])
if bootstrap is not False:
# make bootstrap simulated datasets, and compute averages and
# standard deviations of regression coefficients
asum = scipy.zeros(4)
assum = scipy.zeros(4)
bsum = scipy.zeros(4)
bssum = scipy.zeros(4)
sda = scipy.zeros(4)
sdb = scipy.zeros(4)
for i in range(nsim):
samples = _bootspbec(npts, x1, x2, x1err, x2err, cerr)
(x1sim, x2sim, x1errsim, x2errsim, cerrsim) = samples
besssim = _bess(npts, x1sim, x2sim, x1errsim, x2errsim, cerrsim)
(asim, bsim, avarsim, bvarsim, xi, zeta) = besssim
asum += asim
assum += asim ** 2
bsum += bsim
bssum += bsim ** 2
aavg = asum / nsim
bavg = bsum / nsim
for i in range(4):
sdtest = assum[i] - nsim * aavg[i] ** 2
if sdtest > 0:
sda[i] = scipy.sqrt(sdtest / (nsim - 1))
sdtest = bssum[i] - nsim * bavg[i] ** 2
if sdtest > 0:
sdb[i] = scipy.sqrt(sdtest / (nsim - 1))
if verbose in ('normal', 'debug'):
print('%s B err(B)' %('Fit'.ljust(19)), end=' ')
print(' A err(A)')
for i in range(4):
print('%s %9.2e +/- %8.2e %10.3e +/- %9.3e' \
%(models[1][i].ljust(16), b[i],
scipy.sqrt(bvar[i]), a[i], scipy.sqrt(avar[i])))
if bootstrap is not False:
print('%s %9.2e +/- %8.2e %10.3e +/- %9.3e' \
%('bootstrap'.ljust(16), bavg[i], sdb[i], aavg[i], sda[i]))
print('')
if verbose == 'debug':
print('cov[%s] =' %models[model])
print(covar_ab)
if bootstrap is not False:
if full_output:
return (a[j], sda[j]), (b[j], sdb[j]), covar_ab
else:
return (a[j], sda[j]), (b[j], sdb[j])
if full_output:
return (a[j], scipy.sqrt(avar[j])), (b[j], scipy.sqrt(bvar[j])), covar_ab
else:
return (a[j], scipy.sqrt(avar[j])), (b[j], scipy.sqrt(bvar[j]))