1 // Copyright John Maddock 2010.
2 // Copyright Paul A. Bristow 2010.
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
10 #define BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
12 #include <boost/math/distributions/fwd.hpp>
13 #include <boost/math/special_functions/gamma.hpp> // for incomplete beta.
14 #include <boost/math/distributions/complement.hpp> // for complements.
15 #include <boost/math/distributions/detail/common_error_handling.hpp> // for error checks.
16 #include <boost/math/special_functions/fpclassify.hpp> // for isfinite
18 // See http://en.wikipedia.org/wiki/Scaled-inverse-chi-square_distribution
19 // for definitions of this scaled version.
20 // See http://en.wikipedia.org/wiki/Inverse-chi-square_distribution
21 // for unscaled version.
23 // http://reference.wolfram.com/mathematica/ref/InverseChiSquareDistribution.html
24 // Weisstein, Eric W. "Inverse Chi-Squared Distribution." From MathWorld--A Wolfram Web Resource.
25 // http://mathworld.wolfram.com/InverseChi-SquaredDistribution.html
29 namespace boost{ namespace math{
33 template <class RealType, class Policy>
34 inline bool check_inverse_chi_squared( // Check both distribution parameters.
36 RealType degrees_of_freedom, // degrees_of_freedom (aka nu).
37 RealType scale, // scale (aka sigma^2)
41 return check_scale(function, scale, result, pol)
42 && check_df(function, degrees_of_freedom,
44 } // bool check_inverse_chi_squared
47 template <class RealType = double, class Policy = policies::policy<> >
48 class inverse_chi_squared_distribution
51 typedef RealType value_type;
52 typedef Policy policy_type;
54 inverse_chi_squared_distribution(RealType df, RealType l_scale) : m_df(df), m_scale (l_scale)
58 "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
59 m_df, &result, Policy())
60 && detail::check_scale(
61 "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
62 m_scale, &result, Policy());
63 } // inverse_chi_squared_distribution constructor
65 inverse_chi_squared_distribution(RealType df = 1) : m_df(df)
68 m_scale = 1 / m_df ; // Default scale = 1 / degrees of freedom (Wikipedia definition 1).
70 "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
71 m_df, &result, Policy());
72 } // inverse_chi_squared_distribution
74 RealType degrees_of_freedom()const
76 return m_df; // aka nu
80 return m_scale; // aka xi
83 // Parameter estimation: NOT implemented yet.
84 //static RealType find_degrees_of_freedom(
85 // RealType difference_from_variance,
89 // RealType hint = 100);
93 RealType m_df; // degrees of freedom are treated as a real number.
94 RealType m_scale; // distribution scale.
96 }; // class chi_squared_distribution
98 typedef inverse_chi_squared_distribution<double> inverse_chi_squared;
100 #ifdef __cpp_deduction_guides
101 template <class RealType>
102 inverse_chi_squared_distribution(RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
103 template <class RealType>
104 inverse_chi_squared_distribution(RealType,RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
107 template <class RealType, class Policy>
108 inline const std::pair<RealType, RealType> range(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
109 { // Range of permissible values for random variable x.
110 using boost::math::tools::max_value;
111 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + infinity.
114 template <class RealType, class Policy>
115 inline const std::pair<RealType, RealType> support(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
116 { // Range of supported values for random variable x.
117 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
118 return std::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>()); // 0 to + infinity.
121 template <class RealType, class Policy>
122 RealType pdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
124 BOOST_MATH_STD_USING // for ADL of std functions.
125 RealType df = dist.degrees_of_freedom();
126 RealType scale = dist.scale();
127 RealType error_result;
129 static const char* function = "boost::math::pdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
131 if(false == detail::check_inverse_chi_squared
132 (function, df, scale, &error_result, Policy())
134 { // Bad distribution.
137 if((x < 0) || !(boost::math::isfinite)(x))
139 return policies::raise_domain_error<RealType>(
140 function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
144 { // Treat as special case.
147 // Wikipedia scaled inverse chi sq (df, scale) related to inv gamma (df/2, df * scale /2)
148 // so use inverse gamma pdf with shape = df/2, scale df * scale /2
149 // RealType shape = df /2; // inv_gamma shape
150 // RealType scale = df * scale/2; // inv_gamma scale
151 // RealType result = gamma_p_derivative(shape, scale / x, Policy()) * scale / (x * x);
152 RealType result = df * scale/2 / x;
153 if(result < tools::min_value<RealType>())
154 return 0; // Random variable is near enough infinite.
155 result = gamma_p_derivative(df/2, result, Policy()) * df * scale/2;
156 if(result != 0) // prevent 0 / 0, gamma_p_derivative -> 0 faster than x^2
161 template <class RealType, class Policy>
162 inline RealType cdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
164 static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
165 RealType df = dist.degrees_of_freedom();
166 RealType scale = dist.scale();
167 RealType error_result;
170 detail::check_inverse_chi_squared(function, df, scale, &error_result, Policy())
172 { // Bad distribution.
175 if((x < 0) || !(boost::math::isfinite)(x))
177 return policies::raise_domain_error<RealType>(
178 function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
181 { // Treat zero as a special case.
184 // RealType shape = df /2; // inv_gamma shape,
185 // RealType scale = df * scale/2; // inv_gamma scale,
186 // result = boost::math::gamma_q(shape, scale / x, Policy()); // inverse_gamma code.
187 return boost::math::gamma_q(df / 2, (df * (scale / 2)) / x, Policy());
190 template <class RealType, class Policy>
191 inline RealType quantile(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& p)
193 using boost::math::gamma_q_inv;
194 RealType df = dist.degrees_of_freedom();
195 RealType scale = dist.scale();
197 static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
199 RealType error_result;
200 if(false == detail::check_df(
201 function, df, &error_result, Policy())
202 && detail::check_probability(
203 function, p, &error_result, Policy()))
207 if(false == detail::check_probability(
208 function, p, &error_result, Policy()))
212 // RealType shape = df /2; // inv_gamma shape,
213 // RealType scale = df * scale/2; // inv_gamma scale,
214 // result = scale / gamma_q_inv(shape, p, Policy());
215 RealType result = gamma_q_inv(df /2, p, Policy());
217 return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
218 result = df * (scale / 2) / result;
222 template <class RealType, class Policy>
223 inline RealType cdf(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
225 using boost::math::gamma_q_inv;
226 RealType const& df = c.dist.degrees_of_freedom();
227 RealType const& scale = c.dist.scale();
228 RealType const& x = c.param;
229 static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
231 RealType error_result;
232 if(false == detail::check_df(
233 function, df, &error_result, Policy()))
238 { // Treat zero as a special case.
241 if((x < 0) || !(boost::math::isfinite)(x))
243 return policies::raise_domain_error<RealType>(
244 function, "inverse Chi Square parameter was %1%, but must be > 0 !", x, Policy());
246 // RealType shape = df /2; // inv_gamma shape,
247 // RealType scale = df * scale/2; // inv_gamma scale,
248 // result = gamma_p(shape, scale/c.param, Policy()); use inv_gamma.
250 return gamma_p(df / 2, (df * scale/2) / x, Policy()); // OK
251 } // cdf(complemented
253 template <class RealType, class Policy>
254 inline RealType quantile(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
256 using boost::math::gamma_q_inv;
258 RealType const& df = c.dist.degrees_of_freedom();
259 RealType const& scale = c.dist.scale();
260 RealType const& q = c.param;
261 static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
263 RealType error_result;
264 if(false == detail::check_df(function, df, &error_result, Policy()))
268 if(false == detail::check_probability(function, q, &error_result, Policy()))
272 // RealType shape = df /2; // inv_gamma shape,
273 // RealType scale = df * scale/2; // inv_gamma scale,
274 // result = scale / gamma_p_inv(shape, q, Policy()); // using inv_gamma.
275 RealType result = gamma_p_inv(df/2, q, Policy());
277 return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
278 result = (df * scale / 2) / result;
280 } // quantile(const complement
282 template <class RealType, class Policy>
283 inline RealType mean(const inverse_chi_squared_distribution<RealType, Policy>& dist)
284 { // Mean of inverse Chi-Squared distribution.
285 RealType df = dist.degrees_of_freedom();
286 RealType scale = dist.scale();
288 static const char* function = "boost::math::mean(const inverse_chi_squared_distribution<%1%>&)";
290 return policies::raise_domain_error<RealType>(
292 "inverse Chi-Squared distribution only has a mode for degrees of freedom > 2, but got degrees of freedom = %1%.",
294 return (df * scale) / (df - 2);
297 template <class RealType, class Policy>
298 inline RealType variance(const inverse_chi_squared_distribution<RealType, Policy>& dist)
299 { // Variance of inverse Chi-Squared distribution.
300 RealType df = dist.degrees_of_freedom();
301 RealType scale = dist.scale();
302 static const char* function = "boost::math::variance(const inverse_chi_squared_distribution<%1%>&)";
305 return policies::raise_domain_error<RealType>(
307 "inverse Chi-Squared distribution only has a variance for degrees of freedom > 4, but got degrees of freedom = %1%.",
310 return 2 * df * df * scale * scale / ((df - 2)*(df - 2) * (df - 4));
313 template <class RealType, class Policy>
314 inline RealType mode(const inverse_chi_squared_distribution<RealType, Policy>& dist)
315 { // mode is not defined in Mathematica.
316 // See Discussion section http://en.wikipedia.org/wiki/Talk:Scaled-inverse-chi-square_distribution
317 // for origin of the formula used below.
319 RealType df = dist.degrees_of_freedom();
320 RealType scale = dist.scale();
321 static const char* function = "boost::math::mode(const inverse_chi_squared_distribution<%1%>&)";
323 return policies::raise_domain_error<RealType>(
325 "inverse Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.",
327 return (df * scale) / (df + 2);
330 //template <class RealType, class Policy>
331 //inline RealType median(const inverse_chi_squared_distribution<RealType, Policy>& dist)
332 //{ // Median is given by Quantile[dist, 1/2]
333 // RealType df = dist.degrees_of_freedom();
335 // return tools::domain_error<RealType>(
336 // BOOST_CURRENT_FUNCTION,
337 // "The inverse_Chi-Squared distribution only has a median for degrees of freedom >= 0, but got degrees of freedom = %1%.",
341 // Now implemented via quantile(half) in derived accessors.
343 template <class RealType, class Policy>
344 inline RealType skewness(const inverse_chi_squared_distribution<RealType, Policy>& dist)
346 BOOST_MATH_STD_USING // For ADL
347 RealType df = dist.degrees_of_freedom();
348 static const char* function = "boost::math::skewness(const inverse_chi_squared_distribution<%1%>&)";
350 return policies::raise_domain_error<RealType>(
352 "inverse Chi-Squared distribution only has a skewness for degrees of freedom > 6, but got degrees of freedom = %1%.",
355 return 4 * sqrt (2 * (df - 4)) / (df - 6); // Not a function of scale.
358 template <class RealType, class Policy>
359 inline RealType kurtosis(const inverse_chi_squared_distribution<RealType, Policy>& dist)
361 RealType df = dist.degrees_of_freedom();
362 static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
364 return policies::raise_domain_error<RealType>(
366 "inverse Chi-Squared distribution only has a kurtosis for degrees of freedom > 8, but got degrees of freedom = %1%.",
369 return kurtosis_excess(dist) + 3;
372 template <class RealType, class Policy>
373 inline RealType kurtosis_excess(const inverse_chi_squared_distribution<RealType, Policy>& dist)
375 RealType df = dist.degrees_of_freedom();
376 static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
378 return policies::raise_domain_error<RealType>(
380 "inverse Chi-Squared distribution only has a kurtosis excess for degrees of freedom > 8, but got degrees of freedom = %1%.",
383 return 12 * (5 * df - 22) / ((df - 6 )*(df - 8)); // Not a function of scale.
387 // Parameter estimation comes last:
393 // This include must be at the end, *after* the accessors
394 // for this distribution have been defined, in order to
395 // keep compilers that support two-phase lookup happy.
396 #include <boost/math/distributions/detail/derived_accessors.hpp>
398 #endif // BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP