chi_squared.hpp 12 KB

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