1
// Copyright John Maddock 2006.
2
// Copyright Paul A. Bristow 2006.
3
// Use, modification and distribution are subject to the
4
// Boost Software License, Version 1.0. (See accompanying file
5
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
#ifndef BOOST_STATS_STUDENTS_T_HPP
8
#define BOOST_STATS_STUDENTS_T_HPP
10
// http://en.wikipedia.org/wiki/Student%27s_t_distribution
11
// http://www.itl.nist.gov/div898/handbook/eda/section3/eda3664.htm
13
#include <boost/math/distributions/fwd.hpp>
14
#include <boost/math/special_functions/beta.hpp> // for ibeta(a, b, x).
15
#include <boost/math/distributions/complement.hpp>
16
#include <boost/math/distributions/detail/common_error_handling.hpp>
21
# pragma warning(push)
22
# pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
25
namespace boost{ namespace math{
27
template <class RealType = double, class Policy = policies::policy<> >
28
class students_t_distribution
31
typedef RealType value_type;
32
typedef Policy policy_type;
34
students_t_distribution(RealType i) : m_df(i)
38
"boost::math::students_t_distribution<%1%>::students_t_distribution", m_df, &result, Policy());
39
} // students_t_distribution
41
RealType degrees_of_freedom()const
46
// Parameter estimation:
47
static RealType find_degrees_of_freedom(
48
RealType difference_from_mean,
58
RealType m_df; // degrees of freedom are a real number.
61
typedef students_t_distribution<double> students_t;
63
template <class RealType, class Policy>
64
inline const std::pair<RealType, RealType> range(const students_t_distribution<RealType, Policy>& /*dist*/)
65
{ // Range of permissible values for random variable x.
66
using boost::math::tools::max_value;
67
return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
70
template <class RealType, class Policy>
71
inline const std::pair<RealType, RealType> support(const students_t_distribution<RealType, Policy>& /*dist*/)
72
{ // Range of supported values for random variable x.
73
// This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
74
using boost::math::tools::max_value;
75
return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
78
template <class RealType, class Policy>
79
inline RealType pdf(const students_t_distribution<RealType, Policy>& dist, const RealType& t)
81
BOOST_FPU_EXCEPTION_GUARD
82
BOOST_MATH_STD_USING // for ADL of std functions
84
RealType degrees_of_freedom = dist.degrees_of_freedom();
86
RealType error_result;
87
if(false == detail::check_df(
88
"boost::math::pdf(const students_t_distribution<%1%>&, %1%)", degrees_of_freedom, &error_result, Policy()))
90
// Might conceivably permit df = +infinity and use normal distribution.
92
RealType basem1 = t * t / degrees_of_freedom;
95
result = exp(-boost::math::log1p(basem1, Policy()) * (1+degrees_of_freedom) / 2);
99
result = pow(1 / (1 + basem1), (degrees_of_freedom + 1) / 2);
101
result /= sqrt(degrees_of_freedom) * boost::math::beta(degrees_of_freedom / 2, RealType(0.5f), Policy());
105
template <class RealType, class Policy>
106
inline RealType cdf(const students_t_distribution<RealType, Policy>& dist, const RealType& t)
108
RealType degrees_of_freedom = dist.degrees_of_freedom();
110
RealType error_result;
111
if(false == detail::check_df(
112
"boost::math::cdf(const students_t_distribution<%1%>&, %1%)", degrees_of_freedom, &error_result, Policy()))
120
// Calculate probability of Student's t using the incomplete beta function.
121
// probability = ibeta(degrees_of_freedom / 2, 1/2, degrees_of_freedom / (degrees_of_freedom + t*t))
123
// However when t is small compared to the degrees of freedom, that formula
124
// suffers from rounding error, use the identity formula to work around
127
// I[x](a,b) = 1 - I[1-x](b,a)
131
// x = df / (df + t^2)
135
// 1 - x = t^2 / (df + t^2)
138
RealType probability;
139
if(degrees_of_freedom > 2 * t2)
141
RealType z = t2 / (degrees_of_freedom + t2);
142
probability = ibetac(static_cast<RealType>(0.5), degrees_of_freedom / 2, z, Policy()) / 2;
146
RealType z = degrees_of_freedom / (degrees_of_freedom + t2);
147
probability = ibeta(degrees_of_freedom / 2, static_cast<RealType>(0.5), z, Policy()) / 2;
149
return (t > 0 ? 1 - probability : probability);
152
template <class RealType, class Policy>
153
inline RealType quantile(const students_t_distribution<RealType, Policy>& dist, const RealType& p)
155
BOOST_MATH_STD_USING // for ADL of std functions
157
// Obtain parameters:
159
RealType degrees_of_freedom = dist.degrees_of_freedom();
160
RealType probability = p;
162
// Check for domain errors:
164
static const char* function = "boost::math::quantile(const students_t_distribution<%1%>&, %1%)";
165
RealType error_result;
166
if(false == detail::check_df(
167
function, degrees_of_freedom, &error_result, Policy())
168
&& detail::check_probability(function, probability, &error_result, Policy()))
171
// Special cases, regardless of degrees_of_freedom.
172
if (probability == 0)
173
return -policies::raise_overflow_error<RealType>(function, 0, Policy());
174
if (probability == 1)
175
return policies::raise_overflow_error<RealType>(function, 0, Policy());
176
if (probability == static_cast<RealType>(0.5))
179
// This next block is disabled in favour of a faster method than
180
// incomplete beta inverse, code retained for future reference:
184
// Calculate quantile of Student's t using the incomplete beta function inverse:
186
probability = (probability > 0.5) ? 1 - probability : probability;
188
x = ibeta_inv(degrees_of_freedom / 2, RealType(0.5), 2 * probability, &y);
189
if(degrees_of_freedom * y > tools::max_value<RealType>() * x)
190
t = tools::overflow_error<RealType>(function);
192
t = sqrt(degrees_of_freedom * y / x);
194
// Figure out sign based on the size of p:
202
// Depending on how many digits RealType has, this may forward
203
// to the incomplete beta inverse as above. Otherwise uses a
204
// faster method that is accurate to ~15 digits everywhere
205
// and a couple of epsilon at double precision and in the central
206
// region where most use cases will occur...
208
return boost::math::detail::fast_students_t_quantile(degrees_of_freedom, probability, Policy());
211
template <class RealType, class Policy>
212
inline RealType cdf(const complemented2_type<students_t_distribution<RealType, Policy>, RealType>& c)
214
return cdf(c.dist, -c.param);
217
template <class RealType, class Policy>
218
inline RealType quantile(const complemented2_type<students_t_distribution<RealType, Policy>, RealType>& c)
220
return -quantile(c.dist, c.param);
224
// Parameter estimation follows:
228
// Functors for finding degrees of freedom:
230
template <class RealType, class Policy>
231
struct sample_size_func
233
sample_size_func(RealType a, RealType b, RealType s, RealType d)
234
: alpha(a), beta(b), ratio(s*s/(d*d)) {}
236
RealType operator()(const RealType& df)
238
if(df <= tools::min_value<RealType>())
240
students_t_distribution<RealType, Policy> t(df);
241
RealType qa = quantile(complement(t, alpha));
242
RealType qb = quantile(complement(t, beta));
249
RealType alpha, beta, ratio;
252
} // namespace detail
254
template <class RealType, class Policy>
255
RealType students_t_distribution<RealType, Policy>::find_degrees_of_freedom(
256
RealType difference_from_mean,
262
static const char* function = "boost::math::students_t_distribution<%1%>::find_degrees_of_freedom";
264
// Check for domain errors:
266
RealType error_result;
267
if(false == detail::check_probability(
268
function, alpha, &error_result, Policy())
269
&& detail::check_probability(function, beta, &error_result, Policy()))
275
detail::sample_size_func<RealType, Policy> f(alpha, beta, sd, difference_from_mean);
276
tools::eps_tolerance<RealType> tol(policies::digits<RealType, Policy>());
277
boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
278
std::pair<RealType, RealType> r = tools::bracket_and_solve_root(f, hint, RealType(2), false, tol, max_iter, Policy());
279
RealType result = r.first + (r.second - r.first) / 2;
280
if(max_iter >= policies::get_max_root_iterations<Policy>())
282
policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
283
" either there is no answer to how many degrees of freedom are required"
284
" or the answer is infinite. Current best guess is %1%", result, Policy());
289
template <class RealType, class Policy>
290
inline RealType mean(const students_t_distribution<RealType, Policy>& )
295
template <class RealType, class Policy>
296
inline RealType variance(const students_t_distribution<RealType, Policy>& dist)
299
RealType error_result;
300
if(false == detail::check_df(
301
"boost::math::variance(students_t_distribution<%1%> const&, %1%)", dist.degrees_of_freedom(), &error_result, Policy()))
304
RealType v = dist.degrees_of_freedom();
308
template <class RealType, class Policy>
309
inline RealType mode(const students_t_distribution<RealType, Policy>& /*dist*/)
314
template <class RealType, class Policy>
315
inline RealType median(const students_t_distribution<RealType, Policy>& /*dist*/)
320
template <class RealType, class Policy>
321
inline RealType skewness(const students_t_distribution<RealType, Policy>& dist)
323
if(dist.degrees_of_freedom() <= 3)
325
policies::raise_domain_error<RealType>(
326
"boost::math::skewness(students_t_distribution<%1%> const&, %1%)",
327
"Skewness is undefined for degrees of freedom <= 3, but got %1%.",
328
dist.degrees_of_freedom(), Policy());
333
template <class RealType, class Policy>
334
inline RealType kurtosis(const students_t_distribution<RealType, Policy>& dist)
336
RealType df = dist.degrees_of_freedom();
339
policies::raise_domain_error<RealType>(
340
"boost::math::kurtosis(students_t_distribution<%1%> const&, %1%)",
341
"Skewness is undefined for degrees of freedom <= 3, but got %1%.",
344
return 3 * (df - 2) / (df - 4);
347
template <class RealType, class Policy>
348
inline RealType kurtosis_excess(const students_t_distribution<RealType, Policy>& dist)
350
// see http://mathworld.wolfram.com/Kurtosis.html
351
RealType df = dist.degrees_of_freedom();
354
policies::raise_domain_error<RealType>(
355
"boost::math::kurtosis_excess(students_t_distribution<%1%> const&, %1%)",
356
"Skewness is undefined for degrees of freedom <= 3, but got %1%.",
366
# pragma warning(pop)
369
// This include must be at the end, *after* the accessors
370
// for this distribution have been defined, in order to
371
// keep compilers that support two-phase lookup happy.
372
#include <boost/math/distributions/detail/derived_accessors.hpp>
374
#endif // BOOST_STATS_STUDENTS_T_HPP