QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
studenttdistribution.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Roland Lichters
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#include <ql/math/distributions/studenttdistribution.hpp>
21#include <ql/math/distributions/gammadistribution.hpp>
22#include <ql/math/beta.hpp>
23
24namespace QuantLib {
25
27 static GammaFunction G;
28 Real g1 = std::exp (G.logValue(0.5 * (n_ + 1)));
29 Real g2 = std::exp (G.logValue(0.5 * n_));
30
31 Real power = std::pow (1. + x*x / n_, 0.5 * (n_ + 1));
32
33 return g1 / (g2 * power * std::sqrt (M_PI * n_));
34 }
35
37 Real xx = 1.0 * n_ / (x*x + n_);
38 Real sig = (x > 0 ? 1.0 : - 1.0);
39
40 return 0.5 + 0.5 * sig * ( incompleteBetaFunction (0.5 * n_, 0.5, 1.0)
41 -incompleteBetaFunction (0.5 * n_, 0.5, xx));
42 }
43
45 QL_REQUIRE (y >= 0 && y <= 1, "argument out of range [0, 1]");
46
47 Real x = 0;
48 Size count = 0;
49
50 // do a few newton steps to find x
51 do {
52 x -= (f_(x) - y) / d_(x);
53 count++;
54 }
55 while (std::fabs(f_(x) - y) > accuracy_ && count < maxIterations_);
56
57 QL_REQUIRE (count < maxIterations_,
58 "maximum number of iterations " << maxIterations_
59 << " reached in InverseCumulativeStudent, "
60 << "y=" << y << ", x=" << x);
61
62 return x;
63 }
64
65}
66
Gamma function class.
Real logValue(Real x) const
CumulativeStudentDistribution f_
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real incompleteBetaFunction(Real a, Real b, Real x, Real accuracy, Integer maxIteration)
Incomplete Beta function.
Definition: beta.cpp:67