-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurve_fitting.py
More file actions
307 lines (237 loc) · 9.46 KB
/
curve_fitting.py
File metadata and controls
307 lines (237 loc) · 9.46 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed May 22 14:54:51 2019
@author: Ben
"""
# ToC:
# interpolate(x_list, y_list, **kwargs)
# _lin_function(x_1, y_1, x_2, y_2, x)
# _linear_interpolation(x_list, y_list, resolution)
# _get_poly_coeffs(x_list, y_list)
# _poly_function(coeffs, x)
# _polynomial_interpolation(x_list, y_list, resolution)
# _get_spline_coeffs(x_list, y_list)
# _spline_function(coeffs, x)
# _spline_interpolation(x_list, y_list, start_2nd_d, end_2nd_d, resolution)
# curve_fit(x_list, y_list, n, resolution)
import numpy as np
import matplotlib.pyplot as plt
# all-purpose interpolation function
def interpolate(x_list, y_list, **kwargs):
"""
x_list: list - x points
y_list: list - y poitns
**kwargs:
interpolation_type: str - "linear", "polynomial", "spline", default is "linear"
resolution: int - approx density of points per 1 x unit
start_2nd_d: float - value of first 2nd derivative for cubic spline
end_2nd_d: float - value of last 2nd derivative for cubic spline
plots scatterplot and graph
"""
interpolation_type = None
resolution = 1000 # default resolution
start_2nd_d = 0 # for cubic spline
end_2nd_d = 0
# unpacking **kwargs
if kwargs is not None:
for key in kwargs.keys():
# print("%s == %s" %(key, kwargs[key]))
if key == 'interpolation_type':
interpolation_type = kwargs[key]
elif key == 'resolution':
resolution = kwargs[key]
elif key == 'start_2nd_d':
start_2nd_d = kwargs[key]
elif key == 'end_2nd_d':
end_2nd_d = kwargs[key]
if interpolation_type == 'linear':
print('Using linear interpolation')
x_plot, y_plot = _linear_interpolation(x_list, y_list, resolution)
title = 'Linear Interpolation'
elif interpolation_type == 'polynomial':
print('Using polynomial interpolation')
x_plot, y_plot = _polynomial_interpolation(x_list, y_list, resolution)
title = 'Polynomial Interpolation'
elif interpolation_type == 'spline':
print('Using cubic spline interpolation')
x_plot, y_plot = _spline_interpolation(x_list, y_list, start_2nd_d, end_2nd_d, resolution)
title = 'Cubic Spline Interpolation'
else:
print("Defaulting to linear interpolation")
x_plot, y_plot = _linear_interpolation(x_list, y_list, resolution)
title = 'Linear Interpolation'
print(f"Resolution: {resolution} points per x unit")
plt.scatter(x_list, y_list)
plt.plot(x_plot, y_plot)
plt.title(title)
plt.xlabel('x')
plt.ylabel('y')
# what about returning thea cutal points?
# linear
def _lin_function(x_1, y_1, x_2,y_2,x):
"""
x_1: float
y_1: float
x_2: float
y_2: float
x: float
returns tuple(x_plot:list, y_plot: list)
"""
return y_1 + (x-x_1)/(x_2-x_1)*(y_2-y_1)
def _linear_interpolation(x_list, y_list, resolution):
"""
x_list: list - x points
y_list: list
resolution: int - approx number of poitns per 1 x unit
returns tuple(x_plot: list, y_plot: list)
"""
x_plot = []
y_plot = []
# calculates points interval by interval
for i in range(len(x_list)-1):
x_points = np.linspace(x_list[i], x_list[i+1], int(abs(x_list[i+1]-x_list[i])*resolution)) # inclusive
y_points = _lin_function(x_list[i], y_list[i], x_list[i+1], y_list[i+1], x_points)
x_plot.extend(x_points)
y_plot.extend(y_points)
return x_plot, y_plot
# polynomial
def _get_poly_coeffs(x_list, y_list):
"""
x_list: list
y_list: list
returns polynomial coefficient of length {degree+1}
"""
length = len(x_list)
A = np.zeros((length,length))
b = np.zeros((length,1))
for i in range(length):
b[i] = y_list[i]
for j in range(length):
A[i][j] = np.power(x_list[i], length-1-j)
return np.linalg.inv(A).dot(b)
def _poly_function(coeffs, x):
"""
coeffs: list
x: float
calculates y(x) for polynomials of given coefficients
"""
length = len(coeffs)
output = 0
for i in range(length):
output += coeffs[i]*np.power(x, length-1-i)
return output
def _polynomial_interpolation(x_list, y_list, resolution):
"""
x_list: list - x points
y_list: list
resolution: int - approx number of points per 1 x unit
returns tuple(x_plot: list, y_plot: list)
"""
coeffs = _get_poly_coeffs(x_list, y_list)
x_plot = []
y_plot = []
# instantiate x points
for i in range(len(x_list)-1):
x_points = np.linspace(x_list[i], x_list[i+1], int(abs(xList[i+1]-x_list[i])*resolution))
y_points = _poly_function(coeffs, x_points)
x_plot.extend(x_points)
y_plot.extend(y_points)
print(f"Interpolating with polynomial of degree {len(coeffs)-1}")
return x_plot, y_plot
# spline interpolation
def _get_spline_coeffs(x_list, y_list, start_2nd_d, end_2nd_d):
"""
# for cubic spline
x_list: list - x points
y_list: list - y points
start_2nd_d: float - value of 2nd derivative for first point
end_2nd_d: float - value of 2nd derivative for last point
returns a vector of a, b, c, d coefficients for each cubic spline function
"""
#Ax = b
length = len(x_list)
A = np.zeros( ((length-1)*4, (length-1)*4) )
b = np.zeros((length-1)*4)
# first 2nd derivative - first row
A[0,0:2] = [6*x_list[0], 2]
b[0] = start_2nd_d
# loop over spline itnervals
for i in range(length-2): # stops at number of points-2, since last point is a special case
A[1 + 4*i + 0][i*4:(i+1)*4] = [x_list[i]**3, x_list[i]**2, x_list[i], 1]
b[1 + 4*i + 0] = y_list[i]
A[1 + 4*i + 1][i*4:(i+1)*4] = [3*x_list[i+1]**2, 2*x_list[i+1], 1, 0]
A[1 + 4*i + 1][(i+1)*4:(i+2)*4] = [-3*x_list[i+1]**2, -2*x_list[i+1], -1, 0]
A[1 + 4*i + 2][i*4:(i+1)*4] = [6*x_list[i+1], 2, 0, 0]
A[1 + 4*i + 2][(i+1)*4:(i+2)*4] = [-6*x_list[i+1], -2, 0, 0]
A[1 + 4*i + 3][i*4:(i+1)*4] = [x_list[i+1]**3, x_list[i+1]**2, x_list[i+1], 1]
b[1 + 4*i + 3] = y_list[i+1]
# last 3 rows
A[-3][-4:] = [x_list[-2]**3, x_list[-2]**2, x_list[-2], 1]
b[-3] = y_list[length-2]
A[-2][-4:] = [x_list[-1]**3, x_list[-1]**2, x_list[-1], 1]
b[-2] = y_list[-1]
A[-1][-4:] = [6*x_list[-1], 2, 0, 0]
b[-1] = end_2nd_d
return np.linalg.inv(A).dot(b)
def _spline_function(coeffs, x):
"""
coeffs: list - cubic spline coefficients
x: float
returns y(x) according to cubic spline coefficients
"""
a,b,c,d = coeffs
return a*x**3 + b*x**2 + c*x + d
def _spline_interpolation(x_list, y_list, start_2nd_d, end_2nd_d, resolution):
"""
x_list: list - x points
y_list: list - y points
start_2nd_d: float - value of 2nd derivative for first point
end_2nd_d: float - value of 2nd derivative for last point
resolution: int - approx number of points per 1 x unit in return tuple
returns tuple (x_plot: list, y_plot: list)
"""
coeffs = _get_spline_coeffs(x_list, y_list, start_2nd_d, end_2nd_d)
x_plot = []
y_plot = []
for i in range(len(x_list)-1):
x_points = np.linspace(x_list[i], x_list[i+1], int(abs(x_list[i+1]-x_list[i])*resolution))
y_points = _spline_function(coeffs[i*4 : (i+1)*4], x_points)
x_plot.extend(x_points)
y_plot.extend(y_points)
return x_plot, y_plot
# curve fitting
def curve_fit(x_list, y_list, deg, resolution):
"""
x_list: list - x points
y_list: list - y points, same length as x_list
deg: int - degree of polynomial to be fitted with LSR
resolution: int - approx number of points per x unit
"""
length = len(x_list)
# check so a polynomial of deg degree isn't underfitted
if deg > length-1:
print('Error: polynomial is underfit')
return # blank
# if exact fit or overfit, use pseudoinverse
else:
print(f"Curve-fitting using polynomial of degree {deg}")
x_plot = []
y_plot = []
# get coefficients
# Ax=b --> x ~ (A^t A)^-1 (A^t) b
A = np.zeros((length,deg+1))
b = np.array([y_list]).transpose() # column vector
# create A row by row
for i in range(length):
x_val = x_list[i]
for j in range(deg):
A[i][j] = x_val**(deg - j) # also includes ^0, but that's alright for now
A[i][deg] = 1
coeffs = (np.linalg.inv( A.transpose().dot(A) )).dot( (A.transpose()).dot(b) )
# get points
for i in range(length-1):
x_points = np.linspace(x_list[i], x_list[i+1], int(abs(x_list[i+1]-x_list[i])*resolution))
x_plot.extend(x_points)
y_points = _poly_function(coeffs, x_points)
y_plot.extend(y_points)
return (x_plot, y_plot)