fisher_f.hpp 14 KB

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