
P1: JYS
c04 JWBK378-Fletcher May 23, 2009 4:21 Printer: Yet to come
44 Financial Modelling in Python
for j in range(n):
s = 0.0
for jj in range(n):
s += v[jj, j]*tmp[jj]
x[j] = s
return x
4.6 GENERALISED LINEAR LEAST SQUARES
Generalised linear least squares is a method for fitting a set of data points (x
i
, y
i
)
i = 1,..,N
to a
linear combination of basis functions. The general form of this kind of model is
y(x) =
M
k=1
a
k
f
k
(x) (4.9)
where f
1
(x), ..., f
M
(x) are the basis functions. The central idea behind the method is to find
the fitting coefficients a
1
, ..., a
M
by minimising the merit function
χ
2
=
N
i=1
y
i
−
M
k=1
a
k
f
k
(x
i
)
σ
i
2
. (4.10)
The σ
i
represent the measurement error, or equivalently the standard deviation, of the ith data
point. In [5] it is shown that the solution of the above equation can be calculated by solving
the normal equations
M
j=1
α
kj
a
j
= β
k
(4.11)
where
α
kj
=
N
i=1
f
j
(x
i
) f
k
(x
i
)
σ
2
i
(4.12)
and
β
k
=
N
i=1
y
i
f
k
(x
i
)
σ
2
i
. (4.13)
The normal equations can be solved using LU decomposition and backsubstitution but the solu-
tion is susceptible to roundoff error. It is common practice to use singular value decomposition
to solve this problem, and this is the route we have taken.
The implementation of the generalised linear least squares algorithm can be found in the
ppf.math.generalised
least squares module and the details of the implementa-
tion are shown below.
def generalised least squares fit(y, x, sig, fit fos):
tol = ...