### QuantLib Swaption Pricing Setup and Execution Source: https://rkapl123.github.io/OREAnnotatedSource/qle/test_2mclgmswaptionengine_8cpp_source This snippet demonstrates the setup and execution of swaption pricing using multiple QuantLib models and engines. It includes data preparation, model instantiation (Gsr, LGM), engine configuration, NPV calculation, and performance timing. ```C++ std::vector stepDates = std::vector(exerciseDates.begin(), exerciseDates.end() - 1); Array stepTimes(stepDates.size()); for (Size i = 0; i < stepDates.size(); ++i) { stepTimes[i] = yts->timeFromReference(stepDates[i]); } std::vector sigmas(stepDates.size() + 1); for (Size i = 0; i < sigmas.size(); ++i) { sigmas[i] = 0.0050 + (0.0080 - 0.0050) * std::exp(-0.2 * static_cast(i)); } Real reversion = 0.03; QuantLib::ext::shared_ptr gsr = QuantLib::ext::make_shared(yts, stepDates, sigmas, reversion, 50.0); QuantLib::ext::shared_ptr lgmParam = QuantLib::ext::make_shared( EURCurrency(), yts, stepTimes, Array(sigmas.begin(), sigmas.end()), stepTimes, Array(sigmas.size(), reversion)); QuantLib::ext::shared_ptr lgm = QuantLib::ext::make_shared(lgmParam); QuantLib::ext::shared_ptr lgmGaussian1d = QuantLib::ext::make_shared(lgm); QuantLib::ext::shared_ptr swaptionEngineGsr = QuantLib::ext::make_shared(gsr, 64, 7.0, true, false); QuantLib::ext::shared_ptr swaptionEngineLgm = QuantLib::ext::make_shared(lgmGaussian1d, 64, 7.0, true, false); QuantLib::ext::shared_ptr swaptionEngineLgm2 = QuantLib::ext::make_shared(lgm, 7.0, 16, 7.0, 32); Size polynomOrder = 4; LsmBasisSystem::PolynomialType polynomType = LsmBasisSystem::Monomial; Size tSamples = 10000; Size pSamples = 10000; QuantLib::ext::shared_ptr stateProcess = lgm->stateProcess(); QuantLib::ext::shared_ptr swaptionEngineLgmMC = QuantLib::ext::shared_ptr( new McLgmSwaptionEngine(lgm, MersenneTwisterAntithetic, SobolBrownianBridge, tSamples, pSamples, 42, 43, polynomOrder, polynomType)); // Calculate the T0 price of the bermudan swaption using different engines swaption->setPricingEngine(swaptionEngineGsr); Real npvGsr = swaption->NPV(); swaption->setPricingEngine(swaptionEngineLgm); Real npvLgm = swaption->NPV(); swaption->setPricingEngine(swaptionEngineLgm2); Real npvLgm2 = swaption->NPV(); boost::timer::cpu_timer timer; swaption->setPricingEngine(swaptionEngineLgmMC); Real npvLgmMc = swaption->NPV(); Real undNpvMc = swaption->result("underlyingNpv"); BOOST_TEST_MESSAGE("Underlying value mc = " << undNpvMc); BOOST_TEST_MESSAGE("npvGsr: " << npvGsr << ", npvLgm: " << npvLgm << ", npvLgm2: " << npvLgm << ", npvLgmMc: " << npvLgmMc); timer.stop(); BOOST_TEST_MESSAGE("timing mc engine: " << timer.elapsed().wall * 1e-6 << " ms"); Real tol = 2E-4; // basis point tolerance BOOST_CHECK_SMALL(std::fabs(undNpvMc - undlSwap->NPV()), tol); BOOST_CHECK_SMALL(std::fabs(npvGsr - npvLgm), tol); BOOST_CHECK_SMALL(std::fabs(npvGsr - npvLgm2), tol); BOOST_CHECK_SMALL(std::fabs(npvGsr - npvLgmMc), tol); ``` -------------------------------- ### Swaption Engine Test Setup Source: https://rkapl123.github.io/OREAnnotatedSource/qle/test_2blackswaptionenginedeltagamma_8cpp_source This snippet demonstrates the setup for a Boost Test Suite case that tests the BlackSwaptionEngine. It initializes test data and creates pricing engines for swaptions with different volatility inputs. ```C++ BOOST_FIXTURE_TEST_SUITE(QuantExtTestSuite, [qle::test::TopLevelFixture](classqle_1_1test_1_1_top_level_fixture.html)) BOOST_AUTO_TEST_SUITE(BlackSwaptionEngineDeltaGammaTest) [342](test_2blackswaptionenginedeltagamma_8cpp.html#a9d9f2a926c8024f2eff1e6e873fdeb2c)[BOOST_AUTO_TEST_CASE](test_2blackswaptionenginedeltagamma_8cpp.html#a9d9f2a926c8024f2eff1e6e873fdeb2c)(testNpvDeltasGammaVegas) { TestData d; QuantLib::ext::shared_ptr engineLn0 = QuantLib::ext::make_shared(d.discountCurve, Handle(d.lnVol)); QuantLib::ext::shared_ptr engineSln0 = QuantLib::ext::make_shared(d.discountCurve, Handle(d.slnVol), Actual365Fixed(), d.slnShift); ``` -------------------------------- ### C++: Setup Financial Environment for Pricing Source: https://rkapl123.github.io/OREAnnotatedSource/qle/test_2analyticeuropeanenginedeltagamma_8cpp_source Initializes QuantLib and QuantExt financial structures including yield term structures, volatility, and the GeneralizedBlackScholesProcess. This setup is crucial for pricing engine calculations within the ORE framework. Dependencies include QuantLib and QuantExt libraries. ```C++ namespace { struct TestData { TestData() : refDate(Date(22, Aug, 2016)) { Settings::instance().evaluationDate() = refDate; rateDiscount = Handle(QuantLib::ext::make_shared( 0, NullCalendar(), Handle(QuantLib::ext::make_shared(0.02)), Actual365Fixed())); divDiscount = Handle(QuantLib::ext::make_shared( 0, NullCalendar(), Handle(QuantLib::ext::make_shared(0.03)), Actual365Fixed())); pillarDates.push_back(refDate + 1 * Years); pillarDates.push_back(refDate + 2 * Years); pillarDates.push_back(refDate + 3 * Years); pillarDates.push_back(refDate + 4 * Years); pillarDates.push_back(refDate + 5 * Years); pillarDates.push_back(refDate + 7 * Years); pillarDates.push_back(refDate + 10 * Years); pillarDates.push_back(refDate + 15 * Years); pillarDates.push_back(refDate + 20 * Years); std::vector> tmpRateSpr, tmpDivSpr; for (Size i = 0; i < pillarDates.size(); ++i) { QuantLib::ext::shared_ptr qr = QuantLib::ext::make_shared(0.0); QuantLib::ext::shared_ptr qd = QuantLib::ext::make_shared(0.0); rateSpreads.push_back(qr); divSpreads.push_back(qd); tmpRateSpr.push_back(Handle(qr)); tmpDivSpr.push_back(Handle(qd)); pillarTimes.push_back(rateDiscount->timeFromReference(pillarDates[i])); } rateCurve = Handle(QuantLib::ext::make_shared>( rateDiscount, tmpRateSpr, pillarDates)); divCurve = Handle(QuantLib::ext::make_shared>( divDiscount, tmpDivSpr, pillarDates)); rateCurve->enableExtrapolation(); divCurve->enableExtrapolation(); vol = QuantLib::ext::make_shared(0.20); volTs = Handle( QuantLib::ext::make_shared(0, NullCalendar(), Handle(vol), Actual365Fixed())); spot = QuantLib::ext::make_shared(100.0); process = QuantLib::ext::make_shared(Handle(spot), divCurve, rateCurve, volTs); } Date refDate; Handle rateDiscount, divDiscount, rateCurve, divCurve; std::vector pillarDates; std::vector> rateSpreads, divSpreads; std::vector pillarTimes; QuantLib::ext::shared_ptr vol, spot; Handle volTs; QuantLib::ext::shared_ptr process; }; } // namespace ``` -------------------------------- ### C++ Currency Swap Engine Delta/Gamma Test Setup Source: https://rkapl123.github.io/OREAnnotatedSource/qle/test_2discountingcurrencyswapenginedeltagamma_8cpp_source This C++ code snippet demonstrates the setup of test data for currency swap engine delta and gamma calculations. It initializes reference dates, yield term structures (discount and forward curves), and market quotes using QuantLib and QuantExt libraries, preparing the environment for financial instrument pricing tests. ```C++ #include "toplevelfixture.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace QuantLib; using namespace QuantExt; using namespace boost::unit_test_framework; using std::vector; namespace { struct TestData { TestData() : refDate(Date(22, Aug, 2016)) { Settings::instance().evaluationDate() = refDate; baseDiscount = Handle(QuantLib::ext::make_shared( refDate, Handle(QuantLib::ext::make_shared(0.02)), Actual365Fixed())); baseForward = Handle(QuantLib::ext::make_shared( refDate, Handle(QuantLib::ext::make_shared(0.03)), Actual365Fixed())); baseDiscountFor = Handle(QuantLib::ext::make_shared( refDate, Handle(QuantLib::ext::make_shared(0.04)), Actual365Fixed())); baseForwardFor = Handle(QuantLib::ext::make_shared( refDate, Handle(QuantLib::ext::make_shared(0.05)), Actual365Fixed())); fxQuote = QuantLib::ext::make_shared(0.90); pillarDates.push_back(refDate + 1 * Years); pillarDates.push_back(refDate + 2 * Years); pillarDates.push_back(refDate + 3 * Years); pillarDates.push_back(refDate + 4 * Years); pillarDates.push_back(refDate + 5 * Years); pillarDates.push_back(refDate + 7 * Years); pillarDates.push_back(refDate + 10 * Years); std::vector> tmpDiscSpr, tmpFwdSpr, tmpDiscSprFor, tmpFwdSprFor; for (Size i = 0; i < pillarDates.size(); ++i) { QuantLib::ext::shared_ptr qd = QuantLib::ext::make_shared(0.0); QuantLib::ext::shared_ptr qf = QuantLib::ext::make_shared(0.0); QuantLib::ext::shared_ptr qdf = QuantLib::ext::make_shared(0.0); QuantLib::ext::shared_ptr qff = QuantLib::ext::make_shared(0.0); discountSpreads.push_back(qd); forwardSpreads.push_back(qf); discountSpreadsFor.push_back(qdf); forwardSpreadsFor.push_back(qff); tmpDiscSpr.push_back(Handle(qd)); tmpFwdSpr.push_back(Handle(qf)); tmpDiscSprFor.push_back(Handle(qdf)); tmpFwdSprFor.push_back(Handle(qff)); pillarTimes.push_back(baseDiscount->timeFromReference(pillarDates[i])); } discountCurve = Handle(QuantLib::ext::make_shared>( baseDiscount, tmpDiscSpr, pillarDates)); forwardCurve = Handle(QuantLib::ext::make_shared>( baseForward, tmpFwdSpr, pillarDates)); discountCurveFor = Handle(QuantLib::ext::make_shared>( baseDiscountFor, tmpDiscSprFor, pillarDates)); forwardCurveFor = Handle(QuantLib::ext::make_shared>( baseForwardFor, tmpFwdSprFor, pillarDates)); } Date refDate; Handle baseDiscount, baseForward, baseDiscountFor, baseForwardFor; std::vector> discountSpreads, forwardSpreads, discountSpreadsFor, forwardSpreadsFor; std::vector pillarDates; std::vector