Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
survivalprobabilitycurve.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>
21#include <ql/math/interpolations/loginterpolation.hpp>
22#include <ql/quotes/simplequote.hpp>
23#include <ql/termstructures/credit/interpolatedsurvivalprobabilitycurve.hpp>
24#include <ql/termstructures/defaulttermstructure.hpp>
25#include <ql/termstructures/interpolatedcurve.hpp>
26#include <ql/termstructures/yieldtermstructure.hpp>
27#include <ql/time/calendars/nullcalendar.hpp>
28#include <ql/time/daycounters/actualactual.hpp>
30
31using namespace boost::unit_test_framework;
32using namespace QuantLib;
33using std::vector;
34
35BOOST_FIXTURE_TEST_SUITE(QuantExtTestSuite, qle::test::TopLevelFixture)
36
37BOOST_AUTO_TEST_SUITE(SurvivalProbabilityCruveTest)
38
39BOOST_AUTO_TEST_CASE(testSurvivalProbabilityCurve) {
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}
100
101BOOST_AUTO_TEST_SUITE_END()
102
103BOOST_AUTO_TEST_SUITE_END()
DefaultProbabilityTermStructure based on interpolation of survival probability quotes.
BOOST_AUTO_TEST_CASE(testSurvivalProbabilityCurve)
interpolated survival probability term structure
Fixture that can be used at top level.