QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
polarstudenttrng.hpp
Go to the documentation of this file.
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
28#include <ql/errors.hpp>
29
30namespace 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>
51 public:
53 typedef URNG urng_type;
54
55 explicit PolarStudentTRng(Real degFreedom, BigNatural seed = 0)
56 : uniformGenerator_(seed),
57 degFreedom_(degFreedom) {
59 "Invalid degrees of freedom parameter.");
60 }
61
62 explicit PolarStudentTRng(Real degFreedom, const URNG& urng)
63 : uniformGenerator_(urng),
64 degFreedom_(degFreedom) {
66 "Invalid degrees of freedom parameter.");
67 }
68
69 //! returns a sample from a Student-t distribution
70 sample_type next() const;
71 private:
74 };
75
76 template <class URNG>
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(degFreedom_ * (std::pow(rSqr, -2. / degFreedom_) - 1.) / rSqr), 1.};
87 }
88
89}
90
91#endif
Student t random number generator.
PolarStudentTRng(Real degFreedom, BigNatural seed=0)
PolarStudentTRng(Real degFreedom, const URNG &urng)
sample_type next() const
returns a sample from a Student-t distribution
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
ext::shared_ptr< BlackVolTermStructure > v
weighted sample
weighted sample
Definition: sample.hpp:35