Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
multilegoption.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <test/toplevelfixture.hpp>
#include <qle/pricingengines/mcmultilegoptionengine.hpp>
#include <qle/models/crossassetmodel.hpp>
#include <qle/models/fxbsconstantparametrization.hpp>
#include <qle/pricingengines/analyticcclgmfxoptionengine.hpp>
#include <qle/pricingengines/numericlgmmultilegoptionengine.hpp>
#include <ql/cashflows/simplecashflow.hpp>
#include <ql/currencies/america.hpp>
#include <ql/currencies/europe.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/instruments/swaption.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <boost/timer/timer.hpp>

Go to the source code of this file.

Functions

 BOOST_FIXTURE_TEST_CASE (testBermudanSwaption, BermudanTestData)
 
 BOOST_AUTO_TEST_CASE (testFxOption)
 

Function Documentation

◆ BOOST_FIXTURE_TEST_CASE()

BOOST_FIXTURE_TEST_CASE ( testBermudanSwaption  ,
BermudanTestData   
)

Definition at line 100 of file multilegoption.cpp.

100 {
101
102 BOOST_TEST_MESSAGE("Testing pricing of bermudan swaption as multi leg option vs numeric swaption engine");
103
104 auto multiLegOption = QuantLib::ext::make_shared<MultiLegOption>(
105 std::vector<Leg>{underlying->leg(0), underlying->leg(1)}, std::vector<bool>{true, false},
106 std::vector<Currency>{EURCurrency(), EURCurrency()}, exercise);
107
108 auto lgm_p = QuantLib::ext::make_shared<IrLgm1fPiecewiseConstantHullWhiteAdaptor>(EURCurrency(), yts, stepTimes_a, sigmas_a,
109 stepTimes_a, kappas_a);
110
111 auto xasset = Handle<CrossAssetModel>(
112 QuantLib::ext::make_shared<CrossAssetModel>(std::vector<QuantLib::ext::shared_ptr<Parametrization>>{lgm_p}));
113 auto lgm = QuantLib::ext::make_shared<LinearGaussMarkovModel>(lgm_p);
114
115 auto swaptionEngineLgm = QuantLib::ext::make_shared<NumericLgmSwaptionEngine>(lgm, 7.0, 16, 7.0, 32);
116 auto swapEngine = QuantLib::ext::make_shared<DiscountingSwapEngine>(yts);
117
118 auto mcMultiLegOptionEngine = QuantLib::ext::make_shared<McMultiLegOptionEngine>(
119 xasset, SobolBrownianBridge, SobolBrownianBridge, 25000, 0, 42, 42, 4, LsmBasisSystem::Monomial);
120
121 underlying->setPricingEngine(swapEngine);
122 swaption->setPricingEngine(swaptionEngineLgm);
123 Real npvUnd0 = underlying->NPV();
124 Real npv0 = swaption->NPV();
125 BOOST_TEST_MESSAGE("npv (numeric lgm swaption engine): underlying = " << npvUnd0 << ", option = " << npv0);
126
127 boost::timer::cpu_timer timer;
128 multiLegOption->setPricingEngine(mcMultiLegOptionEngine);
129 Real npvUnd1 = multiLegOption->result<Real>("underlyingNpv");
130 Real npv1 = multiLegOption->NPV();
131 timer.stop();
132 BOOST_TEST_MESSAGE("npv (multi leg option engine) : underlying = "
133 << npvUnd1 << ", option = " << npv1 << ", timing " << timer.elapsed().wall * 1e-6 << " ms");
134
135 BOOST_CHECK_SMALL(std::abs(npvUnd0 - npvUnd1), 1.0E-4);
136 BOOST_CHECK_SMALL(std::abs(npv0 - npv1), 1.0E-4);
137
138} // testBermudanSwaption

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( testFxOption  )

Definition at line 140 of file multilegoption.cpp.

