R语言和QuantLib中Nelson-Siegel模型收益曲线建模分析

时间:2022-07-23
本文章向大家介绍R语言和QuantLib中Nelson-Siegel模型收益曲线建模分析,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 原文链接:http://tecdat.cn/?p=11803


Nelson-Siegel- [Svensson]模型是拟合收益曲线的常用方法。可以用其参数的经济可解释性来解释其受欢迎程度,但这很可能是因为欧洲中央银行使用了它。对ECB可能采取的措施不一定在所有情况下都有效:模型参数有时非常不稳定,无法收敛。

纳尔逊(Nelson)和西格尔(Siegel)在其原始论文中从远期利率入手,然后推导了收益率至到期曲线的公式.

Nelson-Siegel模型是简约的,可以生成丰富的收益曲线。

但是,由于简单地使用它,它通常失去了经济上的可解释性,甚至更糟的是无法收敛。

上图显示了这种情况,随后的R代码再现了这种情况。

plot(MATURITY_BASES, oldYields, main="Nelson-Siegel Calibration Failure in YieldCurve R Package")lines(MATURITY_BASES, oldYields)points(newMats, newYields, col="blue")lines(newMats, newYields, col="blue")points(newMats, newNsYields, col="red")lines(newMats, newNsYields, col="red")legend("topleft",legend=c("old observed yields","new observed yields","NS-Fit new yields"), col=c("black","blue", "red"),lty=1)

请注意,newMat [urities]比MATURITY_BASES短一天,因为此代码模仿了一个频繁使用的案例,因为当前的收益曲线与昨天的曲线进行了比较。从某种意义上讲,这是一个简单示例,因为对于给定的到期日,我们已经具有零收益率,并且“仅”需要连接点。实际上,我们通常与票息债券有关,这会使事情变得更加复杂。

您可能会认为,由于YieldCurve软件包的实施不佳而导致收敛失败。我要讲的不是不好的实现,而是要高度依赖所使用的数值方法,如下面的更实际的示例所示。

QuantLib提供更逼真的建模

