students_t.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2006, 2012, 2017.
  3. // Copyright Thomas Mang 2012.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0. (See accompanying file
  6. // 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
  9. // http://en.wikipedia.org/wiki/Student%27s_t_distribution
  10. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3664.htm
  11. #include <boost/math/distributions/fwd.hpp>
  12. #include <boost/math/special_functions/beta.hpp> // for ibeta(a, b, x).
  13. #include <boost/math/special_functions/digamma.hpp>
  14. #include <boost/math/distributions/complement.hpp>
  15. #include <boost/math/distributions/detail/common_error_handling.hpp>
  16. #include <boost/math/distributions/normal.hpp>
  17. #include <utility>
  18. #ifdef BOOST_MSVC
  19. # pragma warning(push)
  20. # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
  21. #endif
  22. namespace boost { namespace math {
  23. template <class RealType = double, class Policy = policies::policy<> >
  24. class students_t_distribution
  25. {
  26. public:
  27. typedef RealType value_type;
  28. typedef Policy policy_type;
  29. students_t_distribution(RealType df) : df_(df)
  30. { // Constructor.
  31. RealType result;
  32. detail::check_df_gt0_to_inf( // Checks that df > 0 or df == inf.
  33. "boost::math::students_t_distribution<%1%>::students_t_distribution", df_, &result, Policy());
  34. } // students_t_distribution
  35. RealType degrees_of_freedom()const
  36. {
  37. return df_;
  38. }
  39. // Parameter estimation:
  40. static RealType find_degrees_of_freedom(
  41. RealType difference_from_mean,
  42. RealType alpha,
  43. RealType beta,
  44. RealType sd,
  45. RealType hint = 100);
  46. private:
  47. // Data member:
  48. RealType df_; // degrees of freedom is a real number > 0 or +infinity.
  49. };
  50. typedef students_t_distribution<double> students_t; // Convenience typedef for double version.
  51. template <class RealType, class Policy>
  52. inline const std::pair<RealType, RealType> range(const students_t_distribution<RealType, Policy>& /*dist*/)
  53. { // Range of permissible values for random variable x.
  54. // Now including infinity.
  55. using boost::math::tools::max_value;
  56. //return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
  57. return std::pair<RealType, RealType>(((::std::numeric_limits<RealType>::is_specialized & ::std::numeric_limits<RealType>::has_infinity) ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>()), ((::std::numeric_limits<RealType>::is_specialized & ::std::numeric_limits<RealType>::has_infinity) ? +std::numeric_limits<RealType>::infinity() : +max_value<RealType>()));
  58. }
  59. template <class RealType, class Policy>
  60. inline const std::pair<RealType, RealType> support(const students_t_distribution<RealType, Policy>& /*dist*/)
  61. { // Range of supported values for random variable x.
  62. // Now including infinity.
  63. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  64. using boost::math::tools::max_value;
  65. //return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
  66. return std::pair<RealType, RealType>(((::std::numeric_limits<RealType>::is_specialized & ::std::numeric_limits<RealType>::has_infinity) ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>()), ((::std::numeric_limits<RealType>::is_specialized & ::std::numeric_limits<RealType>::has_infinity) ? +std::numeric_limits<RealType>::infinity() : +max_value<RealType>()));
  67. }
  68. template <class RealType, class Policy>
  69. inline RealType pdf(const students_t_distribution<RealType, Policy>& dist, const RealType& x)
  70. {
  71. BOOST_FPU_EXCEPTION_GUARD
  72. BOOST_MATH_STD_USING // for ADL of std functions.
  73. RealType error_result;
  74. if(false == detail::check_x_not_NaN(
  75. "boost::math::pdf(const students_t_distribution<%1%>&, %1%)", x, &error_result, Policy()))
  76. return error_result;
  77. RealType df = dist.degrees_of_freedom();
  78. if(false == detail::check_df_gt0_to_inf( // Check that df > 0 or == +infinity.
  79. "boost::math::pdf(const students_t_distribution<%1%>&, %1%)", df, &error_result, Policy()))
  80. return error_result;
  81. RealType result;
  82. if ((boost::math::isinf)(x))
  83. { // - or +infinity.
  84. result = static_cast<RealType>(0);
  85. return result;
  86. }
  87. RealType limit = policies::get_epsilon<RealType, Policy>();
  88. // Use policies so that if policy requests lower precision,
  89. // then get the normal distribution approximation earlier.
  90. limit = static_cast<RealType>(1) / limit; // 1/eps
  91. // for 64-bit double 1/eps = 4503599627370496
  92. if (df > limit)
  93. { // Special case for really big degrees_of_freedom > 1 / eps
  94. // - use normal distribution which is much faster and more accurate.
  95. normal_distribution<RealType, Policy> n(0, 1);
  96. result = pdf(n, x);
  97. }
  98. else
  99. { //
  100. RealType basem1 = x * x / df;
  101. if(basem1 < 0.125)
  102. {
  103. result = exp(-boost::math::log1p(basem1, Policy()) * (1+df) / 2);
  104. }
  105. else
  106. {
  107. result = pow(1 / (1 + basem1), (df + 1) / 2);
  108. }
  109. result /= sqrt(df) * boost::math::beta(df / 2, RealType(0.5f), Policy());
  110. }
  111. return result;
  112. } // pdf
  113. template <class RealType, class Policy>
  114. inline RealType cdf(const students_t_distribution<RealType, Policy>& dist, const RealType& x)
  115. {
  116. RealType error_result;
  117. // degrees_of_freedom > 0 or infinity check:
  118. RealType df = dist.degrees_of_freedom();
  119. if (false == detail::check_df_gt0_to_inf( // Check that df > 0 or == +infinity.
  120. "boost::math::cdf(const students_t_distribution<%1%>&, %1%)", df, &error_result, Policy()))
  121. {
  122. return error_result;
  123. }
  124. // Check for bad x first.
  125. if(false == detail::check_x_not_NaN(
  126. "boost::math::cdf(const students_t_distribution<%1%>&, %1%)", x, &error_result, Policy()))
  127. {
  128. return error_result;
  129. }
  130. if (x == 0)
  131. { // Special case with exact result.
  132. return static_cast<RealType>(0.5);
  133. }
  134. if ((boost::math::isinf)(x))
  135. { // x == - or + infinity, regardless of df.
  136. return ((x < 0) ? static_cast<RealType>(0) : static_cast<RealType>(1));
  137. }
  138. RealType limit = policies::get_epsilon<RealType, Policy>();
  139. // Use policies so that if policy requests lower precision,
  140. // then get the normal distribution approximation earlier.
  141. limit = static_cast<RealType>(1) / limit; // 1/eps
  142. // for 64-bit double 1/eps = 4503599627370496
  143. if (df > limit)
  144. { // Special case for really big degrees_of_freedom > 1 / eps (perhaps infinite?)
  145. // - use normal distribution which is much faster and more accurate.
  146. normal_distribution<RealType, Policy> n(0, 1);
  147. RealType result = cdf(n, x);
  148. return result;
  149. }
  150. else
  151. { // normal df case.
  152. //
  153. // Calculate probability of Student's t using the incomplete beta function.
  154. // probability = ibeta(degrees_of_freedom / 2, 1/2, degrees_of_freedom / (degrees_of_freedom + t*t))
  155. //
  156. // However when t is small compared to the degrees of freedom, that formula
  157. // suffers from rounding error, use the identity formula to work around
  158. // the problem:
  159. //
  160. // I[x](a,b) = 1 - I[1-x](b,a)
  161. //
  162. // and:
  163. //
  164. // x = df / (df + t^2)
  165. //
  166. // so:
  167. //
  168. // 1 - x = t^2 / (df + t^2)
  169. //
  170. RealType x2 = x * x;
  171. RealType probability;
  172. if(df > 2 * x2)
  173. {
  174. RealType z = x2 / (df + x2);
  175. probability = ibetac(static_cast<RealType>(0.5), df / 2, z, Policy()) / 2;
  176. }
  177. else
  178. {
  179. RealType z = df / (df + x2);
  180. probability = ibeta(df / 2, static_cast<RealType>(0.5), z, Policy()) / 2;
  181. }
  182. return (x > 0 ? 1 - probability : probability);
  183. }
  184. } // cdf
  185. template <class RealType, class Policy>
  186. inline RealType quantile(const students_t_distribution<RealType, Policy>& dist, const RealType& p)
  187. {
  188. BOOST_MATH_STD_USING // for ADL of std functions
  189. //
  190. // Obtain parameters:
  191. RealType probability = p;
  192. // Check for domain errors:
  193. RealType df = dist.degrees_of_freedom();
  194. static const char* function = "boost::math::quantile(const students_t_distribution<%1%>&, %1%)";
  195. RealType error_result;
  196. if(false == (detail::check_df_gt0_to_inf( // Check that df > 0 or == +infinity.
  197. function, df, &error_result, Policy())
  198. && detail::check_probability(function, probability, &error_result, Policy())))
  199. return error_result;
  200. // Special cases, regardless of degrees_of_freedom.
  201. if (probability == 0)
  202. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  203. if (probability == 1)
  204. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  205. if (probability == static_cast<RealType>(0.5))
  206. return 0; //
  207. //
  208. #if 0
  209. // This next block is disabled in favour of a faster method than
  210. // incomplete beta inverse, but code retained for future reference:
  211. //
  212. // Calculate quantile of Student's t using the incomplete beta function inverse:
  213. probability = (probability > 0.5) ? 1 - probability : probability;
  214. RealType t, x, y;
  215. x = ibeta_inv(degrees_of_freedom / 2, RealType(0.5), 2 * probability, &y);
  216. if(degrees_of_freedom * y > tools::max_value<RealType>() * x)
  217. t = tools::overflow_error<RealType>(function);
  218. else
  219. t = sqrt(degrees_of_freedom * y / x);
  220. //
  221. // Figure out sign based on the size of p:
  222. //
  223. if(p < 0.5)
  224. t = -t;
  225. return t;
  226. #endif
  227. //
  228. // Depending on how many digits RealType has, this may forward
  229. // to the incomplete beta inverse as above. Otherwise uses a
  230. // faster method that is accurate to ~15 digits everywhere
  231. // and a couple of epsilon at double precision and in the central
  232. // region where most use cases will occur...
  233. //
  234. return boost::math::detail::fast_students_t_quantile(df, probability, Policy());
  235. } // quantile
  236. template <class RealType, class Policy>
  237. inline RealType cdf(const complemented2_type<students_t_distribution<RealType, Policy>, RealType>& c)
  238. {
  239. return cdf(c.dist, -c.param);
  240. }
  241. template <class RealType, class Policy>
  242. inline RealType quantile(const complemented2_type<students_t_distribution<RealType, Policy>, RealType>& c)
  243. {
  244. return -quantile(c.dist, c.param);
  245. }
  246. //
  247. // Parameter estimation follows:
  248. //
  249. namespace detail{
  250. //
  251. // Functors for finding degrees of freedom:
  252. //
  253. template <class RealType, class Policy>
  254. struct sample_size_func
  255. {
  256. sample_size_func(RealType a, RealType b, RealType s, RealType d)
  257. : alpha(a), beta(b), ratio(s*s/(d*d)) {}
  258. RealType operator()(const RealType& df)
  259. {
  260. if(df <= tools::min_value<RealType>())
  261. { //
  262. return 1;
  263. }
  264. students_t_distribution<RealType, Policy> t(df);
  265. RealType qa = quantile(complement(t, alpha));
  266. RealType qb = quantile(complement(t, beta));
  267. qa += qb;
  268. qa *= qa;
  269. qa *= ratio;
  270. qa -= (df + 1);
  271. return qa;
  272. }
  273. RealType alpha, beta, ratio;
  274. };
  275. } // namespace detail
  276. template <class RealType, class Policy>
  277. RealType students_t_distribution<RealType, Policy>::find_degrees_of_freedom(
  278. RealType difference_from_mean,
  279. RealType alpha,
  280. RealType beta,
  281. RealType sd,
  282. RealType hint)
  283. {
  284. static const char* function = "boost::math::students_t_distribution<%1%>::find_degrees_of_freedom";
  285. //
  286. // Check for domain errors:
  287. //
  288. RealType error_result;
  289. if(false == detail::check_probability(
  290. function, alpha, &error_result, Policy())
  291. && detail::check_probability(function, beta, &error_result, Policy()))
  292. return error_result;
  293. if(hint <= 0)
  294. hint = 1;
  295. detail::sample_size_func<RealType, Policy> f(alpha, beta, sd, difference_from_mean);
  296. tools::eps_tolerance<RealType> tol(policies::digits<RealType, Policy>());
  297. boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  298. std::pair<RealType, RealType> r = tools::bracket_and_solve_root(f, hint, RealType(2), false, tol, max_iter, Policy());
  299. RealType result = r.first + (r.second - r.first) / 2;
  300. if(max_iter >= policies::get_max_root_iterations<Policy>())
  301. {
  302. return policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
  303. " either there is no answer to how many degrees of freedom are required"
  304. " or the answer is infinite. Current best guess is %1%", result, Policy());
  305. }
  306. return result;
  307. }
  308. template <class RealType, class Policy>
  309. inline RealType mode(const students_t_distribution<RealType, Policy>& /*dist*/)
  310. {
  311. // Assume no checks on degrees of freedom are useful (unlike mean).
  312. return 0; // Always zero by definition.
  313. }
  314. template <class RealType, class Policy>
  315. inline RealType median(const students_t_distribution<RealType, Policy>& /*dist*/)
  316. {
  317. // Assume no checks on degrees of freedom are useful (unlike mean).
  318. return 0; // Always zero by definition.
  319. }
  320. // See section 5.1 on moments at http://en.wikipedia.org/wiki/Student%27s_t-distribution
  321. template <class RealType, class Policy>
  322. inline RealType mean(const students_t_distribution<RealType, Policy>& dist)
  323. { // Revised for https://svn.boost.org/trac/boost/ticket/7177
  324. RealType df = dist.degrees_of_freedom();
  325. if(((boost::math::isnan)(df)) || (df <= 1) )
  326. { // mean is undefined for moment <= 1!
  327. return policies::raise_domain_error<RealType>(
  328. "boost::math::mean(students_t_distribution<%1%> const&, %1%)",
  329. "Mean is undefined for degrees of freedom < 1 but got %1%.", df, Policy());
  330. return std::numeric_limits<RealType>::quiet_NaN();
  331. }
  332. return 0;
  333. } // mean
  334. template <class RealType, class Policy>
  335. inline RealType variance(const students_t_distribution<RealType, Policy>& dist)
  336. { // http://en.wikipedia.org/wiki/Student%27s_t-distribution
  337. // Revised for https://svn.boost.org/trac/boost/ticket/7177
  338. RealType df = dist.degrees_of_freedom();
  339. if ((boost::math::isnan)(df) || (df <= 2))
  340. { // NaN or undefined for <= 2.
  341. return policies::raise_domain_error<RealType>(
  342. "boost::math::variance(students_t_distribution<%1%> const&, %1%)",
  343. "variance is undefined for degrees of freedom <= 2, but got %1%.",
  344. df, Policy());
  345. return std::numeric_limits<RealType>::quiet_NaN(); // Undefined.
  346. }
  347. if ((boost::math::isinf)(df))
  348. { // +infinity.
  349. return 1;
  350. }
  351. RealType limit = policies::get_epsilon<RealType, Policy>();
  352. // Use policies so that if policy requests lower precision,
  353. // then get the normal distribution approximation earlier.
  354. limit = static_cast<RealType>(1) / limit; // 1/eps
  355. // for 64-bit double 1/eps = 4503599627370496
  356. if (df > limit)
  357. { // Special case for really big degrees_of_freedom > 1 / eps.
  358. return 1;
  359. }
  360. else
  361. {
  362. return df / (df - 2);
  363. }
  364. } // variance
  365. template <class RealType, class Policy>
  366. inline RealType skewness(const students_t_distribution<RealType, Policy>& dist)
  367. {
  368. RealType df = dist.degrees_of_freedom();
  369. if( ((boost::math::isnan)(df)) || (dist.degrees_of_freedom() <= 3))
  370. { // Undefined for moment k = 3.
  371. return policies::raise_domain_error<RealType>(
  372. "boost::math::skewness(students_t_distribution<%1%> const&, %1%)",
  373. "Skewness is undefined for degrees of freedom <= 3, but got %1%.",
  374. dist.degrees_of_freedom(), Policy());
  375. return std::numeric_limits<RealType>::quiet_NaN();
  376. }
  377. return 0; // For all valid df, including infinity.
  378. } // skewness
  379. template <class RealType, class Policy>
  380. inline RealType kurtosis(const students_t_distribution<RealType, Policy>& dist)
  381. {
  382. RealType df = dist.degrees_of_freedom();
  383. if(((boost::math::isnan)(df)) || (df <= 4))
  384. { // Undefined or infinity for moment k = 4.
  385. return policies::raise_domain_error<RealType>(
  386. "boost::math::kurtosis(students_t_distribution<%1%> const&, %1%)",
  387. "Kurtosis is undefined for degrees of freedom <= 4, but got %1%.",
  388. df, Policy());
  389. return std::numeric_limits<RealType>::quiet_NaN(); // Undefined.
  390. }
  391. if ((boost::math::isinf)(df))
  392. { // +infinity.
  393. return 3;
  394. }
  395. RealType limit = policies::get_epsilon<RealType, Policy>();
  396. // Use policies so that if policy requests lower precision,
  397. // then get the normal distribution approximation earlier.
  398. limit = static_cast<RealType>(1) / limit; // 1/eps
  399. // for 64-bit double 1/eps = 4503599627370496
  400. if (df > limit)
  401. { // Special case for really big degrees_of_freedom > 1 / eps.
  402. return 3;
  403. }
  404. else
  405. {
  406. //return 3 * (df - 2) / (df - 4); re-arranged to
  407. return 6 / (df - 4) + 3;
  408. }
  409. } // kurtosis
  410. template <class RealType, class Policy>
  411. inline RealType kurtosis_excess(const students_t_distribution<RealType, Policy>& dist)
  412. {
  413. // see http://mathworld.wolfram.com/Kurtosis.html
  414. RealType df = dist.degrees_of_freedom();
  415. if(((boost::math::isnan)(df)) || (df <= 4))
  416. { // Undefined or infinity for moment k = 4.
  417. return policies::raise_domain_error<RealType>(
  418. "boost::math::kurtosis_excess(students_t_distribution<%1%> const&, %1%)",
  419. "Kurtosis_excess is undefined for degrees of freedom <= 4, but got %1%.",
  420. df, Policy());
  421. return std::numeric_limits<RealType>::quiet_NaN(); // Undefined.
  422. }
  423. if ((boost::math::isinf)(df))
  424. { // +infinity.
  425. return 0;
  426. }
  427. RealType limit = policies::get_epsilon<RealType, Policy>();
  428. // Use policies so that if policy requests lower precision,
  429. // then get the normal distribution approximation earlier.
  430. limit = static_cast<RealType>(1) / limit; // 1/eps
  431. // for 64-bit double 1/eps = 4503599627370496
  432. if (df > limit)
  433. { // Special case for really big degrees_of_freedom > 1 / eps.
  434. return 0;
  435. }
  436. else
  437. {
  438. return 6 / (df - 4);
  439. }
  440. }
  441. template <class RealType, class Policy>
  442. inline RealType entropy(const students_t_distribution<RealType, Policy>& dist)
  443. {
  444. using std::log;
  445. using std::sqrt;
  446. RealType v = dist.degrees_of_freedom();
  447. RealType vp1 = (v+1)/2;
  448. RealType vd2 = v/2;
  449. return vp1*(digamma(vp1) - digamma(vd2)) + log(sqrt(v)*beta(vd2, RealType(1)/RealType(2)));
  450. }
  451. } // namespace math
  452. } // namespace boost
  453. #ifdef BOOST_MSVC
  454. # pragma warning(pop)
  455. #endif
  456. // This include must be at the end, *after* the accessors
  457. // for this distribution have been defined, in order to
  458. // keep compilers that support two-phase lookup happy.
  459. #include <boost/math/distributions/detail/derived_accessors.hpp>
  460. #endif // BOOST_STATS_STUDENTS_T_HPP