| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2003, 2004 Ferdinando Ametrano |
| 5 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | /*! \file inversecumulativersg.hpp |
| 22 | \brief Inverse cumulative random sequence generator |
| 23 | */ |
| 24 | |
| 25 | #ifndef quantlib_inversecumulative_rsg_h |
| 26 | #define quantlib_inversecumulative_rsg_h |
| 27 | |
| 28 | #include <ql/methods/montecarlo/sample.hpp> |
| 29 | #include <utility> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace QuantLib { |
| 33 | |
| 34 | //! Inverse cumulative random sequence generator |
| 35 | /*! It uses a sequence of uniform deviate in (0, 1) as the |
| 36 | source of cumulative distribution values. |
| 37 | Then an inverse cumulative distribution is used to calculate |
| 38 | the distribution deviate. |
| 39 | |
| 40 | The uniform deviate sequence is supplied by USG. |
| 41 | |
| 42 | Class USG must implement the following interface: |
| 43 | \code |
| 44 | USG::sample_type USG::nextSequence() const; |
| 45 | Size USG::dimension() const; |
| 46 | \endcode |
| 47 | |
| 48 | The inverse cumulative distribution is supplied by IC. |
| 49 | |
| 50 | Class IC must implement the following interface: |
| 51 | \code |
| 52 | IC::IC(); |
| 53 | Real IC::operator() const; |
| 54 | \endcode |
| 55 | */ |
| 56 | template <class USG, class IC> |
| 57 | class InverseCumulativeRsg { |
| 58 | public: |
| 59 | typedef Sample<std::vector<Real> > sample_type; |
| 60 | explicit InverseCumulativeRsg(USG uniformSequenceGenerator); |
| 61 | InverseCumulativeRsg(USG uniformSequenceGenerator, const IC& inverseCumulative); |
| 62 | //! returns next sample from the inverse cumulative distribution |
| 63 | const sample_type& nextSequence() const; |
| 64 | const sample_type& lastSequence() const { return x_; } |
| 65 | Size dimension() const { return dimension_; } |
| 66 | private: |
| 67 | USG uniformSequenceGenerator_; |
| 68 | Size dimension_; |
| 69 | mutable sample_type x_; |
| 70 | IC ICD_; |
| 71 | }; |
| 72 | |
| 73 | template <class USG, class IC> |
| 74 | InverseCumulativeRsg<USG, IC>::InverseCumulativeRsg(USG usg) |
| 75 | : uniformSequenceGenerator_(std::move(usg)), dimension_(uniformSequenceGenerator_.dimension()), |
| 76 | x_(std::vector<Real>(dimension_), 1.0) {} |
| 77 | |
| 78 | template <class USG, class IC> |
| 79 | InverseCumulativeRsg<USG, IC>::InverseCumulativeRsg(USG usg, const IC& inverseCum) |
| 80 | : uniformSequenceGenerator_(std::move(usg)), dimension_(uniformSequenceGenerator_.dimension()), |
| 81 | x_(std::vector<Real>(dimension_), 1.0), ICD_(inverseCum) {} |
| 82 | |
| 83 | template <class USG, class IC> |
| 84 | inline const typename InverseCumulativeRsg<USG, IC>::sample_type& |
| 85 | InverseCumulativeRsg<USG, IC>::nextSequence() const { |
| 86 | typename USG::sample_type sample = |
| 87 | uniformSequenceGenerator_.nextSequence(); |
| 88 | x_.weight = sample.weight; |
| 89 | for (Size i = 0; i < dimension_; i++) { |
| 90 | x_.value[i] = ICD_(sample.value[i]); |
| 91 | } |
| 92 | return x_; |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | |
| 97 | |
| 98 | #endif |
| 99 | |