QuantLib 1.34美式看跌期权Vega/Rho无法获取的问题咨询
问题背景
环境信息
操作系统中QuantLib版本:
import QuantLib as ql ql.__version__ '1.34'
美式看跌期权参数
settlementDate = ql.Date(11, ql.July, 2019) maturity = ql.Date(19, ql.July, 2019) stock = 0.28 strike = 0.5 riskFreeRate = 0.05 volatility = 1.7
定价代码实现
calendar = ql.UnitedStates(ql.UnitedStates.NYSE) dayCounter = ql.Actual365Fixed(ql.Actual365Fixed.Standard) ql.Settings.instance().evaluationDate = todayDate AmericanExercise(earliestDate, latestDate, payoffAtExpiry=False) AmericanExercise = ql.AmericanExercise(todayDate,maturity) optionType = ql.Option.Put payoff = ql.PlainVanillaPayoff(type=optionType, strike=strike) AmericanOption = ql.VanillaOption(payoff=payoff,exercise=AmericanExercise) underlying = ql.SimpleQuote(stock) underlyingH = ql.QuoteHandle(underlying) flatRiskFreeTS = ql.YieldTermStructureHandle( ql.FlatForward( settlementDate, riskFreeRate, dayCounter)) flatVolTS = ql.BlackVolTermStructureHandle( ql.BlackConstantVol( settlementDate, calendar, volatility, dayCounter)) bsProcess = ql.BlackScholesProcess( s0=underlyingH, riskFreeTS=flatRiskFreeTS, volTS=flatVolTS) steps = 200 binomial_engine = ql.BinomialVanillaEngine(bsProcess, "crr", steps) AmericanOption.setPricingEngine(binomial_engine)
期权价格及部分希腊值
期权价格:
print("Option value =", AmericanOption.NPV()) Option value = 0.22013426651607249
Delta、Gamma、Theta值:
print("Delta value =", AmericanOption.delta()) Delta value = -0.988975537620728 print("Gamma value =", AmericanOption.gamma()) Gamma value = 0.5635976654806573 print("Theta value =", AmericanOption.theta()) Theta value = -0.03899648147441449
问题现象
无法获取Vega、Rho值:
print("Theta value =", AmericanOption.theta()) Theta value = -0.03899648147441449 >>> print("Vega value =", AmericanOption.vega()) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/debian/mydoc/lib/python3.11/site-packages/QuantLib/QuantLib.py", line 17245, in vega return _QuantLib.OneAssetOption_vega(self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: vega not provided >>> print("Rho value =", AmericanOption.rho()) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/debian/mydoc/lib/python3.11/site-packages/QuantLib/QuantLib.py", line 17249, in rho return _QuantLib.OneAssetOption_rho(self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: rho not provided
确认AmericanOption对象包含vega和rho属性:
'vega' and 'rho' in dir(AmericanOption) True
咨询问题
- 为何运行时无法提供Vega和Rho值?
- 如何计算该美式看跌期权的Vega和Rho?
- 还有哪些支持美式BS模型的Python量化库?
解答
1. 无法获取Vega和Rho的原因
你使用的BinomialVanillaEngine(二叉树引擎)未内置Vega和Rho的直接计算逻辑。QuantLib中不同定价引擎对希腊值的支持范围不同:二叉树引擎主要实现了美式期权定价及Delta、Gamma、Theta这类与标的价格、时间相关的希腊值,但没有针对波动率(Vega)、无风险利率(Rho)敏感度的原生计算接口,因此调用时会抛出not provided错误。
2. 计算美式看跌期权的Vega和Rho的方法
通过有限差分法手动计算,核心思路是微调波动率/无风险利率,重新计算期权价格,再用差值除以变量变动幅度得到近似值:
计算Vega的示例代码
# 波动率微调幅度(取0.01即1%) vol_shift = 0.01 # 记录原价格 original_price = AmericanOption.NPV() # 创建调整后的波动率曲线 new_vol = volatility + vol_shift new_flatVolTS = ql.BlackVolTermStructureHandle( ql.BlackConstantVol(settlementDate, calendar, new_vol, dayCounter)) # 更新BS过程并绑定新引擎 new_bsProcess = ql.BlackScholesProcess(underlyingH, flatRiskFreeTS, new_flatVolTS) new_binomial_engine = ql.BinomialVanillaEngine(new_bsProcess, "crr", steps) AmericanOption.setPricingEngine(new_binomial_engine) # 计算新价格 new_price = AmericanOption.NPV() # 计算Vega(定义为波动率变动1%时的价格变化) vega = (new_price - original_price) / vol_shift print("Vega value =", vega) # 重置回原引擎 AmericanOption.setPricingEngine(binomial_engine)
计算Rho的示例代码
# 无风险利率微调幅度(取0.001即10个基点) rate_shift = 0.001 # 记录原价格 original_price = AmericanOption.NPV() # 创建调整后的无风险利率曲线 new_rate = riskFreeRate + rate_shift new_flatRiskFreeTS = ql.YieldTermStructureHandle( ql.FlatForward(settlementDate, new_rate, dayCounter)) # 更新BS过程并绑定新引擎 new_bsProcess = ql.BlackScholesProcess(underlyingH, new_flatRiskFreeTS, flatVolTS) new_binomial_engine = ql.BinomialVanillaEngine(new_bsProcess, "crr", steps) AmericanOption.setPricingEngine(new_binomial_engine) # 计算新价格 new_price = AmericanOption.NPV() # 计算Rho(定义为利率变动1%时的价格变化,需转换单位) rho = (new_price - original_price) / rate_shift * 0.01 print("Rho value =", rho) # 重置回原引擎 AmericanOption.setPricingEngine(binomial_engine)
3. 支持美式BS模型的Python量化库
- PyQL:QuantLib的另一种Python绑定实现,接口与原生QuantLib一致,支持美式期权定价。
- Scipy:利用其数值计算模块,可手动实现二叉树或有限差分法的美式BS模型定价与希腊值计算。
- Qlib:国内量化研究库,内置期权定价模块,支持美式期权的BS相关计算。
- py_vollib:专注期权定价的库,原生支持美式期权Black-Scholes模型及希腊值计算。
内容的提问来源于stack exchange,提问作者showkey
相关产品推荐
相关产品推荐

