概述
“插值”是量化金融中最常用的工具之一,已知一组离散点以及未知函数 f 在这些点上的值 (xi,f(xi))i∈{0,…,n},要近似求出任意一点 x∈[x0,xn] 上的函数值。
标准的应用场景是对收益率曲线、波动率微笑曲线和波动率曲面的插值。quantlib-python 提供了下列一维和二维插值方法:
插值方法 | 数据维度 |
---|---|
LinearInterpolation | (1-D) |
LogLinearInterpolation | (1-D) |
BackwardFlatInterpolation | (1-D) |
ForwardFlatInterpolation | (1-D) |
BilinearInterpolation | (2-D) |
BicubicSpline | (2-D) |
一维插值方法
一维插值方法常用于收益率曲线、波动率微笑曲线,其对象的构造基本如下:
myInt = XXXInterpolation(x, y)
x:浮点数序列,若干离散的自变量
y:浮点数序列,自变量对应的函数值,与 x 等长
插值类定义了 call 方法,一个插值类对象的使用方式如下,作为一个函数
myInt(x, allowExtrapolation)
x:浮点数,要插值的点
allowExtrapolation:布尔型,allowExtrapolation 为 True 意味着允许外推,默认值是 False。
import QuantLib as ql
import numpy as np
#
xVec = [0.0, 1.0, 2.0, 3.0, 4.0]
yVec = [np.exp(x) for x in xVec]
linInt = ql.LinearInterpolation(xVec, yVec)
print("Exp at 0.0 ", linInt(0.0))
print("Exp at 0.5 ", linInt(0.5))
print("Exp at 1.0 ", linInt(1.0))
Exp at 0.0 1.0
Exp at 0.5 1.8591409142295225
Exp at 1.0 2.718281828459045
其中:
yVec:
[1.0,
2.718281828459045,
7.38905609893065,
20.085536923187668,
54.598150033144236]
参考文档:
https://www.cnblogs.com/xuruilong100/archive/2004/01/13/9873887.html