QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
loginterpolation.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) 2002, 2003, 2008, 2009 Ferdinando Ametrano
5 Copyright (C) 2004, 2007, 2008 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file loginterpolation.hpp
22 \brief log-linear and log-cubic interpolation between discrete points
23*/
24
25#ifndef quantlib_log_interpolation_hpp
26#define quantlib_log_interpolation_hpp
27
32
33namespace QuantLib {
34
35 namespace detail {
36 template<class I1, class I2, class I> class LogInterpolationImpl;
37 template<class I1, class I2, class IN1, class IN2> class LogMixedInterpolationImpl;
38 }
39
40 //! %log-linear interpolation between discrete points
41 /*! \ingroup interpolations
42 \warning See the Interpolation class for information about the
43 required lifetime of the underlying data.
44 */
46 public:
47 /*! \pre the \f$ x \f$ values must be sorted. */
48 template <class I1, class I2>
49 LogLinearInterpolation(const I1& xBegin, const I1& xEnd,
50 const I2& yBegin) {
51 impl_ = ext::shared_ptr<Interpolation::Impl>(new
53 yBegin));
54 impl_->update();
55 }
56 };
57
58 //! log-linear interpolation factory and traits
59 /*! \ingroup interpolations */
60 class LogLinear {
61 public:
62 template <class I1, class I2>
63 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
64 const I2& yBegin) const {
65 return LogLinearInterpolation(xBegin, xEnd, yBegin);
66 }
67 static const bool global = false;
68 static const Size requiredPoints = 2;
69 };
70
71 //! %log-cubic interpolation between discrete points
72 /*! \ingroup interpolations */
74 public:
75 /*! \pre the \f$ x \f$ values must be sorted. */
76 template <class I1, class I2>
77 LogCubicInterpolation(const I1& xBegin, const I1& xEnd,
78 const I2& yBegin,
80 bool monotonic,
82 Real leftConditionValue,
84 Real rightConditionValue) {
85 impl_ = ext::shared_ptr<Interpolation::Impl>(new
87 xBegin, xEnd, yBegin,
88 Cubic(da, monotonic,
89 leftC, leftConditionValue,
90 rightC, rightConditionValue)));
91 impl_->update();
92 }
93 };
94
95 //! log-cubic interpolation factory and traits
96 /*! \ingroup interpolations */
97 class LogCubic {
98 public:
100 bool monotonic = true,
103 Real leftConditionValue = 0.0,
106 Real rightConditionValue = 0.0)
107 : da_(da), monotonic_(monotonic),
108 leftType_(leftCondition), rightType_(rightCondition),
109 leftValue_(leftConditionValue), rightValue_(rightConditionValue) {}
110 template <class I1, class I2>
111 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
112 const I2& yBegin) const {
113 return LogCubicInterpolation(xBegin, xEnd, yBegin,
117 }
118 static const bool global = true;
119 static const Size requiredPoints = 2;
120 private:
125 };
126
127 // convenience classes
128
129 class DefaultLogCubic : public LogCubic {
130 public:
132 : LogCubic(CubicInterpolation::Kruger) {}
133 };
134
136 public:
138 : LogCubic(CubicInterpolation::Spline, true,
139 CubicInterpolation::SecondDerivative, 0.0,
140 CubicInterpolation::SecondDerivative, 0.0) {}
141 };
142
143 class KrugerLog : public LogCubic {
144 public:
146 : LogCubic(CubicInterpolation::Kruger, false,
147 CubicInterpolation::SecondDerivative, 0.0,
148 CubicInterpolation::SecondDerivative, 0.0) {}
149 };
150
151
153 public:
154 /*! \pre the \f$ x \f$ values must be sorted. */
155 template <class I1, class I2>
156 LogCubicNaturalSpline(const I1& xBegin,
157 const I1& xEnd,
158 const I2& yBegin)
159 : LogCubicInterpolation(xBegin, xEnd, yBegin,
160 CubicInterpolation::Spline, false,
161 CubicInterpolation::SecondDerivative, 0.0,
162 CubicInterpolation::SecondDerivative, 0.0) {}
163 };
164
166 public:
167 /*! \pre the \f$ x \f$ values must be sorted. */
168 template <class I1, class I2>
170 const I1& xEnd,
171 const I2& yBegin)
172 : LogCubicInterpolation(xBegin, xEnd, yBegin,
173 CubicInterpolation::Spline, true,
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 KrugerLogCubic(const I1& xBegin,
183 const I1& xEnd,
184 const I2& yBegin)
185 : LogCubicInterpolation(xBegin, xEnd, yBegin,
186 CubicInterpolation::Kruger, false,
187 CubicInterpolation::SecondDerivative, 0.0,
188 CubicInterpolation::SecondDerivative, 0.0) {}
189 };
190
192 public:
193 /*! \pre the \f$ x \f$ values must be sorted. */
194 template <class I1, class I2>
195 HarmonicLogCubic(const I1& xBegin,
196 const I1& xEnd,
197 const I2& yBegin)
198 : LogCubicInterpolation(xBegin, xEnd, yBegin,
199 CubicInterpolation::Harmonic, false,
200 CubicInterpolation::SecondDerivative, 0.0,
201 CubicInterpolation::SecondDerivative, 0.0) {}
202 };
203
205 public:
206 /*! \pre the \f$ x \f$ values must be sorted. */
207 template <class I1, class I2>
208 FritschButlandLogCubic(const I1& xBegin,
209 const I1& xEnd,
210 const I2& yBegin)
211 : LogCubicInterpolation(xBegin, xEnd, yBegin,
212 CubicInterpolation::FritschButland, false,
213 CubicInterpolation::SecondDerivative, 0.0,
214 CubicInterpolation::SecondDerivative, 0.0) {}
215 };
216
218 public:
219 /*! \pre the \f$ x \f$ values must be sorted. */
220 template <class I1, class I2>
221 LogParabolic(const I1& xBegin,
222 const I1& xEnd,
223 const I2& yBegin)
224 : LogCubicInterpolation(xBegin, xEnd, yBegin,
226 CubicInterpolation::SecondDerivative, 0.0,
227 CubicInterpolation::SecondDerivative, 0.0) {}
228 };
229
231 public:
232 /*! \pre the \f$ x \f$ values must be sorted. */
233 template <class I1, class I2>
234 MonotonicLogParabolic(const I1& xBegin,
235 const I1& xEnd,
236 const I2& yBegin)
237 : LogCubicInterpolation(xBegin, xEnd, yBegin,
239 CubicInterpolation::SecondDerivative, 0.0,
240 CubicInterpolation::SecondDerivative, 0.0) {}
241 };
242
243 //! %log-mixedlinearcubic interpolation between discrete points
244 /*! \ingroup interpolations */
246 public:
247 /*! \pre the \f$ x \f$ values must be sorted. */
248 template <class I1, class I2>
249 LogMixedLinearCubicInterpolation(const I1& xBegin, const I1& xEnd,
250 const I2& yBegin, const Size n,
253 bool monotonic,
255 Real leftConditionValue,
257 Real rightConditionValue) {
258 impl_ = ext::shared_ptr<Interpolation::Impl>(new
260 xBegin, xEnd, yBegin,
261 MixedLinearCubic(n, behavior, da, monotonic,
262 leftC, leftConditionValue,
263 rightC, rightConditionValue)));
264 impl_->update();
265 }
266 };
267
268 //! log-cubic interpolation factory and traits
269 /*! \ingroup interpolations */
271 public:
275 bool monotonic = true,
278 Real leftConditionValue = 0.0,
281 Real rightConditionValue = 0.0)
282 : n_(n), behavior_(behavior), da_(da), monotonic_(monotonic),
283 leftType_(leftCondition), rightType_(rightCondition),
284 leftValue_(leftConditionValue), rightValue_(rightConditionValue) {}
285 template <class I1, class I2>
286 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
287 const I2& yBegin) const {
288 return LogMixedLinearCubicInterpolation(xBegin, xEnd, yBegin,
289 n_, behavior_,
293 }
294 static const bool global = true;
295 static const Size requiredPoints = 3;
296 private:
303 };
304
305 // convenience classes
306
308 public:
312 : LogMixedLinearCubic(n, behavior,
313 CubicInterpolation::Kruger) {}
314 };
315
317 public:
321 : LogMixedLinearCubic(n, behavior,
322 CubicInterpolation::Spline, true,
323 CubicInterpolation::SecondDerivative, 0.0,
324 CubicInterpolation::SecondDerivative, 0.0) {}
325 };
326
328 public:
332 : LogMixedLinearCubic(n, behavior,
333 CubicInterpolation::Kruger, false,
334 CubicInterpolation::SecondDerivative, 0.0,
335 CubicInterpolation::SecondDerivative, 0.0) {}
336 };
337
338
340 public:
341 /*! \pre the \f$ x \f$ values must be sorted. */
342 template <class I1, class I2>
343 LogMixedLinearCubicNaturalSpline(const I1& xBegin, const I1& xEnd,
344 const I2& yBegin, const Size n,
347 : LogMixedLinearCubicInterpolation(xBegin, xEnd, yBegin, n, behavior,
348 CubicInterpolation::Spline, false,
349 CubicInterpolation::SecondDerivative, 0.0,
350 CubicInterpolation::SecondDerivative, 0.0) {}
351 };
352
353
354 namespace detail {
355
356 template <class I1, class I2, class Interpolator>
358 : public Interpolation::templateImpl<I1,I2> {
359 public:
360 LogInterpolationImpl(const I1& xBegin, const I1& xEnd,
361 const I2& yBegin,
362 const Interpolator& factory = Interpolator())
363 : Interpolation::templateImpl<I1,I2>(xBegin, xEnd, yBegin,
364 Interpolator::requiredPoints),
365 logY_(xEnd-xBegin) {
366 interpolation_ = factory.interpolate(this->xBegin_,
367 this->xEnd_,
368 logY_.begin());
369 }
370 void update() override {
371 for (Size i=0; i<logY_.size(); ++i) {
372 QL_REQUIRE(this->yBegin_[i]>0.0,
373 "invalid value (" << this->yBegin_[i]
374 << ") at index " << i);
375 logY_[i] = std::log(this->yBegin_[i]);
376 }
378 }
379 Real value(Real x) const override { return std::exp(interpolation_(x, true)); }
380 Real primitive(Real) const override {
381 QL_FAIL("LogInterpolation primitive not implemented");
382 }
383 Real derivative(Real x) const override {
384 return value(x)*interpolation_.derivative(x, true);
385 }
386 Real secondDerivative(Real x) const override {
387 return derivative(x)*interpolation_.derivative(x, true) +
389 }
390
391 private:
392 std::vector<Real> logY_;
394 };
395
396 }
397
398}
399
400#endif
Cubic interpolation factory and traits
Cubic interpolation between discrete points.
@ SecondDerivative
Match value of second derivative at end.
DefaultLogMixedLinearCubic(const Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
FritschButlandLogCubic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
HarmonicLogCubic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
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_
KrugerLogCubic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
KrugerLogMixedLinearCubic(const Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
log-cubic interpolation factory and traits
CubicInterpolation::DerivativeApprox da_
static const bool global
CubicInterpolation::BoundaryCondition rightType_
CubicInterpolation::BoundaryCondition leftType_
static const Size requiredPoints
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
LogCubic(CubicInterpolation::DerivativeApprox da, bool monotonic=true, CubicInterpolation::BoundaryCondition leftCondition=CubicInterpolation::SecondDerivative, Real leftConditionValue=0.0, CubicInterpolation::BoundaryCondition rightCondition=CubicInterpolation::SecondDerivative, Real rightConditionValue=0.0)
log-cubic interpolation between discrete points
LogCubicInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, CubicInterpolation::DerivativeApprox da, bool monotonic, CubicInterpolation::BoundaryCondition leftC, Real leftConditionValue, CubicInterpolation::BoundaryCondition rightC, Real rightConditionValue)
LogCubicNaturalSpline(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
log-linear interpolation factory and traits
static const bool global
static const Size requiredPoints
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
log-linear interpolation between discrete points
LogLinearInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
log-cubic interpolation factory and traits
CubicInterpolation::DerivativeApprox da_
CubicInterpolation::BoundaryCondition rightType_
CubicInterpolation::BoundaryCondition leftType_
LogMixedLinearCubic(const 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)
MixedInterpolation::Behavior behavior_
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
log-mixedlinearcubic interpolation between discrete points
LogMixedLinearCubicInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const Size n, MixedInterpolation::Behavior behavior, CubicInterpolation::DerivativeApprox da, bool monotonic, CubicInterpolation::BoundaryCondition leftC, Real leftConditionValue, CubicInterpolation::BoundaryCondition rightC, Real rightConditionValue)
LogMixedLinearCubicNaturalSpline(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
LogParabolic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
mixed linear/cubic interpolation factory and traits
MonotonicLogCubicNaturalSpline(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
MonotonicLogMixedLinearCubic(const Size n, MixedInterpolation::Behavior behavior=MixedInterpolation::ShareRanges)
MonotonicLogParabolic(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
LogInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const Interpolator &factory=Interpolator())
Real derivative(Real x) const override
Real secondDerivative(Real x) const override
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
mixed interpolation between discrete points
Definition: any.hpp:35