Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
interpolateddiscountcurve.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 interpolateddiscountcurve.hpp
20 \brief interpolated discount term structure
21 \ingroup termstructures
22*/
23
24#ifndef quantext_interpolated_discount_curve_hpp
25#define quantext_interpolated_discount_curve_hpp
26
27#include <boost/make_shared.hpp>
28#include <ql/termstructures/yieldtermstructure.hpp>
30
31namespace QuantExt {
32using namespace QuantLib;
33
34//! InterpolatedDiscountCurve based on loglinear interpolation of DiscountFactors
35/*! InterpolatedDiscountCurve based on loglinear interpolation of DiscountFactors,
36 flat fwd extrapolation is always enabled, the term structure has always a
37 floating reference date
38
39 \ingroup termstructures
40 */
41class InterpolatedDiscountCurve : public YieldTermStructure {
42public:
45 //! \name Constructors
46 //@{
47 //! default constructor
48 InterpolatedDiscountCurve(const std::vector<Time>& times, const std::vector<Handle<Quote>>& quotes,
49 const Natural settlementDays, const Calendar& cal, const DayCounter& dc,
50 const Interpolation interpolation = Interpolation::logLinear,
51 const Extrapolation extrapolation = Extrapolation::flatFwd)
52 : YieldTermStructure(settlementDays, cal, dc), times_(times), interpolation_(interpolation),
53 extrapolation_(extrapolation) {
54 initalise(quotes);
55 }
56
57 //! constructor that takes a vector of dates
58 InterpolatedDiscountCurve(const std::vector<Date>& dates, const std::vector<Handle<Quote>>& quotes,
59 const Natural settlementDays, const Calendar& cal, const DayCounter& dc,
60 const Interpolation interpolation = Interpolation::logLinear,
61 const Extrapolation extrapolation = Extrapolation::flatFwd)
62 : YieldTermStructure(settlementDays, cal, dc), times_(dates.size()), interpolation_(interpolation),
63 extrapolation_(extrapolation) {
64 for (Size i = 0; i < dates.size(); ++i)
65 times_[i] = timeFromReference(dates[i]);
66 initalise(quotes);
67 }
68 //@}
69
70private:
71 void initalise(const std::vector<Handle<Quote>>& quotes) {
72 QL_REQUIRE(times_.size() > 1, "at least two times required");
73 QL_REQUIRE(times_[0] == 0.0, "First time must be 0, got " << times_[0]); // or date=asof
74 QL_REQUIRE(times_.size() == quotes.size(), "size of time and quote vectors do not match");
75 for (Size i = 0; i < quotes.size(); ++i) {
76 quotes_.push_back(QuantLib::ext::make_shared<LogQuote>(quotes[i]));
77 }
78 for (Size i = 0; i < times_.size() - 1; ++i)
79 timeDiffs_.push_back(times_[i + 1] - times_[i]);
80 }
81
82 //! \name TermStructure interface
83 //@{
84 Date maxDate() const override { return Date::maxDate(); } // flat fwd extrapolation
85 //@}
86
87protected:
88 DiscountFactor discountImpl(Time t) const override {
89 if (t > this->times_.back() && extrapolation_ == Extrapolation::flatZero) {
90 Real tMax = this->times_.back();
91 Real dMax = std::exp(quotes_.back()->value());
92 return std::pow(dMax, t / tMax);
93 }
94 std::vector<Time>::const_iterator it = std::upper_bound(times_.begin(), times_.end(), t);
95 Size i = std::min<Size>(it - times_.begin(), times_.size() - 1);
96 Real weight = (times_[i] - t) / timeDiffs_[i - 1];
97 if (interpolation_ == Interpolation::logLinear || t > this->times_.back()) {
98 // this handles flat fwd extrapolation (t > times.back()) as well
99 Real value = (1.0 - weight) * quotes_[i]->value() + weight * quotes_[i - 1]->value();
100 return ::exp(value);
101 } else {
102 Real value =
103 (1.0 - weight) * quotes_[i]->value() / times_[i] + weight * quotes_[i - 1]->value() / times_[i - 1];
104 return ::exp(t * value);
105 }
106 }
107
108private:
109 std::vector<Time> times_;
110 std::vector<Time> timeDiffs_;
111 std::vector<QuantLib::ext::shared_ptr<Quote>> quotes_;
114};
115
116} // namespace QuantExt
117
118#endif
InterpolatedDiscountCurve based on loglinear interpolation of DiscountFactors.
DiscountFactor discountImpl(Time t) const override
InterpolatedDiscountCurve(const std::vector< Time > &times, const std::vector< Handle< Quote > > &quotes, const Natural settlementDays, const Calendar &cal, const DayCounter &dc, const Interpolation interpolation=Interpolation::logLinear, const Extrapolation extrapolation=Extrapolation::flatFwd)
default constructor
void initalise(const std::vector< Handle< Quote > > &quotes)
std::vector< QuantLib::ext::shared_ptr< Quote > > quotes_
InterpolatedDiscountCurve(const std::vector< Date > &dates, const std::vector< Handle< Quote > > &quotes, const Natural settlementDays, const Calendar &cal, const DayCounter &dc, const Interpolation interpolation=Interpolation::logLinear, const Extrapolation extrapolation=Extrapolation::flatFwd)
constructor that takes a vector of dates
stores log of quote for log-linear interpolation