/src/quantlib/ql/methods/finitedifferences/operators/fdmg2op.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2011 Klaus Spanderen |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | /*! \file fdmg2op.cpp */ |
21 | | |
22 | | |
23 | | #include <ql/models/shortrate/twofactormodels/g2.hpp> |
24 | | #include <ql/methods/finitedifferences/meshers/fdmmesher.hpp> |
25 | | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
26 | | #include <ql/methods/finitedifferences/operators/fdmg2op.hpp> |
27 | | #include <ql/methods/finitedifferences/operators/firstderivativeop.hpp> |
28 | | #include <ql/methods/finitedifferences/operators/secondderivativeop.hpp> |
29 | | #include <ql/methods/finitedifferences/operators/secondordermixedderivativeop.hpp> |
30 | | |
31 | | |
32 | | namespace QuantLib { |
33 | | |
34 | | FdmG2Op::FdmG2Op( |
35 | | const ext::shared_ptr<FdmMesher>& mesher, |
36 | | const ext::shared_ptr<G2>& model, |
37 | | Size direction1, Size direction2) |
38 | 0 | : direction1_(direction1), |
39 | 0 | direction2_(direction2), |
40 | 0 | x_(mesher->locations(direction1)), |
41 | 0 | y_(mesher->locations(direction2)), |
42 | 0 | dxMap_(FirstDerivativeOp(direction1, mesher).mult(-x_*model->a()).add( |
43 | 0 | SecondDerivativeOp(direction1, mesher) |
44 | 0 | .mult(0.5*model->sigma()*model->sigma() |
45 | 0 | *Array(mesher->layout()->size(), 1.0)))), |
46 | 0 | dyMap_(FirstDerivativeOp(direction2, mesher).mult(-y_*model->b()).add( |
47 | 0 | SecondDerivativeOp(direction2, mesher) |
48 | 0 | .mult(0.5*model->eta()*model->eta() |
49 | 0 | *Array(mesher->layout()->size(), 1.0)))), |
50 | 0 | corrMap_(SecondOrderMixedDerivativeOp(direction1, direction2, mesher) |
51 | 0 | .mult(Array(mesher->layout()->size(), |
52 | 0 | model->rho()*model->sigma()*model->eta()))), |
53 | 0 | mapX_(direction1, mesher), |
54 | 0 | mapY_(direction2, mesher), |
55 | 0 | model_(model) { |
56 | 0 | } |
57 | | |
58 | 0 | Size FdmG2Op::size() const { return 2U; } |
59 | | |
60 | 0 | void FdmG2Op::setTime(Time t1, Time t2) { |
61 | |
|
62 | 0 | const ext::shared_ptr<TwoFactorModel::ShortRateDynamics> dynamics = |
63 | 0 | model_->dynamics(); |
64 | |
|
65 | 0 | const Real phi = 0.5*( dynamics->shortRate(t1, 0.0, 0.0) |
66 | 0 | + dynamics->shortRate(t2, 0.0, 0.0)); |
67 | |
|
68 | 0 | const Array hr = -0.5*(x_ + y_ + phi); |
69 | 0 | mapX_.axpyb(Array(), dxMap_, dxMap_, hr); |
70 | 0 | mapY_.axpyb(Array(), dyMap_, dyMap_, hr); |
71 | 0 | } |
72 | | |
73 | 0 | Array FdmG2Op::apply(const Array& r) const { |
74 | 0 | return mapX_.apply(r) + mapY_.apply(r) + apply_mixed(r); |
75 | 0 | } |
76 | | |
77 | 0 | Array FdmG2Op::apply_mixed(const Array& r) const { |
78 | 0 | return corrMap_.apply(r); |
79 | 0 | } |
80 | | |
81 | 0 | Array FdmG2Op::apply_direction(Size direction, const Array& r) const { |
82 | 0 | if (direction == direction1_) { |
83 | 0 | return mapX_.apply(r); |
84 | 0 | } |
85 | 0 | else if (direction == direction2_) { |
86 | 0 | return mapY_.apply(r); |
87 | 0 | } |
88 | 0 | else { |
89 | 0 | return Array(r.size(), 0.0); |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | 0 | Array FdmG2Op::solve_splitting(Size direction, const Array& r, Real a) const { |
94 | 0 | if (direction == direction1_) { |
95 | 0 | return mapX_.solve_splitting(r, a, 1.0); |
96 | 0 | } |
97 | 0 | else if (direction == direction2_) { |
98 | 0 | return mapY_.solve_splitting(r, a, 1.0); |
99 | 0 | } |
100 | 0 | else { |
101 | 0 | return Array(r.size(), 0.0); |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | 0 | Array FdmG2Op::preconditioner(const Array& r, Real dt) const { |
106 | 0 | return solve_splitting(direction1_, r, dt); |
107 | 0 | } |
108 | | |
109 | 0 | std::vector<SparseMatrix> FdmG2Op::toMatrixDecomp() const { |
110 | 0 | return { |
111 | 0 | mapX_.toMatrix(), |
112 | 0 | mapY_.toMatrix(), |
113 | 0 | corrMap_.toMatrix() |
114 | 0 | }; |
115 | 0 | } |
116 | | |
117 | | } |
118 | | |