| 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 | /*! \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 | /*! \deprecated Use `auto` or `decltype` instead. |
| 45 | Deprecated in version 1.29. |
| 46 | */ |
| 47 | QL_DEPRECATED |
| 48 | typedef Real argument_type; |
| 49 | |
| 50 | /*! \deprecated Use `auto` or `decltype` instead. |
| 51 | Deprecated in version 1.29. |
| 52 | */ |
| 53 | QL_DEPRECATED |
| 54 | typedef Real result_type; |
| 55 | |
| 56 | StudentDistribution(Integer n) : n_(n) { |
| 57 | QL_REQUIRE(n > 0, "invalid parameter for t-distribution" ); |
| 58 | } |
| 59 | Real operator()(Real x) const; |
| 60 | private: |
| 61 | Integer n_; |
| 62 | }; |
| 63 | |
| 64 | //! Cumulative Student t-distribution |
| 65 | /*! Cumulative distribution function for \f$ n \f$ degrees of freedom |
| 66 | (see mathworld.wolfram.com): |
| 67 | \f[ |
| 68 | F(x) = \int_{-\infty}^x\,f(y)\,dy |
| 69 | = \frac{1}{2}\, |
| 70 | +\,\frac{1}{2}\,sgn(x)\, |
| 71 | \left[ I\left(1,\frac{n}{2},\frac{1}{2}\right) |
| 72 | - I\left(\frac{n}{n+y^2}, \frac{n}{2},\frac{1}{2}\right)\right] |
| 73 | \f] |
| 74 | where \f$ I(z; a, b) \f$ is the regularized incomplete beta function. |
| 75 | */ |
| 76 | class CumulativeStudentDistribution { |
| 77 | public: |
| 78 | /*! \deprecated Use `auto` or `decltype` instead. |
| 79 | Deprecated in version 1.29. |
| 80 | */ |
| 81 | QL_DEPRECATED |
| 82 | typedef Real argument_type; |
| 83 | |
| 84 | /*! \deprecated Use `auto` or `decltype` instead. |
| 85 | Deprecated in version 1.29. |
| 86 | */ |
| 87 | QL_DEPRECATED |
| 88 | typedef Real result_type; |
| 89 | |
| 90 | CumulativeStudentDistribution(Integer n) : n_(n) { |
| 91 | QL_REQUIRE(n > 0, "invalid parameter for t-distribution" ); |
| 92 | } |
| 93 | Real operator()(Real x) const; |
| 94 | private: |
| 95 | Integer n_; |
| 96 | }; |
| 97 | |
| 98 | //! Inverse cumulative Student t-distribution |
| 99 | /*! \todo Find/implement an efficient algorithm for evaluating the |
| 100 | cumulative Student t-distribution, replacing the Newton |
| 101 | iteration |
| 102 | */ |
| 103 | class InverseCumulativeStudent { |
| 104 | public: |
| 105 | /*! \deprecated Use `auto` or `decltype` instead. |
| 106 | Deprecated in version 1.29. |
| 107 | */ |
| 108 | QL_DEPRECATED |
| 109 | typedef Real argument_type; |
| 110 | |
| 111 | /*! \deprecated Use `auto` or `decltype` instead. |
| 112 | Deprecated in version 1.29. |
| 113 | */ |
| 114 | QL_DEPRECATED |
| 115 | typedef Real result_type; |
| 116 | |
| 117 | InverseCumulativeStudent(Integer n, |
| 118 | Real accuracy = 1e-6, |
| 119 | Size maxIterations = 50) |
| 120 | : d_(n), f_(n), accuracy_(accuracy), |
| 121 | maxIterations_(maxIterations) {} |
| 122 | Real operator()(Real x) const; |
| 123 | private: |
| 124 | StudentDistribution d_; |
| 125 | CumulativeStudentDistribution f_; |
| 126 | Real accuracy_; |
| 127 | Size maxIterations_; |
| 128 | }; |
| 129 | |
| 130 | } |
| 131 | |
| 132 | #endif |
| 133 | |