annotate DEPENDENCIES/generic/include/boost/math/distributions/inverse_chi_squared.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 // Copyright John Maddock 2010.
Chris@16 2 // Copyright Paul A. Bristow 2010.
Chris@16 3
Chris@16 4 // Use, modification and distribution are subject to the
Chris@16 5 // Boost Software License, Version 1.0.
Chris@16 6 // (See accompanying file LICENSE_1_0.txt
Chris@16 7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8
Chris@16 9 #ifndef BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
Chris@16 10 #define BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
Chris@16 11
Chris@16 12 #include <boost/math/distributions/fwd.hpp>
Chris@16 13 #include <boost/math/special_functions/gamma.hpp> // for incomplete beta.
Chris@16 14 #include <boost/math/distributions/complement.hpp> // for complements.
Chris@16 15 #include <boost/math/distributions/detail/common_error_handling.hpp> // for error checks.
Chris@16 16 #include <boost/math/special_functions/fpclassify.hpp> // for isfinite
Chris@16 17
Chris@16 18 // See http://en.wikipedia.org/wiki/Scaled-inverse-chi-square_distribution
Chris@16 19 // for definitions of this scaled version.
Chris@16 20 // See http://en.wikipedia.org/wiki/Inverse-chi-square_distribution
Chris@16 21 // for unscaled version.
Chris@16 22
Chris@16 23 // http://reference.wolfram.com/mathematica/ref/InverseChiSquareDistribution.html
Chris@16 24 // Weisstein, Eric W. "Inverse Chi-Squared Distribution." From MathWorld--A Wolfram Web Resource.
Chris@16 25 // http://mathworld.wolfram.com/InverseChi-SquaredDistribution.html
Chris@16 26
Chris@16 27 #include <utility>
Chris@16 28
Chris@16 29 namespace boost{ namespace math{
Chris@16 30
Chris@16 31 namespace detail
Chris@16 32 {
Chris@16 33 template <class RealType, class Policy>
Chris@16 34 inline bool check_inverse_chi_squared( // Check both distribution parameters.
Chris@16 35 const char* function,
Chris@16 36 RealType degrees_of_freedom, // degrees_of_freedom (aka nu).
Chris@16 37 RealType scale, // scale (aka sigma^2)
Chris@16 38 RealType* result,
Chris@16 39 const Policy& pol)
Chris@16 40 {
Chris@16 41 return check_scale(function, scale, result, pol)
Chris@16 42 && check_df(function, degrees_of_freedom,
Chris@16 43 result, pol);
Chris@16 44 } // bool check_inverse_chi_squared
Chris@16 45 } // namespace detail
Chris@16 46
Chris@16 47 template <class RealType = double, class Policy = policies::policy<> >
Chris@16 48 class inverse_chi_squared_distribution
Chris@16 49 {
Chris@16 50 public:
Chris@16 51 typedef RealType value_type;
Chris@16 52 typedef Policy policy_type;
Chris@16 53
Chris@16 54 inverse_chi_squared_distribution(RealType df, RealType l_scale) : m_df(df), m_scale (l_scale)
Chris@16 55 {
Chris@16 56 RealType result;
Chris@16 57 detail::check_df(
Chris@16 58 "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
Chris@16 59 m_df, &result, Policy())
Chris@16 60 && detail::check_scale(
Chris@16 61 "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
Chris@16 62 m_scale, &result, Policy());
Chris@16 63 } // inverse_chi_squared_distribution constructor
Chris@16 64
Chris@16 65 inverse_chi_squared_distribution(RealType df = 1) : m_df(df)
Chris@16 66 {
Chris@16 67 RealType result;
Chris@16 68 m_scale = 1 / m_df ; // Default scale = 1 / degrees of freedom (Wikipedia definition 1).
Chris@16 69 detail::check_df(
Chris@16 70 "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
Chris@16 71 m_df, &result, Policy());
Chris@16 72 } // inverse_chi_squared_distribution
Chris@16 73
Chris@16 74 RealType degrees_of_freedom()const
Chris@16 75 {
Chris@16 76 return m_df; // aka nu
Chris@16 77 }
Chris@16 78 RealType scale()const
Chris@16 79 {
Chris@16 80 return m_scale; // aka xi
Chris@16 81 }
Chris@16 82
Chris@16 83 // Parameter estimation: NOT implemented yet.
Chris@16 84 //static RealType find_degrees_of_freedom(
Chris@16 85 // RealType difference_from_variance,
Chris@16 86 // RealType alpha,
Chris@16 87 // RealType beta,
Chris@16 88 // RealType variance,
Chris@16 89 // RealType hint = 100);
Chris@16 90
Chris@16 91 private:
Chris@16 92 // Data members:
Chris@16 93 RealType m_df; // degrees of freedom are treated as a real number.
Chris@16 94 RealType m_scale; // distribution scale.
Chris@16 95
Chris@16 96 }; // class chi_squared_distribution
Chris@16 97
Chris@16 98 typedef inverse_chi_squared_distribution<double> inverse_chi_squared;
Chris@16 99
Chris@16 100 template <class RealType, class Policy>
Chris@16 101 inline const std::pair<RealType, RealType> range(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
Chris@16 102 { // Range of permissible values for random variable x.
Chris@16 103 using boost::math::tools::max_value;
Chris@16 104 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + infinity.
Chris@16 105 }
Chris@16 106
Chris@16 107 template <class RealType, class Policy>
Chris@16 108 inline const std::pair<RealType, RealType> support(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
Chris@16 109 { // Range of supported values for random variable x.
Chris@16 110 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
Chris@16 111 return std::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>()); // 0 to + infinity.
Chris@16 112 }
Chris@16 113
Chris@16 114 template <class RealType, class Policy>
Chris@16 115 RealType pdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
Chris@16 116 {
Chris@16 117 BOOST_MATH_STD_USING // for ADL of std functions.
Chris@16 118 RealType df = dist.degrees_of_freedom();
Chris@16 119 RealType scale = dist.scale();
Chris@16 120 RealType error_result;
Chris@16 121
Chris@16 122 static const char* function = "boost::math::pdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
Chris@16 123
Chris@16 124 if(false == detail::check_inverse_chi_squared
Chris@16 125 (function, df, scale, &error_result, Policy())
Chris@16 126 )
Chris@16 127 { // Bad distribution.
Chris@16 128 return error_result;
Chris@16 129 }
Chris@16 130 if((x < 0) || !(boost::math::isfinite)(x))
Chris@16 131 { // Bad x.
Chris@16 132 return policies::raise_domain_error<RealType>(
Chris@16 133 function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
Chris@16 134 }
Chris@16 135
Chris@16 136 if(x == 0)
Chris@16 137 { // Treat as special case.
Chris@16 138 return 0;
Chris@16 139 }
Chris@16 140 // Wikipedia scaled inverse chi sq (df, scale) related to inv gamma (df/2, df * scale /2)
Chris@16 141 // so use inverse gamma pdf with shape = df/2, scale df * scale /2
Chris@16 142 // RealType shape = df /2; // inv_gamma shape
Chris@16 143 // RealType scale = df * scale/2; // inv_gamma scale
Chris@16 144 // RealType result = gamma_p_derivative(shape, scale / x, Policy()) * scale / (x * x);
Chris@16 145 RealType result = df * scale/2 / x;
Chris@16 146 if(result < tools::min_value<RealType>())
Chris@16 147 return 0; // Random variable is near enough infinite.
Chris@16 148 result = gamma_p_derivative(df/2, result, Policy()) * df * scale/2;
Chris@16 149 if(result != 0) // prevent 0 / 0, gamma_p_derivative -> 0 faster than x^2
Chris@16 150 result /= (x * x);
Chris@16 151 return result;
Chris@16 152 } // pdf
Chris@16 153
Chris@16 154 template <class RealType, class Policy>
Chris@16 155 inline RealType cdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
Chris@16 156 {
Chris@16 157 static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
Chris@16 158 RealType df = dist.degrees_of_freedom();
Chris@16 159 RealType scale = dist.scale();
Chris@16 160 RealType error_result;
Chris@16 161
Chris@16 162 if(false ==
Chris@16 163 detail::check_inverse_chi_squared(function, df, scale, &error_result, Policy())
Chris@16 164 )
Chris@16 165 { // Bad distribution.
Chris@16 166 return error_result;
Chris@16 167 }
Chris@16 168 if((x < 0) || !(boost::math::isfinite)(x))
Chris@16 169 { // Bad x.
Chris@16 170 return policies::raise_domain_error<RealType>(
Chris@16 171 function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
Chris@16 172 }
Chris@16 173 if (x == 0)
Chris@16 174 { // Treat zero as a special case.
Chris@16 175 return 0;
Chris@16 176 }
Chris@16 177 // RealType shape = df /2; // inv_gamma shape,
Chris@16 178 // RealType scale = df * scale/2; // inv_gamma scale,
Chris@16 179 // result = boost::math::gamma_q(shape, scale / x, Policy()); // inverse_gamma code.
Chris@16 180 return boost::math::gamma_q(df / 2, (df * (scale / 2)) / x, Policy());
Chris@16 181 } // cdf
Chris@16 182
Chris@16 183 template <class RealType, class Policy>
Chris@16 184 inline RealType quantile(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& p)
Chris@16 185 {
Chris@16 186 using boost::math::gamma_q_inv;
Chris@16 187 RealType df = dist.degrees_of_freedom();
Chris@16 188 RealType scale = dist.scale();
Chris@16 189
Chris@16 190 static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
Chris@16 191 // Error check:
Chris@16 192 RealType error_result;
Chris@16 193 if(false == detail::check_df(
Chris@16 194 function, df, &error_result, Policy())
Chris@16 195 && detail::check_probability(
Chris@16 196 function, p, &error_result, Policy()))
Chris@16 197 {
Chris@16 198 return error_result;
Chris@16 199 }
Chris@16 200 if(false == detail::check_probability(
Chris@16 201 function, p, &error_result, Policy()))
Chris@16 202 {
Chris@16 203 return error_result;
Chris@16 204 }
Chris@16 205 // RealType shape = df /2; // inv_gamma shape,
Chris@16 206 // RealType scale = df * scale/2; // inv_gamma scale,
Chris@16 207 // result = scale / gamma_q_inv(shape, p, Policy());
Chris@16 208 RealType result = gamma_q_inv(df /2, p, Policy());
Chris@16 209 if(result == 0)
Chris@16 210 return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
Chris@16 211 result = df * (scale / 2) / result;
Chris@16 212 return result;
Chris@16 213 } // quantile
Chris@16 214
Chris@16 215 template <class RealType, class Policy>
Chris@16 216 inline RealType cdf(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
Chris@16 217 {
Chris@16 218 using boost::math::gamma_q_inv;
Chris@16 219 RealType const& df = c.dist.degrees_of_freedom();
Chris@16 220 RealType const& scale = c.dist.scale();
Chris@16 221 RealType const& x = c.param;
Chris@16 222 static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
Chris@16 223 // Error check:
Chris@16 224 RealType error_result;
Chris@16 225 if(false == detail::check_df(
Chris@16 226 function, df, &error_result, Policy()))
Chris@16 227 {
Chris@16 228 return error_result;
Chris@16 229 }
Chris@16 230 if (x == 0)
Chris@16 231 { // Treat zero as a special case.
Chris@16 232 return 1;
Chris@16 233 }
Chris@16 234 if((x < 0) || !(boost::math::isfinite)(x))
Chris@16 235 {
Chris@16 236 return policies::raise_domain_error<RealType>(
Chris@16 237 function, "inverse Chi Square parameter was %1%, but must be > 0 !", x, Policy());
Chris@16 238 }
Chris@16 239 // RealType shape = df /2; // inv_gamma shape,
Chris@16 240 // RealType scale = df * scale/2; // inv_gamma scale,
Chris@16 241 // result = gamma_p(shape, scale/c.param, Policy()); use inv_gamma.
Chris@16 242
Chris@16 243 return gamma_p(df / 2, (df * scale/2) / x, Policy()); // OK
Chris@16 244 } // cdf(complemented
Chris@16 245
Chris@16 246 template <class RealType, class Policy>
Chris@16 247 inline RealType quantile(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
Chris@16 248 {
Chris@16 249 using boost::math::gamma_q_inv;
Chris@16 250
Chris@16 251 RealType const& df = c.dist.degrees_of_freedom();
Chris@16 252 RealType const& scale = c.dist.scale();
Chris@16 253 RealType const& q = c.param;
Chris@16 254 static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
Chris@16 255 // Error check:
Chris@16 256 RealType error_result;
Chris@16 257 if(false == detail::check_df(function, df, &error_result, Policy()))
Chris@16 258 {
Chris@16 259 return error_result;
Chris@16 260 }
Chris@16 261 if(false == detail::check_probability(function, q, &error_result, Policy()))
Chris@16 262 {
Chris@16 263 return error_result;
Chris@16 264 }
Chris@16 265 // RealType shape = df /2; // inv_gamma shape,
Chris@16 266 // RealType scale = df * scale/2; // inv_gamma scale,
Chris@16 267 // result = scale / gamma_p_inv(shape, q, Policy()); // using inv_gamma.
Chris@16 268 RealType result = gamma_p_inv(df/2, q, Policy());
Chris@16 269 if(result == 0)
Chris@16 270 return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
Chris@16 271 result = (df * scale / 2) / result;
Chris@16 272 return result;
Chris@16 273 } // quantile(const complement
Chris@16 274
Chris@16 275 template <class RealType, class Policy>
Chris@16 276 inline RealType mean(const inverse_chi_squared_distribution<RealType, Policy>& dist)
Chris@16 277 { // Mean of inverse Chi-Squared distribution.
Chris@16 278 RealType df = dist.degrees_of_freedom();
Chris@16 279 RealType scale = dist.scale();
Chris@16 280
Chris@16 281 static const char* function = "boost::math::mean(const inverse_chi_squared_distribution<%1%>&)";
Chris@16 282 if(df <= 2)
Chris@16 283 return policies::raise_domain_error<RealType>(
Chris@16 284 function,
Chris@16 285 "inverse Chi-Squared distribution only has a mode for degrees of freedom > 2, but got degrees of freedom = %1%.",
Chris@16 286 df, Policy());
Chris@16 287 return (df * scale) / (df - 2);
Chris@16 288 } // mean
Chris@16 289
Chris@16 290 template <class RealType, class Policy>
Chris@16 291 inline RealType variance(const inverse_chi_squared_distribution<RealType, Policy>& dist)
Chris@16 292 { // Variance of inverse Chi-Squared distribution.
Chris@16 293 RealType df = dist.degrees_of_freedom();
Chris@16 294 RealType scale = dist.scale();
Chris@16 295 static const char* function = "boost::math::variance(const inverse_chi_squared_distribution<%1%>&)";
Chris@16 296 if(df <= 4)
Chris@16 297 {
Chris@16 298 return policies::raise_domain_error<RealType>(
Chris@16 299 function,
Chris@16 300 "inverse Chi-Squared distribution only has a variance for degrees of freedom > 4, but got degrees of freedom = %1%.",
Chris@16 301 df, Policy());
Chris@16 302 }
Chris@16 303 return 2 * df * df * scale * scale / ((df - 2)*(df - 2) * (df - 4));
Chris@16 304 } // variance
Chris@16 305
Chris@16 306 template <class RealType, class Policy>
Chris@16 307 inline RealType mode(const inverse_chi_squared_distribution<RealType, Policy>& dist)
Chris@16 308 { // mode is not defined in Mathematica.
Chris@16 309 // See Discussion section http://en.wikipedia.org/wiki/Talk:Scaled-inverse-chi-square_distribution
Chris@16 310 // for origin of the formula used below.
Chris@16 311
Chris@16 312 RealType df = dist.degrees_of_freedom();
Chris@16 313 RealType scale = dist.scale();
Chris@16 314 static const char* function = "boost::math::mode(const inverse_chi_squared_distribution<%1%>&)";
Chris@16 315 if(df < 0)
Chris@16 316 return policies::raise_domain_error<RealType>(
Chris@16 317 function,
Chris@16 318 "inverse Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.",
Chris@16 319 df, Policy());
Chris@16 320 return (df * scale) / (df + 2);
Chris@16 321 }
Chris@16 322
Chris@16 323 //template <class RealType, class Policy>
Chris@16 324 //inline RealType median(const inverse_chi_squared_distribution<RealType, Policy>& dist)
Chris@16 325 //{ // Median is given by Quantile[dist, 1/2]
Chris@16 326 // RealType df = dist.degrees_of_freedom();
Chris@16 327 // if(df <= 1)
Chris@16 328 // return tools::domain_error<RealType>(
Chris@16 329 // BOOST_CURRENT_FUNCTION,
Chris@16 330 // "The inverse_Chi-Squared distribution only has a median for degrees of freedom >= 0, but got degrees of freedom = %1%.",
Chris@16 331 // df);
Chris@16 332 // return df;
Chris@16 333 //}
Chris@16 334 // Now implemented via quantile(half) in derived accessors.
Chris@16 335
Chris@16 336 template <class RealType, class Policy>
Chris@16 337 inline RealType skewness(const inverse_chi_squared_distribution<RealType, Policy>& dist)
Chris@16 338 {
Chris@16 339 BOOST_MATH_STD_USING // For ADL
Chris@16 340 RealType df = dist.degrees_of_freedom();
Chris@16 341 static const char* function = "boost::math::skewness(const inverse_chi_squared_distribution<%1%>&)";
Chris@16 342 if(df <= 6)
Chris@16 343 return policies::raise_domain_error<RealType>(
Chris@16 344 function,
Chris@16 345 "inverse Chi-Squared distribution only has a skewness for degrees of freedom > 6, but got degrees of freedom = %1%.",
Chris@16 346 df, Policy());
Chris@16 347
Chris@16 348 return 4 * sqrt (2 * (df - 4)) / (df - 6); // Not a function of scale.
Chris@16 349 }
Chris@16 350
Chris@16 351 template <class RealType, class Policy>
Chris@16 352 inline RealType kurtosis(const inverse_chi_squared_distribution<RealType, Policy>& dist)
Chris@16 353 {
Chris@16 354 RealType df = dist.degrees_of_freedom();
Chris@16 355 static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
Chris@16 356 if(df <= 8)
Chris@16 357 return policies::raise_domain_error<RealType>(
Chris@16 358 function,
Chris@16 359 "inverse Chi-Squared distribution only has a kurtosis for degrees of freedom > 8, but got degrees of freedom = %1%.",
Chris@16 360 df, Policy());
Chris@16 361
Chris@16 362 return kurtosis_excess(dist) + 3;
Chris@16 363 }
Chris@16 364
Chris@16 365 template <class RealType, class Policy>
Chris@16 366 inline RealType kurtosis_excess(const inverse_chi_squared_distribution<RealType, Policy>& dist)
Chris@16 367 {
Chris@16 368 RealType df = dist.degrees_of_freedom();
Chris@16 369 static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
Chris@16 370 if(df <= 8)
Chris@16 371 return policies::raise_domain_error<RealType>(
Chris@16 372 function,
Chris@16 373 "inverse Chi-Squared distribution only has a kurtosis excess for degrees of freedom > 8, but got degrees of freedom = %1%.",
Chris@16 374 df, Policy());
Chris@16 375
Chris@16 376 return 12 * (5 * df - 22) / ((df - 6 )*(df - 8)); // Not a function of scale.
Chris@16 377 }
Chris@16 378
Chris@16 379 //
Chris@16 380 // Parameter estimation comes last:
Chris@16 381 //
Chris@16 382
Chris@16 383 } // namespace math
Chris@16 384 } // namespace boost
Chris@16 385
Chris@16 386 // This include must be at the end, *after* the accessors
Chris@16 387 // for this distribution have been defined, in order to
Chris@16 388 // keep compilers that support two-phase lookup happy.
Chris@16 389 #include <boost/math/distributions/detail/derived_accessors.hpp>
Chris@16 390
Chris@16 391 #endif // BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP