cannam@160: // inverse_gamma.hpp cannam@160: cannam@160: // Copyright Paul A. Bristow 2010. cannam@160: // Copyright John Maddock 2010. cannam@160: // Use, modification and distribution are subject to the cannam@160: // Boost Software License, Version 1.0. (See accompanying file cannam@160: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) cannam@160: cannam@160: #ifndef BOOST_STATS_INVERSE_GAMMA_HPP cannam@160: #define BOOST_STATS_INVERSE_GAMMA_HPP cannam@160: cannam@160: // Inverse Gamma Distribution is a two-parameter family cannam@160: // of continuous probability distributions cannam@160: // on the positive real line, which is the distribution of cannam@160: // the reciprocal of a variable distributed according to the gamma distribution. cannam@160: cannam@160: // http://en.wikipedia.org/wiki/Inverse-gamma_distribution cannam@160: // http://rss.acs.unt.edu/Rdoc/library/pscl/html/igamma.html cannam@160: cannam@160: // See also gamma distribution at gamma.hpp: cannam@160: // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366b.htm cannam@160: // http://mathworld.wolfram.com/GammaDistribution.html cannam@160: // http://en.wikipedia.org/wiki/Gamma_distribution cannam@160: cannam@160: #include cannam@160: #include cannam@160: #include cannam@160: #include cannam@160: cannam@160: #include cannam@160: cannam@160: namespace boost{ namespace math cannam@160: { cannam@160: namespace detail cannam@160: { cannam@160: cannam@160: template cannam@160: inline bool check_inverse_gamma_shape( cannam@160: const char* function, // inverse_gamma cannam@160: RealType shape, // shape aka alpha cannam@160: RealType* result, // to update, perhaps with NaN cannam@160: const Policy& pol) cannam@160: { // Sources say shape argument must be > 0 cannam@160: // but seems logical to allow shape zero as special case, cannam@160: // returning pdf and cdf zero (but not < 0). cannam@160: // (Functions like mean, variance with other limits on shape are checked cannam@160: // in version including an operator & limit below). cannam@160: if((shape < 0) || !(boost::math::isfinite)(shape)) cannam@160: { cannam@160: *result = policies::raise_domain_error( cannam@160: function, cannam@160: "Shape parameter is %1%, but must be >= 0 !", shape, pol); cannam@160: return false; cannam@160: } cannam@160: return true; cannam@160: } //bool check_inverse_gamma_shape cannam@160: cannam@160: template cannam@160: inline bool check_inverse_gamma_x( cannam@160: const char* function, cannam@160: RealType const& x, cannam@160: RealType* result, const Policy& pol) cannam@160: { cannam@160: if((x < 0) || !(boost::math::isfinite)(x)) cannam@160: { cannam@160: *result = policies::raise_domain_error( cannam@160: function, cannam@160: "Random variate is %1% but must be >= 0 !", x, pol); cannam@160: return false; cannam@160: } cannam@160: return true; cannam@160: } cannam@160: cannam@160: template cannam@160: inline bool check_inverse_gamma( cannam@160: const char* function, // TODO swap these over, so shape is first. cannam@160: RealType scale, // scale aka beta cannam@160: RealType shape, // shape aka alpha cannam@160: RealType* result, const Policy& pol) cannam@160: { cannam@160: return check_scale(function, scale, result, pol) cannam@160: && check_inverse_gamma_shape(function, shape, result, pol); cannam@160: } // bool check_inverse_gamma cannam@160: cannam@160: } // namespace detail cannam@160: cannam@160: template > cannam@160: class inverse_gamma_distribution cannam@160: { cannam@160: public: cannam@160: typedef RealType value_type; cannam@160: typedef Policy policy_type; cannam@160: cannam@160: inverse_gamma_distribution(RealType l_shape = 1, RealType l_scale = 1) cannam@160: : m_shape(l_shape), m_scale(l_scale) cannam@160: { cannam@160: RealType result; cannam@160: detail::check_inverse_gamma( cannam@160: "boost::math::inverse_gamma_distribution<%1%>::inverse_gamma_distribution", cannam@160: l_scale, l_shape, &result, Policy()); cannam@160: } cannam@160: cannam@160: RealType shape()const cannam@160: { cannam@160: return m_shape; cannam@160: } cannam@160: cannam@160: RealType scale()const cannam@160: { cannam@160: return m_scale; cannam@160: } cannam@160: private: cannam@160: // cannam@160: // Data members: cannam@160: // cannam@160: RealType m_shape; // distribution shape cannam@160: RealType m_scale; // distribution scale cannam@160: }; cannam@160: cannam@160: typedef inverse_gamma_distribution inverse_gamma; cannam@160: // typedef - but potential clash with name of inverse gamma *function*. cannam@160: // but there is a typedef for gamma cannam@160: // typedef boost::math::gamma_distribution gamma; cannam@160: cannam@160: // Allow random variable x to be zero, treated as a special case (unlike some definitions). cannam@160: cannam@160: template cannam@160: inline const std::pair range(const inverse_gamma_distribution& /* dist */) cannam@160: { // Range of permissible values for random variable x. cannam@160: using boost::math::tools::max_value; cannam@160: return std::pair(static_cast(0), max_value()); cannam@160: } cannam@160: cannam@160: template cannam@160: inline const std::pair support(const inverse_gamma_distribution& /* dist */) cannam@160: { // Range of supported values for random variable x. cannam@160: // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero. cannam@160: using boost::math::tools::max_value; cannam@160: using boost::math::tools::min_value; cannam@160: return std::pair(static_cast(0), max_value()); cannam@160: } cannam@160: cannam@160: template cannam@160: inline RealType pdf(const inverse_gamma_distribution& dist, const RealType& x) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::pdf(const inverse_gamma_distribution<%1%>&, %1%)"; cannam@160: cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: if(false == detail::check_inverse_gamma(function, scale, shape, &result, Policy())) cannam@160: { // distribution parameters bad. cannam@160: return result; cannam@160: } cannam@160: if(x == 0) cannam@160: { // Treat random variate zero as a special case. cannam@160: return 0; cannam@160: } cannam@160: else if(false == detail::check_inverse_gamma_x(function, x, &result, Policy())) cannam@160: { // x bad. cannam@160: return result; cannam@160: } cannam@160: result = scale / x; cannam@160: if(result < tools::min_value()) cannam@160: return 0; // random variable is infinite or so close as to make no difference. cannam@160: result = gamma_p_derivative(shape, result, Policy()) * scale; cannam@160: if(0 != result) cannam@160: { cannam@160: if(x < 0) cannam@160: { cannam@160: // x * x may under or overflow, likewise our result, cannam@160: // so be extra careful about the arithmetic: cannam@160: RealType lim = tools::max_value() * x; cannam@160: if(lim < result) cannam@160: return policies::raise_overflow_error(function, "PDF is infinite.", Policy()); cannam@160: result /= x; cannam@160: if(lim < result) cannam@160: return policies::raise_overflow_error(function, "PDF is infinite.", Policy()); cannam@160: result /= x; cannam@160: } cannam@160: result /= (x * x); cannam@160: } cannam@160: // better than naive cannam@160: // result = (pow(scale, shape) * pow(x, (-shape -1)) * exp(-scale/x) ) / tgamma(shape); cannam@160: return result; cannam@160: } // pdf cannam@160: cannam@160: template cannam@160: inline RealType cdf(const inverse_gamma_distribution& dist, const RealType& x) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::cdf(const inverse_gamma_distribution<%1%>&, %1%)"; cannam@160: cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: if(false == detail::check_inverse_gamma(function, scale, shape, &result, Policy())) cannam@160: { // distribution parameters bad. cannam@160: return result; cannam@160: } cannam@160: if (x == 0) cannam@160: { // Treat zero as a special case. cannam@160: return 0; cannam@160: } cannam@160: else if(false == detail::check_inverse_gamma_x(function, x, &result, Policy())) cannam@160: { // x bad cannam@160: return result; cannam@160: } cannam@160: result = boost::math::gamma_q(shape, scale / x, Policy()); cannam@160: // result = tgamma(shape, scale / x) / tgamma(shape); // naive using tgamma cannam@160: return result; cannam@160: } // cdf cannam@160: cannam@160: template cannam@160: inline RealType quantile(const inverse_gamma_distribution& dist, const RealType& p) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: using boost::math::gamma_q_inv; cannam@160: cannam@160: static const char* function = "boost::math::quantile(const inverse_gamma_distribution<%1%>&, %1%)"; cannam@160: cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: if(false == detail::check_inverse_gamma(function, scale, shape, &result, Policy())) cannam@160: return result; cannam@160: if(false == detail::check_probability(function, p, &result, Policy())) cannam@160: return result; cannam@160: if(p == 1) cannam@160: { cannam@160: return policies::raise_overflow_error(function, 0, Policy()); cannam@160: } cannam@160: result = gamma_q_inv(shape, p, Policy()); cannam@160: if((result < 1) && (result * tools::max_value() < scale)) cannam@160: return policies::raise_overflow_error(function, "Value of random variable in inverse gamma distribution quantile is infinite.", Policy()); cannam@160: result = scale / result; cannam@160: return result; cannam@160: } cannam@160: cannam@160: template cannam@160: inline RealType cdf(const complemented2_type, RealType>& c) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::quantile(const gamma_distribution<%1%>&, %1%)"; cannam@160: cannam@160: RealType shape = c.dist.shape(); cannam@160: RealType scale = c.dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: if(false == detail::check_inverse_gamma(function, scale, shape, &result, Policy())) cannam@160: return result; cannam@160: if(false == detail::check_inverse_gamma_x(function, c.param, &result, Policy())) cannam@160: return result; cannam@160: cannam@160: if(c.param == 0) cannam@160: return 1; // Avoid division by zero cannam@160: cannam@160: //result = 1. - gamma_q(shape, c.param / scale, Policy()); cannam@160: result = gamma_p(shape, scale/c.param, Policy()); cannam@160: return result; cannam@160: } cannam@160: cannam@160: template cannam@160: inline RealType quantile(const complemented2_type, RealType>& c) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::quantile(const inverse_gamma_distribution<%1%>&, %1%)"; cannam@160: cannam@160: RealType shape = c.dist.shape(); cannam@160: RealType scale = c.dist.scale(); cannam@160: RealType q = c.param; cannam@160: cannam@160: RealType result = 0; cannam@160: if(false == detail::check_inverse_gamma(function, scale, shape, &result, Policy())) cannam@160: return result; cannam@160: if(false == detail::check_probability(function, q, &result, Policy())) cannam@160: return result; cannam@160: cannam@160: if(q == 0) cannam@160: { cannam@160: return policies::raise_overflow_error(function, 0, Policy()); cannam@160: } cannam@160: result = gamma_p_inv(shape, q, Policy()); cannam@160: if((result < 1) && (result * tools::max_value() < scale)) cannam@160: return policies::raise_overflow_error(function, "Value of random variable in inverse gamma distribution quantile is infinite.", Policy()); cannam@160: result = scale / result; cannam@160: return result; cannam@160: } cannam@160: cannam@160: template cannam@160: inline RealType mean(const inverse_gamma_distribution& dist) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::mean(const inverse_gamma_distribution<%1%>&)"; cannam@160: cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: cannam@160: if(false == detail::check_scale(function, scale, &result, Policy())) cannam@160: { cannam@160: return result; cannam@160: } cannam@160: if((shape <= 1) || !(boost::math::isfinite)(shape)) cannam@160: { cannam@160: result = policies::raise_domain_error( cannam@160: function, cannam@160: "Shape parameter is %1%, but for a defined mean it must be > 1", shape, Policy()); cannam@160: return result; cannam@160: } cannam@160: result = scale / (shape - 1); cannam@160: return result; cannam@160: } // mean cannam@160: cannam@160: template cannam@160: inline RealType variance(const inverse_gamma_distribution& dist) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::variance(const inverse_gamma_distribution<%1%>&)"; cannam@160: cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: if(false == detail::check_scale(function, scale, &result, Policy())) cannam@160: { cannam@160: return result; cannam@160: } cannam@160: if((shape <= 2) || !(boost::math::isfinite)(shape)) cannam@160: { cannam@160: result = policies::raise_domain_error( cannam@160: function, cannam@160: "Shape parameter is %1%, but for a defined variance it must be > 2", shape, Policy()); cannam@160: return result; cannam@160: } cannam@160: result = (scale * scale) / ((shape - 1) * (shape -1) * (shape -2)); cannam@160: return result; cannam@160: } cannam@160: cannam@160: template cannam@160: inline RealType mode(const inverse_gamma_distribution& dist) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::mode(const inverse_gamma_distribution<%1%>&)"; cannam@160: cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: if(false == detail::check_inverse_gamma(function, scale, shape, &result, Policy())) cannam@160: { cannam@160: return result; cannam@160: } cannam@160: // Only defined for shape >= 0, but is checked by check_inverse_gamma. cannam@160: result = scale / (shape + 1); cannam@160: return result; cannam@160: } cannam@160: cannam@160: //template cannam@160: //inline RealType median(const gamma_distribution& dist) cannam@160: //{ // Wikipedia does not define median, cannam@160: // so rely on default definition quantile(0.5) in derived accessors. cannam@160: // return result. cannam@160: //} cannam@160: cannam@160: template cannam@160: inline RealType skewness(const inverse_gamma_distribution& dist) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::skewness(const inverse_gamma_distribution<%1%>&)"; cannam@160: cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: RealType result = 0; cannam@160: cannam@160: if(false == detail::check_scale(function, scale, &result, Policy())) cannam@160: { cannam@160: return result; cannam@160: } cannam@160: if((shape <= 3) || !(boost::math::isfinite)(shape)) cannam@160: { cannam@160: result = policies::raise_domain_error( cannam@160: function, cannam@160: "Shape parameter is %1%, but for a defined skewness it must be > 3", shape, Policy()); cannam@160: return result; cannam@160: } cannam@160: result = (4 * sqrt(shape - 2) ) / (shape - 3); cannam@160: return result; cannam@160: } cannam@160: cannam@160: template cannam@160: inline RealType kurtosis_excess(const inverse_gamma_distribution& dist) cannam@160: { cannam@160: BOOST_MATH_STD_USING // for ADL of std functions cannam@160: cannam@160: static const char* function = "boost::math::kurtosis_excess(const inverse_gamma_distribution<%1%>&)"; cannam@160: cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: if(false == detail::check_scale(function, scale, &result, Policy())) cannam@160: { cannam@160: return result; cannam@160: } cannam@160: if((shape <= 4) || !(boost::math::isfinite)(shape)) cannam@160: { cannam@160: result = policies::raise_domain_error( cannam@160: function, cannam@160: "Shape parameter is %1%, but for a defined kurtosis excess it must be > 4", shape, Policy()); cannam@160: return result; cannam@160: } cannam@160: result = (30 * shape - 66) / ((shape - 3) * (shape - 4)); cannam@160: return result; cannam@160: } cannam@160: cannam@160: template cannam@160: inline RealType kurtosis(const inverse_gamma_distribution& dist) cannam@160: { cannam@160: static const char* function = "boost::math::kurtosis(const inverse_gamma_distribution<%1%>&)"; cannam@160: RealType shape = dist.shape(); cannam@160: RealType scale = dist.scale(); cannam@160: cannam@160: RealType result = 0; cannam@160: cannam@160: if(false == detail::check_scale(function, scale, &result, Policy())) cannam@160: { cannam@160: return result; cannam@160: } cannam@160: if((shape <= 4) || !(boost::math::isfinite)(shape)) cannam@160: { cannam@160: result = policies::raise_domain_error( cannam@160: function, cannam@160: "Shape parameter is %1%, but for a defined kurtosis it must be > 4", shape, Policy()); cannam@160: return result; cannam@160: } cannam@160: return kurtosis_excess(dist) + 3; cannam@160: } cannam@160: cannam@160: } // namespace math cannam@160: } // namespace boost cannam@160: cannam@160: // This include must be at the end, *after* the accessors cannam@160: // for this distribution have been defined, in order to cannam@160: // keep compilers that support two-phase lookup happy. cannam@160: #include cannam@160: cannam@160: #endif // BOOST_STATS_INVERSE_GAMMA_HPP