在本篇中,我们将介绍Q宽客常用工具之一:函数插值。接着将函数插值应用于一个实际的金融建模场景中:波动率曲面构造。
通过本篇的学习您将学习到:
- 如何在
scipy
中使用函数插值模块:interpolate
; - 波动率曲面构造的原理;
- 将
interpolate
运用于波动率曲面构造。
1. 如何使用scipy
做函数插值
函数插值,即在离散数据的基础上补插连续函数,估算出函数在其他点处的近似值的方法。在scipy
中,所有的与函数插值相关的功能都在scipy.interpolate
模块中
from scipy import interpolate
dir(interpolate)[:5]
['Akima1DInterpolator',
'BPoly',
'BarycentricInterpolator',
'BivariateSpline',
'CloughTocher2DInterpolator']
作为介绍性质的本篇,我们将只关注interpolate.spline
的使用,即样条插值方法:
xk
离散的自变量值,为序列yk
对应xk
的函数值,为与xk
长度相同的序列xnew
需要进行插值的自变量值序列order
样条插值使用的函数基德阶数,为1时使用线性函数
Interpolate a curve at new points using a spline fit
Parameters
----------
xk, yk : array_like
The x and y values that define the curve.
xnew : array_like
The x values where spline should estimate the y values.
order : int
Default is 3.
kind : string
One of {'smoothest'}
conds : Don't know
Don't know
Returns
-------
spline : ndarray
An array of y values; the spline evaluated at the positions `xnew`.
1.1 三角函数(np.sin
)插值
一例胜千言!让我们这里用实际的一个示例,来说明如何在scipy
中使用函数插值。这里的目标函数是三角函数:
f(x)=sin(x)
假设我们已经观测到的 f(x) 在离散点 x=(1,3,5,7,9,11,13) 的值:
1.1 三角函数(np.sin)插值
一例胜千言!让我们这里用实际的一个示例,来说明如何在scipy中使用函数插值。这里的目标函数是三角函数:
f(x)=sin(x)
假设我们已经观测到的f(x)在离散点x=(1,3,5,7,9,11,13)的值:
首先我们使用最简单的线性插值算法,这里面只要将
spline
的参数
order
设置为1即可:
xnew = np.linspace(1.0,13.0,500)
ynewLinear = interpolate.spline(x,y,xnew,order = 1)
ynewLinear[:5]
array([ 0.84147