348 lines · plain
1// Copyright John Maddock 2006, 2007.2// Copyright Paul A. Bristow 2008, 2010.3 4// Use, modification and distribution are subject to the5// Boost Software License, Version 1.0.6// (See accompanying file LICENSE_1_0.txt7// or copy at http://www.boost.org/LICENSE_1_0.txt)8 9#ifndef BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP10#define BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP11 12#include <boost/math/tools/config.hpp>13#include <boost/math/tools/type_traits.hpp>14#include <boost/math/tools/numeric_limits.hpp>15#include <boost/math/tools/cstdint.hpp>16#include <boost/math/tools/toms748_solve.hpp>17#include <boost/math/distributions/fwd.hpp>18#include <boost/math/special_functions/gamma.hpp> // for incomplete beta.19#include <boost/math/distributions/complement.hpp> // complements20#include <boost/math/distributions/detail/common_error_handling.hpp> // error checks21#include <boost/math/special_functions/fpclassify.hpp>22 23namespace boost{ namespace math{24 25template <class RealType = double, class Policy = policies::policy<> >26class chi_squared_distribution27{28public:29 using value_type = RealType;30 using policy_type = Policy;31 32 BOOST_MATH_GPU_ENABLED explicit chi_squared_distribution(RealType i) : m_df(i)33 {34 RealType result;35 detail::check_df(36 "boost::math::chi_squared_distribution<%1%>::chi_squared_distribution", m_df, &result, Policy());37 } // chi_squared_distribution38 39 BOOST_MATH_GPU_ENABLED RealType degrees_of_freedom()const40 {41 return m_df;42 }43 44 // Parameter estimation:45 BOOST_MATH_GPU_ENABLED static RealType find_degrees_of_freedom(46 RealType difference_from_variance,47 RealType alpha,48 RealType beta,49 RealType variance,50 RealType hint = 100);51 52private:53 //54 // Data member:55 //56 RealType m_df; // degrees of freedom is a positive real number.57}; // class chi_squared_distribution58 59using chi_squared = chi_squared_distribution<double>;60 61#ifdef __cpp_deduction_guides62template <class RealType>63chi_squared_distribution(RealType)->chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;64#endif65 66#ifdef _MSC_VER67#pragma warning(push)68#pragma warning(disable:4127)69#endif70 71template <class RealType, class Policy>72BOOST_MATH_GPU_ENABLED inline boost::math::pair<RealType, RealType> range(const chi_squared_distribution<RealType, Policy>& /*dist*/)73{ // Range of permissible values for random variable x.74 BOOST_MATH_IF_CONSTEXPR (boost::math::numeric_limits<RealType>::has_infinity)75 { 76 return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), boost::math::numeric_limits<RealType>::infinity()); // 0 to + infinity.77 }78 else79 {80 using boost::math::tools::max_value;81 return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max.82 }83}84 85#ifdef _MSC_VER86#pragma warning(pop)87#endif88 89template <class RealType, class Policy>90BOOST_MATH_GPU_ENABLED inline boost::math::pair<RealType, RealType> support(const chi_squared_distribution<RealType, Policy>& /*dist*/)91{ // Range of supported values for random variable x.92 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.93 return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>()); // 0 to + infinity.94}95 96template <class RealType, class Policy>97BOOST_MATH_GPU_ENABLED RealType pdf(const chi_squared_distribution<RealType, Policy>& dist, const RealType& chi_square)98{99 BOOST_MATH_STD_USING // for ADL of std functions100 RealType degrees_of_freedom = dist.degrees_of_freedom();101 // Error check:102 RealType error_result;103 104 constexpr auto function = "boost::math::pdf(const chi_squared_distribution<%1%>&, %1%)";105 106 if(false == detail::check_df(107 function, degrees_of_freedom, &error_result, Policy()))108 return error_result;109 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 116 if(chi_square == 0)117 {118 // Handle special cases:119 if(degrees_of_freedom < 2)120 {121 return policies::raise_overflow_error<RealType>(122 function, 0, Policy());123 }124 else if(degrees_of_freedom == 2)125 {126 return 0.5f;127 }128 else129 {130 return 0;131 }132 }133 134 return gamma_p_derivative(degrees_of_freedom / 2, chi_square / 2, Policy()) / 2;135} // pdf136 137template <class RealType, class Policy>138BOOST_MATH_GPU_ENABLED inline RealType cdf(const chi_squared_distribution<RealType, Policy>& dist, const RealType& chi_square)139{140 RealType degrees_of_freedom = dist.degrees_of_freedom();141 // Error check:142 RealType error_result;143 constexpr auto function = "boost::math::cdf(const chi_squared_distribution<%1%>&, %1%)";144 145 if(false == detail::check_df(146 function, degrees_of_freedom, &error_result, Policy()))147 return error_result;148 149 if((chi_square < 0) || !(boost::math::isfinite)(chi_square))150 {151 return policies::raise_domain_error<RealType>(152 function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy());153 }154 155 return boost::math::gamma_p(degrees_of_freedom / 2, chi_square / 2, Policy());156} // cdf157 158template <class RealType, class Policy>159BOOST_MATH_GPU_ENABLED inline RealType quantile(const chi_squared_distribution<RealType, Policy>& dist, const RealType& p)160{161 RealType degrees_of_freedom = dist.degrees_of_freedom();162 constexpr auto function = "boost::math::quantile(const chi_squared_distribution<%1%>&, %1%)";163 // Error check:164 RealType error_result;165 if(false ==166 (167 detail::check_df(function, degrees_of_freedom, &error_result, Policy())168 && detail::check_probability(function, p, &error_result, Policy()))169 )170 return error_result;171 172 return 2 * boost::math::gamma_p_inv(degrees_of_freedom / 2, p, Policy());173} // quantile174 175template <class RealType, class Policy>176BOOST_MATH_GPU_ENABLED inline RealType cdf(const complemented2_type<chi_squared_distribution<RealType, Policy>, RealType>& c)177{178 RealType const& degrees_of_freedom = c.dist.degrees_of_freedom();179 RealType const& chi_square = c.param;180 constexpr auto function = "boost::math::cdf(const chi_squared_distribution<%1%>&, %1%)";181 // Error check:182 RealType error_result;183 if(false == detail::check_df(184 function, degrees_of_freedom, &error_result, Policy()))185 return error_result;186 187 if((chi_square < 0) || !(boost::math::isfinite)(chi_square))188 {189 return policies::raise_domain_error<RealType>(190 function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy());191 }192 193 return boost::math::gamma_q(degrees_of_freedom / 2, chi_square / 2, Policy());194}195 196template <class RealType, class Policy>197BOOST_MATH_GPU_ENABLED inline RealType quantile(const complemented2_type<chi_squared_distribution<RealType, Policy>, RealType>& c)198{199 RealType const& degrees_of_freedom = c.dist.degrees_of_freedom();200 RealType const& q = c.param;201 constexpr auto function = "boost::math::quantile(const chi_squared_distribution<%1%>&, %1%)";202 // Error check:203 RealType error_result;204 if(false == (205 detail::check_df(function, degrees_of_freedom, &error_result, Policy())206 && detail::check_probability(function, q, &error_result, Policy()))207 )208 return error_result;209 210 return 2 * boost::math::gamma_q_inv(degrees_of_freedom / 2, q, Policy());211}212 213template <class RealType, class Policy>214BOOST_MATH_GPU_ENABLED inline RealType mean(const chi_squared_distribution<RealType, Policy>& dist)215{ // Mean of Chi-Squared distribution = v.216 return dist.degrees_of_freedom();217} // mean218 219template <class RealType, class Policy>220BOOST_MATH_GPU_ENABLED inline RealType variance(const chi_squared_distribution<RealType, Policy>& dist)221{ // Variance of Chi-Squared distribution = 2v.222 return 2 * dist.degrees_of_freedom();223} // variance224 225template <class RealType, class Policy>226BOOST_MATH_GPU_ENABLED inline RealType mode(const chi_squared_distribution<RealType, Policy>& dist)227{228 RealType df = dist.degrees_of_freedom();229 constexpr auto function = "boost::math::mode(const chi_squared_distribution<%1%>&)";230 231 if(df < 2)232 return policies::raise_domain_error<RealType>(233 function,234 "Chi-Squared distribution only has a mode for degrees of freedom >= 2, but got degrees of freedom = %1%.",235 df, Policy());236 return df - 2;237}238 239template <class RealType, class Policy>240BOOST_MATH_GPU_ENABLED inline RealType skewness(const chi_squared_distribution<RealType, Policy>& dist)241{242 BOOST_MATH_STD_USING // For ADL243 RealType df = dist.degrees_of_freedom();244 return sqrt (8 / df);245}246 247template <class RealType, class Policy>248BOOST_MATH_GPU_ENABLED inline RealType kurtosis(const chi_squared_distribution<RealType, Policy>& dist)249{250 RealType df = dist.degrees_of_freedom();251 return 3 + 12 / df;252}253 254template <class RealType, class Policy>255BOOST_MATH_GPU_ENABLED inline RealType kurtosis_excess(const chi_squared_distribution<RealType, Policy>& dist)256{257 RealType df = dist.degrees_of_freedom();258 return 12 / df;259}260 261//262// Parameter estimation comes last:263//264namespace detail265{266 267template <class RealType, class Policy>268struct df_estimator269{270 BOOST_MATH_GPU_ENABLED df_estimator(RealType a, RealType b, RealType variance, RealType delta)271 : alpha(a), beta(b), ratio(delta/variance)272 { // Constructor273 }274 275 BOOST_MATH_GPU_ENABLED RealType operator()(const RealType& df)276 {277 if(df <= tools::min_value<RealType>())278 return 1;279 chi_squared_distribution<RealType, Policy> cs(df);280 281 RealType result;282 if(ratio > 0)283 {284 RealType r = 1 + ratio;285 result = cdf(cs, quantile(complement(cs, alpha)) / r) - beta;286 }287 else288 { // ratio <= 0289 RealType r = 1 + ratio;290 result = cdf(complement(cs, quantile(cs, alpha) / r)) - beta;291 }292 return result;293 }294private:295 RealType alpha;296 RealType beta;297 RealType ratio; // Difference from variance / variance, so fractional.298};299 300} // namespace detail301 302template <class RealType, class Policy>303BOOST_MATH_GPU_ENABLED RealType chi_squared_distribution<RealType, Policy>::find_degrees_of_freedom(304 RealType difference_from_variance,305 RealType alpha,306 RealType beta,307 RealType variance,308 RealType hint)309{310 constexpr auto function = "boost::math::chi_squared_distribution<%1%>::find_degrees_of_freedom(%1%,%1%,%1%,%1%,%1%)";311 // Check for domain errors:312 RealType error_result;313 if(false ==314 detail::check_probability(function, alpha, &error_result, Policy())315 && detail::check_probability(function, beta, &error_result, Policy()))316 { // Either probability is outside 0 to 1.317 return error_result;318 }319 320 if(hint <= 0)321 { // No hint given, so guess df = 1.322 hint = 1;323 }324 325 detail::df_estimator<RealType, Policy> f(alpha, beta, variance, difference_from_variance);326 tools::eps_tolerance<RealType> tol(policies::digits<RealType, Policy>());327 boost::math::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();328 boost::math::pair<RealType, RealType> r =329 tools::bracket_and_solve_root(f, hint, RealType(2), false, tol, max_iter, Policy());330 RealType result = r.first + (r.second - r.first) / 2;331 if(max_iter >= policies::get_max_root_iterations<Policy>())332 {333 policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:" // LCOV_EXCL_LINE334 " 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_LINE335 }336 return result;337}338 339} // namespace math340} // namespace boost341 342// This include must be at the end, *after* the accessors343// for this distribution have been defined, in order to344// keep compilers that support two-phase lookup happy.345#include <boost/math/distributions/detail/derived_accessors.hpp>346 347#endif // BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP348