chi_squared.hpp 11 KB

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