Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
correlationtermstructure.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 <boost/test/unit_test.hpp>
23
24#include <ql/quotes/simplequote.hpp>
25#include <ql/time/calendars/nullcalendar.hpp>
26
27#include <boost/make_shared.hpp>
28
29using namespace QuantExt;
30using namespace QuantLib;
31using namespace boost::unit_test_framework;
32
33BOOST_FIXTURE_TEST_SUITE(QuantExtTestSuite, qle::test::TopLevelFixture)
34
35BOOST_AUTO_TEST_SUITE(CorrelationTermstructureTest)
36
37BOOST_AUTO_TEST_CASE(testFlatCorrelation) {
38
39 QuantLib::ext::shared_ptr<SimpleQuote> q = QuantLib::ext::make_shared<SimpleQuote>(0.02);
40 Handle<Quote> hq(q);
41 Handle<FlatCorrelation> flatCorr(QuantLib::ext::make_shared<FlatCorrelation>(0, NullCalendar(), hq, Actual365Fixed()));
42
43 // check we get the expected quote value
44 BOOST_CHECK_MESSAGE(flatCorr->correlation(1) == 0.02, "unexpected correlation value: " << flatCorr->correlation(1));
45
46 // move market data
47 q->setValue(0.03);
48
49 BOOST_CHECK_MESSAGE(flatCorr->correlation(1) == 0.03, "unexpected correlation value: " << flatCorr->correlation(1));
50
51 // check failures
52
53 q->setValue(-1.1);
54 BOOST_CHECK_THROW(flatCorr->correlation(1), QuantLib::Error);
55
56 q->setValue(1.1);
57 BOOST_CHECK_THROW(flatCorr->correlation(1), QuantLib::Error);
58}
59
60BOOST_AUTO_TEST_CASE(testInterpolatedCorrelationCurve) {
61
62 // build interpolated correlation curve
63 std::vector<Time> times;
64 std::vector<QuantLib::ext::shared_ptr<SimpleQuote> > simpleQuotes;
65 std::vector<Handle<Quote> > quotes;
66
67 Size numYears = 10;
68 for (Size i = 1; i < numYears; i++) {
69
70 Real corr = 0.1;
71
72 simpleQuotes.push_back(QuantLib::ext::make_shared<SimpleQuote>(corr));
73 quotes.push_back(Handle<Quote>(simpleQuotes.back()));
74
75 times.push_back(static_cast<Time>(i));
76 }
77
78 Handle<PiecewiseLinearCorrelationCurve> interpCorr(
79 QuantLib::ext::make_shared<PiecewiseLinearCorrelationCurve>(times, quotes, Actual365Fixed(), NullCalendar()));
80
81 Time t = 1;
82 while (t < numYears) {
83 BOOST_CHECK_MESSAGE(interpCorr->correlation(t) == 0.1,
84 "unexpected correlation value: " << interpCorr->correlation(t));
85 t += 0.5;
86 }
87
88 // Now check quotes update
89 for (Size i = 0; i < simpleQuotes.size(); ++i) {
90 simpleQuotes[i]->setValue(1);
91 }
92
93 t = 1;
94 while (t < numYears) {
95 BOOST_CHECK_MESSAGE(interpCorr->correlation(t) == 1,
96 "unexpected correlation value: " << interpCorr->correlation(t));
97 t += 0.5;
98 }
99
100 // Now check interpolation
101 for (Size i = 0; i < simpleQuotes.size(); ++i) {
102 simpleQuotes[i]->setValue(0.1 + 0.01 * i);
103 }
104
105 Real tol = 1.0E-8;
106 BOOST_CHECK_CLOSE(interpCorr->correlation(1.5), 0.105, tol);
107 BOOST_CHECK_CLOSE(interpCorr->correlation(2.5), 0.115, tol);
108 BOOST_CHECK_CLOSE(interpCorr->correlation(3.5), 0.125, tol);
109 BOOST_CHECK_CLOSE(interpCorr->correlation(11), 0.18, tol);
110}
111
112BOOST_AUTO_TEST_SUITE_END()
113
114BOOST_AUTO_TEST_SUITE_END()
Term structure of flat correlations.
interpolated correlation term structure
BOOST_AUTO_TEST_CASE(testFlatCorrelation)
Fixture that can be used at top level.