日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++:实现量化基础互换利率basis swap rate helpers测试实例

發(fā)布時間:2023/12/18 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++:实现量化基础互换利率basis swap rate helpers测试实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C++:實現(xiàn)量化基礎(chǔ)互換利率basis swap rate helpers測試實例

#include "basisswapratehelpers.hpp" #include "utilities.hpp" #include <ql/experimental/termstructures/basisswapratehelpers.hpp> #include <ql/indexes/ibor/sofr.hpp> #include <ql/indexes/ibor/usdlibor.hpp> #include <ql/cashflows/iborcoupon.hpp> #include <ql/cashflows/overnightindexedcoupon.hpp> #include <ql/instruments/swap.hpp> #include <ql/pricingengines/swap/discountingswapengine.hpp> #include <ql/termstructures/yield/piecewiseyieldcurve.hpp> #include <ql/time/calendars/unitedstates.hpp>using namespace QuantLib; using namespace boost::unit_test_framework;namespace basisswapratehelpers_test {struct BasisSwapQuote {Integer n;TimeUnit units;Spread basis;};void testIborIborBootstrap(bool bootstrapBaseCurve) {std::vector<BasisSwapQuote> quotes = {{ 1, Years, 0.0010 },{ 2, Years, 0.0012 },{ 3, Years, 0.0015 },{ 5, Years, 0.0015 },{ 8, Years, 0.0018 },{ 10, Years, 0.0020 },{ 15, Years, 0.0021 },{ 20, Years, 0.0021 },};auto settlementDays = 2;auto calendar = UnitedStates(UnitedStates::GovernmentBond);auto convention = Following;auto endOfMonth = false;Handle<YieldTermStructure> knownForecastCurve(flatRate(0.01, Actual365Fixed()));Handle<YieldTermStructure> discountCurve(flatRate(0.005, Actual365Fixed()));ext::shared_ptr<IborIndex> baseIndex, otherIndex;if (bootstrapBaseCurve) {baseIndex = ext::make_shared<USDLibor>(3 * Months);otherIndex = ext::make_shared<USDLibor>(6 * Months, knownForecastCurve);} else {baseIndex = ext::make_shared<USDLibor>(3 * Months, knownForecastCurve);otherIndex = ext::make_shared<USDLibor>(6 * Months);}std::vector<ext::shared_ptr<RateHelper>> helpers;for (auto q : quotes) {auto h = ext::make_shared<IborIborBasisSwapRateHelper>(Handle<Quote>(ext::make_shared<SimpleQuote>(q.basis)),Period(q.n, q.units), settlementDays, calendar, convention, endOfMonth,baseIndex, otherIndex, discountCurve, bootstrapBaseCurve);helpers.push_back(h);}auto bootstrappedCurve = ext::make_shared<PiecewiseYieldCurve<ZeroYield, Linear>>(0, calendar, helpers, Actual365Fixed());Date today = Settings::instance().evaluationDate();Date spot = calendar.advance(today, settlementDays, Days);if (bootstrapBaseCurve) {baseIndex = ext::make_shared<USDLibor>(3 * Months, Handle<YieldTermStructure>(bootstrappedCurve));otherIndex = ext::make_shared<USDLibor>(6 * Months, knownForecastCurve);} else {baseIndex = ext::make_shared<USDLibor>(3 * Months, knownForecastCurve);otherIndex = ext::make_shared<USDLibor>(6 * Months, Handle<YieldTermStructure>(bootstrappedCurve));}for (auto q : quotes) {// create swaps and check they're fairDate maturity = calendar.advance(spot, q.n, q.units, convention);Schedule s1 =MakeSchedule().from(spot).to(maturity).withTenor(baseIndex->tenor()).withCalendar(calendar).withConvention(convention).withRule(DateGeneration::Forward);Leg leg1 = IborLeg(s1, baseIndex).withSpreads(q.basis).withNotionals(100.0);Schedule s2 =MakeSchedule().from(spot).to(maturity).withTenor(otherIndex->tenor()).withCalendar(calendar).withConvention(convention).withRule(DateGeneration::Forward);Leg leg2 = IborLeg(s2, otherIndex).withNotionals(100.0);Swap swap(leg1, leg2);swap.setPricingEngine(ext::make_shared<DiscountingSwapEngine>(discountCurve));Real NPV = swap.NPV();Real tolerance = 1e-8;if (std::fabs(NPV) > tolerance) {BOOST_ERROR("Failed to price fair " << q.n << "-year(s) swap:"<< "\n calculated: " << NPV);}}}void testOvernightIborBootstrap(bool externalDiscountCurve) {std::vector<BasisSwapQuote> quotes = {{ 1, Years, 0.0010 },{ 2, Years, 0.0012 },{ 3, Years, 0.0015 },{ 5, Years, 0.0015 },{ 8, Years, 0.0018 },{ 10, Years, 0.0020 },{ 15, Years, 0.0021 },{ 20, Years, 0.0021 },};auto settlementDays = 2;auto calendar = UnitedStates(UnitedStates::GovernmentBond);auto convention = Following;auto endOfMonth = false;Handle<YieldTermStructure> knownForecastCurve(flatRate(0.01, Actual365Fixed()));RelinkableHandle<YieldTermStructure> discountCurve;if (externalDiscountCurve)discountCurve.linkTo(flatRate(0.005, Actual365Fixed()));auto baseIndex = ext::make_shared<Sofr>(knownForecastCurve);auto otherIndex = ext::make_shared<USDLibor>(6 * Months);std::vector<ext::shared_ptr<RateHelper>> helpers;for (auto q : quotes) {auto h = ext::make_shared<OvernightIborBasisSwapRateHelper>(Handle<Quote>(ext::make_shared<SimpleQuote>(q.basis)),Period(q.n, q.units), settlementDays, calendar, convention, endOfMonth,baseIndex, otherIndex, discountCurve);helpers.push_back(h);}auto bootstrappedCurve = ext::make_shared<PiecewiseYieldCurve<ZeroYield, Linear>>(0, calendar, helpers, Actual365Fixed());Date today = Settings::instance().evaluationDate();Date spot = calendar.advance(today, settlementDays, Days);otherIndex = ext::make_shared<USDLibor>(6 * Months, Handle<YieldTermStructure>(bootstrappedCurve));for (auto q : quotes) {// create swaps and check they're fairDate maturity = calendar.advance(spot, q.n, q.units, convention);Schedule s =MakeSchedule().from(spot).to(maturity).withTenor(otherIndex->tenor()).withCalendar(calendar).withConvention(convention).withRule(DateGeneration::Forward);Leg leg1 = OvernightLeg(s, baseIndex).withSpreads(q.basis).withNotionals(100.0);Leg leg2 = IborLeg(s, otherIndex).withNotionals(100.0);Swap swap(leg1, leg2);if (externalDiscountCurve) {swap.setPricingEngine(ext::make_shared<DiscountingSwapEngine>(discountCurve));} else {swap.setPricingEngine(ext::make_shared<DiscountingSwapEngine>(Handle<YieldTermStructure>(bootstrappedCurve)));}Real NPV = swap.NPV();Real tolerance = 1e-8;if (std::fabs(NPV) > tolerance) {BOOST_ERROR("Failed to price fair " << q.n << "-year(s) swap:"<< "\n calculated: " << NPV);}}}}void BasisSwapRateHelpersTest::testIborIborBaseCurveBootstrap() {BOOST_TEST_MESSAGE("Testing IBOR-IBOR basis-swap rate helpers (base curve bootstrap)...");basisswapratehelpers_test::testIborIborBootstrap(true); }void BasisSwapRateHelpersTest::testIborIborOtherCurveBootstrap() {BOOST_TEST_MESSAGE("Testing IBOR-IBOR basis-swap rate helpers (other curve bootstrap)...");basisswapratehelpers_test::testIborIborBootstrap(false); }void BasisSwapRateHelpersTest::testOvernightIborBootstrap() {BOOST_TEST_MESSAGE("Testing overnight-IBOR basis-swap rate helpers...");basisswapratehelpers_test::testOvernightIborBootstrap(false); }void BasisSwapRateHelpersTest::testOvernightIborBootstrapWithDiscountCurve() {BOOST_TEST_MESSAGE("Testing overnight-IBOR basis-swap rate helpers with external discount curve...");basisswapratehelpers_test::testOvernightIborBootstrap(true); }test_suite* BasisSwapRateHelpersTest::suite() {auto* suite = BOOST_TEST_SUITE("Basis swap rate helpers tests");suite->add(QUANTLIB_TEST_CASE(&BasisSwapRateHelpersTest::testIborIborBaseCurveBootstrap));suite->add(QUANTLIB_TEST_CASE(&BasisSwapRateHelpersTest::testIborIborOtherCurveBootstrap));suite->add(QUANTLIB_TEST_CASE(&BasisSwapRateHelpersTest::testOvernightIborBootstrap));suite->add(QUANTLIB_TEST_CASE(&BasisSwapRateHelpersTest::testOvernightIborBootstrapWithDiscountCurve));return suite; }

該博文為原創(chuàng)文章,未經(jīng)博主同意不得轉(zhuǎn)。
本文章博客地址:https://cplusplus.blog.csdn.net/article/details/128310872

總結(jié)

以上是生活随笔為你收集整理的C++:实现量化基础互换利率basis swap rate helpers测试实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。