QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdmg2op.cpp
Go to the documentation of this file.
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 <http://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
30
31
32namespace QuantLib {
33
35 const ext::shared_ptr<FdmMesher>& mesher,
36 const ext::shared_ptr<G2>& model,
37 Size direction1, Size direction2)
38 : direction1_(direction1),
39 direction2_(direction2),
40 x_(mesher->locations(direction1)),
41 y_(mesher->locations(direction2)),
42 dxMap_(FirstDerivativeOp(direction1, mesher).mult(-x_*model->a()).add(
43 SecondDerivativeOp(direction1, mesher)
44 .mult(0.5*model->sigma()*model->sigma()
45 *Array(mesher->layout()->size(), 1.0)))),
46 dyMap_(FirstDerivativeOp(direction2, mesher).mult(-y_*model->b()).add(
47 SecondDerivativeOp(direction2, mesher)
48 .mult(0.5*model->eta()*model->eta()
49 *Array(mesher->layout()->size(), 1.0)))),
50 corrMap_(SecondOrderMixedDerivativeOp(direction1, direction2, mesher)
51 .mult(Array(mesher->layout()->size(),
52 model->rho()*model->sigma()*model->eta()))),
53 mapX_(direction1, mesher),
54 mapY_(direction2, mesher),
55 model_(model) {
56 }
57
58 Size FdmG2Op::size() const { return 2U; }
59
60 void FdmG2Op::setTime(Time t1, Time t2) {
61
62 const ext::shared_ptr<TwoFactorModel::ShortRateDynamics> dynamics =
63 model_->dynamics();
64
65 const Real phi = 0.5*( dynamics->shortRate(t1, 0.0, 0.0)
66 + dynamics->shortRate(t2, 0.0, 0.0));
67
68 const Array hr = -0.5*(x_ + y_ + phi);
69 mapX_.axpyb(Array(), dxMap_, dxMap_, hr);
70 mapY_.axpyb(Array(), dyMap_, dyMap_, hr);
71 }
72
73 Array FdmG2Op::apply(const Array& r) const {
74 return mapX_.apply(r) + mapY_.apply(r) + apply_mixed(r);
75 }
76
78 return corrMap_.apply(r);
79 }
80
81 Array FdmG2Op::apply_direction(Size direction, const Array& r) const {
82 if (direction == direction1_) {
83 return mapX_.apply(r);
84 }
85 else if (direction == direction2_) {
86 return mapY_.apply(r);
87 }
88 else {
89 return Array(r.size(), 0.0);
90 }
91 }
92
93 Array FdmG2Op::solve_splitting(Size direction, const Array& r, Real a) const {
94 if (direction == direction1_) {
95 return mapX_.solve_splitting(r, a, 1.0);
96 }
97 else if (direction == direction2_) {
98 return mapY_.solve_splitting(r, a, 1.0);
99 }
100 else {
101 return Array(r.size(), 0.0);
102 }
103 }
104
106 return solve_splitting(direction1_, r, dt);
107 }
108
109 std::vector<SparseMatrix> FdmG2Op::toMatrixDecomp() const {
110 return {
111 mapX_.toMatrix(),
112 mapY_.toMatrix(),
114 };
115 }
116
117}
118
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const override
Definition: fdmg2op.cpp:58
const TripleBandLinearOp dxMap_
Definition: fdmg2op.hpp:58
Array apply_direction(Size direction, const Array &r) const override
Definition: fdmg2op.cpp:81
Array preconditioner(const Array &r, Real s) const override
Definition: fdmg2op.cpp:105
std::vector< SparseMatrix > toMatrixDecomp() const override
Definition: fdmg2op.cpp:109
const Array x_
Definition: fdmg2op.hpp:57
const TripleBandLinearOp dyMap_
Definition: fdmg2op.hpp:58
void setTime(Time t1, Time t2) override
Time is required.
Definition: fdmg2op.cpp:60
Array apply_mixed(const Array &r) const override
Definition: fdmg2op.cpp:77
FdmG2Op(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< G2 > &model, Size direction1, Size direction2)
Definition: fdmg2op.cpp:34
Array solve_splitting(Size direction, const Array &r, Real s) const override
Definition: fdmg2op.cpp:93
const Array y_
Definition: fdmg2op.hpp:57
NinePointLinearOp corrMap_
Definition: fdmg2op.hpp:60
const Size direction1_
Definition: fdmg2op.hpp:56
Array apply(const Array &r) const override
Definition: fdmg2op.cpp:73
TripleBandLinearOp mapY_
Definition: fdmg2op.hpp:61
const ext::shared_ptr< G2 > model_
Definition: fdmg2op.hpp:63
const Size direction2_
Definition: fdmg2op.hpp:56
TripleBandLinearOp mapX_
Definition: fdmg2op.hpp:61
SparseMatrix toMatrix() const override
Array apply(const Array &r) const override
SparseMatrix toMatrix() const override
Array solve_splitting(const Array &r, Real a, Real b=1.0) const
void axpyb(const Array &a, const TripleBandLinearOp &x, const TripleBandLinearOp &y, const Array &b)
Array apply(const Array &r) const override
ext::function< Real(Real)> b
FDM G2 operator.
memory layout of a fdm linear operator
mesher for a fdm grid
first derivative linear operator
Two-factor additive Gaussian Model G2++.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Real rho
Real sigma
Definition: any.hpp:35
ext::shared_ptr< YieldTermStructure > r
second derivative operator
second order mixed derivative linear operator
std::uint64_t x_