-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathcurve_fit.jl
executable file
·330 lines (286 loc) · 9.83 KB
/
curve_fit.jl
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
struct LsqFitResult{P,R,J,W <: AbstractArray,T}
param::P
resid::R
jacobian::J
converged::Bool
trace::T
wt::W
end
StatsAPI.coef(lfr::LsqFitResult) = lfr.param
StatsAPI.dof(lfr::LsqFitResult) = nobs(lfr) - length(coef(lfr))
StatsAPI.nobs(lfr::LsqFitResult) = length(lfr.resid)
StatsAPI.rss(lfr::LsqFitResult) = sum(abs2, lfr.resid)
StatsAPI.weights(lfr::LsqFitResult) = lfr.wt
StatsAPI.residuals(lfr::LsqFitResult) = lfr.resid
mse(lfr::LsqFitResult) = rss(lfr) / dof(lfr)
isconverged(lsr::LsqFitResult) = lsr.converged
function check_data_health(xdata, ydata)
if any(ismissing, xdata) || any(ismissing, ydata)
error("Data contains `missing` values and a fit cannot be performed")
end
if any(isinf, xdata) || any(isinf, ydata) || any(isnan, xdata) || any(isnan, ydata)
error("Data contains `Inf` or `NaN` values and a fit cannot be performed")
end
end
# provide a method for those who have their own Jacobian function
function lmfit(f, g, p0::AbstractArray, wt::AbstractArray; kwargs...)
r = f(p0)
R = OnceDifferentiable(f, g, p0, copy(r); inplace=false)
lmfit(R, p0, wt; kwargs...)
end
# for inplace f and inplace g
function lmfit(f!, g!, p0::AbstractArray, wt::AbstractArray, r::AbstractArray; kwargs...)
R = OnceDifferentiable(f!, g!, p0, copy(r); inplace=true)
lmfit(R, p0, wt; kwargs...)
end
# for inplace f only
function lmfit(
f,
p0::AbstractArray,
wt::AbstractArray,
r::AbstractArray;
autodiff=:finite,
kwargs...,
)
R = OnceDifferentiable(f, p0, copy(r); inplace=true, autodiff=autodiff)
lmfit(R, p0, wt; kwargs...)
end
function lmfit(f, p0::AbstractArray, wt::AbstractArray; autodiff=:finite, kwargs...)
# this is a convenience function for the curve_fit() methods
# which assume f(p) is the cost functionj i.e. the residual of a
# model where
# model(xpts, params...) = ydata + error (noise)
# this minimizes f(p) using a least squares sum of squared error:
# rss = sum(f(p)^2)
#
# returns p, f(p), g(p) where
# p : best fit parameters
# f(p) : function evaluated at best fit p, (weighted) residuals
# g(p) : estimated Jacobian at p (Jacobian with respect to p)
# construct Jacobian function, which uses finite difference method
r = f(p0)
autodiff = autodiff == :forwarddiff ? :forward : autodiff
R = OnceDifferentiable(f, p0, copy(r); inplace=false, autodiff=autodiff)
lmfit(R, p0, wt; kwargs...)
end
function lmfit(
R::OnceDifferentiable,
p0::AbstractArray,
wt::AbstractArray;
autodiff=:finite,
kwargs...,
)
results = levenberg_marquardt(R, p0; kwargs...)
p = results.minimizer
converged = isconverged(results)
return LsqFitResult(p, value!(R, p), jacobian!(R, p), converged, results.trace, wt)
end
"""
curve_fit(model, xdata, ydata, p0) -> fit
curve_fit(model, xdata, ydata, wt, p0) -> fit
Fit data to a non-linear `model`. `p0` is an initial model parameter guess (see Example),
and `wt` is an optional array of weights.
The return object is a composite type (`LsqFitResult`), with some interesting values:
* `fit.resid` : residuals = vector of residuals
* `fit.jacobian` : estimated Jacobian at solution
additionally, it is possible to query the degrees of freedom with
* `dof(fit)`
* `coef(fit)`
## Example
```julia
# a two-parameter exponential model
# x: array of independent variables
# p: array of model parameters
model(x, p) = p[1]*exp.(-x.*p[2])
# some example data
# xdata: independent variables
# ydata: dependent variable
xdata = range(0, stop=10, length=20)
ydata = model(xdata, [1.0 2.0]) + 0.01*randn(length(xdata))
p0 = [0.5, 0.5]
fit = curve_fit(model, xdata, ydata, p0)
```
"""
function curve_fit end
function curve_fit(
model,
xdata::AbstractArray,
ydata::AbstractArray,
p0::AbstractArray;
inplace=false,
kwargs...,
)
check_data_health(xdata, ydata)
# construct the cost function
T = eltype(ydata)
if inplace
f! = (F, p) -> (model(F, xdata, p); @. F = F - ydata)
lmfit(f!, p0, T[], ydata; kwargs...)
else
f = (p) -> model(xdata, p) - ydata
lmfit(f, p0, T[]; kwargs...)
end
end
function curve_fit(
model,
jacobian_model,
xdata::AbstractArray,
ydata::AbstractArray,
p0::AbstractArray;
inplace=false,
kwargs...,
)
check_data_health(xdata, ydata)
T = eltype(ydata)
if inplace
f! = (F, p) -> (model(F, xdata, p); @. F = F - ydata)
g! = (G, p) -> jacobian_model(G, xdata, p)
lmfit(f!, g!, p0, T[], copy(ydata); kwargs...)
else
f = (p) -> model(xdata, p) - ydata
g = (p) -> jacobian_model(xdata, p)
lmfit(f, g, p0, T[]; kwargs...)
end
end
function curve_fit(
model,
xdata::AbstractArray,
ydata::AbstractArray,
wt::AbstractArray,
p0::AbstractArray;
inplace=false,
kwargs...,
)
check_data_health(xdata, ydata)
# construct a weighted cost function, with a vector weight for each ydata
# for example, this might be wt = 1/sigma where sigma is some error term
u = sqrt.(wt) # to be consistant with the matrix form
if inplace
f! = (F, p) -> (model(F, xdata, p); @. F = u * (F - ydata))
lmfit(f!, p0, wt, ydata; kwargs...)
else
f = (p) -> u .* (model(xdata, p) - ydata)
lmfit(f, p0, wt; kwargs...)
end
end
function curve_fit(
model,
jacobian_model,
xdata::AbstractArray,
ydata::AbstractArray,
wt::AbstractArray,
p0::AbstractArray;
inplace=false,
kwargs...,
)
check_data_health(xdata, ydata)
u = sqrt.(wt) # to be consistant with the matrix form
if inplace
f! = (F, p) -> (model(F, xdata, p); @. F = u * (F - ydata))
g! = (G, p) -> (jacobian_model(G, xdata, p); @. G = u * G)
lmfit(f!, g!, p0, wt, ydata; kwargs...)
else
f = (p) -> u .* (model(xdata, p) - ydata)
g = (p) -> u .* (jacobian_model(xdata, p))
lmfit(f, g, p0, wt; kwargs...)
end
end
function curve_fit(
model,
xdata::AbstractArray,
ydata::AbstractArray,
wt::AbstractMatrix,
p0::AbstractArray;
kwargs...,
)
check_data_health(xdata, ydata)
# as before, construct a weighted cost function with where this
# method uses a matrix weight.
# for example: an inverse_covariance matrix
# Cholesky is effectively a sqrt of a matrix, which is what we want
# to minimize in the least-squares of levenberg_marquardt()
# This requires the matrix to be positive definite
u = cholesky(wt).U
f(p) = u * (model(xdata, p) - ydata)
lmfit(f, p0, wt; kwargs...)
end
function curve_fit(
model,
jacobian_model,
xdata::AbstractArray,
ydata::AbstractArray,
wt::AbstractMatrix,
p0::AbstractArray;
kwargs...,
)
check_data_health(xdata, ydata)
u = cholesky(wt).U
f(p) = u * (model(xdata, p) - ydata)
g(p) = u * (jacobian_model(xdata, p))
lmfit(f, g, p0, wt; kwargs...)
end
function StatsAPI.vcov(fit::LsqFitResult)
# computes covariance matrix of fit parameters
J = fit.jacobian
if isempty(fit.wt)
r = fit.resid
# compute the covariance matrix from the QR decomposition
Q, R = qr(J)
Rinv = inv(R)
covar = Rinv * Rinv' * mse(fit)
else
covar = inv(J' * J)
end
return covar
end
function StatsAPI.stderror(fit::LsqFitResult; rtol::Real=NaN, atol::Real=0)
# computes standard error of estimates from
# fit : a LsqFitResult from a curve_fit()
# atol : absolute tolerance for approximate comparisson to 0.0 in negativity check
# rtol : relative tolerance for approximate comparisson to 0.0 in negativity check
covar = vcov(fit)
# then the standard errors are given by the sqrt of the diagonal
vars = diag(covar)
vratio = minimum(vars) / maximum(vars)
if !isapprox(
vratio,
0.0,
atol=atol,
rtol=isnan(rtol) ? Base.rtoldefault(vratio, 0.0, 0) : rtol,
) && vratio < 0.0
error("Covariance matrix is negative for atol=$atol and rtol=$rtol")
end
return sqrt.(abs.(vars))
end
function margin_error(fit::LsqFitResult, alpha=0.05; rtol::Real=NaN, atol::Real=0)
# computes margin of error at alpha significance level from
# fit : a LsqFitResult from a curve_fit()
# alpha : significance level, e.g. alpha=0.05 for 95% confidence
# atol : absolute tolerance for approximate comparisson to 0.0 in negativity check
# rtol : relative tolerance for approximate comparisson to 0.0 in negativity check
std_errors = stderror(fit; rtol=rtol, atol=atol)
dist = TDist(dof(fit))
critical_values = eltype(coef(fit))(quantile(dist, Float64(1 - alpha / 2)))
# scale standard errors by quantile of the student-t distribution (critical values)
return std_errors * critical_values
end
function StatsAPI.confint(fit::LsqFitResult; level=0.95, rtol::Real=NaN, atol::Real=0)
# computes confidence intervals at alpha significance level from
# fit : a LsqFitResult from a curve_fit()
# level : confidence level
# atol : absolute tolerance for approximate comparisson to 0.0 in negativity check
# rtol : relative tolerance for approximate comparisson to 0.0 in negativity check
std_errors = stderror(fit; rtol=rtol, atol=atol)
margin_of_errors = margin_error(fit, 1 - level; rtol=rtol, atol=atol)
return collect(zip(coef(fit) - margin_of_errors, coef(fit) + margin_of_errors))
end
@deprecate(confidence_interval(fit::LsqFitResult, alpha=0.05; rtol::Real=NaN, atol::Real=0),
confint(fit; level=(1 - alpha), rtol=rtol, atol=atol))
@deprecate estimate_covar(fit::LsqFitResult) vcov(fit)
@deprecate standard_errors(args...; kwargs...) stderror(args...; kwargs...)
@deprecate estimate_errors(
fit::LsqFitResult,
confidence=0.95;
rtol::Real=NaN,
atol::Real=0,
) margin_error(fit, 1 - confidence; rtol=rtol, atol=atol)