Chris@16: // Copyright John Maddock 2010. Chris@16: // Copyright Paul A. Bristow 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_INVERSE_CHI_SQUARED_HPP Chris@16: #define BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP Chris@16: Chris@16: #include Chris@16: #include // for incomplete beta. Chris@16: #include // for complements. Chris@16: #include // for error checks. Chris@16: #include // for isfinite Chris@16: Chris@16: // See http://en.wikipedia.org/wiki/Scaled-inverse-chi-square_distribution Chris@16: // for definitions of this scaled version. Chris@16: // See http://en.wikipedia.org/wiki/Inverse-chi-square_distribution Chris@16: // for unscaled version. Chris@16: Chris@16: // http://reference.wolfram.com/mathematica/ref/InverseChiSquareDistribution.html Chris@16: // Weisstein, Eric W. "Inverse Chi-Squared Distribution." From MathWorld--A Wolfram Web Resource. Chris@16: // http://mathworld.wolfram.com/InverseChi-SquaredDistribution.html Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: inline bool check_inverse_chi_squared( // Check both distribution parameters. Chris@16: const char* function, Chris@16: RealType degrees_of_freedom, // degrees_of_freedom (aka nu). Chris@16: RealType scale, // scale (aka sigma^2) Chris@16: RealType* result, Chris@16: const Policy& pol) Chris@16: { Chris@16: return check_scale(function, scale, result, pol) Chris@16: && check_df(function, degrees_of_freedom, Chris@16: result, pol); Chris@16: } // bool check_inverse_chi_squared Chris@16: } // namespace detail Chris@16: Chris@16: template > Chris@16: class inverse_chi_squared_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType value_type; Chris@16: typedef Policy policy_type; Chris@16: Chris@16: inverse_chi_squared_distribution(RealType df, RealType l_scale) : m_df(df), m_scale (l_scale) Chris@16: { Chris@16: RealType result; Chris@16: detail::check_df( Chris@16: "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution", Chris@16: m_df, &result, Policy()) Chris@16: && detail::check_scale( Chris@16: "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution", Chris@16: m_scale, &result, Policy()); Chris@16: } // inverse_chi_squared_distribution constructor Chris@16: Chris@16: inverse_chi_squared_distribution(RealType df = 1) : m_df(df) Chris@16: { Chris@16: RealType result; Chris@16: m_scale = 1 / m_df ; // Default scale = 1 / degrees of freedom (Wikipedia definition 1). Chris@16: detail::check_df( Chris@16: "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution", Chris@16: m_df, &result, Policy()); Chris@16: } // inverse_chi_squared_distribution Chris@16: Chris@16: RealType degrees_of_freedom()const Chris@16: { Chris@16: return m_df; // aka nu Chris@16: } Chris@16: RealType scale()const Chris@16: { Chris@16: return m_scale; // aka xi Chris@16: } Chris@16: Chris@16: // Parameter estimation: NOT implemented yet. 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: // Data members: Chris@16: RealType m_df; // degrees of freedom are treated as a real number. Chris@16: RealType m_scale; // distribution scale. Chris@16: Chris@16: }; // class chi_squared_distribution Chris@16: Chris@16: typedef inverse_chi_squared_distribution inverse_chi_squared; Chris@16: Chris@16: template Chris@16: inline const std::pair range(const inverse_chi_squared_distribution& /*dist*/) Chris@16: { // Range of permissible values for random variable x. Chris@16: using boost::math::tools::max_value; Chris@16: return std::pair(static_cast(0), max_value()); // 0 to + infinity. Chris@16: } Chris@16: Chris@16: template Chris@16: inline const std::pair support(const inverse_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 inverse_chi_squared_distribution& dist, const RealType& x) Chris@16: { Chris@16: BOOST_MATH_STD_USING // for ADL of std functions. Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: RealType scale = dist.scale(); Chris@16: RealType error_result; Chris@16: Chris@16: static const char* function = "boost::math::pdf(const inverse_chi_squared_distribution<%1%>&, %1%)"; Chris@16: Chris@16: if(false == detail::check_inverse_chi_squared Chris@16: (function, df, scale, &error_result, Policy()) Chris@16: ) Chris@16: { // Bad distribution. Chris@16: return error_result; Chris@16: } Chris@16: if((x < 0) || !(boost::math::isfinite)(x)) Chris@16: { // Bad x. Chris@16: return policies::raise_domain_error( Chris@16: function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy()); Chris@16: } Chris@16: Chris@16: if(x == 0) Chris@16: { // Treat as special case. Chris@16: return 0; Chris@16: } Chris@16: // Wikipedia scaled inverse chi sq (df, scale) related to inv gamma (df/2, df * scale /2) Chris@16: // so use inverse gamma pdf with shape = df/2, scale df * scale /2 Chris@16: // RealType shape = df /2; // inv_gamma shape Chris@16: // RealType scale = df * scale/2; // inv_gamma scale Chris@16: // RealType result = gamma_p_derivative(shape, scale / x, Policy()) * scale / (x * x); Chris@16: RealType result = df * scale/2 / x; Chris@16: if(result < tools::min_value()) Chris@16: return 0; // Random variable is near enough infinite. Chris@16: result = gamma_p_derivative(df/2, result, Policy()) * df * scale/2; Chris@16: if(result != 0) // prevent 0 / 0, gamma_p_derivative -> 0 faster than x^2 Chris@16: result /= (x * x); Chris@16: return result; Chris@16: } // pdf Chris@16: Chris@16: template Chris@16: inline RealType cdf(const inverse_chi_squared_distribution& dist, const RealType& x) Chris@16: { Chris@16: static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)"; Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: RealType scale = dist.scale(); Chris@16: RealType error_result; Chris@16: Chris@16: if(false == Chris@16: detail::check_inverse_chi_squared(function, df, scale, &error_result, Policy()) Chris@16: ) Chris@16: { // Bad distribution. Chris@16: return error_result; Chris@16: } Chris@16: if((x < 0) || !(boost::math::isfinite)(x)) Chris@16: { // Bad x. Chris@16: return policies::raise_domain_error( Chris@16: function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy()); Chris@16: } Chris@16: if (x == 0) Chris@16: { // Treat zero as a special case. Chris@16: return 0; Chris@16: } Chris@16: // RealType shape = df /2; // inv_gamma shape, Chris@16: // RealType scale = df * scale/2; // inv_gamma scale, Chris@16: // result = boost::math::gamma_q(shape, scale / x, Policy()); // inverse_gamma code. Chris@16: return boost::math::gamma_q(df / 2, (df * (scale / 2)) / x, Policy()); Chris@16: } // cdf Chris@16: Chris@16: template Chris@16: inline RealType quantile(const inverse_chi_squared_distribution& dist, const RealType& p) Chris@16: { Chris@16: using boost::math::gamma_q_inv; Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: RealType scale = dist.scale(); Chris@16: Chris@16: static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)"; Chris@16: // Error check: Chris@16: RealType error_result; Chris@16: if(false == detail::check_df( Chris@16: function, df, &error_result, Policy()) Chris@16: && detail::check_probability( Chris@16: function, p, &error_result, Policy())) Chris@16: { Chris@16: return error_result; Chris@16: } Chris@16: if(false == detail::check_probability( Chris@16: function, p, &error_result, Policy())) Chris@16: { Chris@16: return error_result; Chris@16: } Chris@16: // RealType shape = df /2; // inv_gamma shape, Chris@16: // RealType scale = df * scale/2; // inv_gamma scale, Chris@16: // result = scale / gamma_q_inv(shape, p, Policy()); Chris@16: RealType result = gamma_q_inv(df /2, p, Policy()); Chris@16: if(result == 0) Chris@16: return policies::raise_overflow_error(function, "Random variable is infinite.", Policy()); Chris@16: result = df * (scale / 2) / result; Chris@16: return result; Chris@16: } // quantile Chris@16: Chris@16: template Chris@16: inline RealType cdf(const complemented2_type, RealType>& c) Chris@16: { Chris@16: using boost::math::gamma_q_inv; Chris@16: RealType const& df = c.dist.degrees_of_freedom(); Chris@16: RealType const& scale = c.dist.scale(); Chris@16: RealType const& x = c.param; Chris@16: static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)"; Chris@16: // Error check: Chris@16: RealType error_result; Chris@16: if(false == detail::check_df( Chris@16: function, df, &error_result, Policy())) Chris@16: { Chris@16: return error_result; Chris@16: } Chris@16: if (x == 0) Chris@16: { // Treat zero as a special case. Chris@16: return 1; Chris@16: } Chris@16: if((x < 0) || !(boost::math::isfinite)(x)) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "inverse Chi Square parameter was %1%, but must be > 0 !", x, Policy()); Chris@16: } Chris@16: // RealType shape = df /2; // inv_gamma shape, Chris@16: // RealType scale = df * scale/2; // inv_gamma scale, Chris@16: // result = gamma_p(shape, scale/c.param, Policy()); use inv_gamma. Chris@16: Chris@16: return gamma_p(df / 2, (df * scale/2) / x, Policy()); // OK Chris@16: } // cdf(complemented Chris@16: Chris@16: template Chris@16: inline RealType quantile(const complemented2_type, RealType>& c) Chris@16: { Chris@16: using boost::math::gamma_q_inv; Chris@16: Chris@16: RealType const& df = c.dist.degrees_of_freedom(); Chris@16: RealType const& scale = c.dist.scale(); Chris@16: RealType const& q = c.param; Chris@16: static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)"; Chris@16: // Error check: Chris@16: RealType error_result; Chris@16: if(false == detail::check_df(function, df, &error_result, Policy())) Chris@16: { Chris@16: return error_result; Chris@16: } Chris@16: if(false == detail::check_probability(function, q, &error_result, Policy())) Chris@16: { Chris@16: return error_result; Chris@16: } Chris@16: // RealType shape = df /2; // inv_gamma shape, Chris@16: // RealType scale = df * scale/2; // inv_gamma scale, Chris@16: // result = scale / gamma_p_inv(shape, q, Policy()); // using inv_gamma. Chris@16: RealType result = gamma_p_inv(df/2, q, Policy()); Chris@16: if(result == 0) Chris@16: return policies::raise_overflow_error(function, "Random variable is infinite.", Policy()); Chris@16: result = (df * scale / 2) / result; Chris@16: return result; Chris@16: } // quantile(const complement Chris@16: Chris@16: template Chris@16: inline RealType mean(const inverse_chi_squared_distribution& dist) Chris@16: { // Mean of inverse Chi-Squared distribution. Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: RealType scale = dist.scale(); Chris@16: Chris@16: static const char* function = "boost::math::mean(const inverse_chi_squared_distribution<%1%>&)"; Chris@16: if(df <= 2) Chris@16: return policies::raise_domain_error( Chris@16: function, Chris@16: "inverse 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 * scale) / (df - 2); Chris@16: } // mean Chris@16: Chris@16: template Chris@16: inline RealType variance(const inverse_chi_squared_distribution& dist) Chris@16: { // Variance of inverse Chi-Squared distribution. Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: RealType scale = dist.scale(); Chris@16: static const char* function = "boost::math::variance(const inverse_chi_squared_distribution<%1%>&)"; Chris@16: if(df <= 4) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, Chris@16: "inverse Chi-Squared distribution only has a variance for degrees of freedom > 4, but got degrees of freedom = %1%.", Chris@16: df, Policy()); Chris@16: } Chris@16: return 2 * df * df * scale * scale / ((df - 2)*(df - 2) * (df - 4)); Chris@16: } // variance Chris@16: Chris@16: template Chris@16: inline RealType mode(const inverse_chi_squared_distribution& dist) Chris@16: { // mode is not defined in Mathematica. Chris@16: // See Discussion section http://en.wikipedia.org/wiki/Talk:Scaled-inverse-chi-square_distribution Chris@16: // for origin of the formula used below. Chris@16: Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: RealType scale = dist.scale(); Chris@16: static const char* function = "boost::math::mode(const inverse_chi_squared_distribution<%1%>&)"; Chris@16: if(df < 0) Chris@16: return policies::raise_domain_error( Chris@16: function, Chris@16: "inverse Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.", Chris@16: df, Policy()); Chris@16: return (df * scale) / (df + 2); Chris@16: } Chris@16: Chris@16: //template Chris@16: //inline RealType median(const inverse_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 inverse_Chi-Squared distribution only has a median for degrees of freedom >= 0, but got degrees of freedom = %1%.", Chris@16: // df); Chris@16: // return df; Chris@16: //} Chris@16: // Now implemented via quantile(half) in derived accessors. Chris@16: Chris@16: template Chris@16: inline RealType skewness(const inverse_chi_squared_distribution& dist) Chris@16: { Chris@16: BOOST_MATH_STD_USING // For ADL Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: static const char* function = "boost::math::skewness(const inverse_chi_squared_distribution<%1%>&)"; Chris@16: if(df <= 6) Chris@16: return policies::raise_domain_error( Chris@16: function, Chris@16: "inverse Chi-Squared distribution only has a skewness for degrees of freedom > 6, but got degrees of freedom = %1%.", Chris@16: df, Policy()); Chris@16: Chris@16: return 4 * sqrt (2 * (df - 4)) / (df - 6); // Not a function of scale. Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType kurtosis(const inverse_chi_squared_distribution& dist) Chris@16: { Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)"; Chris@16: if(df <= 8) Chris@16: return policies::raise_domain_error( Chris@16: function, Chris@16: "inverse Chi-Squared distribution only has a kurtosis for degrees of freedom > 8, but got degrees of freedom = %1%.", Chris@16: df, Policy()); Chris@16: Chris@16: return kurtosis_excess(dist) + 3; Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType kurtosis_excess(const inverse_chi_squared_distribution& dist) Chris@16: { Chris@16: RealType df = dist.degrees_of_freedom(); Chris@16: static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)"; Chris@16: if(df <= 8) Chris@16: return policies::raise_domain_error( Chris@16: function, Chris@16: "inverse Chi-Squared distribution only has a kurtosis excess for degrees of freedom > 8, but got degrees of freedom = %1%.", Chris@16: df, Policy()); Chris@16: Chris@16: return 12 * (5 * df - 22) / ((df - 6 )*(df - 8)); // Not a function of scale. Chris@16: } Chris@16: Chris@16: // Chris@16: // Parameter estimation comes last: 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_INVERSE_CHI_SQUARED_HPP