20Method Of Least Squares (MOLS) for matching a piecewise model to the general
27from scipy
import integrate
29from .
import basis_functions
as bf
30from .
import gte_and_other_methods
as gom
32from .
import sampling_methods
as sm
36def bvec(points, f0, ngte, kgte):
37 return [gom.gte(t - bf.knotslist[0], f0, ngte, kgte)
for t
in points]
44 ints = len(bf.knotslist) - 1
45 firstzeros = [0] * j * s
46 lastzeros = [0] * (ints - 1 - j) * s
51 for thiss
in range(s):
52 coeffb = bf.basisfunctionvalue(point, j, 0, thiss, coeffs)
53 coeffc = bf.basisfunctionvalue(point, j, 1, thiss, coeffs)
55 coeffsb.append(coeffb)
56 coeffsc.append(coeffc)
58 return firstzeros + coeffsb + coeffsc + lastzeros
62def a(points, coeffs, s):
66 amat.append(
rowofa(point, coeffs, s))
73 return np.diag(bf.listpseudoinv(np.diagonal(mat), 1 / 2))
78 return np.matmul(np.transpose(mat), mat)
82def sols(coeffs, ppint, s, f0, ngte, kgte, conditioning=True):
85 points = sm.samplepoints(ppint, f0, ngte, kgte)
88 logging.debug(
"Reattempting MOLS fitting with greater sampling")
91 b =
bvec(points, f0, ngte, kgte)
92 amat =
a(points, coeffs, s)
99 pm = np.identity(len(atamat))
101 lhs = np.matmul(pm, np.matmul(atamat, pm))
102 rhs = np.matmul(np.matmul(pm, np.transpose(amat)), b)
105 "Condition number for matrix A is: " +
"{:.2E}".
format(np.linalg.cond(amat))
108 "Condition number for matrix A' is: " +
"{:.2E}".
format(np.linalg.cond(lhs))
112 params = np.matmul(pm, np.linalg.solve(lhs, rhs))
113 except np.linalg.LinAlgError
as error:
116 "Error in calculating MOLS parameters, using Python LSTSQ method instead"
118 params = np.matmul(pm, np.linalg.lstsq(lhs, rhs, rcond=-1)[0])
125 ints =
int(len(params) / s) - 1
127 partedsols = np.zeros((ints, 2, s))
129 for i
in range(ints):
130 solsb = np.array(params[i * s : (i + 1) * s])
131 solsc = np.array(params[(i + 1) * s : (i + 2) * s])
133 partedsols[i][0] = solsb
134 partedsols[i][1] = solsc
140 knotnuma, knotnumb, coeffs, ppint, s, f0, ngte, kgte, conditioning=True
145 points = sm.samplepointswithinknots(
146 knotnuma, knotnumb, ppint, f0, ngte, kgte
150 logging.debug(
"Reattempting MOLS fitting with greater sampling")
153 b =
bvec(points, f0, ngte, kgte)
154 amat =
a(points, coeffs, s)
156 for i
in range(knotnuma * s):
157 [row.pop(0)
for row
in amat]
164 pm = np.identity(len(atamat))
166 lhs = np.matmul(pm, np.matmul(atamat, pm))
167 rhs = np.matmul(np.matmul(pm, np.transpose(amat)), b)
170 "Condition number for matrix A is: " +
"{:.2E}".
format(np.linalg.cond(amat))
173 "Condition number for matrix A' is: " +
"{:.2E}".
format(np.linalg.cond(lhs))
177 params = np.matmul(pm, np.linalg.solve(lhs, rhs))
178 except np.linalg.LinAlgError
as error:
181 "Error in calculating MOLS parameters, using Python LSTSQ method instead"
183 params = np.matmul(pm, np.linalg.lstsq(lhs, rhs)[0])
185 zerobuff = [0] * (s * knotnuma)
186 paramszerobuff = zerobuff + list(params)
188 return paramszerobuff
196 s = len(params[0][0])
200 j = sm.thisint(point)
203 t = point - bf.knotslist[0]
213 return f0 + f1 * t + 1 / 2 * f2 * t**2
214 elif t > bf.knotslist[-1] - bf.knotslist[0]:
215 f0 = params[-1][1][0]
216 f1 = params[-1][1][1]
220 f2 = params[-1][1][2]
222 return f0 + f1 * t + 1 / 2 * f2 * t**2
226 j = sm.thisint(point)
233 s = np.shape(params)[2]
237 for thiss
in range(s):
238 basisfuncs = bf.basisfunctionvalue(point, j, 0, thiss, coeffs)
239 basisfunce = bf.basisfunctionvalue(point, j, 1, thiss, coeffs)
242 val += params[0][0][thiss] * basisfuncs
243 val += params[0][1][thiss] * basisfunce
245 val += params[j][0][thiss] * basisfuncs
246 val += params[j][1][thiss] * basisfunce
251def phase(point, coeffs, params, ignoreintcheck=False):
254 t, coeffs, params, ignoreintcheck=ignoreintcheck
257 phasemodel = 2 * np.pi * integrate.quad(model, bf.knotslist[0], point, epsabs=0)[0]
266 - gom.gte(point - bf.knotslist[0], f0, ngte, kgte)
def solsbetweenknots(knotnuma, knotnumb, coeffs, ppint, s, f0, ngte, kgte, conditioning=True)
def phase(point, coeffs, params, ignoreintcheck=False)
def modelvalueatpoint(point, coeffs, params, ignoreintcheck=False, singleseg=False)
def errorvalueatpoint(point, coeffs, params, f0, ngte, kgte)
def bvec(points, f0, ngte, kgte)
def rowofa(point, coeffs, s)
def sols(coeffs, ppint, s, f0, ngte, kgte, conditioning=True)