Chris@16: // Copyright John Maddock 2006, 2007. Chris@16: // Copyright Paul A. Bristow 2008, 2010. Chris@16: Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt Chris@16: // or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP Chris@16: #define BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP Chris@16: Chris@16: #include Chris@16: #include // for incomplete beta. Chris@16: #include // complements Chris@16: #include // error checks Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: Chris@16: template > Chris@16: class chi_squared_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType value_type; Chris@16: typedef Policy policy_type; Chris@16: Chris@16: chi_squared_distribution(RealType i) : m_df(i) Chris@16: { Chris@16: RealType result; Chris@16: detail::check_df( Chris@16: "boost::math::chi_squared_distribution<%1%>::chi_squared_distribution", m_df, &result, Policy()); Chris@16: } // chi_squared_distribution Chris@16: Chris@16: RealType degrees_of_freedom()const Chris@16: { Chris@16: return m_df; Chris@16: } Chris@16: Chris@16: // Parameter estimation: Chris@16: static RealType find_degrees_of_freedom( Chris@16: RealType difference_from_variance, Chris@16: RealType alpha, Chris@16: RealType beta, Chris@16: RealType variance, Chris@16: RealType hint = 100); Chris@16: Chris@16: private: Chris@16: // Chris@16: // Data member: Chris@16: // Chris@16: RealType m_df; // degrees of freedom is a positive real number. Chris@16: }; // class chi_squared_distribution Chris@16: Chris@16: typedef chi_squared_distribution chi_squared; Chris@16: Chris@101: #ifdef BOOST_MSVC Chris@101: #pragma warning(push) Chris@101: #pragma warning(disable:4127) Chris@101: #endif Chris@101: Chris@16: template Chris@16: inline const std::pair range(const chi_squared_distribution& /*dist*/) Chris@16: { // Range of permissible values for random variable x. Chris@16: if (std::numeric_limits::has_infinity) Chris@16: { Chris@16: return std::pair(static_cast(0), std::numeric_limits::infinity()); // 0 to + infinity. Chris@16: } Chris@16: else Chris@16: { Chris@16: using boost::math::tools::max_value; Chris@16: return std::pair(static_cast(0), max_value()); // 0 to + max. Chris@16: } Chris@16: } Chris@16: Chris@101: #ifdef BOOST_MSVC Chris@101: #pragma warning(pop) Chris@101: #endif Chris@101: Chris@16: template Chris@16: inline const std::pair support(const chi_squared_distribution& /*dist*/) Chris@16: { // Range of supported values for random variable x. Chris@16: // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero. Chris@16: return std::pair(static_cast(0), tools::max_value()); // 0 to + infinity. Chris@16: } Chris@16: Chris@16: template Chris@16: RealType pdf(const chi_squared_distribution& dist, const RealType& chi_square) Chris@16: { Chris@16: BOOST_MATH_STD_USING // for ADL of std functions Chris@16: RealType degrees_of_freedom = dist.degrees_of_freedom(); Chris@16: // Error check: Chris@16: RealType error_result; Chris@16: Chris@16: static const char* function = "boost::math::pdf(const chi_squared_distribution<%1%>&, %1%)"; Chris@16: Chris@16: if(false == detail::check_df( Chris@16: function, degrees_of_freedom, &error_result, Policy())) Chris@16: return error_result; Chris@16: Chris@16: if((chi_square < 0) || !(boost::math::isfinite)(chi_square)) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy()); Chris@16: } Chris@16: Chris@16: if(chi_square == 0) Chris@16: { Chris@16: // Handle special cases: Chris@16: if(degrees_of_freedom < 2) Chris@16: { Chris@16: return policies::raise_overflow_error( Chris@16: function, 0, Policy()); Chris@16: } Chris@16: else if(degrees_of_freedom == 2) Chris@16: { Chris@16: return 0.5f; Chris@16: } Chris@16: else Chris@16: { Chris@16: return 0; Chris@16: } Chris@16: } Chris@16: Chris@16: return gamma_p_derivative(degrees_of_freedom / 2, chi_square / 2, Policy()) / 2; Chris@16: } // pdf Chris@16: Chris@16: template Chris@16: inline RealType cdf(const chi_squared_distribution& dist, const RealType& chi_square) Chris@16: { Chris@16: RealType degrees_of_freedom = dist.degrees_of_freedom(); Chris@16: // Error check: Chris@16: RealType error_result; Chris@16: static const char* function = "boost::math::cdf(const chi_squared_distribution<%1%>&, %1%)"; Chris@16: Chris@16: if(false == detail::check_df( Chris@16: function, degrees_of_freedom, &error_result, Policy())) Chris@16: return error_result; Chris@16: Chris@16: if((chi_square < 0) || !(boost::math::isfinite)(chi_square)) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy()); Chris@16: } Chris@16: Chris@16: return boost::math::gamma_p(degrees_of_freedom / 2, chi_square / 2, Policy()); Chris@16: } // cdf Chris@16: Chris@16: template Chris@16: inline RealType quantile(const chi_squared_distribution& dist, const RealType& p) Chris@16: { Chris@16: RealType degrees_of_freedom = dist.degrees_of_freedom(); Chris@16: static const char* function = "boost::math::quantile(const chi_squared_distribution<%1%>&, %1%)"; Chris@16: // Error check: Chris@16: RealType error_result; Chris@16: if(false == Chris@16: ( Chris@16: detail::check_df(function, degrees_of_freedom, &error_result, Policy()) Chris@16: && detail::check_probability(function, p, &error_result, Policy())) Chris@16: ) Chris@16: return error_result; Chris@16: Chris@16: return 2 * boost::math::gamma_p_inv(degrees_of_freedom / 2, p, Policy()); Chris@16: } // quantile Chris@16: Chris@16: template Chris@16: inline RealType cdf(const complemented2_type, RealType>& c) Chris@16: { Chris@16: RealType const& degrees_of_freedom = c.dist.degrees_of_freedom(); Chris@16: RealType const& chi_square = c.param; Chris@16: static const char* function = "boost::math::cdf(const chi_squared_distribution<%1%>&, %1%)"; Chris@16: // Error check: Chris@16: RealType error_result; Chris@16: if(false == detail::check_df( Chris@16: function, degrees_of_freedom, &error_result, Policy())) Chris@16: return error_result; Chris@16: Chris@16: if((chi_square < 0) || !(boost::math::isfinite)(chi_square)) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Chi Square parameter was %1%, but must be > 0 !", chi_square, Policy()); Chris@16: } Chris@16: Chris@16: return boost::math::gamma_q(degrees_of_freedom / 2, chi_square / 2, Policy()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType quantile(const complemented2_type, RealType>& c) Chris@16: { Chris@16: RealType const& degrees_of_freedom = c.dist.degrees_of_freedom(); Chris@16: RealType const& q = c.param; Chris@16: static const char* function = "boost::math::quantile(const chi_squared_distribution<%1%>&, %1%)"; Chris@16: // Error check: Chris@16: RealType error_result; Chris@16: if(false == ( Chris@16: detail::check_df(function, degrees_of_freedom, &error_result, Policy()) Chris@16: && detail::check_probability(function, q, &error_result, Policy())) Chris@16: ) Chris@16: return error_result; Chris@16: Chris@16: return 2 * boost::math::gamma_q_inv(degrees_of_freedom / 2, q, Policy()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType mean(const chi_squared_distribution& dist) Chris@16: { // Mean of Chi-Squared distribution = v. Chris@16: return dist.degrees_of_freedom(); Chris@16: } // mean Chris@16: Chris@16: template Chris@16: inline RealType variance(const chi_squared_distribution& dist) Chris@16: { // Variance of Chi-Squared distribution = 2v. Chris@16: return 2 * dist.degrees_of_freedom(); Chris@16: } // variance Chris@16: Chris@16: template Chris@16: inline RealType mode(const chi_squared_distribution& dist) Chris@16: { Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: static const char* function = "boost::math::mode(const chi_squared_distribution<%1%>&)"; Chris@16: // Most sources only define mode for df >= 2, Chris@16: // but for 0 <= df <= 2, the pdf maximum actually occurs at random variate = 0; Chris@16: // So one could extend the definition of mode thus: Chris@16: //if(df < 0) Chris@16: //{ Chris@16: // return policies::raise_domain_error( Chris@16: // function, Chris@16: // "Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.", Chris@16: // df, Policy()); Chris@16: //} Chris@16: //return (df <= 2) ? 0 : df - 2; Chris@16: Chris@16: if(df < 2) Chris@16: return policies::raise_domain_error( Chris@16: function, Chris@16: "Chi-Squared distribution only has a mode for degrees of freedom >= 2, but got degrees of freedom = %1%.", Chris@16: df, Policy()); Chris@16: return df - 2; Chris@16: } Chris@16: Chris@16: //template Chris@16: //inline RealType median(const chi_squared_distribution& dist) Chris@16: //{ // Median is given by Quantile[dist, 1/2] Chris@16: // RealType df = dist.degrees_of_freedom(); Chris@16: // if(df <= 1) Chris@16: // return tools::domain_error( Chris@16: // BOOST_CURRENT_FUNCTION, Chris@16: // "The Chi-Squared distribution only has a mode for degrees of freedom >= 2, but got degrees of freedom = %1%.", Chris@16: // df); Chris@16: // return df - RealType(2)/3; Chris@16: //} Chris@16: // Now implemented via quantile(half) in derived accessors. Chris@16: Chris@16: template Chris@16: inline RealType skewness(const chi_squared_distribution& dist) Chris@16: { Chris@16: BOOST_MATH_STD_USING // For ADL Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: return sqrt (8 / df); // == 2 * sqrt(2 / df); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType kurtosis(const chi_squared_distribution& dist) Chris@16: { Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: return 3 + 12 / df; Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType kurtosis_excess(const chi_squared_distribution& dist) Chris@16: { Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: return 12 / df; Chris@16: } Chris@16: Chris@16: // Chris@16: // Parameter estimation comes last: Chris@16: // Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: template Chris@16: struct df_estimator Chris@16: { Chris@16: df_estimator(RealType a, RealType b, RealType variance, RealType delta) Chris@16: : alpha(a), beta(b), ratio(delta/variance) Chris@16: { // Constructor Chris@16: } Chris@16: Chris@16: RealType operator()(const RealType& df) Chris@16: { Chris@16: if(df <= tools::min_value()) Chris@16: return 1; Chris@16: chi_squared_distribution cs(df); Chris@16: Chris@16: RealType result; Chris@16: if(ratio > 0) Chris@16: { Chris@16: RealType r = 1 + ratio; Chris@16: result = cdf(cs, quantile(complement(cs, alpha)) / r) - beta; Chris@16: } Chris@16: else Chris@16: { // ratio <= 0 Chris@16: RealType r = 1 + ratio; Chris@16: result = cdf(complement(cs, quantile(cs, alpha) / r)) - beta; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: private: Chris@16: RealType alpha; Chris@16: RealType beta; Chris@16: RealType ratio; // Difference from variance / variance, so fractional. Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template Chris@16: RealType chi_squared_distribution::find_degrees_of_freedom( Chris@16: RealType difference_from_variance, Chris@16: RealType alpha, Chris@16: RealType beta, Chris@16: RealType variance, Chris@16: RealType hint) Chris@16: { Chris@16: static const char* function = "boost::math::chi_squared_distribution<%1%>::find_degrees_of_freedom(%1%,%1%,%1%,%1%,%1%)"; Chris@16: // Check for domain errors: Chris@16: RealType error_result; Chris@16: if(false == Chris@16: detail::check_probability(function, alpha, &error_result, Policy()) Chris@16: && detail::check_probability(function, beta, &error_result, Policy())) Chris@16: { // Either probability is outside 0 to 1. Chris@16: return error_result; Chris@16: } Chris@16: Chris@16: if(hint <= 0) Chris@16: { // No hint given, so guess df = 1. Chris@16: hint = 1; Chris@16: } Chris@16: Chris@16: detail::df_estimator f(alpha, beta, variance, difference_from_variance); Chris@16: tools::eps_tolerance tol(policies::digits()); Chris@16: boost::uintmax_t max_iter = policies::get_max_root_iterations(); Chris@16: std::pair r = Chris@16: tools::bracket_and_solve_root(f, hint, RealType(2), false, tol, max_iter, Policy()); Chris@16: RealType result = r.first + (r.second - r.first) / 2; Chris@16: if(max_iter >= policies::get_max_root_iterations()) Chris@16: { Chris@16: policies::raise_evaluation_error(function, "Unable to locate solution in a reasonable time:" Chris@16: " either there is no answer to how many degrees of freedom are required" Chris@16: " or the answer is infinite. Current best guess is %1%", result, Policy()); Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: // This include must be at the end, *after* the accessors Chris@16: // for this distribution have been defined, in order to Chris@16: // keep compilers that support two-phase lookup happy. Chris@16: #include Chris@16: Chris@16: #endif // BOOST_MATH_DISTRIBUTIONS_CHI_SQUARED_HPP