Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
discountcurve.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/quotes/simplequote.hpp>
22#include <ql/termstructures/yield/discountcurve.hpp>
23#include <ql/time/calendars/nullcalendar.hpp>
24#include <ql/time/daycounters/actualactual.hpp>
26
27using namespace boost::unit_test_framework;
28using namespace QuantLib;
29using std::vector;
30
31BOOST_FIXTURE_TEST_SUITE(QuantExtTestSuite, qle::test::TopLevelFixture)
32
33BOOST_AUTO_TEST_SUITE(DiscountCurveTest)
34
35BOOST_AUTO_TEST_CASE(testDiscountCurve) {
36
37 // FIXME: test curve1 or 2 or both
38 BOOST_TEST_MESSAGE("Testing QuantExt::InteroplatedDiscountCurve2...");
39
40 SavedSettings backup;
41 Settings::instance().evaluationDate() = Date(1, Dec, 2015);
42 Date today = Settings::instance().evaluationDate();
43
44 vector<Date> dates;
45 vector<Real> times;
46 vector<DiscountFactor> dfs;
47 vector<Handle<Quote> > quotes;
48
49 Size numYears = 30;
50 int startYear = 2015;
51 DayCounter dc = ActualActual(ActualActual::ISDA);
52 Calendar cal = NullCalendar();
53
54 for (Size i = 0; i < numYears; i++) {
55
56 // rate
57 Real rate = 0.01 + i * 0.001;
58 // 1 year apart
59 dates.push_back(Date(1, Dec, startYear + i));
60 Time t = dc.yearFraction(today, dates.back());
61 times.push_back(t);
62
63 // set up Quote of DiscountFactors
64 DiscountFactor df = ::exp(-rate * t);
65 Handle<Quote> q(QuantLib::ext::make_shared<SimpleQuote>(df));
66 quotes.push_back(q);
67 dfs.push_back(df);
68 }
69
70 // Test against the QL curve
71 QuantLib::ext::shared_ptr<YieldTermStructure> ytsBase;
72 ytsBase =
73 QuantLib::ext::shared_ptr<YieldTermStructure>(new QuantLib::InterpolatedDiscountCurve<LogLinear>(dates, dfs, dc, cal));
74 ytsBase->enableExtrapolation();
75
76 QuantLib::ext::shared_ptr<YieldTermStructure> ytsTest(new QuantExt::InterpolatedDiscountCurve2(times, quotes, dc));
77
78 // now check that they give the same discount factors (including extrapolation)
79 for (Time t = 0.1; t < numYears + 10.0; t += 0.1) {
80 BOOST_CHECK_CLOSE(ytsBase->discount(t), ytsTest->discount(t), 1e-12);
81 }
82}
83
84BOOST_AUTO_TEST_SUITE_END()
85
86BOOST_AUTO_TEST_SUITE_END()
InterpolatedDiscountCurve2 as in QuantLib, but with floating discount quotes and floating reference d...
BOOST_AUTO_TEST_CASE(testDiscountCurve)
interpolated discount term structure
Fixture that can be used at top level.