Python for Finance: Analyze Big Financial Data

(Elle) #1

Approximation


To begin with, let us import the libraries that we need for the moment — NumPy and


matplotlib.pyplot:


In  [ 1 ]:  import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Throughout this discussion, the main example function we will use is the following, which


is comprised of a trigonometric term and a linear term:


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

The main focus is the approximation of this function over a given interval by regression


and interpolation. First, let us generate a plot of the function to get a better view of what


exactly the approximation shall achieve. The interval of interest shall be [–2,2].


Figure 9-1 displays the function over the fixed interval defined via the linspace function.


np.linspace(start, stop, num) returns num points beginning with start and ending


with stop, with the subintervals between two consecutive points being evenly spaced:


In  [ 3 ]:  x   =   np.linspace(- 2     *   np.pi,   2  *   np.pi,   50 )
In [ 4 ]: plt.plot(x, f(x), ‘b’)
plt.grid(True)
plt.xlabel(‘x’)
plt.ylabel(‘f(x)’)

Figure 9-1. Example function plot

Regression


Regression is a rather efficient tool when it comes to function approximation. It is not only


suited to approximate one-dimensional functions but also works well in higher


dimensions. The numerical techniques needed to come up with regression results are


easily implemented and quickly executed. Basically, the task of regression, given a set of


so-called basis functions bd, d ∈ {1,...,D}, is to find optimal parameters


according to Equation 9-1, where yi ≡ f(xi) for i ∈ {1,⋯, I} observation points. The xi are


considered independent observations and the yi dependent observations (in a functional or


statistical sense).


Equation 9-1. Minimization problem of regression

Free download pdf