/src/quantlib/ql/math/distributions/studenttdistribution.hpp
Line | Count | Source |
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 | | <https://www.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 studenttdistribution.hpp |
21 | | \brief Student's t-distribution |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_student_t_distribution_hpp |
25 | | #define quantlib_student_t_distribution_hpp |
26 | | |
27 | | #include <ql/errors.hpp> |
28 | | #include <ql/types.hpp> |
29 | | #include <functional> |
30 | | |
31 | | namespace QuantLib { |
32 | | |
33 | | //! Student t-distribution |
34 | | /*! Probability density function for \f$ n \f$ degrees of freedom |
35 | | (see mathworld.wolfram.com or wikipedia.org): |
36 | | \f[ |
37 | | f(x) = \frac {\Gamma\left(\frac{n+1}{2}\right)} {\sqrt{n\pi} |
38 | | \, \Gamma\left(\frac{n}{2}\right)}\: |
39 | | \frac {1} {\left(1+\frac{x^2}{n}\right)^{(n+1)/2}} |
40 | | \f] |
41 | | */ |
42 | | class StudentDistribution { |
43 | | public: |
44 | 0 | StudentDistribution(Integer n) : n_(n) { |
45 | 0 | QL_REQUIRE(n > 0, "invalid parameter for t-distribution"); |
46 | 0 | } |
47 | | Real operator()(Real x) const; |
48 | | private: |
49 | | Integer n_; |
50 | | }; |
51 | | |
52 | | //! Cumulative Student t-distribution |
53 | | /*! Cumulative distribution function for \f$ n \f$ degrees of freedom |
54 | | (see mathworld.wolfram.com): |
55 | | \f[ |
56 | | F(x) = \int_{-\infty}^x\,f(y)\,dy |
57 | | = \frac{1}{2}\, |
58 | | +\,\frac{1}{2}\,sgn(x)\, |
59 | | \left[ I\left(1,\frac{n}{2},\frac{1}{2}\right) |
60 | | - I\left(\frac{n}{n+y^2}, \frac{n}{2},\frac{1}{2}\right)\right] |
61 | | \f] |
62 | | where \f$ I(z; a, b) \f$ is the regularized incomplete beta function. |
63 | | */ |
64 | | class CumulativeStudentDistribution { |
65 | | public: |
66 | 0 | CumulativeStudentDistribution(Integer n) : n_(n) { |
67 | 0 | QL_REQUIRE(n > 0, "invalid parameter for t-distribution"); |
68 | 0 | } |
69 | | Real operator()(Real x) const; |
70 | | private: |
71 | | Integer n_; |
72 | | }; |
73 | | |
74 | | //! Inverse cumulative Student t-distribution |
75 | | /*! \todo Find/implement an efficient algorithm for evaluating the |
76 | | cumulative Student t-distribution, replacing the Newton |
77 | | iteration |
78 | | */ |
79 | | class InverseCumulativeStudent { |
80 | | public: |
81 | | InverseCumulativeStudent(Integer n, |
82 | | Real accuracy = 1e-6, |
83 | | Size maxIterations = 50) |
84 | | : d_(n), f_(n), accuracy_(accuracy), |
85 | 0 | maxIterations_(maxIterations) {} |
86 | | Real operator()(Real x) const; |
87 | | private: |
88 | | StudentDistribution d_; |
89 | | CumulativeStudentDistribution f_; |
90 | | Real accuracy_; |
91 | | Size maxIterations_; |
92 | | }; |
93 | | |
94 | | } |
95 | | |
96 | | #endif |