Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:41

0001 // Copyright John Maddock 2010.
0002 // Copyright Paul A. Bristow 2010.
0003 
0004 // Use, modification and distribution are subject to the
0005 // Boost Software License, Version 1.0.
0006 // (See accompanying file LICENSE_1_0.txt
0007 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 #ifndef BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
0010 #define BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
0011 
0012 #include <boost/math/distributions/fwd.hpp>
0013 #include <boost/math/special_functions/gamma.hpp> // for incomplete beta.
0014 #include <boost/math/distributions/complement.hpp> // for complements.
0015 #include <boost/math/distributions/detail/common_error_handling.hpp> // for error checks.
0016 #include <boost/math/special_functions/fpclassify.hpp> // for isfinite
0017 
0018 // See http://en.wikipedia.org/wiki/Scaled-inverse-chi-square_distribution
0019 // for definitions of this scaled version.
0020 // See http://en.wikipedia.org/wiki/Inverse-chi-square_distribution
0021 // for unscaled version.
0022 
0023 // http://reference.wolfram.com/mathematica/ref/InverseChiSquareDistribution.html
0024 // Weisstein, Eric W. "Inverse Chi-Squared Distribution." From MathWorld--A Wolfram Web Resource.
0025 // http://mathworld.wolfram.com/InverseChi-SquaredDistribution.html
0026 
0027 #include <utility>
0028 
0029 namespace boost{ namespace math{
0030 
0031 namespace detail
0032 {
0033   template <class RealType, class Policy>
0034   inline bool check_inverse_chi_squared( // Check both distribution parameters.
0035         const char* function,
0036         RealType degrees_of_freedom, // degrees_of_freedom (aka nu).
0037         RealType scale,  // scale (aka sigma^2)
0038         RealType* result,
0039         const Policy& pol)
0040   {
0041      return check_scale(function, scale, result, pol)
0042        && check_df(function, degrees_of_freedom,
0043        result, pol);
0044   } // bool check_inverse_chi_squared
0045 } // namespace detail
0046 
0047 template <class RealType = double, class Policy = policies::policy<> >
0048 class inverse_chi_squared_distribution
0049 {
0050 public:
0051    typedef RealType value_type;
0052    typedef Policy policy_type;
0053 
0054    inverse_chi_squared_distribution(RealType df, RealType l_scale) : m_df(df), m_scale (l_scale)
0055    {
0056       RealType result;
0057       detail::check_df(
0058          "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
0059          m_df, &result, Policy())
0060          && detail::check_scale(
0061 "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
0062          m_scale, &result,  Policy());
0063    } // inverse_chi_squared_distribution constructor 
0064 
0065    inverse_chi_squared_distribution(RealType df = 1) : m_df(df)
0066    {
0067       RealType result;
0068       m_scale = 1 / m_df ; // Default scale = 1 / degrees of freedom (Wikipedia definition 1).
0069       detail::check_df(
0070          "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
0071          m_df, &result, Policy());
0072    } // inverse_chi_squared_distribution
0073 
0074    RealType degrees_of_freedom()const
0075    {
0076       return m_df; // aka nu
0077    }
0078    RealType scale()const
0079    {
0080       return m_scale;  // aka xi
0081    }
0082 
0083    // Parameter estimation:  NOT implemented yet.
0084    //static RealType find_degrees_of_freedom(
0085    //   RealType difference_from_variance,
0086    //   RealType alpha,
0087    //   RealType beta,
0088    //   RealType variance,
0089    //   RealType hint = 100);
0090 
0091 private:
0092    // Data members:
0093    RealType m_df;  // degrees of freedom are treated as a real number.
0094    RealType m_scale;  // distribution scale.
0095 
0096 }; // class chi_squared_distribution
0097 
0098 typedef inverse_chi_squared_distribution<double> inverse_chi_squared;
0099 
0100 #ifdef __cpp_deduction_guides
0101 template <class RealType>
0102 inverse_chi_squared_distribution(RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
0103 template <class RealType>
0104 inverse_chi_squared_distribution(RealType,RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
0105 #endif
0106 
0107 template <class RealType, class Policy>
0108 inline const std::pair<RealType, RealType> range(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
0109 {  // Range of permissible values for random variable x.
0110    using boost::math::tools::max_value;
0111    return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + infinity.
0112 }
0113 
0114 template <class RealType, class Policy>
0115 inline const std::pair<RealType, RealType> support(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
0116 {  // Range of supported values for random variable x.
0117    // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
0118    return std::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>()); // 0 to + infinity.
0119 }
0120 
0121 template <class RealType, class Policy>
0122 RealType pdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
0123 {
0124    BOOST_MATH_STD_USING  // for ADL of std functions.
0125    RealType df = dist.degrees_of_freedom();
0126    RealType scale = dist.scale();
0127    RealType error_result;
0128 
0129    static const char* function = "boost::math::pdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
0130 
0131    if(false == detail::check_inverse_chi_squared
0132      (function, df, scale, &error_result, Policy())
0133      )
0134    { // Bad distribution.
0135       return error_result;
0136    }
0137    if((x < 0) || !(boost::math::isfinite)(x))
0138    { // Bad x.
0139       return policies::raise_domain_error<RealType>(
0140          function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
0141    }
0142 
0143    if(x == 0)
0144    { // Treat as special case.
0145      return 0;
0146    }
0147    // Wikipedia scaled inverse chi sq (df, scale) related to inv gamma (df/2, df * scale /2) 
0148    // so use inverse gamma pdf with shape = df/2, scale df * scale /2 
0149    // RealType shape = df /2; // inv_gamma shape
0150    // RealType scale = df * scale/2; // inv_gamma scale
0151    // RealType result = gamma_p_derivative(shape, scale / x, Policy()) * scale / (x * x);
0152    RealType result = df * scale/2 / x;
0153    if(result < tools::min_value<RealType>())
0154       return 0; // Random variable is near enough infinite.
0155    result = gamma_p_derivative(df/2, result, Policy()) * df * scale/2;
0156    if(result != 0) // prevent 0 / 0,  gamma_p_derivative -> 0 faster than x^2
0157       result /= (x * x);
0158    return result;
0159 } // pdf
0160 
0161 template <class RealType, class Policy>
0162 inline RealType cdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
0163 {
0164    static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
0165    RealType df = dist.degrees_of_freedom();
0166    RealType scale = dist.scale();
0167    RealType error_result;
0168 
0169    if(false ==
0170        detail::check_inverse_chi_squared(function, df, scale, &error_result, Policy())
0171      )
0172    { // Bad distribution.
0173       return error_result;
0174    }
0175    if((x < 0) || !(boost::math::isfinite)(x))
0176    { // Bad x.
0177       return policies::raise_domain_error<RealType>(
0178          function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
0179    }
0180    if (x == 0)
0181    { // Treat zero as a special case.
0182      return 0;
0183    }
0184    // RealType shape = df /2; // inv_gamma shape,
0185    // RealType scale = df * scale/2; // inv_gamma scale,
0186    // result = boost::math::gamma_q(shape, scale / x, Policy()); // inverse_gamma code.
0187    return boost::math::gamma_q(df / 2, (df * (scale / 2)) / x, Policy());
0188 } // cdf
0189 
0190 template <class RealType, class Policy>
0191 inline RealType quantile(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& p)
0192 {
0193    using boost::math::gamma_q_inv;
0194    RealType df = dist.degrees_of_freedom();
0195    RealType scale = dist.scale();
0196 
0197    static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
0198    // Error check:
0199    RealType error_result;
0200    if(false == detail::check_df(
0201          function, df, &error_result, Policy())
0202          && detail::check_probability(
0203             function, p, &error_result, Policy()))
0204    {
0205       return error_result;
0206    }
0207    if(false == detail::check_probability(
0208             function, p, &error_result, Policy()))
0209    {
0210       return error_result;
0211    }
0212    // RealType shape = df /2; // inv_gamma shape,
0213    // RealType scale = df * scale/2; // inv_gamma scale,
0214    // result = scale / gamma_q_inv(shape, p, Policy());
0215       RealType result = gamma_q_inv(df /2, p, Policy());
0216       if(result == 0)
0217          return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
0218       result = df * (scale / 2) / result;
0219       return result;
0220 } // quantile
0221 
0222 template <class RealType, class Policy>
0223 inline RealType cdf(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
0224 {
0225    using boost::math::gamma_q_inv;
0226    RealType const& df = c.dist.degrees_of_freedom();
0227    RealType const& scale = c.dist.scale();
0228    RealType const& x = c.param;
0229    static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
0230    // Error check:
0231    RealType error_result;
0232    if(false == detail::check_df(
0233          function, df, &error_result, Policy()))
0234    {
0235       return error_result;
0236    }
0237    if (x == 0)
0238    { // Treat zero as a special case.
0239      return 1;
0240    }
0241    if((x < 0) || !(boost::math::isfinite)(x))
0242    {
0243       return policies::raise_domain_error<RealType>(
0244          function, "inverse Chi Square parameter was %1%, but must be > 0 !", x, Policy());
0245    }
0246    // RealType shape = df /2; // inv_gamma shape,
0247    // RealType scale = df * scale/2; // inv_gamma scale,
0248    // result = gamma_p(shape, scale/c.param, Policy()); use inv_gamma.
0249 
0250    return gamma_p(df / 2, (df * scale/2) / x, Policy()); // OK
0251 } // cdf(complemented
0252 
0253 template <class RealType, class Policy>
0254 inline RealType quantile(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
0255 {
0256    using boost::math::gamma_q_inv;
0257 
0258    RealType const& df = c.dist.degrees_of_freedom();
0259    RealType const& scale = c.dist.scale();
0260    RealType const& q = c.param;
0261    static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
0262    // Error check:
0263    RealType error_result;
0264    if(false == detail::check_df(function, df, &error_result, Policy()))
0265    {
0266       return error_result;
0267    }
0268    if(false == detail::check_probability(function, q, &error_result, Policy()))
0269    {
0270       return error_result;
0271    }
0272    // RealType shape = df /2; // inv_gamma shape,
0273    // RealType scale = df * scale/2; // inv_gamma scale,
0274    // result = scale / gamma_p_inv(shape, q, Policy());  // using inv_gamma.
0275    RealType result = gamma_p_inv(df/2, q, Policy());
0276    if(result == 0)
0277       return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
0278    result = (df * scale / 2) / result;
0279    return result;
0280 } // quantile(const complement
0281 
0282 template <class RealType, class Policy>
0283 inline RealType mean(const inverse_chi_squared_distribution<RealType, Policy>& dist)
0284 { // Mean of inverse Chi-Squared distribution.
0285    RealType df = dist.degrees_of_freedom();
0286    RealType scale = dist.scale();
0287 
0288    static const char* function = "boost::math::mean(const inverse_chi_squared_distribution<%1%>&)";
0289    if(df <= 2)
0290       return policies::raise_domain_error<RealType>(
0291          function,
0292          "inverse Chi-Squared distribution only has a mode for degrees of freedom > 2, but got degrees of freedom = %1%.",
0293          df, Policy());
0294   return (df * scale) / (df - 2);
0295 } // mean
0296 
0297 template <class RealType, class Policy>
0298 inline RealType variance(const inverse_chi_squared_distribution<RealType, Policy>& dist)
0299 { // Variance of inverse Chi-Squared distribution.
0300    RealType df = dist.degrees_of_freedom();
0301    RealType scale = dist.scale();
0302    static const char* function = "boost::math::variance(const inverse_chi_squared_distribution<%1%>&)";
0303    if(df <= 4)
0304    {
0305       return policies::raise_domain_error<RealType>(
0306          function,
0307          "inverse Chi-Squared distribution only has a variance for degrees of freedom > 4, but got degrees of freedom = %1%.",
0308          df, Policy());
0309    }
0310    return 2 * df * df * scale * scale / ((df - 2)*(df - 2) * (df - 4));
0311 } // variance
0312 
0313 template <class RealType, class Policy>
0314 inline RealType mode(const inverse_chi_squared_distribution<RealType, Policy>& dist)
0315 { // mode is not defined in Mathematica.
0316   // See Discussion section http://en.wikipedia.org/wiki/Talk:Scaled-inverse-chi-square_distribution
0317   // for origin of the formula used below.
0318 
0319    RealType df = dist.degrees_of_freedom();
0320    RealType scale = dist.scale();
0321    static const char* function = "boost::math::mode(const inverse_chi_squared_distribution<%1%>&)";
0322    if(df < 0)
0323       return policies::raise_domain_error<RealType>(
0324          function,
0325          "inverse Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.",
0326          df, Policy());
0327    return (df * scale) / (df + 2);
0328 }
0329 
0330 //template <class RealType, class Policy>
0331 //inline RealType median(const inverse_chi_squared_distribution<RealType, Policy>& dist)
0332 //{ // Median is given by Quantile[dist, 1/2]
0333 //   RealType df = dist.degrees_of_freedom();
0334 //   if(df <= 1)
0335 //      return tools::domain_error<RealType>(
0336 //         BOOST_CURRENT_FUNCTION,
0337 //         "The inverse_Chi-Squared distribution only has a median for degrees of freedom >= 0, but got degrees of freedom = %1%.",
0338 //         df);
0339 //   return df;
0340 //}
0341 // Now implemented via quantile(half) in derived accessors.
0342 
0343 template <class RealType, class Policy>
0344 inline RealType skewness(const inverse_chi_squared_distribution<RealType, Policy>& dist)
0345 {
0346    BOOST_MATH_STD_USING // For ADL
0347    RealType df = dist.degrees_of_freedom();
0348    static const char* function = "boost::math::skewness(const inverse_chi_squared_distribution<%1%>&)";
0349    if(df <= 6)
0350       return policies::raise_domain_error<RealType>(
0351          function,
0352          "inverse Chi-Squared distribution only has a skewness for degrees of freedom > 6, but got degrees of freedom = %1%.",
0353          df, Policy());
0354 
0355    return 4 * sqrt (2 * (df - 4)) / (df - 6);  // Not a function of scale.
0356 }
0357 
0358 template <class RealType, class Policy>
0359 inline RealType kurtosis(const inverse_chi_squared_distribution<RealType, Policy>& dist)
0360 {
0361    RealType df = dist.degrees_of_freedom();
0362    static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
0363    if(df <= 8)
0364       return policies::raise_domain_error<RealType>(
0365          function,
0366          "inverse Chi-Squared distribution only has a kurtosis for degrees of freedom > 8, but got degrees of freedom = %1%.",
0367          df, Policy());
0368 
0369    return kurtosis_excess(dist) + 3;
0370 }
0371 
0372 template <class RealType, class Policy>
0373 inline RealType kurtosis_excess(const inverse_chi_squared_distribution<RealType, Policy>& dist)
0374 {
0375    RealType df = dist.degrees_of_freedom();
0376    static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
0377    if(df <= 8)
0378       return policies::raise_domain_error<RealType>(
0379          function,
0380          "inverse Chi-Squared distribution only has a kurtosis excess for degrees of freedom > 8, but got degrees of freedom = %1%.",
0381          df, Policy());
0382 
0383    return 12 * (5 * df - 22) / ((df - 6 )*(df - 8));  // Not a function of scale.
0384 }
0385 
0386 //
0387 // Parameter estimation comes last:
0388 //
0389 
0390 } // namespace math
0391 } // namespace boost
0392 
0393 // This include must be at the end, *after* the accessors
0394 // for this distribution have been defined, in order to
0395 // keep compilers that support two-phase lookup happy.
0396 #include <boost/math/distributions/detail/derived_accessors.hpp>
0397 
0398 #endif // BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP