annotate DEPENDENCIES/generic/include/boost/math/distributions/fisher_f.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 2006.
Chris@16 2
Chris@16 3 // Use, modification and distribution are subject to the
Chris@16 4 // Boost Software License, Version 1.0.
Chris@16 5 // (See accompanying file LICENSE_1_0.txt
Chris@16 6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7
Chris@16 8 #ifndef BOOST_MATH_DISTRIBUTIONS_FISHER_F_HPP
Chris@16 9 #define BOOST_MATH_DISTRIBUTIONS_FISHER_F_HPP
Chris@16 10
Chris@16 11 #include <boost/math/distributions/fwd.hpp>
Chris@16 12 #include <boost/math/special_functions/beta.hpp> // for incomplete beta.
Chris@16 13 #include <boost/math/distributions/complement.hpp> // complements
Chris@16 14 #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
Chris@16 15 #include <boost/math/special_functions/fpclassify.hpp>
Chris@16 16
Chris@16 17 #include <utility>
Chris@16 18
Chris@16 19 namespace boost{ namespace math{
Chris@16 20
Chris@16 21 template <class RealType = double, class Policy = policies::policy<> >
Chris@16 22 class fisher_f_distribution
Chris@16 23 {
Chris@16 24 public:
Chris@16 25 typedef RealType value_type;
Chris@16 26 typedef Policy policy_type;
Chris@16 27
Chris@16 28 fisher_f_distribution(const RealType& i, const RealType& j) : m_df1(i), m_df2(j)
Chris@16 29 {
Chris@16 30 static const char* function = "fisher_f_distribution<%1%>::fisher_f_distribution";
Chris@16 31 RealType result;
Chris@16 32 detail::check_df(
Chris@16 33 function, m_df1, &result, Policy());
Chris@16 34 detail::check_df(
Chris@16 35 function, m_df2, &result, Policy());
Chris@16 36 } // fisher_f_distribution
Chris@16 37
Chris@16 38 RealType degrees_of_freedom1()const
Chris@16 39 {
Chris@16 40 return m_df1;
Chris@16 41 }
Chris@16 42 RealType degrees_of_freedom2()const
Chris@16 43 {
Chris@16 44 return m_df2;
Chris@16 45 }
Chris@16 46
Chris@16 47 private:
Chris@16 48 //
Chris@16 49 // Data members:
Chris@16 50 //
Chris@16 51 RealType m_df1; // degrees of freedom are a real number.
Chris@16 52 RealType m_df2; // degrees of freedom are a real number.
Chris@16 53 };
Chris@16 54
Chris@16 55 typedef fisher_f_distribution<double> fisher_f;
Chris@16 56
Chris@16 57 template <class RealType, class Policy>
Chris@16 58 inline const std::pair<RealType, RealType> range(const fisher_f_distribution<RealType, Policy>& /*dist*/)
Chris@16 59 { // Range of permissible values for random variable x.
Chris@16 60 using boost::math::tools::max_value;
Chris@16 61 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
Chris@16 62 }
Chris@16 63
Chris@16 64 template <class RealType, class Policy>
Chris@16 65 inline const std::pair<RealType, RealType> support(const fisher_f_distribution<RealType, Policy>& /*dist*/)
Chris@16 66 { // Range of supported values for random variable x.
Chris@16 67 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
Chris@16 68 using boost::math::tools::max_value;
Chris@16 69 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
Chris@16 70 }
Chris@16 71
Chris@16 72 template <class RealType, class Policy>
Chris@16 73 RealType pdf(const fisher_f_distribution<RealType, Policy>& dist, const RealType& x)
Chris@16 74 {
Chris@16 75 BOOST_MATH_STD_USING // for ADL of std functions
Chris@16 76 RealType df1 = dist.degrees_of_freedom1();
Chris@16 77 RealType df2 = dist.degrees_of_freedom2();
Chris@16 78 // Error check:
Chris@16 79 RealType error_result = 0;
Chris@16 80 static const char* function = "boost::math::pdf(fisher_f_distribution<%1%> const&, %1%)";
Chris@16 81 if(false == detail::check_df(
Chris@16 82 function, df1, &error_result, Policy())
Chris@16 83 && detail::check_df(
Chris@16 84 function, df2, &error_result, Policy()))
Chris@16 85 return error_result;
Chris@16 86
Chris@16 87 if((x < 0) || !(boost::math::isfinite)(x))
Chris@16 88 {
Chris@16 89 return policies::raise_domain_error<RealType>(
Chris@16 90 function, "Random variable parameter was %1%, but must be > 0 !", x, Policy());
Chris@16 91 }
Chris@16 92
Chris@16 93 if(x == 0)
Chris@16 94 {
Chris@16 95 // special cases:
Chris@16 96 if(df1 < 2)
Chris@16 97 return policies::raise_overflow_error<RealType>(
Chris@16 98 function, 0, Policy());
Chris@16 99 else if(df1 == 2)
Chris@16 100 return 1;
Chris@16 101 else
Chris@16 102 return 0;
Chris@16 103 }
Chris@16 104
Chris@16 105 //
Chris@16 106 // You reach this formula by direct differentiation of the
Chris@16 107 // cdf expressed in terms of the incomplete beta.
Chris@16 108 //
Chris@16 109 // There are two versions so we don't pass a value of z
Chris@16 110 // that is very close to 1 to ibeta_derivative: for some values
Chris@16 111 // of df1 and df2, all the change takes place in this area.
Chris@16 112 //
Chris@16 113 RealType v1x = df1 * x;
Chris@16 114 RealType result;
Chris@16 115 if(v1x > df2)
Chris@16 116 {
Chris@16 117 result = (df2 * df1) / ((df2 + v1x) * (df2 + v1x));
Chris@16 118 result *= ibeta_derivative(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy());
Chris@16 119 }
Chris@16 120 else
Chris@16 121 {
Chris@16 122 result = df2 + df1 * x;
Chris@16 123 result = (result * df1 - x * df1 * df1) / (result * result);
Chris@16 124 result *= ibeta_derivative(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy());
Chris@16 125 }
Chris@16 126 return result;
Chris@16 127 } // pdf
Chris@16 128
Chris@16 129 template <class RealType, class Policy>
Chris@16 130 inline RealType cdf(const fisher_f_distribution<RealType, Policy>& dist, const RealType& x)
Chris@16 131 {
Chris@16 132 static const char* function = "boost::math::cdf(fisher_f_distribution<%1%> const&, %1%)";
Chris@16 133 RealType df1 = dist.degrees_of_freedom1();
Chris@16 134 RealType df2 = dist.degrees_of_freedom2();
Chris@16 135 // Error check:
Chris@16 136 RealType error_result = 0;
Chris@16 137 if(false == detail::check_df(
Chris@16 138 function, df1, &error_result, Policy())
Chris@16 139 && detail::check_df(
Chris@16 140 function, df2, &error_result, Policy()))
Chris@16 141 return error_result;
Chris@16 142
Chris@16 143 if((x < 0) || !(boost::math::isfinite)(x))
Chris@16 144 {
Chris@16 145 return policies::raise_domain_error<RealType>(
Chris@16 146 function, "Random Variable parameter was %1%, but must be > 0 !", x, Policy());
Chris@16 147 }
Chris@16 148
Chris@16 149 RealType v1x = df1 * x;
Chris@16 150 //
Chris@16 151 // There are two equivalent formulas used here, the aim is
Chris@16 152 // to prevent the final argument to the incomplete beta
Chris@16 153 // from being too close to 1: for some values of df1 and df2
Chris@16 154 // the rate of change can be arbitrarily large in this area,
Chris@16 155 // whilst the value we're passing will have lost information
Chris@16 156 // content as a result of being 0.999999something. Better
Chris@16 157 // to switch things around so we're passing 1-z instead.
Chris@16 158 //
Chris@16 159 return v1x > df2
Chris@16 160 ? boost::math::ibetac(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy())
Chris@16 161 : boost::math::ibeta(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy());
Chris@16 162 } // cdf
Chris@16 163
Chris@16 164 template <class RealType, class Policy>
Chris@16 165 inline RealType quantile(const fisher_f_distribution<RealType, Policy>& dist, const RealType& p)
Chris@16 166 {
Chris@16 167 static const char* function = "boost::math::quantile(fisher_f_distribution<%1%> const&, %1%)";
Chris@16 168 RealType df1 = dist.degrees_of_freedom1();
Chris@16 169 RealType df2 = dist.degrees_of_freedom2();
Chris@16 170 // Error check:
Chris@16 171 RealType error_result = 0;
Chris@16 172 if(false == (detail::check_df(
Chris@16 173 function, df1, &error_result, Policy())
Chris@16 174 && detail::check_df(
Chris@16 175 function, df2, &error_result, Policy())
Chris@16 176 && detail::check_probability(
Chris@16 177 function, p, &error_result, Policy())))
Chris@16 178 return error_result;
Chris@16 179
Chris@16 180 // With optimizations turned on, gcc wrongly warns about y being used
Chris@16 181 // uninitializated unless we initialize it to something:
Chris@16 182 RealType x, y(0);
Chris@16 183
Chris@16 184 x = boost::math::ibeta_inv(df1 / 2, df2 / 2, p, &y, Policy());
Chris@16 185
Chris@16 186 return df2 * x / (df1 * y);
Chris@16 187 } // quantile
Chris@16 188
Chris@16 189 template <class RealType, class Policy>
Chris@16 190 inline RealType cdf(const complemented2_type<fisher_f_distribution<RealType, Policy>, RealType>& c)
Chris@16 191 {
Chris@16 192 static const char* function = "boost::math::cdf(fisher_f_distribution<%1%> const&, %1%)";
Chris@16 193 RealType df1 = c.dist.degrees_of_freedom1();
Chris@16 194 RealType df2 = c.dist.degrees_of_freedom2();
Chris@16 195 RealType x = c.param;
Chris@16 196 // Error check:
Chris@16 197 RealType error_result = 0;
Chris@16 198 if(false == detail::check_df(
Chris@16 199 function, df1, &error_result, Policy())
Chris@16 200 && detail::check_df(
Chris@16 201 function, df2, &error_result, Policy()))
Chris@16 202 return error_result;
Chris@16 203
Chris@16 204 if((x < 0) || !(boost::math::isfinite)(x))
Chris@16 205 {
Chris@16 206 return policies::raise_domain_error<RealType>(
Chris@16 207 function, "Random Variable parameter was %1%, but must be > 0 !", x, Policy());
Chris@16 208 }
Chris@16 209
Chris@16 210 RealType v1x = df1 * x;
Chris@16 211 //
Chris@16 212 // There are two equivalent formulas used here, the aim is
Chris@16 213 // to prevent the final argument to the incomplete beta
Chris@16 214 // from being too close to 1: for some values of df1 and df2
Chris@16 215 // the rate of change can be arbitrarily large in this area,
Chris@16 216 // whilst the value we're passing will have lost information
Chris@16 217 // content as a result of being 0.999999something. Better
Chris@16 218 // to switch things around so we're passing 1-z instead.
Chris@16 219 //
Chris@16 220 return v1x > df2
Chris@16 221 ? boost::math::ibeta(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy())
Chris@16 222 : boost::math::ibetac(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy());
Chris@16 223 }
Chris@16 224
Chris@16 225 template <class RealType, class Policy>
Chris@16 226 inline RealType quantile(const complemented2_type<fisher_f_distribution<RealType, Policy>, RealType>& c)
Chris@16 227 {
Chris@16 228 static const char* function = "boost::math::quantile(fisher_f_distribution<%1%> const&, %1%)";
Chris@16 229 RealType df1 = c.dist.degrees_of_freedom1();
Chris@16 230 RealType df2 = c.dist.degrees_of_freedom2();
Chris@16 231 RealType p = c.param;
Chris@16 232 // Error check:
Chris@16 233 RealType error_result = 0;
Chris@16 234 if(false == (detail::check_df(
Chris@16 235 function, df1, &error_result, Policy())
Chris@16 236 && detail::check_df(
Chris@16 237 function, df2, &error_result, Policy())
Chris@16 238 && detail::check_probability(
Chris@16 239 function, p, &error_result, Policy())))
Chris@16 240 return error_result;
Chris@16 241
Chris@16 242 RealType x, y;
Chris@16 243
Chris@16 244 x = boost::math::ibetac_inv(df1 / 2, df2 / 2, p, &y, Policy());
Chris@16 245
Chris@16 246 return df2 * x / (df1 * y);
Chris@16 247 }
Chris@16 248
Chris@16 249 template <class RealType, class Policy>
Chris@16 250 inline RealType mean(const fisher_f_distribution<RealType, Policy>& dist)
Chris@16 251 { // Mean of F distribution = v.
Chris@16 252 static const char* function = "boost::math::mean(fisher_f_distribution<%1%> const&)";
Chris@16 253 RealType df1 = dist.degrees_of_freedom1();
Chris@16 254 RealType df2 = dist.degrees_of_freedom2();
Chris@16 255 // Error check:
Chris@16 256 RealType error_result = 0;
Chris@16 257 if(false == detail::check_df(
Chris@16 258 function, df1, &error_result, Policy())
Chris@16 259 && detail::check_df(
Chris@16 260 function, df2, &error_result, Policy()))
Chris@16 261 return error_result;
Chris@16 262 if(df2 <= 2)
Chris@16 263 {
Chris@16 264 return policies::raise_domain_error<RealType>(
Chris@16 265 function, "Second degree of freedom was %1% but must be > 2 in order for the distribution to have a mean.", df2, Policy());
Chris@16 266 }
Chris@16 267 return df2 / (df2 - 2);
Chris@16 268 } // mean
Chris@16 269
Chris@16 270 template <class RealType, class Policy>
Chris@16 271 inline RealType variance(const fisher_f_distribution<RealType, Policy>& dist)
Chris@16 272 { // Variance of F distribution.
Chris@16 273 static const char* function = "boost::math::variance(fisher_f_distribution<%1%> const&)";
Chris@16 274 RealType df1 = dist.degrees_of_freedom1();
Chris@16 275 RealType df2 = dist.degrees_of_freedom2();
Chris@16 276 // Error check:
Chris@16 277 RealType error_result = 0;
Chris@16 278 if(false == detail::check_df(
Chris@16 279 function, df1, &error_result, Policy())
Chris@16 280 && detail::check_df(
Chris@16 281 function, df2, &error_result, Policy()))
Chris@16 282 return error_result;
Chris@16 283 if(df2 <= 4)
Chris@16 284 {
Chris@16 285 return policies::raise_domain_error<RealType>(
Chris@16 286 function, "Second degree of freedom was %1% but must be > 4 in order for the distribution to have a valid variance.", df2, Policy());
Chris@16 287 }
Chris@16 288 return 2 * df2 * df2 * (df1 + df2 - 2) / (df1 * (df2 - 2) * (df2 - 2) * (df2 - 4));
Chris@16 289 } // variance
Chris@16 290
Chris@16 291 template <class RealType, class Policy>
Chris@16 292 inline RealType mode(const fisher_f_distribution<RealType, Policy>& dist)
Chris@16 293 {
Chris@16 294 static const char* function = "boost::math::mode(fisher_f_distribution<%1%> const&)";
Chris@16 295 RealType df1 = dist.degrees_of_freedom1();
Chris@16 296 RealType df2 = dist.degrees_of_freedom2();
Chris@16 297 // Error check:
Chris@16 298 RealType error_result = 0;
Chris@16 299 if(false == detail::check_df(
Chris@16 300 function, df1, &error_result, Policy())
Chris@16 301 && detail::check_df(
Chris@16 302 function, df2, &error_result, Policy()))
Chris@16 303 return error_result;
Chris@16 304 if(df2 <= 2)
Chris@16 305 {
Chris@16 306 return policies::raise_domain_error<RealType>(
Chris@16 307 function, "Second degree of freedom was %1% but must be > 2 in order for the distribution to have a mode.", df2, Policy());
Chris@16 308 }
Chris@16 309 return df2 * (df1 - 2) / (df1 * (df2 + 2));
Chris@16 310 }
Chris@16 311
Chris@16 312 //template <class RealType, class Policy>
Chris@16 313 //inline RealType median(const fisher_f_distribution<RealType, Policy>& dist)
Chris@16 314 //{ // Median of Fisher F distribution is not defined.
Chris@16 315 // return tools::domain_error<RealType>(BOOST_CURRENT_FUNCTION, "Median is not implemented, result is %1%!", std::numeric_limits<RealType>::quiet_NaN());
Chris@16 316 // } // median
Chris@16 317
Chris@16 318 // Now implemented via quantile(half) in derived accessors.
Chris@16 319
Chris@16 320 template <class RealType, class Policy>
Chris@16 321 inline RealType skewness(const fisher_f_distribution<RealType, Policy>& dist)
Chris@16 322 {
Chris@16 323 static const char* function = "boost::math::skewness(fisher_f_distribution<%1%> const&)";
Chris@16 324 BOOST_MATH_STD_USING // ADL of std names
Chris@16 325 // See http://mathworld.wolfram.com/F-Distribution.html
Chris@16 326 RealType df1 = dist.degrees_of_freedom1();
Chris@16 327 RealType df2 = dist.degrees_of_freedom2();
Chris@16 328 // Error check:
Chris@16 329 RealType error_result = 0;
Chris@16 330 if(false == detail::check_df(
Chris@16 331 function, df1, &error_result, Policy())
Chris@16 332 && detail::check_df(
Chris@16 333 function, df2, &error_result, Policy()))
Chris@16 334 return error_result;
Chris@16 335 if(df2 <= 6)
Chris@16 336 {
Chris@16 337 return policies::raise_domain_error<RealType>(
Chris@16 338 function, "Second degree of freedom was %1% but must be > 6 in order for the distribution to have a skewness.", df2, Policy());
Chris@16 339 }
Chris@16 340 return 2 * (df2 + 2 * df1 - 2) * sqrt((2 * df2 - 8) / (df1 * (df2 + df1 - 2))) / (df2 - 6);
Chris@16 341 }
Chris@16 342
Chris@16 343 template <class RealType, class Policy>
Chris@16 344 RealType kurtosis_excess(const fisher_f_distribution<RealType, Policy>& dist);
Chris@16 345
Chris@16 346 template <class RealType, class Policy>
Chris@16 347 inline RealType kurtosis(const fisher_f_distribution<RealType, Policy>& dist)
Chris@16 348 {
Chris@16 349 return 3 + kurtosis_excess(dist);
Chris@16 350 }
Chris@16 351
Chris@16 352 template <class RealType, class Policy>
Chris@16 353 inline RealType kurtosis_excess(const fisher_f_distribution<RealType, Policy>& dist)
Chris@16 354 {
Chris@16 355 static const char* function = "boost::math::kurtosis_excess(fisher_f_distribution<%1%> const&)";
Chris@16 356 // See http://mathworld.wolfram.com/F-Distribution.html
Chris@16 357 RealType df1 = dist.degrees_of_freedom1();
Chris@16 358 RealType df2 = dist.degrees_of_freedom2();
Chris@16 359 // Error check:
Chris@16 360 RealType error_result = 0;
Chris@16 361 if(false == detail::check_df(
Chris@16 362 function, df1, &error_result, Policy())
Chris@16 363 && detail::check_df(
Chris@16 364 function, df2, &error_result, Policy()))
Chris@16 365 return error_result;
Chris@16 366 if(df2 <= 8)
Chris@16 367 {
Chris@16 368 return policies::raise_domain_error<RealType>(
Chris@16 369 function, "Second degree of freedom was %1% but must be > 8 in order for the distribution to have a kutosis.", df2, Policy());
Chris@16 370 }
Chris@16 371 RealType df2_2 = df2 * df2;
Chris@16 372 RealType df1_2 = df1 * df1;
Chris@16 373 RealType n = -16 + 20 * df2 - 8 * df2_2 + df2_2 * df2 + 44 * df1 - 32 * df2 * df1 + 5 * df2_2 * df1 - 22 * df1_2 + 5 * df2 * df1_2;
Chris@16 374 n *= 12;
Chris@16 375 RealType d = df1 * (df2 - 6) * (df2 - 8) * (df1 + df2 - 2);
Chris@16 376 return n / d;
Chris@16 377 }
Chris@16 378
Chris@16 379 } // namespace math
Chris@16 380 } // namespace boost
Chris@16 381
Chris@16 382 // This include must be at the end, *after* the accessors
Chris@16 383 // for this distribution have been defined, in order to
Chris@16 384 // keep compilers that support two-phase lookup happy.
Chris@16 385 #include <boost/math/distributions/detail/derived_accessors.hpp>
Chris@16 386
Chris@16 387 #endif // BOOST_MATH_DISTRIBUTIONS_FISHER_F_HPP