黑色Karasinski模型的校准

时间:2019-04-08 23:50:37

标签: python finance

我对 QuantLib python软件包还是很陌生,我一直在尝试运行和理解此python编写的代码,用于 Black Karasinski模型的校准,但是我在 ql.CalibrationHelper.RelativePriceError 行上遇到问题。我总是收到错误消息: AttributeError:'function'对象没有属性'RelativePriceError'

请任何知道哪里出了问题的人

import QuantLib as ql
from collections import namedtuple
import math

displacement = 0.
voltype = ql.Normal
def create_swaption_helpers(data, index, term_structure, engine):
    nominal = 1.0
    swaptions = [ql.SwaptionHelper(ql.Period(swap.start, ql.Years),
                                   ql.Period(swap.length, ql.Years),
                                   ql.QuoteHandle(ql.SimpleQuote(swap.volatility)),
                                   index, index.tenor(),
                                   index.dayCounter(), index.dayCounter(),
                                   term_structure,
                                   ql.CalibrationHelper.RelativePriceError,
                                   ql.nullDouble(),
                                   nominal,
                                   ql.ShiftedLognormal,
                                   displacement) for swap in data]
    for swap in swaptions:
        swap.setPricingEngine(engine)
    return swaptions

def calibration_report(swaptions, data):
    print ("-"*82)
    print ("%15s %15s %15s %15s %15s" % 
    "Model Price", "Market Price", "Implied Vol", "Market Vol", "RelError")
    print ("-"*82)
    cum_err = 0.0
    for i, s in enumerate(swaptions):
        model_price = s.modelValue()
        market_vol = data[i].volatility
        black_price = s.blackPrice(market_vol)
        rel_error = model_price/black_price - 1.0
        implied_vol = s.impliedVolatility(model_price,
                                          1e-5, 50, 0.0, 0.50)
        rel_error2 = implied_vol/market_vol-1.0
        cum_err += rel_error2*rel_error2

        print ("%15.5f %15.5f %15.5f %15.5f %15.5f" % 
        model_price, black_price, implied_vol, market_vol, rel_error)
    print ("-"*82)
    print ("Cumulative Error : %15.5f" % math.sqrt(cum_err))

today = ql.Date(15, ql.February, 2002);
settlement= ql.Date(19, ql.February, 2002);
term_structure = ql.YieldTermStructureHandle(
    ql.FlatForward(settlement,0.04875825,ql.Actual365Fixed())
    )
index = ql.Euribor1Y(term_structure)
CalibrationData = namedtuple("CalibrationData", 
                             "start, length, volatility")
data = [CalibrationData(1, 5, 0.1148),
        CalibrationData(2, 4, 0.1108),
        CalibrationData(3, 3, 0.1070),
        CalibrationData(4, 2, 0.1021),
        CalibrationData(5, 1, 0.1000 )]


model = ql.BlackKarasinski(term_structure)
engine = ql.TreeSwaptionEngine(model, 100)
swaptions = create_swaption_helpers(data, index, term_structure, engine)

optimization_method = ql.LevenbergMarquardt(1.0e-8,1.0e-8,1.0e-8)
end_criteria = ql.EndCriteria(10000, 100, 1e-6, 1e-8, 1e-8)
model.calibrate(swaptions, optimization_method, end_criteria)

1 个答案:

答案 0 :(得分:1)

这是包装中的问题。它来自基础C ++库中被重命名的类。该软件包试图提供一个向后兼容的别名,但是没有用。

要解决此问题,请改用ql.BlackCalibrationHelper.RelativePriceError(即类的新名称)。