/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* #include <ql/qldefines.hpp>#ifdef BOOST_MSVC#  include <ql/auto_link.hpp>#endif#include <ql/termstructures/yield/fittedbonddiscountcurve.hpp>#include <ql/termstructures/yield/piecewiseyieldcurve.hpp>#include <ql/termstructures/yield/flatforward.hpp>#include <ql/termstructures/yield/bondhelpers.hpp>#include <ql/termstructures/yield/nonlinearfittingmethods.hpp>#include <ql/pricingengines/bond/bondfunctions.hpp>#include <ql/time/calendars/target.hpp>#include <ql/time/daycounters/simpledaycounter.hpp>#include <boost/timer.hpp>#include <iostream>#include <iomanip>#include <boost/make_shared.hpp>#include "ql/time/daycounters/thirty360.hpp"#include "ql/time/daycounters/actualactual.hpp"#include "ql/math/optimization/conjugategradient.hpp"#include "ql/math/optimization/simplex.hpp"using namespace QuantLib;int main(int, char*[]) {    try {        Calendar calendar = NullCalendar();        Date today = Date(18, December, 2017);        Settings::instance().evaluationDate() = today;        //market data        double cleanPrices1[] = { 107.96, 135.88, 110.6,   133.46, 135.8,  142.155, 121.045, 134.97, 117.04,            101.61, 128.67, 106.615, 106.36, 99.515, 101.21,  105.655, 114.828 };        double cleanPrices2[] = { 107.9,  134.965, 110.37,  132.89, 135.62,140.845, 120.585, 133.995, 116.745,            101.58, 128.115,105.985, 105.395,99.385, 100.79,104.955, 114.7985 };        double cleanPrices3[] = { 107.96, 134.625, 110.58, 132.65, 135.145, 140.585, 120.385, 133.735, 116.635,            101.62, 127.925, 105.6, 105.085, 99.29, 100.6, 104.945, 114.7415 };        double cleanPrices4[] = { 107.78, 134.39, 110.175, 132.445, 134.905, 139.515, 120.115, 133.475, 116.455,            101.58, 127.845, 105.53,104.805, 99.07, 100.46, 104.885, 114.6225 };        double coupons[] = { 0.053, 0.045, 0.04, 0.0435, 0.044, 0.043, 0.03375, 0.03875, 0.03,            0.015, 0.03625, 0.01375, 0.01625, 0, 0.00625, 0.01875, 0.04 };        Real redemption = 100.0;        const double FACE_VALUE = 100.0;        //Bond helpers        const Size numberOfBonds = (sizeof(coupons) / sizeof(coupons[0]));        std::vector<boost::shared_ptr<BondHelper> > bondHelpersA;        std::vector< boost::shared_ptr<SimpleQuote> > quoteA;        std::vector<boost::shared_ptr<BondHelper> > bondHelpersB;        std::vector< boost::shared_ptr<SimpleQuote> > quoteB;        std::vector<boost::shared_ptr<BondHelper> > bondHelpersC;        std::vector< boost::shared_ptr<SimpleQuote> > quoteC;        std::vector<boost::shared_ptr<BondHelper> > bondHelpersD;        std::vector< boost::shared_ptr<SimpleQuote> > quoteD;        for (Size i = 0; i < numberOfBonds; i++) {            boost::shared_ptr<SimpleQuote> cp1(new SimpleQuote(cleanPrices1<em class="d4pbbc-italic"></em>));            quoteA.push_back(cp1);            boost::shared_ptr<SimpleQuote> cp2(new SimpleQuote(cleanPrices2<em class="d4pbbc-italic"></em>));            quoteB.push_back(cp2);            boost::shared_ptr<SimpleQuote> cp3(new SimpleQuote(cleanPrices3<em class="d4pbbc-italic"></em>));            quoteC.push_back(cp3);            boost::shared_ptr<SimpleQuote> cp4(new SimpleQuote(cleanPrices4<em class="d4pbbc-italic"></em>));            quoteD.push_back(cp4);        }        RelinkableHandle<Quote> quoteHandleA[numberOfBonds];        RelinkableHandle<Quote> quoteHandleB[numberOfBonds];        RelinkableHandle<Quote> quoteHandleC[numberOfBonds];        RelinkableHandle<Quote> quoteHandleD[numberOfBonds];        for (Size j = 0; j < numberOfBonds; j++) {            quoteHandleA[j].linkTo(quoteA[j]);            quoteHandleB[j].linkTo(quoteB[j]);            quoteHandleC[j].linkTo(quoteC[j]);            quoteHandleD[j].linkTo(quoteD[j]);        }        std::vector<boost::shared_ptr<BondHelper> > instrumentsA;        std::vector<boost::shared_ptr<BondHelper> > instrumentsB;        std::vector<boost::shared_ptr<BondHelper> > instrumentsC;        std::vector<boost::shared_ptr<BondHelper> > instrumentsD;        for (Size j = 0; j < numberOfBonds; j++) {            Date bondSettlementDate = calendar.advance(issueDates[j], settlementDays[j] * Days);            Schedule schedule(bondSettlementDate, maturityDates[j], Period(frequency),                calendar, accrualConvention, accrualConvention,                DateGeneration::Backward, false);            boost::shared_ptr<FixedRateBond> bond(new FixedRateBond(settlementDays[j],                FACE_VALUE,                schedule,                std::vector<Rate>(1, coupons[j]),                dc[j],                convention,                redemption,                issueDates[j]));            instrumentsA.push_back(helperA);            instrumentsB.push_back(helperB);            instrumentsC.push_back(helperC);            instrumentsD.push_back(helperD);        }        //Nelson-Siegel Fittings        Real tolerance = 1.0e-14;        Size max = 10000;        boost::shared_ptr<FittedBondDiscountCurve> tsA(            new FittedBondDiscountCurve(curveSettlementDays,                calendar,                instrumentsA,                ActualActual(),                NelsonSiegelFitting(),                tolerance,                max));        boost::shared_ptr<FittedBondDiscountCurve> tsB(            new FittedBondDiscountCurve(curveSettlementDays,                calendar,                instrumentsB,                ActualActual(),                NelsonSiegelFitting(),                tolerance,                max));        boost::shared_ptr<FittedBondDiscountCurve> tsC(            new FittedBondDiscountCurve(curveSettlementDays,                calendar,                instrumentsC,                ActualActual(),                NelsonSiegelFitting(),                tolerance,                max));        boost::shared_ptr<FittedBondDiscountCurve> tsD(            new FittedBondDiscountCurve(curveSettlementDays,                calendar,                instrumentsD,                ActualActual(),                NelsonSiegelFitting(),                tolerance,                max));        std::cout << tsA->fitResults().numberOfIterations() << std::endl;        std::cout << tsB->fitResults().numberOfIterations() << std::endl;        std::cout << tsC->fitResults().numberOfIterations() << std::endl;        std::cout << tsD->fitResults().numberOfIterations() << std::endl;        std::cout << tsA->fitResults().solution() << std::endl;        std::cout << tsB->fitResults().solution() << std::endl;        std::cout << tsC->fitResults().solution() << std::endl;        std::cout << tsD->fitResults().solution() << std::endl;        return 0;    }    catch (std::exception& e) {        std::cerr << e.what() << std::endl;        return 1;    }    catch (...) {        std::cerr << "unknown error" << std::endl;        return 1;    }}
*/

正式而言,收益曲线每天的变化并不显着,但是模型参数却可以:

Nelson-Siegel意识到了这些问题,并提供了解决这些问题的方法。特别是,他们考虑了Taus的时间序列,并确定了Taus的最佳拟合值的中值和合理范围。 但是,与往常一样,原始论文被引用的次数可能多于阅读次数。此外,如果需要按时间顺序排列的收益率数据,可能会感到困惑,而不是仅仅考虑相关日期的数据。即使处理时间序列不是问题,Nelson和Siegel也没有指定正式的算法来选择的最佳值。这就是我们将在后续工作中尝试做的,敬请期待!