Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
staticallycorrectedyieldtermstructure.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include "toplevelfixture.hpp"
20#include "utilities.hpp"
21#include <boost/test/unit_test.hpp>
22
24
25#include <ql/quotes/simplequote.hpp>
26#include <ql/termstructures/yield/flatforward.hpp>
27#include <ql/time/calendars/nullcalendar.hpp>
28
29#include <boost/make_shared.hpp>
30
31using namespace QuantExt;
32using namespace QuantLib;
33using namespace boost::unit_test_framework;
34
35BOOST_FIXTURE_TEST_SUITE(QuantExtTestSuite, qle::test::TopLevelFixture)
36
37BOOST_AUTO_TEST_SUITE(StaticallyCorrectedYieVldTermStructureTest)
38
39BOOST_AUTO_TEST_CASE(testCorrectedYts) {
40
41 SavedSettings backup;
42
43 Date refDate(18, Feb, 2016);
44 Settings::instance().evaluationDate() = refDate;
45
46 QuantLib::ext::shared_ptr<SimpleQuote> q = QuantLib::ext::make_shared<SimpleQuote>(0.02);
47 Handle<Quote> hq(q);
48 Handle<YieldTermStructure> floating(QuantLib::ext::make_shared<FlatForward>(0, NullCalendar(), hq, Actual365Fixed()));
49
50 Handle<YieldTermStructure> source(QuantLib::ext::make_shared<FlatForward>(refDate, 0.02, Actual365Fixed()));
51 Handle<YieldTermStructure> target(QuantLib::ext::make_shared<FlatForward>(refDate, 0.03, Actual365Fixed()));
52
53 // we can not test the flavours of roll down with flat
54 // termstructures, todo add tests with non-flat ts for this
55
56 Handle<YieldTermStructure> corrected1(
57 QuantLib::ext::make_shared<StaticallyCorrectedYieldTermStructure>(floating, source, target, ConstantDiscounts));
58 Handle<YieldTermStructure> corrected2(
59 QuantLib::ext::make_shared<StaticallyCorrectedYieldTermStructure>(floating, source, target, ForwardForward));
60
61 Real tol = 1.0E-12;
62
63 BOOST_CHECK_MESSAGE(std::fabs(target->discount(1.0) - corrected1->discount(1.0)) < tol,
64 "can not verify corrected1 df ("
65 << corrected1->discount(1.0) << ") against target df (" << target->discount(1.0)
66 << ") on eval date " << Settings::instance().evaluationDate() << ", difference is "
67 << (corrected1->discount(1.0) - target->discount(1.0)) << ", tolerance is " << tol);
68 BOOST_CHECK_MESSAGE(std::fabs(target->discount(1.0) - corrected2->discount(1.0)) < tol,
69 "can not verify corrected2 df ("
70 << corrected2->discount(1.0) << ") against target df (" << target->discount(1.0)
71 << ") on eval date " << Settings::instance().evaluationDate() << ", difference is "
72 << (corrected2->discount(1.0) - target->discount(1.0)) << ", tolerance is " << tol);
73
74 // move market data
75 q->setValue(0.03);
76
77 BOOST_CHECK_MESSAGE(std::fabs(floating->discount(1.0) * target->discount(1.0) / source->discount(1.0) -
78 corrected1->discount(1.0)) < tol,
79 "can not verify corrected1 df ("
80 << corrected1->discount(1.0) << ") against expected df ("
81 << floating->discount(1.0) * target->discount(1.0) / source->discount(1.0)
82 << ") on eval date " << Settings::instance().evaluationDate() << ", difference is "
83 << (corrected1->discount(1.0) -
84 floating->discount(1.0) * target->discount(1.0) / source->discount(1.0))
85 << ", tolerance is " << tol);
86 BOOST_CHECK_MESSAGE(std::fabs(floating->discount(1.0) * target->discount(1.0) / source->discount(1.0) -
87 corrected2->discount(1.0)) < tol,
88 "can not verify corrected2 df ("
89 << corrected2->discount(1.0) << ") against expected df ("
90 << floating->discount(1.0) * target->discount(1.0) / source->discount(1.0)
91 << ") on eval date " << Settings::instance().evaluationDate() << ", difference is "
92 << (corrected2->discount(1.0) -
93 floating->discount(1.0) * target->discount(1.0) / source->discount(1.0))
94 << ", tolerance is " << tol);
95
96 // move forward in time
97 Settings::instance().evaluationDate() = Date(18, Feb, 2022);
98 Real t = Actual365Fixed().yearFraction(refDate, Settings::instance().evaluationDate());
99
100 BOOST_CHECK_MESSAGE(std::fabs(floating->discount(1.0) * target->discount(1.0) / source->discount(1.0) -
101 corrected1->discount(1.0)) < tol,
102 "can not verify corrected1 df ("
103 << corrected1->discount(1.0) << ") against expected df ("
104 << floating->discount(1.0) * target->discount(1.0) / source->discount(1.0)
105 << ") on eval date " << Settings::instance().evaluationDate() << ", difference is "
106 << (corrected1->discount(1.0) -
107 floating->discount(1.0) * target->discount(1.0) / source->discount(1.0))
108 << ", tolerance is " << tol);
109 BOOST_CHECK_MESSAGE(std::fabs(floating->discount(1.0) * target->discount(t + 1.0) * source->discount(t) /
110 (target->discount(t) * source->discount(t + 1.0)) -
111 corrected2->discount(1.0)) < tol,
112 "can not verify corrected2 df ("
113 << corrected2->discount(1.0) << ") against expected df ("
114 << floating->discount(1.0) * target->discount(t + 1.0) * source->discount(t) /
115 (target->discount(t) * source->discount(t + 1.0))
116 << ") on eval date " << Settings::instance().evaluationDate() << ", difference is "
117 << floating->discount(1.0) * target->discount(t + 1.0) * source->discount(t) /
118 (target->discount(t) * source->discount(t + 1.0)) -
119 corrected2->discount(1.0)
120 << ", tolerance is " << tol);
121}
122
123BOOST_AUTO_TEST_SUITE_END()
124
125BOOST_AUTO_TEST_SUITE_END()
@ ConstantDiscounts
BOOST_AUTO_TEST_CASE(testCorrectedYts)
Statically corrected yield term structure.
Fixture that can be used at top level.
helper macros and methods for tests