students_t.hpp 18 KB

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