-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfunctionfit.py
108 lines (80 loc) · 2.6 KB
/
functionfit.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
'''
A class that offers a fit on a time series.
Written by R. Jolivet, June 2014.
'''
import numpy as np
import datetime as dt
import scipy.optimize as sciopt
import sys
class functionfit(object):
'''
A class that fits a fiunction to a time series
Args:
* function : An objective function predicting the data
Kwargs:
* verbose : Talk to me
Returns:
* None
'''
def __init__(self, function, verbose=True):
# print
if verbose:
print ("---------------------------------")
print ("---------------------------------")
print ("Initialize a function fit object")
# Save the necessary bits
self.verbose = verbose
self.function = function
# All done
return
def doFit(self, timeseries, m0, solver='L-BFGS-B', iteration=1000, tol=1e-8):
'''
Performs the fit
Args:
* timeseries : instance of a timeseries class
* m0 : initial model
Kwargs:
* solver : type of solver from scipy.optimize.minimize
* iteration : maximum number of iteration
* tol : tolerance of the fit
Returns:
* None
'''
# Create the function to minimize
def residuals(m, data, function, time, err):
return np.sqrt(np.sum(1./err * (data-function(m, time))**2))
# Get stuff
data = timeseries.value
time = timeseries.time
err = timeseries.error
# Minimize
res = sciopt.minimize(residuals, m0,
args=(data, self.function, time, err),
method=solver,
options={'disp': self.verbose, 'maxiter': iteration},
tol=tol)
# Save
self.solution = res
self.m = res.x
# All done
return
def predict(self, timeseries, set2ts=True):
'''
Given the results of the fit, this routine predicts the time series.
Args:
* timeseries : timeseries instance.
Kwargs:
* set2ts : Put the results in timeseries.synth
Returns:
* None
'''
# Create
synth = np.zeros(timeseries.value.shape)
# Build the synthetics
synth += self.function(self.m, timeseries.time)
# All done
if set2ts:
timeseries.synth = synth
return
else:
return synth