Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
lgmconvolutionsolver.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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 lgmconvolutionsolver.hpp
20 \brief numeric convolution solver for the LGM model
21
22 \ingroup engines
23*/
24
25#pragma once
26
27#include <qle/models/lgm.hpp>
28
29namespace QuantExt {
30
31//! Numerical convolution solver for the LGM model
32/*! Reference: Hagan, Methodology for callable swaps and Bermudan
33 exercise into swaptions
34*/
35
37public:
38 LgmConvolutionSolver(const QuantLib::ext::shared_ptr<LinearGaussMarkovModel>& model, const Real sy, const Size ny,
39 const Real sx, const Size nx);
40
41 /* get grid size */
42 Size gridSize() const { return 2 * mx_ + 1; }
43
44 /* get discretised states grid at time t */
45 std::vector<Real> stateGrid(const Real t) const;
46
47 /* roll back an deflated NPV array from t1 to t0 */
48 template <typename ValueType = Real>
49 std::vector<ValueType> rollback(const std::vector<ValueType>& v, const Real t1, const Real t0,
50 const ValueType zero = ValueType(0.0)) const;
51
52 /* the underlying model */
53 const QuantLib::ext::shared_ptr<LinearGaussMarkovModel>& model() const { return model_; }
54
55private:
56 QuantLib::ext::shared_ptr<LinearGaussMarkovModel> model_;
57 int mx_, my_, nx_;
58 Real h_;
59 std::vector<Real> y_, w_;
60};
61
62// rollback implementation
63
64template <typename ValueType>
65std::vector<ValueType> LgmConvolutionSolver::rollback(const std::vector<ValueType>& v, const Real t1, const Real t0,
66 const ValueType zero) const {
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}
108
109} // namespace QuantExt
Numerical convolution solver for the LGM model.
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
std::vector< Real > stateGrid(const Real t) const
QuantLib::ext::shared_ptr< LinearGaussMarkovModel > model_
lgm model class
JY INF index sigma component.