Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
cms.cpp File Reference
#include <boost/make_shared.hpp>
#include <boost/test/unit_test.hpp>
#include <ored/marketdata/marketimpl.hpp>
#include <ored/portfolio/builders/cms.hpp>
#include <ored/portfolio/capfloor.hpp>
#include <ored/portfolio/enginedata.hpp>
#include <ored/portfolio/envelope.hpp>
#include <ored/portfolio/legdata.hpp>
#include <ored/portfolio/schedule.hpp>
#include <ored/portfolio/swap.hpp>
#include <ored/utilities/indexparser.hpp>
#include <oret/toplevelfixture.hpp>
#include <ql/termstructures/volatility/swaption/swaptionconstantvol.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <iostream>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testCMSAnalyticHagan)
 
 BOOST_AUTO_TEST_CASE (testCMSNumericalHagan)
 
 BOOST_AUTO_TEST_CASE (testCMSLinearTsr)
 
 BOOST_AUTO_TEST_CASE (cmsCapFloor)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/4]

BOOST_AUTO_TEST_CASE ( testCMSAnalyticHagan  )

Definition at line 245 of file cms.cpp.

245 {
246 BOOST_TEST_MESSAGE("Testing CMS Analytic Hagan price...");
247
248 // build market
249 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
250 Settings::instance().evaluationDate() = market->asofDate();
251 CommonVars vars;
252 QuantLib::ext::shared_ptr<ore::data::Swap> cmsSwap = vars.makeSwap();
253
254 // Build and price
255 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
256 engineData->model("CMS") = "Hagan";
257 engineData->engine("CMS") = "Analytic";
258 map<string, string> engineparams;
259 engineparams["YieldCurveModel"] = "Standard";
260 engineparams["MeanReversion"] = "0.0";
261 engineData->engineParameters("CMS") = engineparams;
262
263 engineData->model("Swap") = "DiscountedCashflows";
264 engineData->engine("Swap") = "DiscountingSwapEngineOptimised";
265
266 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
267
268 cmsSwap->build(engineFactory);
269
270 Real expectedNPV = 3440673.46;
271 Real npv = cmsSwap->instrument()->NPV();
272
273 BOOST_TEST_MESSAGE("Hagan Analytic price is " << npv);
274 outputCoupons(cmsSwap);
275
276 BOOST_CHECK_CLOSE(npv, expectedNPV, 1.0);
277}

◆ BOOST_AUTO_TEST_CASE() [2/4]

BOOST_AUTO_TEST_CASE ( testCMSNumericalHagan  )

Definition at line 279 of file cms.cpp.

279 {
280 BOOST_TEST_MESSAGE("Testing CMS Numerical Hagan price...");
281
282 // build market
283 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
284 Settings::instance().evaluationDate() = market->asofDate();
285
286 CommonVars vars;
287 QuantLib::ext::shared_ptr<ore::data::Swap> cmsSwap = vars.makeSwap();
288
289 // Build and price
290 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
291 engineData->model("CMS") = "Hagan";
292 engineData->engine("CMS") = "Numerical";
293 map<string, string> engineparams;
294 engineparams["YieldCurveModel"] = "Standard";
295 engineparams["MeanReversion"] = "0.0";
296 engineparams["UpperLimit"] = "0.0";
297 engineparams["LowerLimit"] = "1.0";
298 engineparams["Precision"] = "0.000001";
299 engineData->engineParameters("CMS") = engineparams;
300
301 engineData->model("Swap") = "DiscountedCashflows";
302 engineData->engine("Swap") = "DiscountingSwapEngineOptimised";
303
304 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
305
306 cmsSwap->build(engineFactory);
307
308 Real expectedNPV = 3440673.46;
309 Real npv = cmsSwap->instrument()->NPV();
310
311 BOOST_TEST_MESSAGE("Hagan Numerical price is " << npv);
312 outputCoupons(cmsSwap);
313
314 BOOST_CHECK_CLOSE(npv, expectedNPV, 1.0);
315}

◆ BOOST_AUTO_TEST_CASE() [3/4]

BOOST_AUTO_TEST_CASE ( testCMSLinearTsr  )

Definition at line 317 of file cms.cpp.

317 {
318 BOOST_TEST_MESSAGE("Testing CMS Linear TSR price...");
319
320 // build market
321 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
322 Settings::instance().evaluationDate() = market->asofDate();
323
324 CommonVars vars;
325 QuantLib::ext::shared_ptr<ore::data::Swap> cmsSwap = vars.makeSwap();
326
327 // Build and price
328 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
329 engineData->model("CMS") = "LinearTSR";
330 engineData->engine("CMS") = "LinearTSRPricer";
331 map<string, string> engineparams;
332 engineparams["MeanReversion"] = "0.0";
333 engineparams["Policy"] = "RateBound";
334 engineparams["LowerRateBoundLogNormal"] = "0.0001";
335 engineparams["UpperRateBoundLogNormal"] = "2.0000";
336 engineData->engineParameters("CMS") = engineparams;
337
338 engineData->model("Swap") = "DiscountedCashflows";
339 engineData->engine("Swap") = "DiscountingSwapEngineOptimised";
340
341 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
342
343 cmsSwap->build(engineFactory);
344
345 Real expectedNPV = 3440673.46;
346 Real npv = cmsSwap->instrument()->NPV();
347
348 BOOST_TEST_MESSAGE("Linear TSR price is " << npv);
349 outputCoupons(cmsSwap);
350
351 BOOST_CHECK_CLOSE(npv, expectedNPV, 1.0);
352}

