QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mixedinterpolation.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) 2010 Ferdinando Ametrano
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 mixedinterpolation.hpp
21 \brief mixed interpolation between discrete points
22*/
23
24#ifndef quantlib_mixed_interpolation_hpp
25#define quantlib_mixed_interpolation_hpp
26
30
31namespace QuantLib {
32
33 namespace detail {
34
35 template<class I1, class I2, class Ia, class Ib>
36 class MixedInterpolationImpl;
37
38 }
39
40
42 enum Behavior {
43 ShareRanges, /*!< Define both interpolations over the
44 whole range defined by the passed
45 iterators. This is the default
46 behavior. */
47 SplitRanges /*!< Define the first interpolation over the
48 first part of the range, and the second
49 interpolation over the second part. */
50 };
51 };
52
53 //! mixed linear/cubic interpolation between discrete points
54 /*! \ingroup interpolations
55 \warning See the Interpolation class for information about the
56 required lifetime of the underlying data.
57 */
59 public:
60 /*! \pre the \f$ x \f$ values must be sorted. */
61 template <class I1, class I2>
62 MixedLinearCubicInterpolation(const I1& xBegin, const I1& xEnd,
63 const I2& yBegin, Size n,
66 bool monotonic,
68 Real leftConditionValue,
70 Real rightConditionValue) {
71 impl_ = ext::shared_ptr<Interpolation::Impl>(new
73 xBegin, xEnd, yBegin, n, behavior,
74 Linear(),
75 Cubic(da, monotonic,
76 leftC, leftConditionValue,
77 rightC, rightConditionValue)));
78 impl_->update();
79 }
80 };
81
82 //! mixed linear/cubic interpolation factory and traits
83 /*! \ingroup interpolations */
85 public:
89 bool monotonic = true,
92 Real leftConditionValue = 0.0,
95 Real rightConditionValue = 0.0)
96 : n_(n), behavior_(behavior), da_(da), monotonic_(monotonic),
97 leftType_(leftCondition), rightType_(rightCondition),
98 leftValue_(leftConditionValue), rightValue_(rightConditionValue) {}
99 template <class I1, class I2>
100 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
101 const I2& yBegin) const {
102 return MixedLinearCubicInterpolation(xBegin, xEnd,
103 yBegin, n_, behavior_,
107 }
108 // fix below
109 static const bool global = true;
110 static const Size requiredPoints = 3;
111 private:
118 };
119
120 // convenience classes
121
123 public:
124 /*! \pre the \f$ x \f$ values must be sorted. */
125 template <class I1, class I2>
126 MixedLinearCubicNaturalSpline(const I1& xBegin, const I1& xEnd,
127 const I2& yBegin, Size n,
130 : MixedLinearCubicInterpolation(xBegin, xEnd, yBegin, n, behavior,
131 CubicInterpolation::Spline, false,
132 CubicInterpolation::SecondDerivative, 0.0,
133 CubicInterpolation::SecondDerivative, 0.0) {}
134 };
135
137 public:
138 /*! \pre the \f$ x \f$ values must be sorted. */
139 template <class I1, class I2>
140 MixedLinearMonotonicCubicNaturalSpline(const I1& xBegin, const I1& xEnd,
141 const I2& yBegin, Size n,
144 : MixedLinearCubicInterpolation(xBegin, xEnd, yBegin, n, behavior,
145 CubicInterpolation::Spline, true,
146 CubicInterpolation::SecondDerivative, 0.0,
147 CubicInterpolation::SecondDerivative, 0.0) {}
148 };
149
151 public:
152 /*! \pre the \f$ x \f$ values must be sorted. */
153 template <class I1, class I2>
154 MixedLinearKrugerCubic(const I1& xBegin, const I1& xEnd,
155 const I2& yBegin, Size n,
158 : MixedLinearCubicInterpolation(xBegin, xEnd, yBegin, n, behavior,
159 CubicInterpolation::Kruger, false,
160 CubicInterpolation::SecondDerivative, 0.0,
161 CubicInterpolation::SecondDerivative, 0.0) {}
162 };
163
165 public:
166 /*! \pre the \f$ x \f$ values must be sorted. */
167 template <class I1, class I2>
168 MixedLinearFritschButlandCubic(const I1& xBegin, const I1& xEnd,
169 const I2& yBegin, Size n,
172 : MixedLinearCubicInterpolation(xBegin, xEnd, yBegin, n, behavior,
173 CubicInterpolation::FritschButland, false,
174 CubicInterpolation::SecondDerivative, 0.0,
175 CubicInterpolation::SecondDerivative, 0.0) {}
176 };
177
179 public:
180 /*! \pre the \f$ x \f$ values must be sorted. */
181 template <class I1, class I2>
182 MixedLinearParabolic(const I1& xBegin, const I1& xEnd,
183 const I2& yBegin, Size n,
186 : MixedLinearCubicInterpolation(xBegin, xEnd, yBegin, n, behavior,
188 CubicInterpolation::SecondDerivative, 0.0,
189 CubicInterpolation::SecondDerivative, 0.0) {}
190 };
191
193 public:
194 /*! \pre the \f$ x \f$ values must be sorted. */
195 template <class I1, class I2>
196 MixedLinearMonotonicParabolic(const I1& xBegin, const I1& xEnd,
197 const I2& yBegin, Size n,
200 : MixedLinearCubicInterpolation(xBegin, xEnd, yBegin, n, behavior,
202 CubicInterpolation::SecondDerivative, 0.0,
203 CubicInterpolation::SecondDerivative, 0.0) {}
204 };
205
206 namespace detail {
207
208 template <class I1, class I2, class Interpolator1, class Interpolator2>
210 : public Interpolation::templateImpl<I1,I2> {
211 public:
212 MixedInterpolationImpl(const I1& xBegin, const I1& xEnd,
213 const I2& yBegin, Size n,
216 const Interpolator1& factory1 = Interpolator1(),
217 const Interpolator2& factory2 = Interpolator2())
218 : Interpolation::templateImpl<I1,I2>(
219 xBegin, xEnd, yBegin,
220 std::max(Size(Interpolator1::requiredPoints),
221 Size(Interpolator2::requiredPoints))),
222 n_(n) {
223
224 xBegin2_ = this->xBegin_ + n_;
225 yBegin2_ = this->yBegin_ + n_;
226
227 QL_REQUIRE(xBegin2_<this->xEnd_,
228 "too large n (" << n << ") for " <<
229 this->xEnd_-this->xBegin_ << "-element x sequence");
230
231 switch (behavior) {
233 interpolation1_ = factory1.interpolate(this->xBegin_,
234 this->xEnd_,
235 this->yBegin_);
236 interpolation2_ = factory2.interpolate(this->xBegin_,
237 this->xEnd_,
238 this->yBegin_);
239 break;
241 interpolation1_ = factory1.interpolate(this->xBegin_,
242 this->xBegin2_+1,
243 this->yBegin_);
244 interpolation2_ = factory2.interpolate(this->xBegin2_,
245 this->xEnd_,
246 this->yBegin2_);
247 break;
248 default:
249 QL_FAIL("unknown mixed-interpolation behavior: " << behavior);
250 }
251 }
252
253 void update() {
256 }
257 Real value(Real x) const {
258 if (x<*(this->xBegin2_))
259 return interpolation1_(x, true);
260 return interpolation2_(x, true);
261 }
262 Real primitive(Real x) const {
263 if (x<*(this->xBegin2_))
264 return interpolation1_.primitive(x, true);
265 return interpolation2_.primitive(x, true) -
268 }
270 if (x<*(this->xBegin2_))
271 return interpolation1_.derivative(x, true);
272 return interpolation2_.derivative(x, true);
273 }
275 if (x<*(this->xBegin2_))
276 return interpolation1_.secondDerivative(x, true);
277 return interpolation2_.secondDerivative(x, true);
278 }
279 Size switchIndex() { return n_; }
280 private:
285 };
286
287 }
288
289}
290
291#endif
Cubic interpolation factory and traits
Cubic interpolation between discrete points.
@ SecondDerivative
Match value of second derivative at end.
basic template implementation
templateImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const int requiredPoints=2)
base class for 1-D interpolations.
Real secondDerivative(Real x, bool allowExtrapolation=false) const
Real derivative(Real x, bool allowExtrapolation=false) const
ext::shared_ptr< Impl > impl_
Real primitive(Real x, bool allowExtrapolation=false) const
Linear-interpolation factory and traits
mixed linear/cubic interpolation factory and traits
CubicInterpolation::DerivativeApprox da_
MixedLinearCubic(Size n, MixedInterpolation::Behavior behavior, CubicInterpolation::DerivativeApprox da, bool monotonic=true, CubicInterpolation::BoundaryCondition leftCondition=CubicInterpolation::SecondDerivative, Real leftConditionValue=0.0, CubicInterpolation::BoundaryCondition rightCondition=CubicInterpolation::SecondDerivative, Real rightConditionValue=0.0)
CubicInterpolation::BoundaryCondition rightType_
CubicInterpolation::BoundaryCondition leftType_
MixedInterpolation::Behavior behavior_
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
mixed linear/cubic interpolation between discrete points
MixedLinearCubicInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Size n, MixedInterpolation::Behavior behavior, CubicInterpolation::DerivativeApprox da, bool monotonic, CubicInterpolation::BoundaryCondition leftC, Real leftConditionValue, CubicInterpolation::BoundaryCondition rightC, Real rightConditionValue)
MixedLinearCubicNaturalSpline(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
MixedLinearFritschButlandCubic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
MixedLinearKrugerCubic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
MixedLinearMonotonicCubicNaturalSpline(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
MixedLinearMonotonicParabolic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
MixedLinearParabolic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
MixedInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges, const Interpolator1 &factory1=Interpolator1(), const Interpolator2 &factory2=Interpolator2())
cubic interpolation between discrete points
output manipulators
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
linear interpolation between discrete points
Definition: any.hpp:35
STL namespace.