/src/quantlib/ql/methods/finitedifferences/operators/fdmhullwhiteop.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 fdmhullwhiteop.cpp */ |
21 | | |
22 | | |
23 | | #include <ql/models/shortrate/onefactormodels/hullwhite.hpp> |
24 | | #include <ql/methods/finitedifferences/meshers/fdmmesher.hpp> |
25 | | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
26 | | #include <ql/methods/finitedifferences/operators/fdmhullwhiteop.hpp> |
27 | | #include <ql/methods/finitedifferences/operators/firstderivativeop.hpp> |
28 | | #include <ql/methods/finitedifferences/operators/secondderivativeop.hpp> |
29 | | |
30 | | namespace QuantLib { |
31 | | |
32 | | FdmHullWhiteOp::FdmHullWhiteOp( |
33 | | const ext::shared_ptr<FdmMesher>& mesher, |
34 | | const ext::shared_ptr<HullWhite>& model, |
35 | | Size direction) |
36 | 0 | : direction_(direction), |
37 | 0 | x_(mesher->locations(direction)), |
38 | 0 | dzMap_(FirstDerivativeOp(direction, mesher).mult(-x_*model->a()).add( |
39 | 0 | SecondDerivativeOp(direction, mesher) |
40 | 0 | .mult(0.5*model->sigma()*model->sigma() |
41 | 0 | *Array(mesher->layout()->size(), 1.0)))), |
42 | 0 | mapT_(direction, mesher), |
43 | 0 | model_(model) { |
44 | 0 | } |
45 | | |
46 | 0 | Size FdmHullWhiteOp::size() const { return 1U; } |
47 | | |
48 | 0 | void FdmHullWhiteOp::setTime(Time t1, Time t2) { |
49 | |
|
50 | 0 | const ext::shared_ptr<OneFactorModel::ShortRateDynamics> dynamics = |
51 | 0 | model_->dynamics(); |
52 | |
|
53 | 0 | const Real phi = 0.5*( dynamics->shortRate(t1, 0.0) |
54 | 0 | + dynamics->shortRate(t2, 0.0)); |
55 | |
|
56 | 0 | mapT_.axpyb(Array(), dzMap_, dzMap_, -(x_+phi)); |
57 | 0 | } |
58 | | |
59 | 0 | Array FdmHullWhiteOp::apply(const Array& r) const { |
60 | 0 | return mapT_.apply(r); |
61 | 0 | } |
62 | | |
63 | 0 | Array FdmHullWhiteOp::apply_mixed(const Array& r) const { |
64 | 0 | return Array(r.size(), 0.0); |
65 | 0 | } |
66 | | |
67 | 0 | Array FdmHullWhiteOp::apply_direction(Size direction, const Array& r) const { |
68 | 0 | if (direction == direction_) |
69 | 0 | return mapT_.apply(r); |
70 | 0 | else { |
71 | 0 | return Array(r.size(), 0.0); |
72 | 0 | } |
73 | 0 | } |
74 | | |
75 | 0 | Array FdmHullWhiteOp::solve_splitting(Size direction, const Array& r, Real a) const { |
76 | 0 | if (direction == direction_) { |
77 | 0 | return mapT_.solve_splitting(r, a, 1.0); |
78 | 0 | } |
79 | 0 | else { |
80 | 0 | return Array(r.size(), 0.0); |
81 | 0 | } |
82 | 0 | } |
83 | | |
84 | 0 | Array FdmHullWhiteOp::preconditioner(const Array& r, Real dt) const { |
85 | 0 | return solve_splitting(direction_, r, dt); |
86 | 0 | } |
87 | | |
88 | 0 | std::vector<SparseMatrix> FdmHullWhiteOp::toMatrixDecomp() const { |
89 | 0 | return std::vector<SparseMatrix>(1, mapT_.toMatrix()); |
90 | 0 | } |
91 | | |
92 | | } |
93 | | |