Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
LgmConvolutionSolver Class Reference

Numerical convolution solver for the LGM model. More...

#include <qle/pricingengines/lgmconvolutionsolver.hpp>

+ Inheritance diagram for LgmConvolutionSolver:
+ Collaboration diagram for LgmConvolutionSolver:

Public Member Functions

 LgmConvolutionSolver (const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > &model, const Real sy, const Size ny, const Real sx, const Size nx)
 Numerical convolution solver for the LGM model. More...
 
Size gridSize () const
 
std::vector< Real > stateGrid (const Real t) const
 
template<typename ValueType = Real>
std::vector< ValueType > rollback (const std::vector< ValueType > &v, const Real t1, const Real t0, const ValueType zero=ValueType(0.0)) const
 
const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > & model () const
 

Private Attributes

QuantLib::ext::shared_ptr< LinearGaussMarkovModelmodel_
 
int mx_
 
int my_
 
int nx_
 
Real h_
 
std::vector< Real > y_
 
std::vector< Real > w_
 

Detailed Description

Numerical convolution solver for the LGM model.

Reference: Hagan, Methodology for callable swaps and Bermudan exercise into swaptions

Definition at line 36 of file lgmconvolutionsolver.hpp.

Constructor & Destructor Documentation

◆ LgmConvolutionSolver()

LgmConvolutionSolver ( const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > &  model,
const Real  sy,
const Size  ny,
const Real  sx,
const Size  nx 
)

Numerical convolution solver for the LGM model.

Reference: Hagan, Methodology for callable swaps and Bermudan exercise into swaptions

Definition at line 30 of file lgmconvolutionsolver.cpp.

32 : model_(model), nx_(nx) {
33
34 // precompute weights
35
36 // number of x, y grid points > 0
37 mx_ = static_cast<Size>(floor(sx * static_cast<Real>(nx)) + 0.5);
38 my_ = static_cast<Size>(floor(sy * static_cast<Real>(ny)) + 0.5);
39
40 // y-grid spacing
41 h_ = 1.0 / static_cast<Real>(ny);
42
43 // weights for convolution in the rollback step
44 CumulativeNormalDistribution N;
45 NormalDistribution G;
46
47 y_.resize(2 * my_ + 1); // x-coordinate / standard deviation of x
48 w_.resize(2 * my_ + 1); // probability weight around y-grid point i
49 for (int i = 0; i <= 2 * my_; i++) {
50 y_[i] = h_ * (i - my_);
51 if (i == 0 || i == 2 * my_)
52 w_[i] = (1. + y_[0] / h_) * N(y_[0] + h_) - y_[0] / h_ * N(y_[0]) + (G(y_[0] + h_) - G(y_[0])) / h_;
53 else
54 w_[i] = (1. + y_[i] / h_) * N(y_[i] + h_) - 2. * y_[i] / h_ * N(y_[i]) -
55 (1. - y_[i] / h_) * N(y_[i] - h_) // opposite sign in the paper
56 + (G(y_[i] + h_) - 2. * G(y_[i]) + G(y_[i] - h_)) / h_;
57 // w_[i] might be negative due to numerical errors
58 if (w_[i] < 0.0) {
59 QL_REQUIRE(w_[i] > -1.0E-10, "LgmConvolutionSolver: negative w (" << w_[i] << ") at i=" << i);
60 w_[i] = 0.0;
61 }
62 }
63}
const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > & model() const
QuantLib::ext::shared_ptr< LinearGaussMarkovModel > model_

Member Function Documentation

◆ gridSize()

Size gridSize ( ) const

Definition at line 42 of file lgmconvolutionsolver.hpp.

42{ return 2 * mx_ + 1; }
+ Here is the caller graph for this function:

◆ stateGrid()

std::vector< Real > stateGrid ( const Real  t) const

Definition at line 65 of file lgmconvolutionsolver.cpp.

