QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
boxmullergaussianrng.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) 2003 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 boxmullergaussianrng.hpp
22 \brief Box-Muller Gaussian random-number generator
23*/
24
25#ifndef quantlib_box_muller_gaussian_rng_h
26#define quantlib_box_muller_gaussian_rng_h
27
29
30namespace QuantLib {
31
32 //! Gaussian random number generator
33 /*! It uses the well-known Box-Muller transformation to return a
34 normal distributed Gaussian deviate with average 0.0 and
35 standard deviation of 1.0, from a uniform deviate in (0,1)
36 supplied by RNG.
37
38 Class RNG must implement the following interface:
39 \code
40 RNG::sample_type RNG::next() const;
41 \endcode
42 */
43 template <class RNG>
45 public:
47 typedef RNG urng_type;
48 explicit BoxMullerGaussianRng(const RNG& uniformGenerator);
49 //! returns a sample from a Gaussian distribution
50 sample_type next() const;
51 private:
53 mutable bool returnFirst_ = true;
56 mutable Real weight_ = 0.0;
57 };
58
59 template <class RNG>
61 : uniformGenerator_(uniformGenerator) {}
62
63 template <class RNG>
66 if (returnFirst_) {
67 Real x1,x2,r,ratio;
68 do {
69 typename RNG::sample_type s1 = uniformGenerator_.next();
70 x1 = s1.value*2.0-1.0;
71 firstWeight_ = s1.weight;
72 typename RNG::sample_type s2 = uniformGenerator_.next();
73 x2 = s2.value*2.0-1.0;
74 secondWeight_ = s2.weight;
75 r = x1*x1+x2*x2;
76 } while (r>=1.0 || r==0.0);
77
78 ratio = std::sqrt(-2.0*std::log(r)/r);
79 firstValue_ = x1*ratio;
80 secondValue_ = x2*ratio;
81 weight_ = firstWeight_*secondWeight_;
82
83 returnFirst_ = false;
84 return {firstValue_, weight_};
85 } else {
86 returnFirst_ = true;
87 return {secondValue_, weight_};
88 }
89 }
90
91}
92
93
94#endif
Gaussian random number generator.
BoxMullerGaussianRng(const RNG &uniformGenerator)
sample_type next() const
returns a sample from a Gaussian distribution
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
ext::shared_ptr< YieldTermStructure > r
weighted sample
weighted sample
Definition: sample.hpp:35