◆ BOOST_AUTO_TEST_CASE() [4/4]

BOOST_AUTO_TEST_CASE ( cmsCapFloor  )

Definition at line 354 of file cms.cpp.

354 {
355 BOOST_TEST_MESSAGE("Testing CMS CapFloor price...");
356
357 // build market
358 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>();
359 Settings::instance().evaluationDate() = market->asofDate();
360
361 CommonVars vars;
362 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
363 engineData->model("CMS") = "Hagan";
364 engineData->engine("CMS") = "Analytic";
365 map<string, string> engineparams;
366 engineparams["YieldCurveModel"] = "Standard";
367 engineparams["MeanReversion"] = "0.0";
368 engineData->engineParameters("CMS") = engineparams;
369
370 engineData->model("Swap") = "DiscountedCashflows";
371 engineData->engine("Swap") = "DiscountingSwapEngineOptimised";
372
373 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
374
375 BOOST_TEST_MESSAGE(
376 "Comparing CMS Cap price to replication by a Single Legged CMS Swap and a Single Leg Capped CMS Swap...");
377 vector<double> capRate(1, 0.021);
378 QuantLib::ext::shared_ptr<ore::data::Swap> cmsLegSwap = vars.makeCmsLegSwap();
379 QuantLib::ext::shared_ptr<ore::data::Swap> cappedCmsLegSwap = vars.makeCappedCmsLegSwap(capRate);
380 QuantLib::ext::shared_ptr<ore::data::CapFloor> cap = vars.makeCap(capRate);
381
382 cmsLegSwap->build(engineFactory);
383 cappedCmsLegSwap->build(engineFactory);
384 cap->build(engineFactory);
385
386 Real cmsLegNpv = cmsLegSwap->instrument()->NPV();
387 Real cappedCmsLegNpv = cappedCmsLegSwap->instrument()->NPV();
388 Real capNpv = cap->instrument()->NPV();
389
390 Real capBySwaps = cmsLegNpv - cappedCmsLegNpv;
391
392 BOOST_TEST_MESSAGE("CMS Leg swap NPV is " << cmsLegNpv);
393 BOOST_TEST_MESSAGE("CMS Capped Leg swap NPV is " << cappedCmsLegNpv);
394 BOOST_TEST_MESSAGE("CMS Cap NPV is " << capNpv);
395 BOOST_TEST_MESSAGE("CMS Cap NPV from Swap replication is " << capBySwaps);
396 BOOST_CHECK_CLOSE(capNpv, capBySwaps, 1.0);
397
398 BOOST_TEST_MESSAGE("Checking CMS Cap with high Cap is zero...");
399 vector<double> capHigh(1, 1.0);
400
401 cap = vars.makeCap(capHigh);
402 cap->build(engineFactory);
403 capNpv = cap->instrument()->NPV();
404 BOOST_TEST_MESSAGE("CMS Cap (Cap of 100%) NPV is " << capNpv);
405 BOOST_CHECK_SMALL(capNpv, 0.01);
406
407 BOOST_TEST_MESSAGE("Checking CMS Cap with low Cap is equal to single leg swap...");
408 vector<double> capLow(1, -1.0);
409
410 cap = vars.makeCap(capLow);
411 cap->build(engineFactory);
412 capNpv = cap->instrument()->NPV();
413 BOOST_TEST_MESSAGE("CMS Cap (Cap of -100%) NPV is " << capNpv);
414 BOOST_CHECK_CLOSE(capNpv, cmsLegNpv, 1.0);
415
416 BOOST_TEST_MESSAGE("Checking CMS Floor with low Cap is equal to zero...");
417 vector<double> floorLow(1, -1.0);
418
419 QuantLib::ext::shared_ptr<ore::data::CapFloor> floor = vars.makeFloor(floorLow);
420 floor->build(engineFactory);
421 Real floorNpv = floor->instrument()->NPV();
422 BOOST_TEST_MESSAGE("CMS Floor (Floor of -100%) NPV is " << floorNpv);
423 BOOST_CHECK_SMALL(floorNpv, 0.01);
424
425 BOOST_TEST_MESSAGE("Checking CMS Cap + CMS Floor = Swap...");
426 vector<double> floorRate(1, 0.021);
427
428 cap = vars.makeCap(capRate);
429 floor = vars.makeFloor(floorRate);
430 QuantLib::ext::shared_ptr<ore::data::Swap> swap = vars.makeSwap(0.021, "6M");
431 cap->build(engineFactory);
432 floor->build(engineFactory);
433 swap->build(engineFactory);
434 capNpv = cap->instrument()->NPV();
435 floorNpv = floor->instrument()->NPV();
436 Real swapNpv = swap->instrument()->NPV();
437 Real capFloorNpv = capNpv - floorNpv;
438 BOOST_TEST_MESSAGE("CMS Cap NPV is " << capNpv);
439 BOOST_TEST_MESSAGE("CMS Floor NPV is " << floorNpv);
440 BOOST_TEST_MESSAGE("CMS Cap + Floor NPV is " << capFloorNpv);
441 BOOST_TEST_MESSAGE("CMS Swap NPV is " << swapNpv);
442 BOOST_CHECK_CLOSE(capFloorNpv, swapNpv, 1.0);
443}