Python for Finance: Analyze Big Financial Data

(Elle) #1

approach also works in general with quadratic and even linear splines. First, the importing


of the respective sublibrary:


In  [ 41 ]: import scipy.interpolate as spi
In [ 42 ]: x = np.linspace(- 2 * np.pi, 2 * np.pi, 25 )

We take again the original example function for illustration purposes:


In  [ 43 ]: def f(x):
return np.sin(x) + 0.5 * x

The application itself, given an x-ordered set of data points, is as simple as the application


of polyfit and polyval. Here, the respective functions are splrep and splev. Table 9-2


lists the major parameters that the splrep function takes.


Table 9-2. Parameters of splrep function


Parameter Description

x

(Ordered) x coordinates (independent variable values)

y

( x-ordered) y coordinates (dependent variable values)

w

Weights to apply to the y coordinates

xb, xe

Interval to fit, if None [x[0], x[-1]]

k

Order of the spline fit ( 1 <= k <= 5)

s

Smoothing factor (the larger, the more smoothing)

full_output

If True additional output is returned

quiet

If True suppress messages

Table 9-3 lists the parameters that the splev function takes.


Table 9-3. Parameters of splev function


Parameter Description

x

(Ordered) x coordinates (independent variable values)

tck

Sequence of length 3 returned by splrep (knots, coefficients, degree)

der

Order of derivative (0 for function, 1 for first derivative)
Free download pdf