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
26 Real StudentDistribution::operator()(Real x) const {
27 static GammaFunction G;
28 Real g1 = std::exp (x: G.logValue(x: 0.5 * (n_ + 1)));
29 Real g2 = std::exp (x: G.logValue(x: 0.5 * n_));
30
31 Real power = std::pow (x: 1. + x*x / n_, y: 0.5 * (n_ + 1));
32
33 return g1 / (g2 * power * std::sqrt (M_PI * n_));
34 }
35
36 Real CumulativeStudentDistribution::operator()(Real x) const {
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 (a: 0.5 * n_, b: 0.5, x: 1.0)
41 -incompleteBetaFunction (a: 0.5 * n_, b: 0.5, x: xx));
42 }
43
44 Real InverseCumulativeStudent::operator()(Real y) const {
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(x: 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
67

source code of quantlib/ql/math/distributions/studenttdistribution.cpp