| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2015 Peter Caspers |
| 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 gsrprocesscore.hpp |
| 21 | \brief Core computations for the gsr process in risk neutral |
| 22 | and T-forward measure. |
| 23 | \warning Results are cached for performance reasons, so if |
| 24 | parameters change, you need to call flushCache() to |
| 25 | avoid inconsistent results. |
| 26 | */ |
| 27 | |
| 28 | #ifndef quantlib_gsr_process_core_hpp |
| 29 | #define quantlib_gsr_process_core_hpp |
| 30 | |
| 31 | #include <ql/math/array.hpp> |
| 32 | #include <ql/math/comparison.hpp> |
| 33 | #include <map> |
| 34 | |
| 35 | namespace QuantLib { |
| 36 | |
| 37 | namespace detail { |
| 38 | |
| 39 | class GsrProcessCore { |
| 40 | public: |
| 41 | GsrProcessCore(const Array& times, const Array& vols, const Array& reversions, Real T = 60.0); |
| 42 | |
| 43 | // conditional expectation, x0 dependent part |
| 44 | Real expectation_x0dep_part(Time w, Real xw, Time dt) const; |
| 45 | |
| 46 | // conditional expectation, x0 independent part |
| 47 | // in the risk neutral measure |
| 48 | Real expectation_rn_part(Time w, Time dt) const; |
| 49 | |
| 50 | // conditional expectation, drift adjustment for |
| 51 | // the T-forward measure |
| 52 | Real expectation_tf_part(Time w, Time dt) const; |
| 53 | |
| 54 | // conditional variance |
| 55 | Real variance(Time w, Time dt) const; |
| 56 | |
| 57 | // y(t) |
| 58 | Real y(Time t) const; |
| 59 | |
| 60 | // G(t,w) |
| 61 | Real G(Time t, Time w) const; |
| 62 | |
| 63 | // sigma |
| 64 | Real sigma(Time t) const; |
| 65 | |
| 66 | // reversion |
| 67 | Real reversion(Time t) const; |
| 68 | |
| 69 | // reset cache |
| 70 | void flushCache() const; |
| 71 | |
| 72 | protected: |
| 73 | const Array ×_, &vols_, &reversions_; |
| 74 | |
| 75 | private: |
| 76 | int lowerIndex(Time t) const; |
| 77 | int upperIndex(Time t) const; |
| 78 | Real time2(Size index) const; |
| 79 | Real cappedTime(Size index, Real cap = Null<Real>()) const; |
| 80 | Real flooredTime(Size index, Real floor = Null<Real>()) const; |
| 81 | Real vol(Size index) const; |
| 82 | Real rev(Size index) const; |
| 83 | bool revZero(Size index) const; |
| 84 | |
| 85 | mutable std::map<std::pair<Real, Real>, Real> cache1_, cache2a_, cache2b_, |
| 86 | cache3_, cache5_; |
| 87 | mutable std::map<Real, Real> cache4_; |
| 88 | Time T_; |
| 89 | mutable std::vector<bool> revZero_; |
| 90 | }; // GsrProcessCore |
| 91 | |
| 92 | // inline definitions |
| 93 | |
| 94 | inline Real GsrProcessCore::sigma(const Time t) const { |
| 95 | return vol(index: lowerIndex(t)); |
| 96 | } |
| 97 | |
| 98 | inline Real GsrProcessCore::reversion(const Time t) const { |
| 99 | return rev(index: lowerIndex(t)); |
| 100 | } |
| 101 | |
| 102 | } // namespace detail |
| 103 | |
| 104 | } // namespace QuantLib |
| 105 | |
| 106 | #endif |
| 107 | |