140 {
141
142 BOOST_TEST_MESSAGE("Testing pricing of fx option as multi leg option vs analytic engine");
143
144 SavedSettings backup;
145 Date refDate(12, January, 2015);
146 Settings::instance().evaluationDate() = refDate;
147
148 auto yts_eur = Handle<YieldTermStructure>(QuantLib::ext::make_shared<FlatForward>(refDate, 0.02, Actual365Fixed()));
149 auto yts_usd = Handle<YieldTermStructure>(QuantLib::ext::make_shared<FlatForward>(refDate, 0.03, Actual365Fixed()));
150
151 auto lgm_eur_p = QuantLib::ext::make_shared<IrLgm1fConstantParametrization>(EURCurrency(), yts_eur, 0.01, 0.01);
152 auto lgm_usd_p = QuantLib::ext::make_shared<IrLgm1fConstantParametrization>(USDCurrency(), yts_usd, 0.01, 0.01);
153
154 Handle<Quote> fxspot(QuantLib::ext::make_shared<SimpleQuote>(0.9));
155
156 auto fx_p = QuantLib::ext::make_shared<FxBsConstantParametrization>(USDCurrency(), fxspot, 0.15);
157
158 Matrix corr(3, 3);
159 // clang-format off
160 corr[0][0] = 1.0; corr[0][1] = 0.2; corr[0][2] = 0.5;
161 corr[1][0] = 0.2; corr[1][1] = 1.0; corr[1][2] = 0.4;
162 corr[2][0] = 0.5; corr[2][1] = 0.4; corr[2][2] = 1.0;
163 // clang-format on
164
165 auto xasset = Handle<CrossAssetModel>(QuantLib::ext::make_shared<CrossAssetModel>(
166 std::vector<QuantLib::ext::shared_ptr<Parametrization>>{lgm_eur_p, lgm_usd_p, fx_p}, corr));
167
168 Date exDate(12, January, 2020);
169 auto exercise = QuantLib::ext::make_shared<EuropeanExercise>(exDate);
170 auto fxOption =
171 QuantLib::ext::make_shared<VanillaOption>(QuantLib::ext::make_shared<PlainVanillaPayoff>(Option::Call, 0.8), exercise);
172
173 Leg usdFlow, eurFlow;
174 usdFlow.push_back(QuantLib::ext::make_shared<SimpleCashFlow>(1.0, exDate + 1));
175 eurFlow.push_back(QuantLib::ext::make_shared<SimpleCashFlow>(-0.8, exDate + 1));
176
177 auto multiLegOption =
178 QuantLib::ext::make_shared<MultiLegOption>(std::vector<Leg>{eurFlow, usdFlow}, std::vector<bool>{false, false},
179 std::vector<Currency>{EURCurrency(), USDCurrency()}, exercise);
180
181 auto analyticFxOptionEngine = QuantLib::ext::make_shared<AnalyticCcLgmFxOptionEngine>(*xasset, 0);
182 fxOption->setPricingEngine(analyticFxOptionEngine);
183 Real npv0 = fxOption->NPV();
184 BOOST_TEST_MESSAGE("npv (analytic cclgm fx option engine): " << npv0);
185
186 // for european options there is no traning phase actually
187 auto mcMultiLegOptionEngine = QuantLib::ext::make_shared<McMultiLegOptionEngine>(
188 xasset, SobolBrownianBridge, SobolBrownianBridge, 25000, 0, 42, 42, 4, LsmBasisSystem::Monomial);
189
190 multiLegOption->setPricingEngine(mcMultiLegOptionEngine);
191 boost::timer::cpu_timer timer;
192 Real npv1 = multiLegOption->NPV();
193 timer.stop();
194 BOOST_TEST_MESSAGE("npv (multi leg option engine) : " << npv1 << ", timing " << timer.elapsed().wall * 1e-6
195 << " ms");
196
197 BOOST_CHECK_SMALL(std::abs(npv1 - npv0), 1.0E-4);
198
199} // testFxOption