inverse_chi_squared.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #ifdef __cpp_deduction_guides
  84. template <class RealType>
  85. inverse_chi_squared_distribution(RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  86. template <class RealType>
  87. inverse_chi_squared_distribution(RealType,RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  88. #endif
  89. template <class RealType, class Policy>
  90. inline const std::pair<RealType, RealType> range(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
  91. { // Range of permissible values for random variable x.
  92. using boost::math::tools::max_value;
  93. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + infinity.
  94. }
  95. template <class RealType, class Policy>
  96. inline const std::pair<RealType, RealType> support(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
  97. { // Range of supported values for random variable x.
  98. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  99. return std::pair<RealType, RealType>(static_cast<RealType>(0), tools::max_value<RealType>()); // 0 to + infinity.
  100. }
  101. template <class RealType, class Policy>
  102. RealType pdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
  103. {
  104. BOOST_MATH_STD_USING // for ADL of std functions.
  105. RealType df = dist.degrees_of_freedom();
  106. RealType scale = dist.scale();
  107. RealType error_result;
  108. static const char* function = "boost::math::pdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
  109. if(false == detail::check_inverse_chi_squared
  110. (function, df, scale, &error_result, Policy())
  111. )
  112. { // Bad distribution.
  113. return error_result;
  114. }
  115. if((x < 0) || !(boost::math::isfinite)(x))
  116. { // Bad x.
  117. return policies::raise_domain_error<RealType>(
  118. function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
  119. }
  120. if(x == 0)
  121. { // Treat as special case.
  122. return 0;
  123. }
  124. // Wikipedia scaled inverse chi sq (df, scale) related to inv gamma (df/2, df * scale /2)
  125. // so use inverse gamma pdf with shape = df/2, scale df * scale /2
  126. // RealType shape = df /2; // inv_gamma shape
  127. // RealType scale = df * scale/2; // inv_gamma scale
  128. // RealType result = gamma_p_derivative(shape, scale / x, Policy()) * scale / (x * x);
  129. RealType result = df * scale/2 / x;
  130. if(result < tools::min_value<RealType>())
  131. return 0; // Random variable is near enough infinite.
  132. result = gamma_p_derivative(df/2, result, Policy()) * df * scale/2;
  133. if(result != 0) // prevent 0 / 0, gamma_p_derivative -> 0 faster than x^2
  134. result /= (x * x);
  135. return result;
  136. } // pdf
  137. template <class RealType, class Policy>
  138. inline RealType cdf(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
  139. {
  140. static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
  141. RealType df = dist.degrees_of_freedom();
  142. RealType scale = dist.scale();
  143. RealType error_result;
  144. if(false ==
  145. detail::check_inverse_chi_squared(function, df, scale, &error_result, Policy())
  146. )
  147. { // Bad distribution.
  148. return error_result;
  149. }
  150. if((x < 0) || !(boost::math::isfinite)(x))
  151. { // Bad x.
  152. return policies::raise_domain_error<RealType>(
  153. function, "inverse Chi Square parameter was %1%, but must be >= 0 !", x, Policy());
  154. }
  155. if (x == 0)
  156. { // Treat zero as a special case.
  157. return 0;
  158. }
  159. // RealType shape = df /2; // inv_gamma shape,
  160. // RealType scale = df * scale/2; // inv_gamma scale,
  161. // result = boost::math::gamma_q(shape, scale / x, Policy()); // inverse_gamma code.
  162. return boost::math::gamma_q(df / 2, (df * (scale / 2)) / x, Policy());
  163. } // cdf
  164. template <class RealType, class Policy>
  165. inline RealType quantile(const inverse_chi_squared_distribution<RealType, Policy>& dist, const RealType& p)
  166. {
  167. using boost::math::gamma_q_inv;
  168. RealType df = dist.degrees_of_freedom();
  169. RealType scale = dist.scale();
  170. static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
  171. // Error check:
  172. RealType error_result;
  173. if(false == detail::check_df(
  174. function, df, &error_result, Policy())
  175. && detail::check_probability(
  176. function, p, &error_result, Policy()))
  177. {
  178. return error_result;
  179. }
  180. if(false == detail::check_probability(
  181. function, p, &error_result, Policy()))
  182. {
  183. return error_result;
  184. }
  185. // RealType shape = df /2; // inv_gamma shape,
  186. // RealType scale = df * scale/2; // inv_gamma scale,
  187. // result = scale / gamma_q_inv(shape, p, Policy());
  188. RealType result = gamma_q_inv(df /2, p, Policy());
  189. if(result == 0)
  190. return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
  191. result = df * (scale / 2) / result;
  192. return result;
  193. } // quantile
  194. template <class RealType, class Policy>
  195. inline RealType cdf(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
  196. {
  197. using boost::math::gamma_q_inv;
  198. RealType const& df = c.dist.degrees_of_freedom();
  199. RealType const& scale = c.dist.scale();
  200. RealType const& x = c.param;
  201. static const char* function = "boost::math::cdf(const inverse_chi_squared_distribution<%1%>&, %1%)";
  202. // Error check:
  203. RealType error_result;
  204. if(false == detail::check_df(
  205. function, df, &error_result, Policy()))
  206. {
  207. return error_result;
  208. }
  209. if (x == 0)
  210. { // Treat zero as a special case.
  211. return 1;
  212. }
  213. if((x < 0) || !(boost::math::isfinite)(x))
  214. {
  215. return policies::raise_domain_error<RealType>(
  216. function, "inverse Chi Square parameter was %1%, but must be > 0 !", x, Policy());
  217. }
  218. // RealType shape = df /2; // inv_gamma shape,
  219. // RealType scale = df * scale/2; // inv_gamma scale,
  220. // result = gamma_p(shape, scale/c.param, Policy()); use inv_gamma.
  221. return gamma_p(df / 2, (df * scale/2) / x, Policy()); // OK
  222. } // cdf(complemented
  223. template <class RealType, class Policy>
  224. inline RealType quantile(const complemented2_type<inverse_chi_squared_distribution<RealType, Policy>, RealType>& c)
  225. {
  226. using boost::math::gamma_q_inv;
  227. RealType const& df = c.dist.degrees_of_freedom();
  228. RealType const& scale = c.dist.scale();
  229. RealType const& q = c.param;
  230. static const char* function = "boost::math::quantile(const inverse_chi_squared_distribution<%1%>&, %1%)";
  231. // Error check:
  232. RealType error_result;
  233. if(false == detail::check_df(function, df, &error_result, Policy()))
  234. {
  235. return error_result;
  236. }
  237. if(false == detail::check_probability(function, q, &error_result, Policy()))
  238. {
  239. return error_result;
  240. }
  241. // RealType shape = df /2; // inv_gamma shape,
  242. // RealType scale = df * scale/2; // inv_gamma scale,
  243. // result = scale / gamma_p_inv(shape, q, Policy()); // using inv_gamma.
  244. RealType result = gamma_p_inv(df/2, q, Policy());
  245. if(result == 0)
  246. return policies::raise_overflow_error<RealType, Policy>(function, "Random variable is infinite.", Policy());
  247. result = (df * scale / 2) / result;
  248. return result;
  249. } // quantile(const complement
  250. template <class RealType, class Policy>
  251. inline RealType mean(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  252. { // Mean of inverse Chi-Squared distribution.
  253. RealType df = dist.degrees_of_freedom();
  254. RealType scale = dist.scale();
  255. static const char* function = "boost::math::mean(const inverse_chi_squared_distribution<%1%>&)";
  256. if(df <= 2)
  257. return policies::raise_domain_error<RealType>(
  258. function,
  259. "inverse Chi-Squared distribution only has a mode for degrees of freedom > 2, but got degrees of freedom = %1%.",
  260. df, Policy());
  261. return (df * scale) / (df - 2);
  262. } // mean
  263. template <class RealType, class Policy>
  264. inline RealType variance(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  265. { // Variance of inverse Chi-Squared distribution.
  266. RealType df = dist.degrees_of_freedom();
  267. RealType scale = dist.scale();
  268. static const char* function = "boost::math::variance(const inverse_chi_squared_distribution<%1%>&)";
  269. if(df <= 4)
  270. {
  271. return policies::raise_domain_error<RealType>(
  272. function,
  273. "inverse Chi-Squared distribution only has a variance for degrees of freedom > 4, but got degrees of freedom = %1%.",
  274. df, Policy());
  275. }
  276. return 2 * df * df * scale * scale / ((df - 2)*(df - 2) * (df - 4));
  277. } // variance
  278. template <class RealType, class Policy>
  279. inline RealType mode(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  280. { // mode is not defined in Mathematica.
  281. // See Discussion section http://en.wikipedia.org/wiki/Talk:Scaled-inverse-chi-square_distribution
  282. // for origin of the formula used below.
  283. RealType df = dist.degrees_of_freedom();
  284. RealType scale = dist.scale();
  285. static const char* function = "boost::math::mode(const inverse_chi_squared_distribution<%1%>&)";
  286. if(df < 0)
  287. return policies::raise_domain_error<RealType>(
  288. function,
  289. "inverse Chi-Squared distribution only has a mode for degrees of freedom >= 0, but got degrees of freedom = %1%.",
  290. df, Policy());
  291. return (df * scale) / (df + 2);
  292. }
  293. //template <class RealType, class Policy>
  294. //inline RealType median(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  295. //{ // Median is given by Quantile[dist, 1/2]
  296. // RealType df = dist.degrees_of_freedom();
  297. // if(df <= 1)
  298. // return tools::domain_error<RealType>(
  299. // BOOST_CURRENT_FUNCTION,
  300. // "The inverse_Chi-Squared distribution only has a median for degrees of freedom >= 0, but got degrees of freedom = %1%.",
  301. // df);
  302. // return df;
  303. //}
  304. // Now implemented via quantile(half) in derived accessors.
  305. template <class RealType, class Policy>
  306. inline RealType skewness(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  307. {
  308. BOOST_MATH_STD_USING // For ADL
  309. RealType df = dist.degrees_of_freedom();
  310. static const char* function = "boost::math::skewness(const inverse_chi_squared_distribution<%1%>&)";
  311. if(df <= 6)
  312. return policies::raise_domain_error<RealType>(
  313. function,
  314. "inverse Chi-Squared distribution only has a skewness for degrees of freedom > 6, but got degrees of freedom = %1%.",
  315. df, Policy());
  316. return 4 * sqrt (2 * (df - 4)) / (df - 6); // Not a function of scale.
  317. }
  318. template <class RealType, class Policy>
  319. inline RealType kurtosis(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  320. {
  321. RealType df = dist.degrees_of_freedom();
  322. static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
  323. if(df <= 8)
  324. return policies::raise_domain_error<RealType>(
  325. function,
  326. "inverse Chi-Squared distribution only has a kurtosis for degrees of freedom > 8, but got degrees of freedom = %1%.",
  327. df, Policy());
  328. return kurtosis_excess(dist) + 3;
  329. }
  330. template <class RealType, class Policy>
  331. inline RealType kurtosis_excess(const inverse_chi_squared_distribution<RealType, Policy>& dist)
  332. {
  333. RealType df = dist.degrees_of_freedom();
  334. static const char* function = "boost::math::kurtosis(const inverse_chi_squared_distribution<%1%>&)";
  335. if(df <= 8)
  336. return policies::raise_domain_error<RealType>(
  337. function,
  338. "inverse Chi-Squared distribution only has a kurtosis excess for degrees of freedom > 8, but got degrees of freedom = %1%.",
  339. df, Policy());
  340. return 12 * (5 * df - 22) / ((df - 6 )*(df - 8)); // Not a function of scale.
  341. }
  342. //
  343. // Parameter estimation comes last:
  344. //
  345. } // namespace math
  346. } // namespace boost
  347. // This include must be at the end, *after* the accessors
  348. // for this distribution have been defined, in order to
  349. // keep compilers that support two-phase lookup happy.
  350. #include <boost/math/distributions/detail/derived_accessors.hpp>
  351. #endif // BOOST_MATH_DISTRIBUTIONS_INVERSE_CHI_SQUARED_HPP