QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
interpolatedcurve.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
24#ifndef quantlib_interpolated_curve_hpp
25#define quantlib_interpolated_curve_hpp
26
27#include <ql/math/interpolation.hpp>
28#include <ql/time/date.hpp>
29#include <ql/time/daycounter.hpp>
30#include <utility>
31#include <vector>
32
33namespace QuantLib {
34
36
40 template <class Interpolator>
42 public:
43 ~InterpolatedCurve() = default;
44
45 protected:
47
48 InterpolatedCurve(std::vector<Time> times,
49 std::vector<Real> data,
50 const Interpolator& i = Interpolator())
51 : times_(std::move(times)), data_(std::move(data)), interpolator_(i) {}
52
53 InterpolatedCurve(std::vector<Time> times,
54 const Interpolator& i = Interpolator())
55 : times_(std::move(times)), data_(times_.size()), interpolator_(i) {}
56
58 const Interpolator& i = Interpolator())
59 : times_(n), data_(n), interpolator_(i) {}
60
61 InterpolatedCurve(const Interpolator& i = Interpolator())
62 : interpolator_(i) {}
64
66
70 }
71
73 times_ = c.times_;
74 data_ = c.data_;
77 return *this;
78 }
80
82
84 : times_(std::move(c.times_)), data_(std::move(c.data_)), interpolator_(std::move(c.interpolator_)) {
86 }
87
89 times_ = std::move(c.times_);
90 data_ = std::move(c.data_);
91 interpolator_ = std::move(c.interpolator_);
93 return *this;
94 }
96
98
99 void setupTimes(const std::vector<Date>& dates,
100 Date referenceDate,
101 const DayCounter& dayCounter) {
102 times_.resize(dates.size());
103 times_[0] = dayCounter.yearFraction(referenceDate, dates[0]);
104 for (Size i = 1; i < dates.size(); i++) {
105 QL_REQUIRE(dates[i] > dates[i-1],
106 "dates not sorted: " << dates[i] << " passed after " << dates[i-1]);
107
108 times_[i] = dayCounter.yearFraction(referenceDate, dates[i]);
109 QL_REQUIRE(!close(this->times_[i], this->times_[i-1]),
110 "two passed dates (" << dates[i-1] << " and " << dates[i]
111 << ") correspond to the same time "
112 << "under this curve's day count convention (" << dayCounter << ")");
113 }
114 }
115
117 interpolation_ = interpolator_.interpolate(times_.begin(),
118 times_.end(),
119 data_.begin());
120 }
122
123 mutable std::vector<Time> times_;
124 mutable std::vector<Real> data_;
126 Interpolator interpolator_;
127 // Usually, the maximum date is the one corresponding to the
128 // last node. However, it might happen that a bit of
129 // extrapolation is used by construction; for instance, when a
130 // curve is bootstrapped and the last relevant date for an
131 // instrument is after the corresponding pillar.
132 // We provide here a slot to store this information, so that
133 // it's available to all derived classes (we should have
134 // probably done the same with the dates_ vector, but moving
135 // it here might not be entirely backwards-compatible).
137 };
138
139}
140
141#endif
142
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Helper class to build interpolated term structures.
InterpolatedCurve(const Interpolator &i=Interpolator())
InterpolatedCurve(Size n, const Interpolator &i=Interpolator())
InterpolatedCurve(std::vector< Time > times, std::vector< Real > data, const Interpolator &i=Interpolator())
void setupTimes(const std::vector< Date > &dates, Date referenceDate, const DayCounter &dayCounter)
InterpolatedCurve(InterpolatedCurve &&c) noexcept
InterpolatedCurve(std::vector< Time > times, const Interpolator &i=Interpolator())
InterpolatedCurve(const InterpolatedCurve &c)
InterpolatedCurve & operator=(const InterpolatedCurve &c)
InterpolatedCurve & operator=(InterpolatedCurve &&c) noexcept
base class for 1-D interpolations.
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
STL namespace.