Skip to content

9.3 数值积分

绝大多数金融模型的期望值、折现值都需要积分计算——但解析解往往不存在。数值积分是将这些连续问题离散化的基础工具。


梯形法则(Trapezoidal Rule)

公式

[a,b][a, b] 均分为 nn 个小区间,每个小区间用梯形近似:

abf(x)dxh2[f(a)+2i=1n1f(a+ih)+f(b)],h=ban\int_a^b f(x) dx \approx \frac{h}{2} \left[ f(a) + 2 \sum_{i=1}^{n-1} f(a + ih) + f(b) \right],\quad h = \frac{b-a}{n}

  • 误差阶O(h2)\mathcal{O}(h^2)O\mathcal{O}大O记号,表示数量级。O(h2)\mathcal{O}(h^2) 意思是误差与 h2h^2 成正比——hh 缩小一半,误差缩小到 1/41/4
  • 每增加一倍节点,误差约减少至 1/41/4

手算实例:01x2dx\int_0^1 x^2 dx 用 4 个梯形

n=4n = 4,步长 h=(10)/4=0.25h = (1 - 0)/4 = 0.25

节点 iixix_if(xi)=xi2f(x_i) = x_i^2权重
00.000.002=0.00000.00^2 = 0.00001(端点)
10.250.252=0.06250.25^2 = 0.06252
20.500.502=0.25000.50^2 = 0.25002
30.750.752=0.56250.75^2 = 0.56252
41.001.002=1.00001.00^2 = 1.00001(端点)

加权和=1×0+2×0.0625+2×0.2500+2×0.5625+1×1.0000=0+0.1250+0.5000+1.1250+1.0000=2.7500\begin{aligned} \text{加权和} &= 1 \times 0 + 2 \times 0.0625 + 2 \times 0.2500 + 2 \times 0.5625 + 1 \times 1.0000 \\ &= 0 + 0.1250 + 0.5000 + 1.1250 + 1.0000 \\ &= 2.7500 \end{aligned}

01x2dx0.252×2.7500=0.34375\int_0^1 x^2 dx \approx \frac{0.25}{2} \times 2.7500 = 0.34375

理论值01x2dx=[x33]01=130.33333\int_0^1 x^2 dx = \left[\frac{x^3}{3}\right]_0^1 = \frac{1}{3} \approx 0.33333

误差0.343750.33333=0.010420.34375 - 0.33333 = 0.01042(约 3.13%)


辛普森法则(Simpson's Rule)

使用抛物线(二次多项式)近似每个小区间,需要 nn 为偶数:

abf(x)dxh3[f(a)+4i=1,oddn1f(a+ih)+2i=2,evenn2f(a+ih)+f(b)]\int_a^b f(x) dx \approx \frac{h}{3} \left[ f(a) + 4 \sum_{i=1, \text{odd}}^{n-1} f(a+ih) + 2 \sum_{i=2, \text{even}}^{n-2} f(a+ih) + f(b) \right]

  • 误差阶O(h4)\mathcal{O}(h^4)
  • 权重模式:1,4,2,4,2,,4,11, 4, 2, 4, 2, \dots, 4, 1

同例:01x2dx\int_0^1 x^2 dx 用 Simpson 法则(n=4n=4

权重模式为 1,4,2,4,11, 4, 2, 4, 1

01x2dx0.253[1×0+4×0.0625+2×0.2500+4×0.5625+1×1.0000]=0.253[0+0.25+0.50+2.25+1.00]=0.253×4.00=0.33333\begin{aligned} \int_0^1 x^2 dx &\approx \frac{0.25}{3} [1 \times 0 + 4 \times 0.0625 + 2 \times 0.2500 + 4 \times 0.5625 + 1 \times 1.0000] \\ &= \frac{0.25}{3} [0 + 0.25 + 0.50 + 2.25 + 1.00] \\ &= \frac{0.25}{3} \times 4.00 = 0.33333 \end{aligned}

对于二次函数 f(x)=x2f(x) = x^2,Simpson 法则给出精确解——因为抛物线的二次多项式可以精确拟合二次函数。


Python 示例

python
import numpy as np

def trapezoidal(f, a, b, n):
    """梯形法则数值积分"""
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    y = f(x)
    return h * (0.5 * y[0] + 0.5 * y[-1] + np.sum(y[1:-1]))

def simpson(f, a, b, n):
    """辛普森法则数值积分(n 必须为偶数)"""
    if n % 2 != 0:
        raise ValueError("n 必须为偶数")
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    y = f(x)
    return h / 3 * (y[0] + y[-1] + 
                    4 * np.sum(y[1:-1:2]) + 
                    2 * np.sum(y[2:-2:2]))

f = lambda x: x**2
print(f"梯形法 (n=4): {trapezoidal(f, 0, 1, 4):.6f}")
print(f"Simpson (n=4): {simpson(f, 0, 1, 4):.6f}")
print(f"理论值:        {1/3:.6f}")
# 输出:
# 梯形法 (n=4): 0.343750
# Simpson (n=4): 0.333333
# 理论值:        0.333333

欧式期权定价

Black-Scholes 公式有解析解,但许多衍生品(如亚式期权、障碍期权)没有闭式解,需要使用数值积分:

V=erTPayoff(ST)q(ST)dSTV = e^{-rT} \int_{-\infty}^{\infty} \text{Payoff}(S_T) \cdot q(S_T) dS_T

其中 q(ST)q(S_T) 是到期价格的风险中性密度。当密度函数复杂或非参数化时,用数值积分计算。

扩展方法

方法精度适用场景
梯形法则O(h2)\mathcal{O}(h^2)一般积分,低维
Simpson 法则O(h4)\mathcal{O}(h^4)精度要求较高时
高斯求积(Gauss-Legendre quadrature——通过选择最优积分点位置和权重,用很少的点达到很高精度的数值求积方法)指数收敛光滑函数,最多 ~100 个节点
蒙特卡洛积分O(1/N)\mathcal{O}(1/\sqrt{N})高维积分(>3 维)

蒙特卡洛定价中,数值积分思想延伸为随机采样——高维积分(如亚式期权的路径积分)只能用 MC 方法。 \n> 下一步:继续学习 9.4 数值Greeks

Built with VitePress