
P1: JYS
c04 JWBK378-Fletcher May 23, 2009 4:21 Printer: Yet to come
54 Financial Modelling in Python
xcurr = root
xmid = 0.5*(xprev+xcurr)
if d+xmid*(c+xmid*(b+xmid*a)) > 0:
bounds.append([xprev, xcurr])
xprev = xcurr
xcurr = xh
xmid = 0.5*(xprev+xcurr)
if d+xmid*(c+xmid*(b+xmid*a)) > 0:
bounds.append([xprev, xcurr])
return bounds
Note that if there are real roots, then we loop through each of the roots and only add a
subinterval if the function at the mid-point is positive. The actual integration then reduces to
a loop over the bounds and is implemented in the method integral
indicator.
class normal distribution:
def integral
indicator(self, cs, indicator, xl, xh, yls = None,
yhs = None):
‘‘‘
>>> mean = 0.05
>>> vol = 0.1
>>> f = normal
distribution(mean, vol)
>>> yls = f.moments(4, -1)
>>> yhs = f.moments(4, 10000)
>>> cs = [0.0,0.0,1.0,1.0]
>>> print f.integral
indicator(cs, cs, -10000, 10000)
0.014125
>>> print cs[2]*(yhs[2]-yls[2])+cs[3]*(yhs[3]-yls[3])
0.014125
‘‘‘
if xl > xh:
raise RuntimeError, \
"lower bound greater than upper bound of integration" \
"domain"
bounds = self.
bounds (indicator, xl, xh)
sum=0
for bound in bounds:
xll = bound[0]
xrr = bound[1]
yll = None
yrr = None
if xll == xl:
yll = yls
if xrr == xh:
yrr = yhs
sum += self.integral(cs, xll, xrr, yll, yrr)
return sum
We will discover later that it is often useful to regrid a piecewise polynomial representation onto
another grid. The regridding algorithm is