QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
polarstudenttrng.hpp
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
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
30namespace QuantLib {
31
33
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) {
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
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
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
weighted sample
Definition: sample.hpp:35