123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- #ifndef BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
- #define BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
- #include <boost/math/distributions/fwd.hpp>
- #include <boost/math/special_functions/gamma.hpp>
- #include <boost/math/distributions/complement.hpp>
- #include <boost/math/distributions/detail/common_error_handling.hpp>
- #include <boost/math/special_functions/fpclassify.hpp>
- #include <utility>
- namespace boost{ namespace math{
- namespace detail
- {
- template <class RealType, class Policy>
- inline bool check_inverse_chi_squared(
- const char* function,
- RealType degrees_of_freedom,
- RealType scale,
- RealType* result,
- const Policy& pol)
- {
- return check_scale(function, scale, result, pol)
- && check_df(function, degrees_of_freedom,
- result, pol);
- }
- }
- template <class RealType = double, class Policy = policies::policy<> >
- class inverse_chi_squared_distribution
- {
- public:
- typedef RealType value_type;
- typedef Policy policy_type;
- inverse_chi_squared_distribution(RealType df, RealType l_scale) : m_df(df), m_scale (l_scale)
- {
- RealType result;
- detail::check_df(
- "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
- m_df, &result, Policy())
- && detail::check_scale(
- "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
- m_scale, &result, Policy());
- }
- inverse_chi_squared_distribution(RealType df = 1) : m_df(df)
- {
- RealType result;
- m_scale = 1 / m_df ;
- detail::check_df(
- "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
- m_df, &result, Policy());
- }
- RealType degrees_of_freedom()const
- {
- return m_df;
- }
- RealType scale()const
- {
- return m_scale;
- }
-
-
-
-
-
-
-
- private:
-
- RealType m_df;
- RealType m_scale;
- };
- typedef inverse_chi_squared_distribution<double> inverse_chi_squared;
- #ifdef __cpp_deduction_guides
- template <class RealType>
- inverse_chi_squared_distribution(RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
- template <class RealType>
- inverse_chi_squared_distribution(RealType,RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
- #endif
- template <class RealType, class Policy>
- inline const std::pair<RealType, RealType> range(const inverse_chi_squared_distribution<RealType, Policy>& )
- {
- using boost::math::tools::max_value;
- return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
- }
- template <class RealType, class Policy>
- inline const std::pair<RealType, RealType> support(const inverse_chi_squared_distribution<RealType, Policy>& )
- {
-
- return std::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>());
- }
- template <class RealType, class Policy>
- RealType pdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
- {
- BOOST_MATH_STD_USING
- RealType df = dist.degrees_of_freedom();
- RealType scale = dist.scale();
- RealType error_result;
- static const char* function = "boost::math::pdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
- if(false == detail::check_inverse_chi_squared
- (function, df, scale, &error_result, Policy())
- )
- {
- return error_result;
- }
- if((x < 0) || !(boost::math::isfinite)(x))
- {
- return policies::raise_domain_error<RealType>(
- function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
- }
- if(x == 0)
- {
- return 0;
- }
-
-
-
-
-
- RealType result = df * scale/2 / x;
- if(result < tools::min_value<RealType>())
- return 0;
- result = gamma_p_derivative(df/2, result, Policy()) * df * scale/2;
- if(result != 0)
- result /= (x * x);
- return result;
- }
- template <class RealType, class Policy>
- inline RealType cdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
- {
- static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
- RealType df = dist.degrees_of_freedom();
- RealType scale = dist.scale();
- RealType error_result;
- if(false ==
- detail::check_inverse_chi_squared(function, df, scale, &error_result, Policy())
- )
- {
- return error_result;
- }
- if((x < 0) || !(boost::math::isfinite)(x))
- {
- return policies::raise_domain_error<RealType>(
- function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
- }
- if (x == 0)
- {
- return 0;
- }
-
-
-
- return boost::math::gamma_q(df / 2, (df * (scale / 2)) / x, Policy());
- }
- template <class RealType, class Policy>
- inline RealType quantile(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& p)
- {
- using boost::math::gamma_q_inv;
- RealType df = dist.degrees_of_freedom();
- RealType scale = dist.scale();
- static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
-
- RealType error_result;
- if(false == detail::check_df(
- function, df, &error_result, Policy())
- && detail::check_probability(
- function, p, &error_result, Policy()))
- {
- return error_result;
- }
- if(false == detail::check_probability(
- function, p, &error_result, Policy()))
- {
- return error_result;
- }
-
-
-
- RealType result = gamma_q_inv(df /2, p, Policy());
- if(result == 0)
- return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
- result = df * (scale / 2) / result;
- return result;
- }
- template <class RealType, class Policy>
- inline RealType cdf(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
- {
- using boost::math::gamma_q_inv;
- RealType const& df = c.dist.degrees_of_freedom();
- RealType const& scale = c.dist.scale();
- RealType const& x = c.param;
- static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
-
- RealType error_result;
- if(false == detail::check_df(
- function, df, &error_result, Policy()))
- {
- return error_result;
- }
- if (x == 0)
- {
- return 1;
- }
- if((x < 0) || !(boost::math::isfinite)(x))
- {
- return policies::raise_domain_error<RealType>(
- function, "inverse Chi Square parameter was %1%, but must be > 0 !", x, Policy());
- }
-
-
-
- return gamma_p(df / 2, (df * scale/2) / x, Policy());
- }
- template <class RealType, class Policy>
- inline RealType quantile(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
- {
- using boost::math::gamma_q_inv;
- RealType const& df = c.dist.degrees_of_freedom();
- RealType const& scale = c.dist.scale();
- RealType const& q = c.param;
- static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
-
- RealType error_result;
- if(false == detail::check_df(function, df, &error_result, Policy()))
- {
- return error_result;
- }
- if(false == detail::check_probability(function, q, &error_result, Policy()))
- {
- return error_result;
- }
-
-
-
- RealType result = gamma_p_inv(df/2, q, Policy());
- if(result == 0)
- return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
- result = (df * scale / 2) / result;
- return result;
- }
- template <class RealType, class Policy>
- inline RealType mean(const inverse_chi_squared_distribution<RealType, Policy>& dist)
- {
- RealType df = dist.degrees_of_freedom();
- RealType scale = dist.scale();
- static const char* function = "boost::math::mean(const inverse_chi_squared_distribution<%1%>&)";
- if(df <= 2)
- return policies::raise_domain_error<RealType>(
- function,
- "inverse Chi-Squared distribution only has a mode for degrees of freedom > 2, but got degrees of freedom = %1%.",
- df, Policy());
- return (df * scale) / (df - 2);
- }
- template <class RealType, class Policy>
- inline RealType variance(const inverse_chi_squared_distribution<RealType, Policy>& dist)
- {
- RealType df = dist.degrees_of_freedom();
- RealType scale = dist.scale();
- static const char* function = "boost::math::variance(const inverse_chi_squared_distribution<%1%>&)";
- if(df <= 4)
- {
- return policies::raise_domain_error<RealType>(
- function,
- "inverse Chi-Squared distribution only has a variance for degrees of freedom > 4, but got degrees of freedom = %1%.",
- df, Policy());
- }
- return 2 * df * df * scale * scale / ((df - 2)*(df - 2) * (df - 4));
- }
- template <class RealType, class Policy>
- inline RealType mode(const inverse_chi_squared_distribution<RealType, Policy>& dist)
- {
-
-
- RealType df = dist.degrees_of_freedom();
- RealType scale = dist.scale();
- static const char* function = "boost::math::mode(const inverse_chi_squared_distribution<%1%>&)";
- if(df < 0)
- return policies::raise_domain_error<RealType>(
- function,
- "inverse Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.",
- df, Policy());
- return (df * scale) / (df + 2);
- }
- template <class RealType, class Policy>
- inline RealType skewness(const inverse_chi_squared_distribution<RealType, Policy>& dist)
- {
- BOOST_MATH_STD_USING
- RealType df = dist.degrees_of_freedom();
- static const char* function = "boost::math::skewness(const inverse_chi_squared_distribution<%1%>&)";
- if(df <= 6)
- return policies::raise_domain_error<RealType>(
- function,
- "inverse Chi-Squared distribution only has a skewness for degrees of freedom > 6, but got degrees of freedom = %1%.",
- df, Policy());
- return 4 * sqrt (2 * (df - 4)) / (df - 6);
- }
- template <class RealType, class Policy>
- inline RealType kurtosis(const inverse_chi_squared_distribution<RealType, Policy>& dist)
- {
- RealType df = dist.degrees_of_freedom();
- static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
- if(df <= 8)
- return policies::raise_domain_error<RealType>(
- function,
- "inverse Chi-Squared distribution only has a kurtosis for degrees of freedom > 8, but got degrees of freedom = %1%.",
- df, Policy());
- return kurtosis_excess(dist) + 3;
- }
- template <class RealType, class Policy>
- inline RealType kurtosis_excess(const inverse_chi_squared_distribution<RealType, Policy>& dist)
- {
- RealType df = dist.degrees_of_freedom();
- static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
- if(df <= 8)
- return policies::raise_domain_error<RealType>(
- function,
- "inverse Chi-Squared distribution only has a kurtosis excess for degrees of freedom > 8, but got degrees of freedom = %1%.",
- df, Policy());
- return 12 * (5 * df - 22) / ((df - 6 )*(df - 8));
- }
- }
- }
- #include <boost/math/distributions/detail/derived_accessors.hpp>
- #endif
|