Black Karasinski模型的校准

问题描述 投票:0回答:1

我是QuantLib python软件包的新手,我一直在尝试运行并理解这个用于校准黑卡拉辛斯基模型的python编写代码,但我在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)
python finance
1个回答
2
投票

这是包中的问题。它来自重命名的底层C ++库中的类。该程序包试图为向后兼容性提供别名,但它不起作用。

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

热门问题
推荐问题
最新问题