
金融科技科学计算【免费下载链接】tf-quant-financeHigh-performance TensorFlow library for quantitative finance.项目地址https://gitcode.com/gh_mirrors/tf/tf-quant-finance点击查看免费下载导读本文聚焦 tf-quant-finance 中tf_quant_finance.experimental.american_option_pricing.andersen_lake.calculate_exercise_boundary它是 Andersen-LakeAndersen, Lake Offengenden, 2015美式期权定价算法中迭代求解提前行权边界函数exercise boundary function的核心 API。在 tf-quant-finance 中该函数在 andersen_lake.py 的定价主流程中被调用用于生成随剩余期限变化的行权边界B(τ)随后边界被用于定价积分。读完本文你将掌握该函数的数学背景、参数语义、内部迭代原理、与相邻 APIboundary_numerator、boundary_denominator、三次样条插值、Gauss-Legendre 积分的协作关系并能基于仓库中的测试用例写出可直接运行的调用示例。一、函数定位美式期权定价流水线中的关键一环美式期权可在到期前任意时刻行权其定价难点在于行权边界的确定。Andersen-Lake 方法以连续时间框架近似求解边界calculate_exercise_boundary正是求解论文 [1] 中公式 (3.9) 的边界函数B的实现。它返回一个可调用对象Callable输入任意[num_options, n]的剩余期限张量输出同形状的边界价格张量。在 tf-quant-finance 中的完整调用链如下andersen_lake.py 中_andersen_lake通过calculate_exercise_boundary(tau_grid, k, r_e_b, q_e_b, sigma, ...)求得边界函数得到的边界函数再被gauss_kronrod用于两项定价积分func_1、func_2最终得到eu_prices term1 - term2的美式价格该函数别名calculate_exercise_boundary exercise_boundary.exercise_boundary直接映射到 exercise_boundary.py 中的exercise_boundary因此 API 文档中的calculate_exercise_boundary与源码模块exercise_boundary是同一实现。注意按remove_undocumented的_allowed_symbols见init.py模块公开符号仅暴露andersen_lakecalculate_exercise_boundary属于andersen_lake模块下的公开函数可通过tf_quant_finance.experimental.american_option_pricing.andersen_lake.calculate_exercise_boundary访问。二、函数签名与全部参数详解tf_quant_finance.experimental.american_option_pricing.andersen_lake.calculate_exercise_boundary( tau_grid, k, r, q, sigma, max_iterations20, tolerance1e-08, integration_num_points32, dtypeNone )参数说明参数形状含义默认值tau_grid[num_options, grid_num_points]到期剩余时间网格τ 网格按行对应每个期权必填k[num_options]期权行权价strike必填r[num_options]年化无风险利率连续复利必填q[num_options]股息率dividend rate必填sigma[num_options]标的收益波动率必填max_iterations标量int边界迭代最大次数提前收敛则提前停止20tolerance标量float新旧边界函数值的相对误差收敛阈值1e-8integration_num_points标量int积分近似所用 Gauss-Legendre 点数32dtypetf.DType所有输入张量的统一 dtypeNone时取tau_grid的 dtypeNone参数细节与源码佐证tau_grid 的构造方式在 andersen_lake.py 中网格由tf.linspace(tau / grid_num_points, tau, grid_num_points, axis-1)生成——即从τ/num_points到τ的等距划分grid_num_points由调用方andersen_lake的grid_num_points默认 10决定。因此tau_grid的第 0 列对应接近到期时刻最后一列对应当前时刻剩余期限 τ。dtype 语义源码 exercise_boundary.py 中先tf.convert_to_tensor(tau_grid, dtypedtype)再由tau_grid.dtype统一其余输入因此dtypeNone时实际由tau_grid决定。示例代码均使用tf.float64这与追求数值精度涉及高斯积分、样条插值、迭代收敛的场景一致。max_iterations 的实践值虽然 API 默认值为20但文档示例与andersen_lake定价入口分别使用了600和30测试用例见下文中分别使用100、200。这说明默认值适用于简单参数而高精度定价通常需要更大的迭代上限。三、数学原理从边界积分方程到迭代求解calculate_exercise_boundary实现的是论文 [1] 公式 (3.9) 中边界函数B的迭代计算。其单步更新在 exercise_boundary.py 的body中完成new_boundary k * exp(-(r - q) * tau_grid) * N / D其中分子N对应公式 (3.7)由boundary_numerator计算分母D对应公式 (3.8)由boundary_denominator计算。分子 N 的构成boundary_numerator源码 exercise_boundary.pyterm1standard_normal_cdf(d_minus(τ, B(τ)/k, r, q, σ))反映边界点处的立即行权收益概率term2r * ∫₀^τ exp(r·u) · Φ(d_minus(τ-u, B(τ)/B(u), r, q, σ)) du反映从当前时刻到到期的利率折现贡献。两者相加得到N返回形状[num_options, grid_num_points]。分母 D 的构成boundary_denominator源码 exercise_boundary.pyterm1standard_normal_cdf(d_plus(τ, B(τ)/k, r, q, σ))term2q * ∫₀^τ exp(q·u) · Φ(d_plus(τ-u, B(τ)/B(u), r, q, σ)) du。分子、分母共享同一被积结构仅将d_plus/d_minus、利率/股息参数互换体现了边界方程中看涨-看跌对偶的结构。辅助函数common.pyd_plus、d_minus、standard_normal_cdf、divide_with_positive_denominator、machine_eps均定义于 common.pyd_plus(τ, z, r, q, σ) (ln(max(z, ε)) (r - q σ²/2)·τ) / (σ·√τ ε)d_minus d_plus - σ·√τstandard_normal_cdf基于 SciPy 的ndtr实现对erf与erfc分段选取以保证数值稳定divide_with_positive_denominator在除以前先assert_greater_equal(0)并以b εε 为机器精度兜底防止数值上为 0 的分母导致除零——这是边界迭代数值稳定性的关键细节。四、迭代收敛机制与数值技巧迭代循环tf.while_loop源码 exercise_boundary.py 使用tf.while_loopcond只要converged为False就继续迭代body单步流程用build_spline(tau_grid, current_boundary_points)对当前边界点构造三次样条将样条封装为边界函数current_exercise_boundary_fn对输入做 reshape 以适配插值接口并用tf.math.maximum(interp, ε)夹逼为正分别调用boundary_numerator、boundary_denominator计算新边界点new_boundary_points计算相对误差max(|diff| / (|new| ε))若relative_error tolerance则置converged True。初始边界 b_0迭代起点 exercise_boundary.pyb_0(τ) k · min(1, r/q) 当 |q| ≥ ε b_0(τ) k 当 |q| ε即无股息时即到期瞬间的边界等于行权价当股息率接近 0 时退化为k。这与边界函数的理论极限一致。数值技巧要点样条而非逐点每次迭代先用三次样条平滑边界点再对任意 τ 求值interpolate从而支持积分过程中任意积分点的边界取值正数夹逼插值结果经tf.math.maximum(interpolation, epsilon)处理保证ln等运算不因非正值失效机器精度兜底相对误差分母加ε避免除零的同时不影响收敛判定的渐近行为while_loop 上限maximum_iterationsmax_iterations保证即使不收敛也不会无限循环。五、可运行示例完整继承自 API 文档API 文档给出的示例calculate_exercise_boundary.md演示了 3 个期权并行构造网格并计算边界的完整流程import tensorflow as tf import tf_quant_finance as tff tau tf.constant([0.01, 0.02, 1], dtypetf.float64) # 剩余期限 k tf.constant([1, 2, 3], dtypetf.float64) # 行权价 r tf.constant([0.01, 0.02, 0.035], dtypetf.float64) # 无风险利率 q tf.constant([0.01, 0.02, 0.07], dtypetf.float64) # 股息率 sigma tf.constant([0.1, 0.15, 0.32], dtypetf.float64) # 波动率 grid_num_points 40 max_iterations 600 tolerance 1e-8 integration_num_points 32 # 构造 tau 网格每个期权一行共 40 列 tau_grid tf.linspace(tf.constant(0.0001, dtypetf.float64), tau, grid_num_points, axis-1) exercise_boundary tff.experimental.american_option_pricing.andersen_lake.calculate_exercise_boundary boundary_fn exercise_boundary(tau_grid, k, r, q, sigma, max_iterations, tolerance, integration_num_points) # boundary_fn 是可调用对象传入 [num_options, n] 的 tau 张量即可求边界 result boundary_fn(tau_grid) # 返回形状 [3, 40] 的边界价格张量要点tau_grid为[3, 40]其中第 0 列是0.0001接近到期第 39 列是各期权的完整剩余期限返回的boundary_fn是Callable对任意[3, n]的输入均返回同形状结果因此可被 Gauss-Kronrod 积分器在任意积分点上复用文档示例为简洁省略了dtype若输入均为tf.float64张量则自动继承若想显式控制可传dtypetf.float64。六、与定价主流程andersen_lake的衔接calculate_exercise_boundary并非独立使用它在andersen_lake定价函数内部被调用其调用前有重要的参数预处理andersen_lake.py看涨/看跌对称化对看涨期权交换spot与strike、r与q使边界计算统一按看跌形式进行基于 American option parity零利率/零股息兜底当r q 0时理论上边界应为无穷大数值上不稳定因此计算边界时强制r_e_b q_e_b 0.1保证迭代收敛此时边界值对最终定价无影响积分维度定价阶段使用 Gauss-Kronrod默认 31 点、自适应、容差 1e-8而边界计算内部使用 Gauss-Legendreintegration_num_points默认 32两者分工明确——前者用于最终价格积分后者用于边界方程中的 0~τ 积分。得到boundary_fn后andersen_lake.py 中term1 r·k·∫₀^τ exp(-r(τ-u))·Φ(-d_minus(τ-u, s/B(u), r, q, σ)) du term2 q·s·∫₀^τ exp(-q(τ-u))·Φ(-d_plus(τ-u, s/B(u), r, q, σ)) du price european_price term1 - term2可见边界函数B(u)在每个积分点u上被求值get_ratio中s_exp / boundary_fn(u)这正是calculate_exercise_boundary返回 Callable 而非张量的原因。七、测试用例对行为的验证仓库中的 exercise_boundary_test.py 覆盖了三个层面可作为行为契约参考分子/分母形状test_numerator、test_denominator使用k[1,2,2]、tau_grid[[0.,0.5,1.],[0.,1.,2.],[0.,3.,6.]]等参数验证输出形状为(3, 3)收敛自洽性test_exercise_boundary断言“边界函数求出的边界”与“再用该边界作为输入迭代一步得到的新边界”在atol1e-8下一致——即返回的函数确实收敛到了不动点边界场景TestRZero无风险利率为 0与TestQZero股息率为 0两个用例验证了退化参数下边界计算的数值稳定性与上文r_e_b/q_e_b兜底逻辑呼应测试以test_util.run_all_in_graph_and_eager_modes装饰表明实现同时支持 TensorFlow 图模式与 Eager 模式执行。八、依赖组件速览组件路径作用boundary_numeratorexercise_boundary.py计算边界更新公式的分子 N公式 3.7boundary_denominatorexercise_boundary.py计算边界更新公式的分母 D公式 3.8standard_normal_cdf/d_plus/d_minuscommon.py正态分布与期权定价所需的变换函数gauss_legendregauss_legendre.py边界方程中 0~τ 积分的求积方法支持 2-15、20、32 点build_spline/interpolatecubic_interpolation.py边界点三次样条构造与求值machine_epscommon.py依据 dtype 返回机器精度用于数值兜底其中gauss_legendre对num_points有约束源码 gauss_legendre.py 中legendre_roots.get(num_points)仅支持预计算常量表中的点数文档标注 2-15、20、32非法值会抛出ValueError。若调整integration_num_points请选择这些取值之一。九、常见问题与使用建议Q1为什么返回的是函数而不是直接返回边界张量因为边界函数需要在后续 Gauss-Kronrod 积分中被任意积分点求值。返回 Callable 后andersen_lake的get_ratio可对[num_options, n, 31]的三维积分点批量求值内部 reshape 为二维后调用interpolate这正是 exercise_boundary.py 与 andersen_lake.py 中 reshape 逻辑的原因。Q2max_iterations如何选择API 默认 20但文档示例与测试分别使用 100~600。若边界尚未收敛可观察tolerance判定建议增大迭代上限测试中以atol1e-8验证收敛自洽可据此校准自己的参数。Q3为什么推荐tf.float64边界方程涉及积分、样条、迭代相对误差判定等多重数值运算float64 可显著降低误差累积。仓库示例与测试全部使用tf.float64。Q4可以直接把tau_grid第 0 列设为 0 吗不建议。示例从0.0001起步因为边界公式在 τ→0 处含σ·√τ分母common.py的d_plus使用ε兜底但接近 0 的网格能避免不必要的数值扰动。Q5该函数与american_option_pricing.andersen_lake的关系andersen_lake是完整定价入口calculate_exercise_boundary是其内部组件。若只关心边界本身如自定义定价逻辑可独立调用本函数若要做端到端定价直接调用 andersen_lake.py 的andersen_lake(...)即可注意其参数使用volatilities、strikes、expiries、spots/forwards等面向用户的命名。十、参考资料[1] Leif Andersen, Mark Lake and Dimitri Offengenden.High-performance American option pricing.2015.论文中公式 (3.7)-(3.9) 分别对应本文的分子 N、分母 D 与边界 B。文中涉及的实现文件均可直接在仓库中查阅exercise_boundary.py、common.py、andersen_lake.py、exercise_boundary_test.py。赞分享金融科技科学计算【免费下载链接】tf-quant-financeHigh-performance TensorFlow library for quantitative finance.项目地址https://gitcode.com/gh_mirrors/tf/tf-quant-finance点击查看免费下载相关推荐美式期权定价方法tf-quant-finance 中的 Andersen-Lake 算法实现美式期权定价一直是量化金融领域的核心挑战之一。与欧式期权不同美式期权可以在到期前的任何时间行权这使得其定价模型更加复杂。在 tf quant finance金融科技科学计算tf-quant-finance 中的 Bjerksund-Stensland 美式期权定价bjerksund_stensland 完全指南tf quant finance 中的 Bjerksund Stensland 美式期权定价 bjerksund_stensland 完全指南 在 tf qu金融科技科学计算酷我音乐API打造无缝音乐体验的开发利器酷我音乐API打造无缝音乐体验的开发利器 在数字化音乐浪潮席卷全球的今天开发者们正不断探索如何更便捷地接入丰富的音乐资源。酷我音乐API Node.js版应金融科技科学计算创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考