Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
survivalprobabilitycurve.cpp File Reference
#include "toplevelfixture.hpp"
#include <boost/test/unit_test.hpp>
#include <ql/math/interpolations/loginterpolation.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/termstructures/credit/interpolatedsurvivalprobabilitycurve.hpp>
#include <ql/termstructures/defaulttermstructure.hpp>
#include <ql/termstructures/interpolatedcurve.hpp>
#include <ql/termstructures/yieldtermstructure.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <qle/termstructures/survivalprobabilitycurve.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testSurvivalProbabilityCurve)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( testSurvivalProbabilityCurve  )

Definition at line 39 of file survivalprobabilitycurve.cpp.

39 {
40
41 BOOST_TEST_MESSAGE("Testing QuantExt::SurvivalProbabilityCurve...");
42
43 Settings::instance().evaluationDate() = Date(1, Dec, 2015);
44 Date today = Settings::instance().evaluationDate();
45
46 vector<Date> dates;
47 vector<Real> times;
48 vector<Probability> sps;
49 vector<Handle<Quote> > quotes;
50 vector<QuantLib::ext::shared_ptr<SimpleQuote> > simpleQuotes;
51
52 Size numYears = 30;
53 int startYear = 2015;
54 DayCounter dc = ActualActual(ActualActual::ISDA);
55 Calendar cal = NullCalendar();
56
57 for (Size i = 0; i < numYears; i++) {
58
59 // rate
60 Real rate = 0.01 + i * 0.001;
61 // 1 year apart
62 dates.push_back(Date(1, Dec, startYear + i));
63 Time t = dc.yearFraction(today, dates.back());
64 times.push_back(t);
65
66 // set up Quote of SurvivalProbabilities
67 Probability sp = ::exp(-rate * t);
68
69 QuantLib::ext::shared_ptr<SimpleQuote> q(new SimpleQuote(sp));
70 simpleQuotes.push_back(q);
71 Handle<Quote> qh(q);
72 quotes.push_back(qh);
73 sps.push_back(sp);
74 }
75
76 // Test against the QL curve
77 QuantLib::ext::shared_ptr<DefaultProbabilityTermStructure> dtsBase;
78 dtsBase = QuantLib::ext::shared_ptr<DefaultProbabilityTermStructure>(
79 new QuantLib::InterpolatedSurvivalProbabilityCurve<Linear>(dates, sps, dc, cal));
80 dtsBase->enableExtrapolation();
81
82 QuantLib::ext::shared_ptr<DefaultProbabilityTermStructure> dtsTest(
83 new QuantExt::SurvivalProbabilityCurve<Linear>(dates, quotes, dc, cal));
84
85 // now check that they give the same survivalProbabilities (including extrapolation)
86 for (Time t = 0.1; t < numYears + 10.0; t += 0.1) {
87 BOOST_CHECK_CLOSE(dtsBase->survivalProbability(t, true), dtsTest->survivalProbability(t, true), 1e-12);
88 }
89
90 for (Size i = 0; i < numYears; i++) {
91 Probability sp = sps[i] + 0.1;
92 simpleQuotes[i]->setValue(sp);
93 }
94 // now check that they no longer match
95 for (Time t = 0.1; t < numYears + 10.0; t += 0.1) {
96 BOOST_CHECK_PREDICATE(std::not_equal_to<Probability>(),
97 (dtsBase->survivalProbability(t, true))(dtsTest->survivalProbability(t, true)));
98 }
99}
DefaultProbabilityTermStructure based on interpolation of survival probability quotes.