
P1: JYS
c09 JWBK378-Fletcher May 12, 2009 19:0 Printer: Yet to come
Pricing using Numerical Methods 133
y += alphas[i]*self. fos[i](x)
return y
def fit
fos(self):
return self.
fos
class fitted
fo:
def
init (self, alphas, fo):
self.
alphas = alphas
self.
fo = fo
def
call (self, x):
return self.
fo(self. alphas, x)
The actual fitting of the approximate sum to the pickup value is carried out by the free
function fit which, in turn, delegates to the generalised least squares algorithm imple-
mented in ppf.math.generalised
least square. The free function takes in the
explanatory variables x and the values to be fitted y and returns an instance of the class
fitted
fo.
def fit(x, y):
if len(x.shape) <>2:
raise RuntimeError, "Expected ’x’ to be 2d array"
if len(y.shape) <>1:
raise RuntimeError, "Expected ’y’ to be 1d array"
num
obs = x.shape[0]
num
expl vars = x.shape[1]
if num
obs <> y.shape[0]:
raise RuntimeError, "’y’ array has wrong size"
fo=n
quadratic fo(num expl vars)
sig = numpy.zeros(num
obs)
sig.fill(1.0)
alphas = generalised
least squares fit(y, x, sig, fo.fit fos())
return fitted
fo(alphas, fo)
Given a fitted function, we need to be able to evaluate it. The free function evaluate
regression, taking in the explanatory variables x and the fitted function fo, does this for
us.
def evaluate regression(x, fo):
if len(x.shape) <>2:
raise RuntimeError, "Expected ’x’ to be a 2d array"
num
obs = x.shape[0]
y = numpy.zeros(num
obs)
for i in range(num ...