| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2014 Jose Aparicio |
| 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 polarstudenttrng.hpp |
| 21 | \brief Polar Student t random-number generator |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_polar_student_t_rng_h |
| 25 | #define quantlib_polar_student_t_rng_h |
| 26 | |
| 27 | #include <ql/methods/montecarlo/sample.hpp> |
| 28 | #include <ql/errors.hpp> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | //! Student t random number generator |
| 33 | /*! Polar transformation based Student T random number generator. |
| 34 | See "Polar Generation of Random Variates With the t-Distribution", |
| 35 | Ralph W. Bailey, April 1994, in Mathematics of Computation, Vol 62-206 |
| 36 | page 779. |
| 37 | The one implemented here is a variant of this algorithm from "Random |
| 38 | Number Generation and Monte Carlo Methods", Springer, 2003, page 185. |
| 39 | Using a uniform RNG on a [-1,1] support, the extra call to the uniform |
| 40 | generator (used for the sign of the sample) is avoided. |
| 41 | |
| 42 | Class RNG must implement the following interface: |
| 43 | \code |
| 44 | URNG::sample_type RNG::next() const; |
| 45 | \endcode |
| 46 | |
| 47 | \warning do not use with a low-discrepancy sequence generator. |
| 48 | */ |
| 49 | template <class URNG> |
| 50 | class PolarStudentTRng { |
| 51 | public: |
| 52 | typedef Sample<Real> sample_type; |
| 53 | typedef URNG urng_type; |
| 54 | |
| 55 | explicit PolarStudentTRng(Real degFreedom, BigNatural seed = 0) |
| 56 | : uniformGenerator_(seed), |
| 57 | degFreedom_(degFreedom) { |
| 58 | QL_REQUIRE(degFreedom_ > 0, |
| 59 | "Invalid degrees of freedom parameter." ); |
| 60 | } |
| 61 | |
| 62 | explicit PolarStudentTRng(Real degFreedom, const URNG& urng) |
| 63 | : uniformGenerator_(urng), |
| 64 | degFreedom_(degFreedom) { |
| 65 | QL_REQUIRE(degFreedom_ > 0, |
| 66 | "Invalid degrees of freedom parameter." ); |
| 67 | } |
| 68 | |
| 69 | //! returns a sample from a Student-t distribution |
| 70 | sample_type next() const; |
| 71 | private: |
| 72 | URNG uniformGenerator_; |
| 73 | mutable Real degFreedom_; |
| 74 | }; |
| 75 | |
| 76 | template <class URNG> |
| 77 | inline typename PolarStudentTRng<URNG>::sample_type |
| 78 | PolarStudentTRng<URNG>::next() const { |
| 79 | Real u, v, rSqr; |
| 80 | do{ |
| 81 | //samples remapped to [-1,1]: |
| 82 | v = 2.* uniformGenerator_.next().value - 1.; |
| 83 | u = 2.* uniformGenerator_.next().value - 1.; |
| 84 | rSqr = v*v + u*u; |
| 85 | }while(rSqr >= 1.); |
| 86 | return {u * std::sqrt(x: degFreedom_ * (std::pow(x: rSqr, y: -2. / degFreedom_) - 1.) / rSqr), 1.}; |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | #endif |
| 92 | |