QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
interpolatedcurve.hpp
Go to the documentation of this file.
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
20/*! \file interpolatedcurve.hpp
21 \brief Helper class to build interpolated term structures
22*/
23
24#ifndef quantlib_interpolated_curve_hpp
25#define quantlib_interpolated_curve_hpp
26
28#include <ql/time/date.hpp>
30#include <utility>
31#include <vector>
32
33namespace QuantLib {
34
35 //! Helper class to build interpolated term structures
36 /*! Interpolated term structures can use proected or private
37 inheritance from this class to obtain the relevant data
38 members and implement correct copy behavior.
39 */
40 template <class Interpolator>
42 public:
43 ~InterpolatedCurve() = default;
44
45 protected:
46 //! \name Building
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) {}
63 //@}
64
65 //! \name Copying
66 //@{
70 }
71
73 times_ = c.times_;
74 data_ = c.data_;
77 return *this;
78 }
79 //@}
80
81 //! \name Moving
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 }
95 //@}
96
97 //! \name Utilities
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 }
121 //@}
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.
date- and time-related classes, typedefs and enumerations
day counter class
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
std::size_t Size
size of a container
Definition: types.hpp:58
base class for 1-D interpolations
Definition: any.hpp:35
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
STL namespace.