Chris@16: // Copyright John Maddock 2006. 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_FISHER_F_HPP Chris@16: #define BOOST_MATH_DISTRIBUTIONS_FISHER_F_HPP Chris@16: Chris@16: #include Chris@16: #include // for incomplete beta. Chris@16: #include // complements Chris@16: #include // error checks Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: Chris@16: template > Chris@16: class fisher_f_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType value_type; Chris@16: typedef Policy policy_type; Chris@16: Chris@16: fisher_f_distribution(const RealType& i, const RealType& j) : m_df1(i), m_df2(j) Chris@16: { Chris@16: static const char* function = "fisher_f_distribution<%1%>::fisher_f_distribution"; Chris@16: RealType result; Chris@16: detail::check_df( Chris@16: function, m_df1, &result, Policy()); Chris@16: detail::check_df( Chris@16: function, m_df2, &result, Policy()); Chris@16: } // fisher_f_distribution Chris@16: Chris@16: RealType degrees_of_freedom1()const Chris@16: { Chris@16: return m_df1; Chris@16: } Chris@16: RealType degrees_of_freedom2()const Chris@16: { Chris@16: return m_df2; Chris@16: } Chris@16: Chris@16: private: Chris@16: // Chris@16: // Data members: Chris@16: // Chris@16: RealType m_df1; // degrees of freedom are a real number. Chris@16: RealType m_df2; // degrees of freedom are a real number. Chris@16: }; Chris@16: Chris@16: typedef fisher_f_distribution fisher_f; Chris@16: Chris@16: template Chris@16: inline const std::pair range(const fisher_f_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()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline const std::pair support(const fisher_f_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: using boost::math::tools::max_value; Chris@16: return std::pair(static_cast(0), max_value()); Chris@16: } Chris@16: Chris@16: template Chris@16: RealType pdf(const fisher_f_distribution& dist, const RealType& x) Chris@16: { Chris@16: BOOST_MATH_STD_USING // for ADL of std functions Chris@16: RealType df1 = dist.degrees_of_freedom1(); Chris@16: RealType df2 = dist.degrees_of_freedom2(); Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: static const char* function = "boost::math::pdf(fisher_f_distribution<%1%> const&, %1%)"; Chris@16: if(false == detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy())) Chris@16: return error_result; Chris@16: Chris@16: if((x < 0) || !(boost::math::isfinite)(x)) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Random variable parameter was %1%, but must be > 0 !", x, Policy()); Chris@16: } Chris@16: Chris@16: if(x == 0) Chris@16: { Chris@16: // special cases: Chris@16: if(df1 < 2) Chris@16: return policies::raise_overflow_error( Chris@16: function, 0, Policy()); Chris@16: else if(df1 == 2) Chris@16: return 1; Chris@16: else Chris@16: return 0; Chris@16: } Chris@16: Chris@16: // Chris@16: // You reach this formula by direct differentiation of the Chris@16: // cdf expressed in terms of the incomplete beta. Chris@16: // Chris@16: // There are two versions so we don't pass a value of z Chris@16: // that is very close to 1 to ibeta_derivative: for some values Chris@16: // of df1 and df2, all the change takes place in this area. Chris@16: // Chris@16: RealType v1x = df1 * x; Chris@16: RealType result; Chris@16: if(v1x > df2) Chris@16: { Chris@16: result = (df2 * df1) / ((df2 + v1x) * (df2 + v1x)); Chris@16: result *= ibeta_derivative(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy()); Chris@16: } Chris@16: else Chris@16: { Chris@16: result = df2 + df1 * x; Chris@16: result = (result * df1 - x * df1 * df1) / (result * result); Chris@16: result *= ibeta_derivative(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy()); Chris@16: } Chris@16: return result; Chris@16: } // pdf Chris@16: Chris@16: template Chris@16: inline RealType cdf(const fisher_f_distribution& dist, const RealType& x) Chris@16: { Chris@16: static const char* function = "boost::math::cdf(fisher_f_distribution<%1%> const&, %1%)"; Chris@16: RealType df1 = dist.degrees_of_freedom1(); Chris@16: RealType df2 = dist.degrees_of_freedom2(); Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy())) Chris@16: return error_result; Chris@16: Chris@16: if((x < 0) || !(boost::math::isfinite)(x)) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Random Variable parameter was %1%, but must be > 0 !", x, Policy()); Chris@16: } Chris@16: Chris@16: RealType v1x = df1 * x; Chris@16: // Chris@16: // There are two equivalent formulas used here, the aim is Chris@16: // to prevent the final argument to the incomplete beta Chris@16: // from being too close to 1: for some values of df1 and df2 Chris@16: // the rate of change can be arbitrarily large in this area, Chris@16: // whilst the value we're passing will have lost information Chris@16: // content as a result of being 0.999999something. Better Chris@16: // to switch things around so we're passing 1-z instead. Chris@16: // Chris@16: return v1x > df2 Chris@16: ? boost::math::ibetac(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy()) Chris@16: : boost::math::ibeta(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy()); Chris@16: } // cdf Chris@16: Chris@16: template Chris@16: inline RealType quantile(const fisher_f_distribution& dist, const RealType& p) Chris@16: { Chris@16: static const char* function = "boost::math::quantile(fisher_f_distribution<%1%> const&, %1%)"; Chris@16: RealType df1 = dist.degrees_of_freedom1(); Chris@16: RealType df2 = dist.degrees_of_freedom2(); Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == (detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy()) Chris@16: && detail::check_probability( Chris@16: function, p, &error_result, Policy()))) Chris@16: return error_result; Chris@16: Chris@16: // With optimizations turned on, gcc wrongly warns about y being used Chris@16: // uninitializated unless we initialize it to something: Chris@16: RealType x, y(0); Chris@16: Chris@16: x = boost::math::ibeta_inv(df1 / 2, df2 / 2, p, &y, Policy()); Chris@16: Chris@16: return df2 * x / (df1 * y); Chris@16: } // quantile Chris@16: Chris@16: template Chris@16: inline RealType cdf(const complemented2_type, RealType>& c) Chris@16: { Chris@16: static const char* function = "boost::math::cdf(fisher_f_distribution<%1%> const&, %1%)"; Chris@16: RealType df1 = c.dist.degrees_of_freedom1(); Chris@16: RealType df2 = c.dist.degrees_of_freedom2(); Chris@16: RealType x = c.param; Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy())) Chris@16: return error_result; Chris@16: Chris@16: if((x < 0) || !(boost::math::isfinite)(x)) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Random Variable parameter was %1%, but must be > 0 !", x, Policy()); Chris@16: } Chris@16: Chris@16: RealType v1x = df1 * x; Chris@16: // Chris@16: // There are two equivalent formulas used here, the aim is Chris@16: // to prevent the final argument to the incomplete beta Chris@16: // from being too close to 1: for some values of df1 and df2 Chris@16: // the rate of change can be arbitrarily large in this area, Chris@16: // whilst the value we're passing will have lost information Chris@16: // content as a result of being 0.999999something. Better Chris@16: // to switch things around so we're passing 1-z instead. Chris@16: // Chris@16: return v1x > df2 Chris@16: ? boost::math::ibeta(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy()) Chris@16: : boost::math::ibetac(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType quantile(const complemented2_type, RealType>& c) Chris@16: { Chris@16: static const char* function = "boost::math::quantile(fisher_f_distribution<%1%> const&, %1%)"; Chris@16: RealType df1 = c.dist.degrees_of_freedom1(); Chris@16: RealType df2 = c.dist.degrees_of_freedom2(); Chris@16: RealType p = c.param; Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == (detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy()) Chris@16: && detail::check_probability( Chris@16: function, p, &error_result, Policy()))) Chris@16: return error_result; Chris@16: Chris@16: RealType x, y; Chris@16: Chris@16: x = boost::math::ibetac_inv(df1 / 2, df2 / 2, p, &y, Policy()); Chris@16: Chris@16: return df2 * x / (df1 * y); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType mean(const fisher_f_distribution& dist) Chris@16: { // Mean of F distribution = v. Chris@16: static const char* function = "boost::math::mean(fisher_f_distribution<%1%> const&)"; Chris@16: RealType df1 = dist.degrees_of_freedom1(); Chris@16: RealType df2 = dist.degrees_of_freedom2(); Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy())) Chris@16: return error_result; Chris@16: if(df2 <= 2) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Second degree of freedom was %1% but must be > 2 in order for the distribution to have a mean.", df2, Policy()); Chris@16: } Chris@16: return df2 / (df2 - 2); Chris@16: } // mean Chris@16: Chris@16: template Chris@16: inline RealType variance(const fisher_f_distribution& dist) Chris@16: { // Variance of F distribution. Chris@16: static const char* function = "boost::math::variance(fisher_f_distribution<%1%> const&)"; Chris@16: RealType df1 = dist.degrees_of_freedom1(); Chris@16: RealType df2 = dist.degrees_of_freedom2(); Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy())) Chris@16: return error_result; Chris@16: if(df2 <= 4) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: 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: } Chris@16: return 2 * df2 * df2 * (df1 + df2 - 2) / (df1 * (df2 - 2) * (df2 - 2) * (df2 - 4)); Chris@16: } // variance Chris@16: Chris@16: template Chris@16: inline RealType mode(const fisher_f_distribution& dist) Chris@16: { Chris@16: static const char* function = "boost::math::mode(fisher_f_distribution<%1%> const&)"; Chris@16: RealType df1 = dist.degrees_of_freedom1(); Chris@16: RealType df2 = dist.degrees_of_freedom2(); Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy())) Chris@16: return error_result; Chris@16: if(df2 <= 2) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Second degree of freedom was %1% but must be > 2 in order for the distribution to have a mode.", df2, Policy()); Chris@16: } Chris@16: return df2 * (df1 - 2) / (df1 * (df2 + 2)); Chris@16: } Chris@16: Chris@16: //template Chris@16: //inline RealType median(const fisher_f_distribution& dist) Chris@16: //{ // Median of Fisher F distribution is not defined. Chris@16: // return tools::domain_error(BOOST_CURRENT_FUNCTION, "Median is not implemented, result is %1%!", std::numeric_limits::quiet_NaN()); Chris@16: // } // median Chris@16: Chris@16: // Now implemented via quantile(half) in derived accessors. Chris@16: Chris@16: template Chris@16: inline RealType skewness(const fisher_f_distribution& dist) Chris@16: { Chris@16: static const char* function = "boost::math::skewness(fisher_f_distribution<%1%> const&)"; Chris@16: BOOST_MATH_STD_USING // ADL of std names Chris@16: // See http://mathworld.wolfram.com/F-Distribution.html Chris@16: RealType df1 = dist.degrees_of_freedom1(); Chris@16: RealType df2 = dist.degrees_of_freedom2(); Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy())) Chris@16: return error_result; Chris@16: if(df2 <= 6) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Second degree of freedom was %1% but must be > 6 in order for the distribution to have a skewness.", df2, Policy()); Chris@16: } Chris@16: return 2 * (df2 + 2 * df1 - 2) * sqrt((2 * df2 - 8) / (df1 * (df2 + df1 - 2))) / (df2 - 6); Chris@16: } Chris@16: Chris@16: template Chris@16: RealType kurtosis_excess(const fisher_f_distribution& dist); Chris@16: Chris@16: template Chris@16: inline RealType kurtosis(const fisher_f_distribution& dist) Chris@16: { Chris@16: return 3 + kurtosis_excess(dist); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType kurtosis_excess(const fisher_f_distribution& dist) Chris@16: { Chris@16: static const char* function = "boost::math::kurtosis_excess(fisher_f_distribution<%1%> const&)"; Chris@16: // See http://mathworld.wolfram.com/F-Distribution.html Chris@16: RealType df1 = dist.degrees_of_freedom1(); Chris@16: RealType df2 = dist.degrees_of_freedom2(); Chris@16: // Error check: Chris@16: RealType error_result = 0; Chris@16: if(false == detail::check_df( Chris@16: function, df1, &error_result, Policy()) Chris@16: && detail::check_df( Chris@16: function, df2, &error_result, Policy())) Chris@16: return error_result; Chris@16: if(df2 <= 8) Chris@16: { Chris@16: return policies::raise_domain_error( Chris@16: function, "Second degree of freedom was %1% but must be > 8 in order for the distribution to have a kutosis.", df2, Policy()); Chris@16: } Chris@16: RealType df2_2 = df2 * df2; Chris@16: RealType df1_2 = df1 * df1; Chris@16: 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: n *= 12; Chris@16: RealType d = df1 * (df2 - 6) * (df2 - 8) * (df1 + df2 - 2); Chris@16: return n / d; 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_FISHER_F_HPP