| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2013, 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 gsr.hpp |
| 21 | \brief GSR 1 factor model |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_gsr_hpp |
| 25 | #define quantlib_gsr_hpp |
| 26 | |
| 27 | #include <ql/models/shortrate/onefactormodels/gaussian1dmodel.hpp> |
| 28 | #include <ql/processes/gsrprocess.hpp> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | //! One factor gsr model, formulation is in forward measure |
| 33 | |
| 34 | class Gsr : public Gaussian1dModel, public CalibratedModel { |
| 35 | |
| 36 | public: |
| 37 | // constant mean reversion |
| 38 | Gsr(const Handle<YieldTermStructure>& termStructure, |
| 39 | std::vector<Date> volstepdates, |
| 40 | const std::vector<Real>& volatilities, |
| 41 | Real reversion, |
| 42 | Real T = 60.0); |
| 43 | // piecewise mean reversion (with same step dates as volatilities) |
| 44 | Gsr(const Handle<YieldTermStructure>& termStructure, |
| 45 | std::vector<Date> volstepdates, |
| 46 | const std::vector<Real>& volatilities, |
| 47 | const std::vector<Real>& reversions, |
| 48 | Real T = 60.0); |
| 49 | // constant mean reversion with floating model data |
| 50 | Gsr(const Handle<YieldTermStructure>& termStructure, |
| 51 | std::vector<Date> volstepdates, |
| 52 | std::vector<Handle<Quote> > volatilities, |
| 53 | const Handle<Quote>& reversion, |
| 54 | Real T = 60.0); |
| 55 | // piecewise mean reversion with floating model data |
| 56 | Gsr(const Handle<YieldTermStructure>& termStructure, |
| 57 | std::vector<Date> volstepdates, |
| 58 | std::vector<Handle<Quote> > volatilities, |
| 59 | std::vector<Handle<Quote> > reversions, |
| 60 | Real T = 60.0); |
| 61 | |
| 62 | Real numeraireTime() const; |
| 63 | void numeraireTime(Real T); |
| 64 | |
| 65 | const Array &reversion() const { return reversion_.params(); } |
| 66 | const Array &volatility() const { return sigma_.params(); } |
| 67 | |
| 68 | // calibration constraints |
| 69 | |
| 70 | // fixed reversions, only volatilities are free |
| 71 | std::vector<bool> FixedReversions() { |
| 72 | std::vector<bool> res(reversions_.size(), true); |
| 73 | std::vector<bool> vol(volatilities_.size(), false); |
| 74 | res.insert(position: res.end(), first: vol.begin(), last: vol.end()); |
| 75 | return res; |
| 76 | } |
| 77 | |
| 78 | // fixed volatilities, only reversions are free |
| 79 | std::vector<bool> FixedVolatilities() { |
| 80 | std::vector<bool> res(reversions_.size(), false); |
| 81 | std::vector<bool> vol(volatilities_.size(), true); |
| 82 | res.insert(position: res.end(), first: vol.begin(), last: vol.end()); |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | std::vector<bool> MoveVolatility(Size i) { |
| 87 | QL_REQUIRE(i < volatilities_.size(), |
| 88 | "volatility with index " << i << " does not exist (0..." |
| 89 | << volatilities_.size() - 1 << ")" ); |
| 90 | std::vector<bool> res(reversions_.size() + volatilities_.size(), true); |
| 91 | res[reversions_.size() + i] = false; |
| 92 | return res; |
| 93 | } |
| 94 | |
| 95 | std::vector<bool> MoveReversion(Size i) { |
| 96 | QL_REQUIRE(i < reversions_.size(), |
| 97 | "reversion with index " << i << " does not exist (0..." |
| 98 | << reversions_.size() - 1 << ")" ); |
| 99 | std::vector<bool> res(reversions_.size() + volatilities_.size(), true); |
| 100 | res[i] = false; |
| 101 | return res; |
| 102 | } |
| 103 | |
| 104 | // With fixed reversion calibrate the volatilities one by one |
| 105 | // to the given helpers. It is assumed that that volatility step |
| 106 | // dates are suitable for this, i.e. they should be identical to |
| 107 | // the fixing dates of the helpers (except for the last one where |
| 108 | // we do not need a step). Also note that the endcritera reflect |
| 109 | // only the status of the last calibration when using this method. |
| 110 | void calibrateVolatilitiesIterative( |
| 111 | const std::vector<ext::shared_ptr<BlackCalibrationHelper> > &helpers, |
| 112 | OptimizationMethod &method, const EndCriteria &endCriteria, |
| 113 | const Constraint &constraint = Constraint(), |
| 114 | const std::vector<Real> &weights = std::vector<Real>()) { |
| 115 | |
| 116 | for (Size i = 0; i < helpers.size(); i++) { |
| 117 | std::vector<ext::shared_ptr<CalibrationHelper> > h(1, helpers[i]); |
| 118 | calibrate(h, method, endCriteria, constraint, weights, |
| 119 | fixParameters: MoveVolatility(i)); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // With fixed volatility calibrate the reversions one by one |
| 124 | // to the given helpers. In this case the step dates must be chosen |
| 125 | // according to the maturities of the calibration instruments. |
| 126 | void calibrateReversionsIterative( |
| 127 | const std::vector<ext::shared_ptr<BlackCalibrationHelper> > &helpers, |
| 128 | OptimizationMethod &method, const EndCriteria &endCriteria, |
| 129 | const Constraint &constraint = Constraint(), |
| 130 | const std::vector<Real> &weights = std::vector<Real>()) { |
| 131 | |
| 132 | for (Size i = 0; i < helpers.size(); i++) { |
| 133 | std::vector<ext::shared_ptr<CalibrationHelper> > h(1, helpers[i]); |
| 134 | calibrate(h, method, endCriteria, constraint, weights, |
| 135 | fixParameters: MoveReversion(i)); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | protected: |
| 140 | Real numeraireImpl(Time t, Real y, const Handle<YieldTermStructure>& yts) const override; |
| 141 | |
| 142 | Real zerobondImpl(Time T, Time t, Real y, const Handle<YieldTermStructure>& yts) const override; |
| 143 | |
| 144 | void generateArguments() override { |
| 145 | ext::static_pointer_cast<GsrProcess>(r: stateProcess_)->flushCache(); |
| 146 | notifyObservers(); |
| 147 | } |
| 148 | |
| 149 | void update() override; |
| 150 | |
| 151 | void performCalculations() const override { |
| 152 | Gaussian1dModel::performCalculations(); |
| 153 | updateTimes(); |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | void updateTimes() const; |
| 158 | void updateVolatility(); |
| 159 | void updateReversion(); |
| 160 | |
| 161 | void initialize(Real); |
| 162 | |
| 163 | Parameter &reversion_, &sigma_; |
| 164 | |
| 165 | std::vector<Handle<Quote> > volatilities_; |
| 166 | std::vector<Handle<Quote> > reversions_; |
| 167 | std::vector<Date> volstepdates_; // this is shared between vols, |
| 168 | // adjusters and reverisons in |
| 169 | // case of piecewise reversions |
| 170 | mutable std::vector<Time> volsteptimes_; |
| 171 | mutable Array volsteptimesArray_; // FIXME this is redundant (just a copy of |
| 172 | // volsteptimes_) |
| 173 | |
| 174 | struct VolatilityObserver : public Observer { |
| 175 | explicit VolatilityObserver(Gsr *p) : p_(p) {} |
| 176 | void update() override { p_->updateVolatility(); } |
| 177 | Gsr *p_; |
| 178 | }; |
| 179 | struct ReversionObserver : public Observer { |
| 180 | explicit ReversionObserver(Gsr *p) : p_(p) {} |
| 181 | void update() override { p_->updateReversion(); } |
| 182 | Gsr *p_; |
| 183 | }; |
| 184 | |
| 185 | ext::shared_ptr<VolatilityObserver> volatilityObserver_; |
| 186 | ext::shared_ptr<ReversionObserver> reversionObserver_; |
| 187 | }; |
| 188 | |
| 189 | inline Real Gsr::numeraireTime() const { |
| 190 | return ext::dynamic_pointer_cast<GsrProcess>(r: stateProcess_) |
| 191 | ->getForwardMeasureTime(); |
| 192 | } |
| 193 | |
| 194 | inline void Gsr::numeraireTime(const Real T) { |
| 195 | ext::dynamic_pointer_cast<GsrProcess>(r: stateProcess_) |
| 196 | ->setForwardMeasureTime(T); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | #endif |
| 201 | |