Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
flatextrapolation.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file flatextrapolation.hpp
20 \brief flat interpolation decorator
21 \ingroup math
22*/
23
24#ifndef quantext_flat_extrapolation_hpp
25#define quantext_flat_extrapolation_hpp
26
27#include <ql/math/interpolation.hpp>
28#include <ql/math/interpolations/cubicinterpolation.hpp>
29#include <ql/math/interpolations/linearinterpolation.hpp>
30#include <ql/math/interpolations/loginterpolation.hpp>
31
32#include <boost/make_shared.hpp>
33
34namespace QuantExt {
35using namespace QuantLib;
36
37//! Flat extrapolation given a base interpolation
38/*! \ingroup math
39 */
40class FlatExtrapolation : public Interpolation {
41private:
42 class FlatExtrapolationImpl : public Interpolation::Impl {
43
44 public:
45 FlatExtrapolationImpl(const QuantLib::ext::shared_ptr<Interpolation>& i) : i_(i) {}
46 void update() override { i_->update(); }
47 Real xMin() const override { return i_->xMin(); }
48 Real xMax() const override { return i_->xMax(); }
49 std::vector<Real> xValues() const override { QL_FAIL("not implemented"); }
50 std::vector<Real> yValues() const override { QL_FAIL("not implemented"); }
51 bool isInRange(Real x) const override { return i_->isInRange(x); }
52 Real value(Real x) const override {
53 Real tmp = std::max(std::min(x, i_->xMax()), i_->xMin());
54 return i_->operator()(tmp);
55 }
56 Real primitive(Real x) const override {
57 if (x >= i_->xMin() && x <= i_->xMax()) {
58 return i_->primitive(x);
59 }
60 if (x < i_->xMin()) {
61 return i_->primitive(i_->xMin()) - (i_->xMin() - x);
62 } else {
63 return i_->primitive(i_->xMax()) + (x - i_->xMax());
64 }
65 }
66 Real derivative(Real x) const override {
67 if (x > i_->xMin() && x < i_->xMax()) {
68 return i_->derivative(x);
69 } else {
70 // that is the left derivative for xmin and
71 // the right derivative for xmax
72 return 0.0;
73 }
74 }
75 Real secondDerivative(Real x) const override {
76 if (x > i_->xMin() && x < i_->xMax()) {
77 return i_->secondDerivative(x);
78 } else {
79 // that is the left derivative for xmin and
80 // the right derivative for xmax
81 return 0.0;
82 }
83 }
84
85 private:
86 const QuantLib::ext::shared_ptr<Interpolation> i_;
87 };
88
89public:
90 FlatExtrapolation(const QuantLib::ext::shared_ptr<Interpolation>& i) {
91 impl_ = QuantLib::ext::make_shared<FlatExtrapolationImpl>(i);
92 impl_->update();
93 }
94};
95
96//! %Linear-interpolation and flat extrapolation factory and traits
98public:
99 template <class I1, class I2> Interpolation interpolate(const I1& xBegin, const I1& xEnd, const I2& yBegin) const {
100 return FlatExtrapolation(QuantLib::ext::make_shared<LinearInterpolation>(xBegin, xEnd, yBegin));
101 }
102 static const bool global = false;
103 static const Size requiredPoints = 2;
104};
105
106//! %Linear-interpolation and flat extrapolation factory and traits
108public:
109 template <class I1, class I2> Interpolation interpolate(const I1& xBegin, const I1& xEnd, const I2& yBegin) const {
110 return FlatExtrapolation(QuantLib::ext::make_shared<LogLinearInterpolation>(xBegin, xEnd, yBegin));
111 }
112 static const bool global = false;
113 static const Size requiredPoints = 2;
114};
115
116//! Hermite interpolation and flat extrapolation factory and traits
118public:
119 template <class I1, class I2> Interpolation interpolate(const I1& xBegin, const I1& xEnd, const I2& yBegin) const {
120 return FlatExtrapolation(QuantLib::ext::make_shared<Parabolic>(xBegin, xEnd, yBegin));
121 }
122 static const bool global = false;
123 static const Size requiredPoints = 2;
124};
125
126//! Cubic interpolation and flat extrapolation factory and traits
128public:
130 QuantLib::CubicInterpolation::DerivativeApprox da = QuantLib::CubicInterpolation::Kruger,
131 bool monotonic = false,
132 QuantLib::CubicInterpolation::BoundaryCondition leftCondition = QuantLib::CubicInterpolation::SecondDerivative,
133 QuantLib::Real leftConditionValue = 0.0,
134 QuantLib::CubicInterpolation::BoundaryCondition rightCondition = QuantLib::CubicInterpolation::SecondDerivative,
135 QuantLib::Real rightConditionValue = 0.0)
136 : da_(da), monotonic_(monotonic), leftType_(leftCondition), rightType_(rightCondition),
137 leftValue_(leftConditionValue), rightValue_(rightConditionValue) {}
138
139 template <class I1, class I2> Interpolation interpolate(const I1& xBegin, const I1& xEnd, const I2& yBegin) const {
140 return FlatExtrapolation(QuantLib::ext::make_shared<CubicInterpolation>(
141 xBegin, xEnd, yBegin, da_, monotonic_, leftType_, leftValue_, rightType_, rightValue_));
142 }
143
144 static const bool global = true;
145 static const Size requiredPoints = 2;
146
147private:
148 QuantLib::CubicInterpolation::DerivativeApprox da_;
150 QuantLib::CubicInterpolation::BoundaryCondition leftType_;
151 QuantLib::CubicInterpolation::BoundaryCondition rightType_;
152 QuantLib::Real leftValue_;
153 QuantLib::Real rightValue_;
154};
155
156} // namespace QuantExt
157
158#endif
Cubic interpolation and flat extrapolation factory and traits.
QuantLib::CubicInterpolation::DerivativeApprox da_
static const bool global
CubicFlat(QuantLib::CubicInterpolation::DerivativeApprox da=QuantLib::CubicInterpolation::Kruger, bool monotonic=false, QuantLib::CubicInterpolation::BoundaryCondition leftCondition=QuantLib::CubicInterpolation::SecondDerivative, QuantLib::Real leftConditionValue=0.0, QuantLib::CubicInterpolation::BoundaryCondition rightCondition=QuantLib::CubicInterpolation::SecondDerivative, QuantLib::Real rightConditionValue=0.0)
QuantLib::Real rightValue_
static const Size requiredPoints
QuantLib::CubicInterpolation::BoundaryCondition leftType_
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
QuantLib::CubicInterpolation::BoundaryCondition rightType_
std::vector< Real > xValues() const override
std::vector< Real > yValues() const override
FlatExtrapolationImpl(const QuantLib::ext::shared_ptr< Interpolation > &i)
const QuantLib::ext::shared_ptr< Interpolation > i_
Flat extrapolation given a base interpolation.
FlatExtrapolation(const QuantLib::ext::shared_ptr< Interpolation > &i)
Hermite interpolation and flat extrapolation factory and traits.
static const bool global
static const Size requiredPoints
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
Linear-interpolation and flat extrapolation factory and traits
static const bool global
static const Size requiredPoints
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
Linear-interpolation and flat extrapolation factory and traits
static const Size requiredPoints
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const