annotate any/include/boost/math/distributions/inverse_chi_squared.hpp @ 160:cff480c41f97

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