Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
logquadraticinterpolation.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 Skandinaviska Enskilda Banken AB (publ)
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 ORE is free software: you can redistribute it and/or modify it
8 under the terms of the Modified BSD License. You should have received a
9 copy of the license along with this program.
10 The license is also available online at <http://opensourcerisk.org>
11 This program is distributed on the basis that it will form a useful
12 contribution to risk analytics and model standardisation, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
15 */
16
17 /*! \file logquadraticinterpolation.hpp
18 \brief log-quadratic interpolation between discrete points
19 */
20
21 #ifndef quantext_log_quadratic_interpolation_hpp
22 #define quantext_log_quadratic_interpolation_hpp
23
24 #include <ql/math/interpolations/loginterpolation.hpp>
25 #include <ql/utilities/dataformatters.hpp>
27
28 namespace QuantExt {
29 using namespace QuantLib;
30
31 namespace detail {
32 template<class I1, class I2, class I> class LogInterpolationImpl;
33 }
34
35 //! %log-quadratic interpolation between discrete points
36 /*! \ingroup interpolations
37 \warning See the Interpolation class for information about the
38 required lifetime of the underlying data.
39 */
40 class LogQuadraticInterpolation : public Interpolation {
41 public:
42 /*! \pre the \f$ x \f$ values must be sorted. */
43 template <class I1, class I2>
44 LogQuadraticInterpolation(const I1& xBegin, const I1& xEnd,
45 const I2& yBegin,
46 Real x_mul = 1, Real x_offset = 0,
47 Real y_mul = 1, Real y_offset = 0,
48 Size skip = 0) {
49 impl_ = ext::shared_ptr<Interpolation::Impl>(new
51 xBegin, xEnd, yBegin,
52 Quadratic(x_mul, x_offset, y_mul, y_offset, skip)));
53 impl_->update();
54 }
55 template <class I1, class I2>
56 std::vector<Real> lambdas() const {
58 Impl;
59 ext::shared_ptr<Impl> p =
60 ext::dynamic_pointer_cast<Impl>(impl_);
61 QL_REQUIRE(p, "unable to cast impl to "
62 "LogInterpolationImpl<I1,I2,Quadratic>");
63
64 ext::shared_ptr<QuadraticInterpolation> p2 =
65 ext::dynamic_pointer_cast<QuadraticInterpolation>(
66 p->interpolation());
67 QL_REQUIRE(p2, "unable to cast interpolation to "
68 "QuadraticInterpolation");
69
70 return p2->lambdas<I1,I2>();
71 }
72 };
73
74 //! log-quadratic interpolation factory and traits
75 /*! \ingroup interpolations */
77 public:
78 LogQuadratic(Real x_mul = 1, Real x_offset = 0,
79 Real y_mul = 1, Real y_offset = 0,
80 Size skip = 0)
81 : x_mul_(x_mul), x_offset_(x_offset),
82 y_mul_(y_mul), y_offset_(y_offset),
83 skip_(skip) {}
84 template <class I1, class I2>
85 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
86 const I2& yBegin) const {
88 xBegin, xEnd, yBegin,
90 }
91 static const bool global = false;
92 static const Size requiredPoints = 2;
93 Real x_mul_;
95 Real y_mul_;
97 Size skip_;
98 };
99
100 namespace detail {
101
102 template <class I1, class I2, class Interpolator>
104 : public Interpolation::templateImpl<I1,I2> {
105 public:
106 LogInterpolationImpl(const I1& xBegin, const I1& xEnd,
107 const I2& yBegin,
108 const Interpolator& factory = Interpolator())
109 : Interpolation::templateImpl<I1,I2>(xBegin, xEnd, yBegin,
110 Interpolator::requiredPoints),
111 logY_(xEnd-xBegin) {
112 interpolation_ = factory.interpolatePtr(this->xBegin_,
113 this->xEnd_,
114 logY_.begin());
115 }
116 QuantLib::ext::shared_ptr<Interpolation> interpolation() const {
117 return interpolation_;
118 }
119 void update() override {
120 for (Size i=0; i<logY_.size(); ++i) {
121 QL_REQUIRE(this->yBegin_[i]>0.0,
122 "invalid value (" << this->yBegin_[i]
123 << ") at index " << i);
124 logY_[i] = std::log(this->yBegin_[i]);
125 }
126 interpolation_->update();
127 }
128 Real value(Real x) const override {
129 return std::exp((*interpolation_)(x, true));
130 }
131 Real primitive(Real) const override {
132 QL_FAIL("LogInterpolation primitive not implemented");
133 }
134 Real derivative(Real x) const override {
135 return value(x)*interpolation_->derivative(x, true);
136 }
137 Real secondDerivative(Real x) const override {
138 return value(x)*interpolation_->secondDerivative(x, true) +
139 derivative(x)*interpolation_->derivative(x, true);
140 }
141 private:
142 std::vector<Real> logY_;
143 QuantLib::ext::shared_ptr<Interpolation> interpolation_;
146 };
147
148 }
149
150 }
151
152 #endif
log-quadratic interpolation factory and traits
LogQuadratic(Real x_mul=1, Real x_offset=0, Real y_mul=1, Real y_offset=0, Size skip=0)
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
log-quadratic interpolation between discrete points
LogQuadraticInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Real x_mul=1, Real x_offset=0, Real y_mul=1, Real y_offset=0, Size skip=0)
Quadratic-interpolation factory and traits
LogInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const Interpolator &factory=Interpolator())
QuantLib::ext::shared_ptr< Interpolation > interpolation() const
QuantLib::ext::shared_ptr< Interpolation > interpolation_
quadratic interpolation between discrete points