students_t.hpp 19 KB

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