chi_squared.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // Copyright John Maddock 2006, 2007.
  2. // Copyright Paul A. Bristow 2008, 2010.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP
  8. #define BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP
  9. #include <boost/math/distributions/fwd.hpp>
  10. #include <boost/math/special_functions/gamma.hpp> // for incomplete beta.
  11. #include <boost/math/distributions/complement.hpp> // complements
  12. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
  13. #include <boost/math/special_functions/fpclassify.hpp>
  14. #include <utility>
  15. namespace boost{ namespace math{
  16. template <class RealType = double, class Policy = policies::policy<> >
  17. class chi_squared_distribution
  18. {
  19. public:
  20. typedef RealType value_type;
  21. typedef Policy policy_type;
  22. chi_squared_distribution(RealType i) : m_df(i)
  23. {
  24. RealType result;
  25. detail::check_df(
  26. "boost::math::chi_squared_distribution<%1%>::chi_squared_distribution", m_df, &result, Policy());
  27. } // chi_squared_distribution
  28. RealType degrees_of_freedom()const
  29. {
  30. return m_df;
  31. }
  32. // Parameter estimation:
  33. static RealType find_degrees_of_freedom(
  34. RealType difference_from_variance,
  35. RealType alpha,
  36. RealType beta,
  37. RealType variance,
  38. RealType hint = 100);
  39. private:
  40. //
  41. // Data member:
  42. //
  43. RealType m_df; // degrees of freedom is a positive real number.
  44. }; // class chi_squared_distribution
  45. typedef chi_squared_distribution<double> chi_squared;
  46. #ifdef BOOST_MSVC
  47. #pragma warning(push)
  48. #pragma warning(disable:4127)
  49. #endif
  50. template <class RealType, class Policy>
  51. inline const std::pair<RealType, RealType> range(const chi_squared_distribution<RealType, Policy>& /*dist*/)
  52. { // Range of permissible values for random variable x.
  53. if (std::numeric_limits<RealType>::has_infinity)
  54. {
  55. return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()); // 0 to + infinity.
  56. }
  57. else
  58. {
  59. using boost::math::tools::max_value;
  60. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max.
  61. }
  62. }
  63. #ifdef BOOST_MSVC
  64. #pragma warning(pop)
  65. #endif
  66. template <class RealType, class Policy>
  67. inline const std::pair<RealType, RealType> support(const chi_squared_distribution<RealType, Policy>& /*dist*/)
  68. { // Range of supported values for random variable x.
  69. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  70. return std::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>()); // 0 to + infinity.
  71. }
  72. template <class RealType, class Policy>
  73. RealType pdf(const chi_squared_distribution<RealType, Policy>& dist, const RealType& chi_square)
  74. {
  75. BOOST_MATH_STD_USING // for ADL of std functions
  76. RealType degrees_of_freedom = dist.degrees_of_freedom();
  77. // Error check:
  78. RealType error_result;
  79. static const char* function = "boost::math::pdf(const chi_squared_distribution<%1%>&, %1%)";
  80. if(false == detail::check_df(
  81. function, degrees_of_freedom, &error_result, Policy()))
  82. return error_result;
  83. if((chi_square < 0) || !(boost::math::isfinite)(chi_square))
  84. {
  85. return policies::raise_domain_error<RealType>(
  86. function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy());
  87. }
  88. if(chi_square == 0)
  89. {
  90. // Handle special cases:
  91. if(degrees_of_freedom < 2)
  92. {
  93. return policies::raise_overflow_error<RealType>(
  94. function, 0, Policy());
  95. }
  96. else if(degrees_of_freedom == 2)
  97. {
  98. return 0.5f;
  99. }
  100. else
  101. {
  102. return 0;
  103. }
  104. }
  105. return gamma_p_derivative(degrees_of_freedom / 2, chi_square / 2, Policy()) / 2;
  106. } // pdf
  107. template <class RealType, class Policy>
  108. inline RealType cdf(const chi_squared_distribution<RealType, Policy>& dist, const RealType& chi_square)
  109. {
  110. RealType degrees_of_freedom = dist.degrees_of_freedom();
  111. // Error check:
  112. RealType error_result;
  113. static const char* function = "boost::math::cdf(const chi_squared_distribution<%1%>&, %1%)";
  114. if(false == detail::check_df(
  115. function, degrees_of_freedom, &error_result, Policy()))
  116. return error_result;
  117. if((chi_square < 0) || !(boost::math::isfinite)(chi_square))
  118. {
  119. return policies::raise_domain_error<RealType>(
  120. function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy());
  121. }
  122. return boost::math::gamma_p(degrees_of_freedom / 2, chi_square / 2, Policy());
  123. } // cdf
  124. template <class RealType, class Policy>
  125. inline RealType quantile(const chi_squared_distribution<RealType, Policy>& dist, const RealType& p)
  126. {
  127. RealType degrees_of_freedom = dist.degrees_of_freedom();
  128. static const char* function = "boost::math::quantile(const chi_squared_distribution<%1%>&, %1%)";
  129. // Error check:
  130. RealType error_result;
  131. if(false ==
  132. (
  133. detail::check_df(function, degrees_of_freedom, &error_result, Policy())
  134. && detail::check_probability(function, p, &error_result, Policy()))
  135. )
  136. return error_result;
  137. return 2 * boost::math::gamma_p_inv(degrees_of_freedom / 2, p, Policy());
  138. } // quantile
  139. template <class RealType, class Policy>
  140. inline RealType cdf(const complemented2_type<chi_squared_distribution<RealType, Policy>, RealType>& c)
  141. {
  142. RealType const& degrees_of_freedom = c.dist.degrees_of_freedom();
  143. RealType const& chi_square = c.param;
  144. static const char* function = "boost::math::cdf(const chi_squared_distribution<%1%>&, %1%)";
  145. // Error check:
  146. RealType error_result;
  147. if(false == detail::check_df(
  148. function, degrees_of_freedom, &error_result, Policy()))
  149. return error_result;
  150. if((chi_square < 0) || !(boost::math::isfinite)(chi_square))
  151. {
  152. return policies::raise_domain_error<RealType>(
  153. function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy());
  154. }
  155. return boost::math::gamma_q(degrees_of_freedom / 2, chi_square / 2, Policy());
  156. }
  157. template <class RealType, class Policy>
  158. inline RealType quantile(const complemented2_type<chi_squared_distribution<RealType, Policy>, RealType>& c)
  159. {
  160. RealType const& degrees_of_freedom = c.dist.degrees_of_freedom();
  161. RealType const& q = c.param;
  162. static const char* function = "boost::math::quantile(const chi_squared_distribution<%1%>&, %1%)";
  163. // Error check:
  164. RealType error_result;
  165. if(false == (
  166. detail::check_df(function, degrees_of_freedom, &error_result, Policy())
  167. && detail::check_probability(function, q, &error_result, Policy()))
  168. )
  169. return error_result;
  170. return 2 * boost::math::gamma_q_inv(degrees_of_freedom / 2, q, Policy());
  171. }
  172. template <class RealType, class Policy>
  173. inline RealType mean(const chi_squared_distribution<RealType, Policy>& dist)
  174. { // Mean of Chi-Squared distribution = v.
  175. return dist.degrees_of_freedom();
  176. } // mean
  177. template <class RealType, class Policy>
  178. inline RealType variance(const chi_squared_distribution<RealType, Policy>& dist)
  179. { // Variance of Chi-Squared distribution = 2v.
  180. return 2 * dist.degrees_of_freedom();
  181. } // variance
  182. template <class RealType, class Policy>
  183. inline RealType mode(const chi_squared_distribution<RealType, Policy>& dist)
  184. {
  185. RealType df = dist.degrees_of_freedom();
  186. static const char* function = "boost::math::mode(const chi_squared_distribution<%1%>&)";
  187. // Most sources only define mode for df >= 2,
  188. // but for 0 <= df <= 2, the pdf maximum actually occurs at random variate = 0;
  189. // So one could extend the definition of mode thus:
  190. //if(df < 0)
  191. //{
  192. // return policies::raise_domain_error<RealType>(
  193. // function,
  194. // "Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.",
  195. // df, Policy());
  196. //}
  197. //return (df <= 2) ? 0 : df - 2;
  198. if(df < 2)
  199. return policies::raise_domain_error<RealType>(
  200. function,
  201. "Chi-Squared distribution only has a mode for degrees of freedom >= 2, but got degrees of freedom = %1%.",
  202. df, Policy());
  203. return df - 2;
  204. }
  205. //template <class RealType, class Policy>
  206. //inline RealType median(const chi_squared_distribution<RealType, Policy>& dist)
  207. //{ // Median is given by Quantile[dist, 1/2]
  208. // RealType df = dist.degrees_of_freedom();
  209. // if(df <= 1)
  210. // return tools::domain_error<RealType>(
  211. // BOOST_CURRENT_FUNCTION,
  212. // "The Chi-Squared distribution only has a mode for degrees of freedom >= 2, but got degrees of freedom = %1%.",
  213. // df);
  214. // return df - RealType(2)/3;
  215. //}
  216. // Now implemented via quantile(half) in derived accessors.
  217. template <class RealType, class Policy>
  218. inline RealType skewness(const chi_squared_distribution<RealType, Policy>& dist)
  219. {
  220. BOOST_MATH_STD_USING // For ADL
  221. RealType df = dist.degrees_of_freedom();
  222. return sqrt (8 / df); // == 2 * sqrt(2 / df);
  223. }
  224. template <class RealType, class Policy>
  225. inline RealType kurtosis(const chi_squared_distribution<RealType, Policy>& dist)
  226. {
  227. RealType df = dist.degrees_of_freedom();
  228. return 3 + 12 / df;
  229. }
  230. template <class RealType, class Policy>
  231. inline RealType kurtosis_excess(const chi_squared_distribution<RealType, Policy>& dist)
  232. {
  233. RealType df = dist.degrees_of_freedom();
  234. return 12 / df;
  235. }
  236. //
  237. // Parameter estimation comes last:
  238. //
  239. namespace detail
  240. {
  241. template <class RealType, class Policy>
  242. struct df_estimator
  243. {
  244. df_estimator(RealType a, RealType b, RealType variance, RealType delta)
  245. : alpha(a), beta(b), ratio(delta/variance)
  246. { // Constructor
  247. }
  248. RealType operator()(const RealType& df)
  249. {
  250. if(df <= tools::min_value<RealType>())
  251. return 1;
  252. chi_squared_distribution<RealType, Policy> cs(df);
  253. RealType result;
  254. if(ratio > 0)
  255. {
  256. RealType r = 1 + ratio;
  257. result = cdf(cs, quantile(complement(cs, alpha)) / r) - beta;
  258. }
  259. else
  260. { // ratio <= 0
  261. RealType r = 1 + ratio;
  262. result = cdf(complement(cs, quantile(cs, alpha) / r)) - beta;
  263. }
  264. return result;
  265. }
  266. private:
  267. RealType alpha;
  268. RealType beta;
  269. RealType ratio; // Difference from variance / variance, so fractional.
  270. };
  271. } // namespace detail
  272. template <class RealType, class Policy>
  273. RealType chi_squared_distribution<RealType, Policy>::find_degrees_of_freedom(
  274. RealType difference_from_variance,
  275. RealType alpha,
  276. RealType beta,
  277. RealType variance,
  278. RealType hint)
  279. {
  280. static const char* function = "boost::math::chi_squared_distribution<%1%>::find_degrees_of_freedom(%1%,%1%,%1%,%1%,%1%)";
  281. // Check for domain errors:
  282. RealType error_result;
  283. if(false ==
  284. detail::check_probability(function, alpha, &error_result, Policy())
  285. && detail::check_probability(function, beta, &error_result, Policy()))
  286. { // Either probability is outside 0 to 1.
  287. return error_result;
  288. }
  289. if(hint <= 0)
  290. { // No hint given, so guess df = 1.
  291. hint = 1;
  292. }
  293. detail::df_estimator<RealType, Policy> f(alpha, beta, variance, difference_from_variance);
  294. tools::eps_tolerance<RealType> tol(policies::digits<RealType, Policy>());
  295. boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  296. std::pair<RealType, RealType> r =
  297. tools::bracket_and_solve_root(f, hint, RealType(2), false, tol, max_iter, Policy());
  298. RealType result = r.first + (r.second - r.first) / 2;
  299. if(max_iter >= policies::get_max_root_iterations<Policy>())
  300. {
  301. policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
  302. " either there is no answer to how many degrees of freedom are required"
  303. " or the answer is infinite. Current best guess is %1%", result, Policy());
  304. }
  305. return result;
  306. }
  307. } // namespace math
  308. } // namespace boost
  309. // This include must be at the end, *after* the accessors
  310. // for this distribution have been defined, in order to
  311. // keep compilers that support two-phase lookup happy.
  312. #include <boost/math/distributions/detail/derived_accessors.hpp>
  313. #endif // BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP