fisher_f.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_DISTRIBUTIONS_FISHER_F_HPP
  7. #define BOOST_MATH_DISTRIBUTIONS_FISHER_F_HPP
  8. #include <boost/math/distributions/fwd.hpp>
  9. #include <boost/math/special_functions/beta.hpp> // for incomplete beta.
  10. #include <boost/math/distributions/complement.hpp> // complements
  11. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
  12. #include <boost/math/special_functions/fpclassify.hpp>
  13. #include <utility>
  14. namespace boost{ namespace math{
  15. template <class RealType = double, class Policy = policies::policy<> >
  16. class fisher_f_distribution
  17. {
  18. public:
  19. typedef RealType value_type;
  20. typedef Policy policy_type;
  21. fisher_f_distribution(const RealType& i, const RealType& j) : m_df1(i), m_df2(j)
  22. {
  23. static const char* function = "fisher_f_distribution<%1%>::fisher_f_distribution";
  24. RealType result;
  25. detail::check_df(
  26. function, m_df1, &result, Policy());
  27. detail::check_df(
  28. function, m_df2, &result, Policy());
  29. } // fisher_f_distribution
  30. RealType degrees_of_freedom1()const
  31. {
  32. return m_df1;
  33. }
  34. RealType degrees_of_freedom2()const
  35. {
  36. return m_df2;
  37. }
  38. private:
  39. //
  40. // Data members:
  41. //
  42. RealType m_df1; // degrees of freedom are a real number.
  43. RealType m_df2; // degrees of freedom are a real number.
  44. };
  45. typedef fisher_f_distribution<double> fisher_f;
  46. template <class RealType, class Policy>
  47. inline const std::pair<RealType, RealType> range(const fisher_f_distribution<RealType, Policy>& /*dist*/)
  48. { // Range of permissible values for random variable x.
  49. using boost::math::tools::max_value;
  50. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  51. }
  52. template <class RealType, class Policy>
  53. inline const std::pair<RealType, RealType> support(const fisher_f_distribution<RealType, Policy>& /*dist*/)
  54. { // Range of supported values for random variable x.
  55. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  56. using boost::math::tools::max_value;
  57. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  58. }
  59. template <class RealType, class Policy>
  60. RealType pdf(const fisher_f_distribution<RealType, Policy>& dist, const RealType& x)
  61. {
  62. BOOST_MATH_STD_USING // for ADL of std functions
  63. RealType df1 = dist.degrees_of_freedom1();
  64. RealType df2 = dist.degrees_of_freedom2();
  65. // Error check:
  66. RealType error_result = 0;
  67. static const char* function = "boost::math::pdf(fisher_f_distribution<%1%> const&, %1%)";
  68. if(false == (detail::check_df(
  69. function, df1, &error_result, Policy())
  70. && detail::check_df(
  71. function, df2, &error_result, Policy())))
  72. return error_result;
  73. if((x < 0) || !(boost::math::isfinite)(x))
  74. {
  75. return policies::raise_domain_error<RealType>(
  76. function, "Random variable parameter was %1%, but must be > 0 !", x, Policy());
  77. }
  78. if(x == 0)
  79. {
  80. // special cases:
  81. if(df1 < 2)
  82. return policies::raise_overflow_error<RealType>(
  83. function, 0, Policy());
  84. else if(df1 == 2)
  85. return 1;
  86. else
  87. return 0;
  88. }
  89. //
  90. // You reach this formula by direct differentiation of the
  91. // cdf expressed in terms of the incomplete beta.
  92. //
  93. // There are two versions so we don't pass a value of z
  94. // that is very close to 1 to ibeta_derivative: for some values
  95. // of df1 and df2, all the change takes place in this area.
  96. //
  97. RealType v1x = df1 * x;
  98. RealType result;
  99. if(v1x > df2)
  100. {
  101. result = (df2 * df1) / ((df2 + v1x) * (df2 + v1x));
  102. result *= ibeta_derivative(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy());
  103. }
  104. else
  105. {
  106. result = df2 + df1 * x;
  107. result = (result * df1 - x * df1 * df1) / (result * result);
  108. result *= ibeta_derivative(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy());
  109. }
  110. return result;
  111. } // pdf
  112. template <class RealType, class Policy>
  113. inline RealType cdf(const fisher_f_distribution<RealType, Policy>& dist, const RealType& x)
  114. {
  115. static const char* function = "boost::math::cdf(fisher_f_distribution<%1%> const&, %1%)";
  116. RealType df1 = dist.degrees_of_freedom1();
  117. RealType df2 = dist.degrees_of_freedom2();
  118. // Error check:
  119. RealType error_result = 0;
  120. if(false == detail::check_df(
  121. function, df1, &error_result, Policy())
  122. && detail::check_df(
  123. function, df2, &error_result, Policy()))
  124. return error_result;
  125. if((x < 0) || !(boost::math::isfinite)(x))
  126. {
  127. return policies::raise_domain_error<RealType>(
  128. function, "Random Variable parameter was %1%, but must be > 0 !", x, Policy());
  129. }
  130. RealType v1x = df1 * x;
  131. //
  132. // There are two equivalent formulas used here, the aim is
  133. // to prevent the final argument to the incomplete beta
  134. // from being too close to 1: for some values of df1 and df2
  135. // the rate of change can be arbitrarily large in this area,
  136. // whilst the value we're passing will have lost information
  137. // content as a result of being 0.999999something. Better
  138. // to switch things around so we're passing 1-z instead.
  139. //
  140. return v1x > df2
  141. ? boost::math::ibetac(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy())
  142. : boost::math::ibeta(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy());
  143. } // cdf
  144. template <class RealType, class Policy>
  145. inline RealType quantile(const fisher_f_distribution<RealType, Policy>& dist, const RealType& p)
  146. {
  147. static const char* function = "boost::math::quantile(fisher_f_distribution<%1%> const&, %1%)";
  148. RealType df1 = dist.degrees_of_freedom1();
  149. RealType df2 = dist.degrees_of_freedom2();
  150. // Error check:
  151. RealType error_result = 0;
  152. if(false == (detail::check_df(
  153. function, df1, &error_result, Policy())
  154. && detail::check_df(
  155. function, df2, &error_result, Policy())
  156. && detail::check_probability(
  157. function, p, &error_result, Policy())))
  158. return error_result;
  159. // With optimizations turned on, gcc wrongly warns about y being used
  160. // uninitializated unless we initialize it to something:
  161. RealType x, y(0);
  162. x = boost::math::ibeta_inv(df1 / 2, df2 / 2, p, &y, Policy());
  163. return df2 * x / (df1 * y);
  164. } // quantile
  165. template <class RealType, class Policy>
  166. inline RealType cdf(const complemented2_type<fisher_f_distribution<RealType, Policy>, RealType>& c)
  167. {
  168. static const char* function = "boost::math::cdf(fisher_f_distribution<%1%> const&, %1%)";
  169. RealType df1 = c.dist.degrees_of_freedom1();
  170. RealType df2 = c.dist.degrees_of_freedom2();
  171. RealType x = c.param;
  172. // Error check:
  173. RealType error_result = 0;
  174. if(false == detail::check_df(
  175. function, df1, &error_result, Policy())
  176. && detail::check_df(
  177. function, df2, &error_result, Policy()))
  178. return error_result;
  179. if((x < 0) || !(boost::math::isfinite)(x))
  180. {
  181. return policies::raise_domain_error<RealType>(
  182. function, "Random Variable parameter was %1%, but must be > 0 !", x, Policy());
  183. }
  184. RealType v1x = df1 * x;
  185. //
  186. // There are two equivalent formulas used here, the aim is
  187. // to prevent the final argument to the incomplete beta
  188. // from being too close to 1: for some values of df1 and df2
  189. // the rate of change can be arbitrarily large in this area,
  190. // whilst the value we're passing will have lost information
  191. // content as a result of being 0.999999something. Better
  192. // to switch things around so we're passing 1-z instead.
  193. //
  194. return v1x > df2
  195. ? boost::math::ibeta(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy())
  196. : boost::math::ibetac(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy());
  197. }
  198. template <class RealType, class Policy>
  199. inline RealType quantile(const complemented2_type<fisher_f_distribution<RealType, Policy>, RealType>& c)
  200. {
  201. static const char* function = "boost::math::quantile(fisher_f_distribution<%1%> const&, %1%)";
  202. RealType df1 = c.dist.degrees_of_freedom1();
  203. RealType df2 = c.dist.degrees_of_freedom2();
  204. RealType p = c.param;
  205. // Error check:
  206. RealType error_result = 0;
  207. if(false == (detail::check_df(
  208. function, df1, &error_result, Policy())
  209. && detail::check_df(
  210. function, df2, &error_result, Policy())
  211. && detail::check_probability(
  212. function, p, &error_result, Policy())))
  213. return error_result;
  214. RealType x, y;
  215. x = boost::math::ibetac_inv(df1 / 2, df2 / 2, p, &y, Policy());
  216. return df2 * x / (df1 * y);
  217. }
  218. template <class RealType, class Policy>
  219. inline RealType mean(const fisher_f_distribution<RealType, Policy>& dist)
  220. { // Mean of F distribution = v.
  221. static const char* function = "boost::math::mean(fisher_f_distribution<%1%> const&)";
  222. RealType df1 = dist.degrees_of_freedom1();
  223. RealType df2 = dist.degrees_of_freedom2();
  224. // Error check:
  225. RealType error_result = 0;
  226. if(false == detail::check_df(
  227. function, df1, &error_result, Policy())
  228. && detail::check_df(
  229. function, df2, &error_result, Policy()))
  230. return error_result;
  231. if(df2 <= 2)
  232. {
  233. return policies::raise_domain_error<RealType>(
  234. function, "Second degree of freedom was %1% but must be > 2 in order for the distribution to have a mean.", df2, Policy());
  235. }
  236. return df2 / (df2 - 2);
  237. } // mean
  238. template <class RealType, class Policy>
  239. inline RealType variance(const fisher_f_distribution<RealType, Policy>& dist)
  240. { // Variance of F distribution.
  241. static const char* function = "boost::math::variance(fisher_f_distribution<%1%> const&)";
  242. RealType df1 = dist.degrees_of_freedom1();
  243. RealType df2 = dist.degrees_of_freedom2();
  244. // Error check:
  245. RealType error_result = 0;
  246. if(false == detail::check_df(
  247. function, df1, &error_result, Policy())
  248. && detail::check_df(
  249. function, df2, &error_result, Policy()))
  250. return error_result;
  251. if(df2 <= 4)
  252. {
  253. return policies::raise_domain_error<RealType>(
  254. function, "Second degree of freedom was %1% but must be > 4 in order for the distribution to have a valid variance.", df2, Policy());
  255. }
  256. return 2 * df2 * df2 * (df1 + df2 - 2) / (df1 * (df2 - 2) * (df2 - 2) * (df2 - 4));
  257. } // variance
  258. template <class RealType, class Policy>
  259. inline RealType mode(const fisher_f_distribution<RealType, Policy>& dist)
  260. {
  261. static const char* function = "boost::math::mode(fisher_f_distribution<%1%> const&)";
  262. RealType df1 = dist.degrees_of_freedom1();
  263. RealType df2 = dist.degrees_of_freedom2();
  264. // Error check:
  265. RealType error_result = 0;
  266. if(false == detail::check_df(
  267. function, df1, &error_result, Policy())
  268. && detail::check_df(
  269. function, df2, &error_result, Policy()))
  270. return error_result;
  271. if(df2 <= 2)
  272. {
  273. return policies::raise_domain_error<RealType>(
  274. function, "Second degree of freedom was %1% but must be > 2 in order for the distribution to have a mode.", df2, Policy());
  275. }
  276. return df2 * (df1 - 2) / (df1 * (df2 + 2));
  277. }
  278. //template <class RealType, class Policy>
  279. //inline RealType median(const fisher_f_distribution<RealType, Policy>& dist)
  280. //{ // Median of Fisher F distribution is not defined.
  281. // return tools::domain_error<RealType>(BOOST_CURRENT_FUNCTION, "Median is not implemented, result is %1%!", std::numeric_limits<RealType>::quiet_NaN());
  282. // } // median
  283. // Now implemented via quantile(half) in derived accessors.
  284. template <class RealType, class Policy>
  285. inline RealType skewness(const fisher_f_distribution<RealType, Policy>& dist)
  286. {
  287. static const char* function = "boost::math::skewness(fisher_f_distribution<%1%> const&)";
  288. BOOST_MATH_STD_USING // ADL of std names
  289. // See http://mathworld.wolfram.com/F-Distribution.html
  290. RealType df1 = dist.degrees_of_freedom1();
  291. RealType df2 = dist.degrees_of_freedom2();
  292. // Error check:
  293. RealType error_result = 0;
  294. if(false == detail::check_df(
  295. function, df1, &error_result, Policy())
  296. && detail::check_df(
  297. function, df2, &error_result, Policy()))
  298. return error_result;
  299. if(df2 <= 6)
  300. {
  301. return policies::raise_domain_error<RealType>(
  302. function, "Second degree of freedom was %1% but must be > 6 in order for the distribution to have a skewness.", df2, Policy());
  303. }
  304. return 2 * (df2 + 2 * df1 - 2) * sqrt((2 * df2 - 8) / (df1 * (df2 + df1 - 2))) / (df2 - 6);
  305. }
  306. template <class RealType, class Policy>
  307. RealType kurtosis_excess(const fisher_f_distribution<RealType, Policy>& dist);
  308. template <class RealType, class Policy>
  309. inline RealType kurtosis(const fisher_f_distribution<RealType, Policy>& dist)
  310. {
  311. return 3 + kurtosis_excess(dist);
  312. }
  313. template <class RealType, class Policy>
  314. inline RealType kurtosis_excess(const fisher_f_distribution<RealType, Policy>& dist)
  315. {
  316. static const char* function = "boost::math::kurtosis_excess(fisher_f_distribution<%1%> const&)";
  317. // See http://mathworld.wolfram.com/F-Distribution.html
  318. RealType df1 = dist.degrees_of_freedom1();
  319. RealType df2 = dist.degrees_of_freedom2();
  320. // Error check:
  321. RealType error_result = 0;
  322. if(false == detail::check_df(
  323. function, df1, &error_result, Policy())
  324. && detail::check_df(
  325. function, df2, &error_result, Policy()))
  326. return error_result;
  327. if(df2 <= 8)
  328. {
  329. return policies::raise_domain_error<RealType>(
  330. function, "Second degree of freedom was %1% but must be > 8 in order for the distribution to have a kutosis.", df2, Policy());
  331. }
  332. RealType df2_2 = df2 * df2;
  333. RealType df1_2 = df1 * df1;
  334. RealType n = -16 + 20 * df2 - 8 * df2_2 + df2_2 * df2 + 44 * df1 - 32 * df2 * df1 + 5 * df2_2 * df1 - 22 * df1_2 + 5 * df2 * df1_2;
  335. n *= 12;
  336. RealType d = df1 * (df2 - 6) * (df2 - 8) * (df1 + df2 - 2);
  337. return n / d;
  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_FISHER_F_HPP