65 {
66 if (close_enough(t, 0.0))
67 return std::vector<Real>(2 * mx_ + 1, 0.0);
68 std::vector<Real> x(2 * mx_ + 1);
69 Real dx = std::sqrt(model_->parametrization()->zeta(t)) / static_cast<Real>(nx_);
70 for (int k = 0; k <= 2 * mx_; ++k) {
71 x[k] = dx * (k - mx_);
72 }
73 return x;
74}
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rollback()

std::vector< ValueType > rollback ( const std::vector< ValueType > &  v,
const Real  t1,
const Real  t0,
const ValueType  zero = ValueType(0.0) 
) const

Definition at line 65 of file lgmconvolutionsolver.hpp.

66 {
67 if (QuantLib::close_enough(t0, t1))
68 return v;
69 QL_REQUIRE(t0 < t1, "LgmConvolutionSolver::rollback(): t0 (" << t0 << ") < t1 (" << t1 << ") required.");
70 Real sigma = std::sqrt(model_->parametrization()->zeta(t1));
71 Real dx = sigma / static_cast<Real>(nx_);
72 if (QuantLib::close_enough(t0, 0.0)) {
73 // rollback from t1 to t0 = 0
74 ValueType value(zero);
75 for (int i = 0; i <= 2 * my_; i++) {
76 // Map y index to x index, not integer in general
77 Real kp = y_[i] * sigma / dx + mx_;
78 // Adjacent integer x index <= k
79 int kk = int(floor(kp));
80 // Get value at kp by linear interpolation on
81 // kk <= kp <= kk + 1 with flat extrapolation
82 value +=
83 w_[i] *
84 (kk < 0 ? v[0] : (kk + 1 > 2 * mx_ ? v[2 * mx_] : (kp - kk) * v[kk + 1] + (1.0 + kk - kp) * v[kk]));
85 }
86 return std::vector<ValueType>(2 * mx_ + 1, value);
87 } else {
88 std::vector<ValueType> value(2 * mx_ + 1, zero);
89 // rollback from t1 to t0 > 0
90 Real std = std::sqrt(model_->parametrization()->zeta(t1) - model_->parametrization()->zeta(t0));
91 Real dx2 = std::sqrt(model_->parametrization()->zeta(t0)) / static_cast<Real>(nx_);
92 for (int k = 0; k <= 2 * mx_; k++) {
93 for (int i = 0; i <= 2 * my_; i++) {
94 // Map y index to x index, not integer in generalTo
95 Real kp = (dx2 * (k - mx_) + y_[i] * std) / dx + mx_;
96 // Adjacent integer x index <= k
97 int kk = int(floor(kp));
98 // Get value at kp by linear interpolation on
99 // kk <= kp <= kk + 1 with flat extrapolation
100 value[k] +=
101 w_[i] *
102 (kk < 0 ? v[0] : (kk + 1 > 2 * mx_ ? v[2 * mx_] : (kp - kk) * v[kk + 1] + (1.0 + kk - kp) * v[kk]));
103 }
104 }
105 return value;
106 }
107}
+ Here is the caller graph for this function:

◆ model()

const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > & model ( ) const

Definition at line 53 of file lgmconvolutionsolver.hpp.

53{ return model_; }
+ Here is the caller graph for this function:

Member Data Documentation

◆ model_

QuantLib::ext::shared_ptr<LinearGaussMarkovModel> model_
private

Definition at line 56 of file lgmconvolutionsolver.hpp.

◆ mx_

int mx_
private

Definition at line 57 of file lgmconvolutionsolver.hpp.

◆ my_

int my_
private

Definition at line 57 of file lgmconvolutionsolver.hpp.

◆ nx_

int nx_
private

Definition at line 57 of file lgmconvolutionsolver.hpp.

◆ h_

Real h_
private

Definition at line 58 of file lgmconvolutionsolver.hpp.

◆ y_

std::vector<Real> y_
private

Definition at line 59 of file lgmconvolutionsolver.hpp.

◆ w_

std::vector<Real> w_
private

Definition at line 59 of file lgmconvolutionsolver.hpp.