Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
QuadraticInterpolationImpl< I1, I2 > Class Template Reference

#include <qle/math/quadraticinterpolation.hpp>

+ Inheritance diagram for QuadraticInterpolationImpl< I1, I2 >:
+ Collaboration diagram for QuadraticInterpolationImpl< I1, I2 >:

Public Member Functions

 QuadraticInterpolationImpl (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)
 
std::vector< Real > lambdas () const
 
void update () override
 
Real value (Real x) const override
 
Real primitive (Real x) const override
 
Real derivative (Real x) const override
 
Real secondDerivative (Real x) const override
 

Private Attributes

const Size n_
 
Real p_
 
Real x_mul_
 
Real x_offset_
 
Real y_mul_
 
Real y_offset_
 
std::vector< Real > x_
 
std::vector< Real > y_
 
std::vector< Real > lambdas_
 

Detailed Description

template<class I1, class I2>
class QuantExt::detail::QuadraticInterpolationImpl< I1, I2 >

Definition at line 103 of file quadraticinterpolation.hpp.

Constructor & Destructor Documentation

◆ QuadraticInterpolationImpl()

QuadraticInterpolationImpl ( 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 
)

Definition at line 106 of file quadraticinterpolation.hpp.

Member Function Documentation

◆ lambdas()

std::vector< Real > lambdas ( ) const

Definition at line 120 of file quadraticinterpolation.hpp.

120 {
121 return lambdas_;
122 }

◆ update()

void update ( )
override

Definition at line 123 of file quadraticinterpolation.hpp.

123 {
124
125 for(Size i=0; i < n_; ++i) {
126 x_[i] = this->xBegin_[i] * x_mul_ + x_offset_;
127 y_[i] = this->yBegin_[i] * y_mul_ + y_offset_;
128 }
129 y_[n_] = 0;
130
131 // Return if x <= 0 or y = 0
132 // Error will be thrown when calling value or
133 // derivative functions
134 for(Size i=0; i < n_; ++i) {
135 if(x_[i] <= 0 || QuantLib::close_enough(y_[i], 0.0)) {
136 p_ = Null<Real>();
137 return;
138 }
139 }
140
141 Matrix q(n_+1, n_+1, 0.0);
142 for(Size i=0; i<n_; ++i) {
143 q[i][0] = q[n_][i+1] = x_[i];
144
145 for(Size j=0; j<i; ++j) {
146 q[i][j+1] += std::pow(x_[i] - x_[j], 3) / 6.0;
147 }
148 Time t = -std::pow(x_[i], 3) / 6.0;
149 for(Size j=1; j<n_+1; ++j) {
150 q[i][j] += t;
151 }
152 }
153 Matrix q_inv = QuantLib::inverse(q);
154 Array l(y_.begin(), y_.end());
155
156 Array lambdaArray = q_inv * l;
157 lambdas_ = std::vector<Real>(lambdaArray.begin(),
158 lambdaArray.end());
159
160 p_ = 0;
161 for(Size i=1; i < n_+1; ++i) {
162 p_ += lambdaArray[i];
163 }
164 }

◆ value()

Real value ( Real  x) const
override

Definition at line 165 of file quadraticinterpolation.hpp.

165 {
166 QL_REQUIRE(p_ != Null<Real>(), "failed to calibrate lambda");
167 x = x * x_mul_ + x_offset_;
168 Real l = lambdas_[0] * x;
169 Real b = 0;
170 for (Size i=0; i<n_ && x_[i]<x; ++i) {
171 b += lambdas_[i+1] * std::pow(x - x_[i], 3);
172 }
173 l += (b - p_ * std::pow(x, 3)) / 6.0;
174 return (l - y_offset_) / y_mul_;
175 }

◆ primitive()

Real primitive ( Real  x) const
override

Definition at line 176 of file quadraticinterpolation.hpp.

176 {
177 QL_FAIL("QuadraticInterpolation primitive is not implemented");
178 }

◆ derivative()

Real derivative ( Real  x) const
override

Definition at line 179 of file quadraticinterpolation.hpp.

179 {
180 QL_REQUIRE(p_ != 0.0, "failed to calibrate lambda");
181 x = x * x_mul_ + x_offset_;
182 Real l = lambdas_[0];
183 Real b = 0;
184 for (Size i=0; i<n_ && x_[i]<x; ++i) {
185 b += lambdas_[i+1] * std::pow(x - x_[i], 2);
186 }
187 l += (b - p_ * std::pow(x, 2)) / 2.0;
188 return l / y_mul_;
189 }

◆ secondDerivative()

Real secondDerivative ( Real  x) const
override

Definition at line 190 of file quadraticinterpolation.hpp.

190 {
191 QL_REQUIRE(p_ != 0.0, "failed to calibrate lambda");
192 x = x * x_mul_ + x_offset_;
193 Real l = 0;
194 Real b = 0;
195 for (Size i=0; i<n_ && x_[i]<x; ++i) {
196 b += lambdas_[i+1] * (x - x_[i]);
197 }
198 l += (b - p_* x);
199 return l / y_mul_;;
200 }

Member Data Documentation

◆ n_

const Size n_
private

Definition at line 202 of file quadraticinterpolation.hpp.

◆ p_

Real p_
private

Definition at line 203 of file quadraticinterpolation.hpp.

◆ x_mul_

Real x_mul_
private

Definition at line 204 of file quadraticinterpolation.hpp.

◆ x_offset_

Real x_offset_
private

Definition at line 205 of file quadraticinterpolation.hpp.

◆ y_mul_

Real y_mul_
private

Definition at line 206 of file quadraticinterpolation.hpp.

◆ y_offset_

Real y_offset_
private

Definition at line 207 of file quadraticinterpolation.hpp.

◆ x_

std::vector<Real> x_
private

Definition at line 208 of file quadraticinterpolation.hpp.

◆ y_

std::vector<Real> y_
private

Definition at line 209 of file quadraticinterpolation.hpp.

◆ lambdas_

std::vector<Real> lambdas_
private

Definition at line 210 of file quadraticinterpolation.hpp.