inverse_chi_squared.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Copyright John Maddock 2010.
  2. // Copyright Paul A. Bristow 2010.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
  8. #define BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP
  9. #include <boost/math/distributions/fwd.hpp>
  10. #include <boost/math/special_functions/gamma.hpp> // for incomplete beta.
  11. #include <boost/math/distributions/complement.hpp> // for complements.
  12. #include <boost/math/distributions/detail/common_error_handling.hpp> // for error checks.
  13. #include <boost/math/special_functions/fpclassify.hpp> // for isfinite
  14. // See http://en.wikipedia.org/wiki/Scaled-inverse-chi-square_distribution
  15. // for definitions of this scaled version.
  16. // See http://en.wikipedia.org/wiki/Inverse-chi-square_distribution
  17. // for unscaled version.
  18. // http://reference.wolfram.com/mathematica/ref/InverseChiSquareDistribution.html
  19. // Weisstein, Eric W. "Inverse Chi-Squared Distribution." From MathWorld--A Wolfram Web Resource.
  20. // http://mathworld.wolfram.com/InverseChi-SquaredDistribution.html
  21. #include <utility>
  22. namespace boost{ namespace math{
  23. namespace detail
  24. {
  25. template <class RealType, class Policy>
  26. inline bool check_inverse_chi_squared( // Check both distribution parameters.
  27. const char* function,
  28. RealType degrees_of_freedom, // degrees_of_freedom (aka nu).
  29. RealType scale, // scale (aka sigma^2)
  30. RealType* result,
  31. const Policy& pol)
  32. {
  33. return check_scale(function, scale, result, pol)
  34. && check_df(function, degrees_of_freedom,
  35. result, pol);
  36. } // bool check_inverse_chi_squared
  37. } // namespace detail
  38. template <class RealType = double, class Policy = policies::policy<> >
  39. class inverse_chi_squared_distribution
  40. {
  41. public:
  42. typedef RealType value_type;
  43. typedef Policy policy_type;
  44. inverse_chi_squared_distribution(RealType df, RealType l_scale) : m_df(df), m_scale (l_scale)
  45. {
  46. RealType result;
  47. detail::check_df(
  48. "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
  49. m_df, &result, Policy())
  50. && detail::check_scale(
  51. "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
  52. m_scale, &result, Policy());
  53. } // inverse_chi_squared_distribution constructor
  54. inverse_chi_squared_distribution(RealType df = 1) : m_df(df)
  55. {
  56. RealType result;
  57. m_scale = 1 / m_df ; // Default scale = 1 / degrees of freedom (Wikipedia definition 1).
  58. detail::check_df(
  59. "boost::math::inverse_chi_squared_distribution<%1%>::inverse_chi_squared_distribution",
  60. m_df, &result, Policy());
  61. } // inverse_chi_squared_distribution
  62. RealType degrees_of_freedom()const
  63. {
  64. return m_df; // aka nu
  65. }
  66. RealType scale()const
  67. {
  68. return m_scale; // aka xi
  69. }
  70. // Parameter estimation: NOT implemented yet.
  71. //static RealType find_degrees_of_freedom(
  72. // RealType difference_from_variance,
  73. // RealType alpha,
  74. // RealType beta,
  75. // RealType variance,
  76. // RealType hint = 100);
  77. private:
  78. // Data members:
  79. RealType m_df; // degrees of freedom are treated as a real number.
  80. RealType m_scale; // distribution scale.
  81. }; // class chi_squared_distribution
  82. typedef inverse_chi_squared_distribution<double> inverse_chi_squared;
  83. template <class RealType, class Policy>
  84. inline const std::pair<RealType, RealType> range(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
  85. { // Range of permissible values for random variable x.
  86. using boost::math::tools::max_value;
  87. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + infinity.
  88. }
  89. template <class RealType, class Policy>
  90. inline const std::pair<RealType, RealType> support(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
  91. { // Range of supported values for random variable x.
  92. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  93. return std::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>()); // 0 to + infinity.
  94. }
  95. template <class RealType, class Policy>
  96. RealType pdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
  97. {
  98. BOOST_MATH_STD_USING // for ADL of std functions.
  99. RealType df = dist.degrees_of_freedom();
  100. RealType scale = dist.scale();
  101. RealType error_result;
  102. static const char* function = "boost::math::pdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
  103. if(false == detail::check_inverse_chi_squared
  104. (function, df, scale, &error_result, Policy())
  105. )
  106. { // Bad distribution.
  107. return error_result;
  108. }
  109. if((x < 0) || !(boost::math::isfinite)(x))
  110. { // Bad x.
  111. return policies::raise_domain_error<RealType>(
  112. function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
  113. }
  114. if(x == 0)
  115. { // Treat as special case.
  116. return 0;
  117. }
  118. // Wikipedia scaled inverse chi sq (df, scale) related to inv gamma (df/2, df * scale /2)
  119. // so use inverse gamma pdf with shape = df/2, scale df * scale /2
  120. // RealType shape = df /2; // inv_gamma shape
  121. // RealType scale = df * scale/2; // inv_gamma scale
  122. // RealType result = gamma_p_derivative(shape, scale / x, Policy()) * scale / (x * x);
  123. RealType result = df * scale/2 / x;
  124. if(result < tools::min_value<RealType>())
  125. return 0; // Random variable is near enough infinite.
  126. result = gamma_p_derivative(df/2, result, Policy()) * df * scale/2;
  127. if(result != 0) // prevent 0 / 0, gamma_p_derivative -> 0 faster than x^2
  128. result /= (x * x);
  129. return result;
  130. } // pdf
  131. template <class RealType, class Policy>
  132. inline RealType cdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
  133. {
  134. static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
  135. RealType df = dist.degrees_of_freedom();
  136. RealType scale = dist.scale();
  137. RealType error_result;
  138. if(false ==
  139. detail::check_inverse_chi_squared(function, df, scale, &error_result, Policy())
  140. )
  141. { // Bad distribution.
  142. return error_result;
  143. }
  144. if((x < 0) || !(boost::math::isfinite)(x))
  145. { // Bad x.
  146. return policies::raise_domain_error<RealType>(
  147. function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
  148. }
  149. if (x == 0)
  150. { // Treat zero as a special case.
  151. return 0;
  152. }
  153. // RealType shape = df /2; // inv_gamma shape,
  154. // RealType scale = df * scale/2; // inv_gamma scale,
  155. // result = boost::math::gamma_q(shape, scale / x, Policy()); // inverse_gamma code.
  156. return boost::math::gamma_q(df / 2, (df * (scale / 2)) / x, Policy());
  157. } // cdf
  158. template <class RealType, class Policy>
  159. inline RealType quantile(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& p)
  160. {
  161. using boost::math::gamma_q_inv;
  162. RealType df = dist.degrees_of_freedom();
  163. RealType scale = dist.scale();
  164. static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
  165. // Error check:
  166. RealType error_result;
  167. if(false == detail::check_df(
  168. function, df, &error_result, Policy())
  169. && detail::check_probability(
  170. function, p, &error_result, Policy()))
  171. {
  172. return error_result;
  173. }
  174. if(false == detail::check_probability(
  175. function, p, &error_result, Policy()))
  176. {
  177. return error_result;
  178. }
  179. // RealType shape = df /2; // inv_gamma shape,
  180. // RealType scale = df * scale/2; // inv_gamma scale,
  181. // result = scale / gamma_q_inv(shape, p, Policy());
  182. RealType result = gamma_q_inv(df /2, p, Policy());
  183. if(result == 0)
  184. return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
  185. result = df * (scale / 2) / result;
  186. return result;
  187. } // quantile
  188. template <class RealType, class Policy>
  189. inline RealType cdf(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
  190. {
  191. using boost::math::gamma_q_inv;
  192. RealType const& df = c.dist.degrees_of_freedom();
  193. RealType const& scale = c.dist.scale();
  194. RealType const& x = c.param;
  195. static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
  196. // Error check:
  197. RealType error_result;
  198. if(false == detail::check_df(
  199. function, df, &error_result, Policy()))
  200. {
  201. return error_result;
  202. }
  203. if (x == 0)
  204. { // Treat zero as a special case.
  205. return 1;
  206. }
  207. if((x < 0) || !(boost::math::isfinite)(x))
  208. {
  209. return policies::raise_domain_error<RealType>(
  210. function, "inverse Chi Square parameter was %1%, but must be > 0 !", x, Policy());
  211. }
  212. // RealType shape = df /2; // inv_gamma shape,
  213. // RealType scale = df * scale/2; // inv_gamma scale,
  214. // result = gamma_p(shape, scale/c.param, Policy()); use inv_gamma.
  215. return gamma_p(df / 2, (df * scale/2) / x, Policy()); // OK
  216. } // cdf(complemented
  217. template <class RealType, class Policy>
  218. inline RealType quantile(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
  219. {
  220. using boost::math::gamma_q_inv;
  221. RealType const& df = c.dist.degrees_of_freedom();
  222. RealType const& scale = c.dist.scale();
  223. RealType const& q = c.param;
  224. static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
  225. // Error check:
  226. RealType error_result;
  227. if(false == detail::check_df(function, df, &error_result, Policy()))
  228. {
  229. return error_result;
  230. }
  231. if(false == detail::check_probability(function, q, &error_result, Policy()))
  232. {
  233. return error_result;
  234. }
  235. // RealType shape = df /2; // inv_gamma shape,
  236. // RealType scale = df * scale/2; // inv_gamma scale,
  237. // result = scale / gamma_p_inv(shape, q, Policy()); // using inv_gamma.
  238. RealType result = gamma_p_inv(df/2, q, Policy());
  239. if(result == 0)
  240. return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
  241. result = (df * scale / 2) / result;
  242. return result;
  243. } // quantile(const complement
  244. template <class RealType, class Policy>
  245. inline RealType mean(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  246. { // Mean of inverse Chi-Squared distribution.
  247. RealType df = dist.degrees_of_freedom();
  248. RealType scale = dist.scale();
  249. static const char* function = "boost::math::mean(const inverse_chi_squared_distribution<%1%>&)";
  250. if(df <= 2)
  251. return policies::raise_domain_error<RealType>(
  252. function,
  253. "inverse Chi-Squared distribution only has a mode for degrees of freedom > 2, but got degrees of freedom = %1%.",
  254. df, Policy());
  255. return (df * scale) / (df - 2);
  256. } // mean
  257. template <class RealType, class Policy>
  258. inline RealType variance(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  259. { // Variance of inverse Chi-Squared distribution.
  260. RealType df = dist.degrees_of_freedom();
  261. RealType scale = dist.scale();
  262. static const char* function = "boost::math::variance(const inverse_chi_squared_distribution<%1%>&)";
  263. if(df <= 4)
  264. {
  265. return policies::raise_domain_error<RealType>(
  266. function,
  267. "inverse Chi-Squared distribution only has a variance for degrees of freedom > 4, but got degrees of freedom = %1%.",
  268. df, Policy());
  269. }
  270. return 2 * df * df * scale * scale / ((df - 2)*(df - 2) * (df - 4));
  271. } // variance
  272. template <class RealType, class Policy>
  273. inline RealType mode(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  274. { // mode is not defined in Mathematica.
  275. // See Discussion section http://en.wikipedia.org/wiki/Talk:Scaled-inverse-chi-square_distribution
  276. // for origin of the formula used below.
  277. RealType df = dist.degrees_of_freedom();
  278. RealType scale = dist.scale();
  279. static const char* function = "boost::math::mode(const inverse_chi_squared_distribution<%1%>&)";
  280. if(df < 0)
  281. return policies::raise_domain_error<RealType>(
  282. function,
  283. "inverse Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.",
  284. df, Policy());
  285. return (df * scale) / (df + 2);
  286. }
  287. //template <class RealType, class Policy>
  288. //inline RealType median(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  289. //{ // Median is given by Quantile[dist, 1/2]
  290. // RealType df = dist.degrees_of_freedom();
  291. // if(df <= 1)
  292. // return tools::domain_error<RealType>(
  293. // BOOST_CURRENT_FUNCTION,
  294. // "The inverse_Chi-Squared distribution only has a median for degrees of freedom >= 0, but got degrees of freedom = %1%.",
  295. // df);
  296. // return df;
  297. //}
  298. // Now implemented via quantile(half) in derived accessors.
  299. template <class RealType, class Policy>
  300. inline RealType skewness(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  301. {
  302. BOOST_MATH_STD_USING // For ADL
  303. RealType df = dist.degrees_of_freedom();
  304. static const char* function = "boost::math::skewness(const inverse_chi_squared_distribution<%1%>&)";
  305. if(df <= 6)
  306. return policies::raise_domain_error<RealType>(
  307. function,
  308. "inverse Chi-Squared distribution only has a skewness for degrees of freedom > 6, but got degrees of freedom = %1%.",
  309. df, Policy());
  310. return 4 * sqrt (2 * (df - 4)) / (df - 6); // Not a function of scale.
  311. }
  312. template <class RealType, class Policy>
  313. inline RealType kurtosis(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  314. {
  315. RealType df = dist.degrees_of_freedom();
  316. static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
  317. if(df <= 8)
  318. return policies::raise_domain_error<RealType>(
  319. function,
  320. "inverse Chi-Squared distribution only has a kurtosis for degrees of freedom > 8, but got degrees of freedom = %1%.",
  321. df, Policy());
  322. return kurtosis_excess(dist) + 3;
  323. }
  324. template <class RealType, class Policy>
  325. inline RealType kurtosis_excess(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  326. {
  327. RealType df = dist.degrees_of_freedom();
  328. static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
  329. if(df <= 8)
  330. return policies::raise_domain_error<RealType>(
  331. function,
  332. "inverse Chi-Squared distribution only has a kurtosis excess for degrees of freedom > 8, but got degrees of freedom = %1%.",
  333. df, Policy());
  334. return 12 * (5 * df - 22) / ((df - 6 )*(df - 8)); // Not a function of scale.
  335. }
  336. //
  337. // Parameter estimation comes last:
  338. //
  339. } // namespace math
  340. } // namespace boost
  341. // This include must be at the end, *after* the accessors
  342. // for this distribution have been defined, in order to
  343. // keep compilers that support two-phase lookup happy.
  344. #include <boost/math/distributions/detail/derived_accessors.hpp>
  345. #endif // BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP