Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
interpolateddiscountcurve2.hpp
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/*! \file interpolateddiscountcurve2.hpp
20 \brief interpolated discount term structure
21 \ingroup termstructures
22*/
23
24#ifndef quantext_interpolated_discount_curve_2_hpp
25#define quantext_interpolated_discount_curve_2_hpp
26
27#include <ql/math/interpolations/loginterpolation.hpp>
28#include <ql/patterns/lazyobject.hpp>
29#include <ql/termstructures/yieldtermstructure.hpp>
30#include <ql/time/calendars/nullcalendar.hpp>
31
32#include <boost/make_shared.hpp>
33
34namespace QuantExt {
35using namespace QuantLib;
36//! InterpolatedDiscountCurve2 as in QuantLib, but with floating discount quotes and floating reference date
37/*! InterpolatedDiscountCurve2 as in QuantLib, but with
38 floating discount quotes and floating reference date,
39 reference date is always the global evaluation date,
40 i.e. settlement days are zero and calendar is NullCalendar()
41
42 \ingroup termstructures
43*/
44class InterpolatedDiscountCurve2 : public YieldTermStructure, public LazyObject {
45public:
48 //! \name Constructors
49 //@{
50 //! times based constructor, note that times should be consistent with day counter dc passed
51 InterpolatedDiscountCurve2(const std::vector<Time>& times, const std::vector<Handle<Quote>>& quotes,
52 const DayCounter& dc, const Interpolation interpolation = Interpolation::logLinear,
53 const Extrapolation extrapolation = Extrapolation::flatFwd)
54 : YieldTermStructure(dc), times_(times), quotes_(quotes), interpolation_(interpolation),
55 extrapolation_(extrapolation), data_(times_.size(), 1.0), today_(Settings::instance().evaluationDate()) {
56 for (Size i = 0; i < quotes.size(); ++i) {
57 QL_REQUIRE(times_.size() > 1, "at least two times required");
58 QL_REQUIRE(times_.size() == quotes.size(), "size of time and quote vectors do not match");
59 QL_REQUIRE(times_[0] == 0.0, "First time must be 0, got " << times_[0]);
60 QL_REQUIRE(!quotes[i].empty(), "quote at index " << i << " is empty");
61 registerWith(quotes_[i]);
62 }
65 QuantLib::ext::make_shared<LogLinearInterpolation>(times_.begin(), times_.end(), data_.begin());
66 } else {
67 dataInterpolation_ = QuantLib::ext::make_shared<LinearInterpolation>(times_.begin(), times_.end(), data_.begin());
68 }
69 registerWith(Settings::instance().evaluationDate());
70 }
71 //! date based constructor
72 InterpolatedDiscountCurve2(const std::vector<Date>& dates, const std::vector<Handle<Quote>>& quotes,
73 const DayCounter& dc, const Interpolation interpolation = Interpolation::logLinear,
74 const Extrapolation extrapolation = Extrapolation::flatFwd)
75 : YieldTermStructure(dc), times_(dates.size(), 0.0), quotes_(quotes), interpolation_(interpolation),
76 extrapolation_(extrapolation), data_(dates.size(), 1.0), today_(Settings::instance().evaluationDate()) {
77 for (Size i = 0; i < dates.size(); ++i)
78 times_[i] = dc.yearFraction(today_, dates[i]);
79 for (Size i = 0; i < quotes.size(); ++i) {
80 QL_REQUIRE(times_.size() > 1, "at least two times required");
81 QL_REQUIRE(times_.size() == quotes.size(), "size of time and quote vectors do not match");
82 QL_REQUIRE(times_[0] == 0.0, "First time must be 0, got " << times_[0]);
83 QL_REQUIRE(!quotes[i].empty(), "quote at index " << i << " is empty");
84 registerWith(quotes_[i]);
85 }
88 QuantLib::ext::make_shared<LogLinearInterpolation>(times_.begin(), times_.end(), data_.begin());
89 } else {
90 dataInterpolation_ = QuantLib::ext::make_shared<LinearInterpolation>(times_.begin(), times_.end(), data_.begin());
91 }
92 registerWith(Settings::instance().evaluationDate());
93 }
94 //@}
95
96 Date maxDate() const override { return Date::maxDate(); }
97 void update() override {
98 LazyObject::update();
99 TermStructure::update();
100 }
101 const Date& referenceDate() const override {
102 calculate();
103 return today_;
104 }
105
106 Calendar calendar() const override { return NullCalendar(); }
107 Natural settlementDays() const override { return 0; }
108
109protected:
110 void performCalculations() const override {
111 today_ = Settings::instance().evaluationDate();
112 for (Size i = 0; i < times_.size(); ++i) {
113 data_[i] = quotes_[i]->value();
114 QL_REQUIRE(data_[i] > 0, "InterpolatedDiscountCurve2: invalid value " << data_[i] << " at index " << i);
115 }
117 for (Size i = 0; i < times_.size(); ++i) {
118 data_[i] = -std::log(data_[std::max<Size>(i, 1)]) / times_[std::max<Size>(i, 1)];
119 }
120 }
121 dataInterpolation_->update();
122 }
123
124 DiscountFactor discountImpl(Time t) const override {
125 calculate();
126 if (t <= this->times_.back()) {
127 Real tmp = (*dataInterpolation_)(t, true);
129 return tmp;
130 else
131 return std::exp(-tmp * t);
132 }
133 Time tMax = this->times_.back();
134 DiscountFactor dMax =
135 interpolation_ == Interpolation::logLinear ? this->data_.back() : std::exp(-this->data_.back() * tMax);
137 Rate instFwdMax = -(*dataInterpolation_).derivative(tMax) / dMax;
138 return dMax * std::exp(-instFwdMax * (t - tMax));
139 } else {
140 return std::pow(dMax, t / tMax);
141 }
142 }
143
144private:
145 std::vector<Time> times_;
146 std::vector<Handle<Quote>> quotes_;
149 mutable std::vector<Real> data_;
150 mutable Date today_;
151 QuantLib::ext::shared_ptr<QuantLib::Interpolation> dataInterpolation_;
152};
153
154} // namespace QuantExt
155
156#endif
InterpolatedDiscountCurve2 as in QuantLib, but with floating discount quotes and floating reference d...
DiscountFactor discountImpl(Time t) const override
InterpolatedDiscountCurve2(const std::vector< Time > &times, const std::vector< Handle< Quote > > &quotes, const DayCounter &dc, const Interpolation interpolation=Interpolation::logLinear, const Extrapolation extrapolation=Extrapolation::flatFwd)
times based constructor, note that times should be consistent with day counter dc passed
InterpolatedDiscountCurve2(const std::vector< Date > &dates, const std::vector< Handle< Quote > > &quotes, const DayCounter &dc, const Interpolation interpolation=Interpolation::logLinear, const Extrapolation extrapolation=Extrapolation::flatFwd)
date based constructor
QuantLib::ext::shared_ptr< QuantLib::Interpolation > dataInterpolation_