QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mixedinterpolation.hpp
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
24#ifndef quantlib_mixed_interpolation_hpp
25#define quantlib_mixed_interpolation_hpp
26
27#include <ql/math/interpolations/linearinterpolation.hpp>
28#include <ql/math/interpolations/cubicinterpolation.hpp>
29#include <ql/utilities/dataformatters.hpp>
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 {
50 };
51 };
52
54
59 public:
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
83
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:
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:
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:
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:
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:
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:
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())
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.