量化分析师的Python日记【Q Quant兵器谱之函数插值】

在本篇中,我们将介绍Q宽客常用工具之一:函数插值。接着将函数插值应用于一个实际的金融建模场景中:波动率曲面构造。

通过本篇的学习您将学习到:

  1. 如何在scipy中使用函数插值模块:interpolate
  2. 波动率曲面构造的原理;

  1. 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
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值