chi_squared.hpp 12 KB

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