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

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

#include <qle/models/lgmconvolutionsolver2.hpp>

+ Inheritance diagram for LgmConvolutionSolver2:
+ Collaboration diagram for LgmConvolutionSolver2:

Public Member Functions

 LgmConvolutionSolver2 (const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > &model, const Real sy, const Size ny, const Real sx, const Size nx)
 
Size gridSize () const override
 
RandomVariable stateGrid (const Real t) const override
 
RandomVariable rollback (const RandomVariable &v, const Real t1, const Real t0, Size steps=Null< Size >()) const override
 
const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > & model () const override
 
- Public Member Functions inherited from LgmBackwardSolver
virtual ~LgmBackwardSolver ()
 
virtual Size gridSize () const =0
 
virtual RandomVariable stateGrid (const Real t) const =0
 
virtual RandomVariable rollback (const RandomVariable &v, const Real t1, const Real t0, Size steps=Null< Size >()) const =0
 
virtual const QuantLib::ext::shared_ptr< LinearGaussMarkovModel > & model () const =0
 

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 37 of file lgmconvolutionsolver2.hpp.

Constructor & Destructor Documentation

◆ LgmConvolutionSolver2()

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

Definition at line 25 of file lgmconvolutionsolver2.cpp.

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

Member Function Documentation

◆ gridSize()

Size gridSize ( ) const
overridevirtual

Implements LgmBackwardSolver.

Definition at line 41 of file lgmconvolutionsolver2.hpp.

41{ return 2 * mx_ + 1; }

◆ stateGrid()

RandomVariable stateGrid ( const Real  t) const
overridevirtual

Implements LgmBackwardSolver.

Definition at line 60 of file lgmconvolutionsolver2.cpp.

60 {
61 if (QuantLib::close_enough(t, 0.0))
62 return RandomVariable(2 * mx_ + 1, 0.0);
63 RandomVariable x(2 * mx_ + 1);
64 Real dx = std::sqrt(model_->parametrization()->zeta(t)) / static_cast<Real>(nx_);
65 for (int k = 0; k <= 2 * mx_; ++k) {
66 x.set(k, dx * (k - mx_));
67 }
68 return x;
69}
+ Here is the call graph for this function:

◆ rollback()

RandomVariable rollback ( const RandomVariable v,
const Real  t1,
const Real  t0,
Size  steps = Null<Size>() 
) const
overridevirtual

Implements LgmBackwardSolver.

Definition at line 71 of file lgmconvolutionsolver2.cpp.

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

◆ model()

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

Implements LgmBackwardSolver.

Definition at line 46 of file lgmconvolutionsolver2.hpp.

46{ return model_; }

Member Data Documentation

◆ model_

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

Definition at line 49 of file lgmconvolutionsolver2.hpp.

◆ mx_

int mx_
private

Definition at line 50 of file lgmconvolutionsolver2.hpp.

◆ my_

int my_
private

Definition at line 50 of file lgmconvolutionsolver2.hpp.

◆ nx_

int nx_
private

Definition at line 50 of file lgmconvolutionsolver2.hpp.

◆ h_

Real h_
private

Definition at line 51 of file lgmconvolutionsolver2.hpp.

◆ y_

std::vector<Real> y_
private

Definition at line 52 of file lgmconvolutionsolver2.hpp.

◆ w_

std::vector<Real> w_
private

Definition at line 52 of file lgmconvolutionsolver2.hpp.