Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
correlationtermstructure.cpp File Reference
#include "toplevelfixture.hpp"
#include <boost/test/unit_test.hpp>
#include <qle/termstructures/flatcorrelation.hpp>
#include <qle/termstructures/interpolatedcorrelationcurve.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <boost/make_shared.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testFlatCorrelation)
 
 BOOST_AUTO_TEST_CASE (testInterpolatedCorrelationCurve)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( testFlatCorrelation  )

Definition at line 37 of file correlationtermstructure.cpp.

37 {
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}

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( testInterpolatedCorrelationCurve  )

Definition at line 60 of file correlationtermstructure.cpp.

60 {
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}