inverse_chi_squared_find_df_example.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // inverse_chi_squared_distribution_find_df_example.cpp
  2. // Copyright Paul A. Bristow 2010.
  3. // Copyright Thomas Mang 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)
  8. //#define BOOST_MATH_INSTRUMENT
  9. // Example 1 of using inverse chi squared distribution
  10. #include <boost/math/distributions/inverse_chi_squared.hpp>
  11. using boost::math::inverse_chi_squared_distribution; // inverse_chi_squared_distribution.
  12. using boost::math::inverse_chi_squared; //typedef for nverse_chi_squared_distribution double.
  13. #include <iostream>
  14. using std::cout; using std::endl;
  15. #include <iomanip>
  16. using std::setprecision;
  17. using std::setw;
  18. #include <cmath>
  19. using std::sqrt;
  20. int main()
  21. {
  22. cout << "Example using Inverse chi squared distribution to find df. " << endl;
  23. try
  24. {
  25. cout.precision(std::numeric_limits<double>::max_digits10); //
  26. int i = std::numeric_limits<double>::max_digits10;
  27. cout << "Show all potentially significant decimal digits std::numeric_limits<double>::max_digits10 = " << i << endl;
  28. cout.precision(3);
  29. double nu = 10.;
  30. double scale1 = 1./ nu; // 1st definition sigma^2 = 1/df;
  31. double scale2 = 1.; // 2nd definition sigma^2 = 1
  32. inverse_chi_squared sichsq(nu, 1/nu); // Explicitly scaled to default scale = 1/df.
  33. inverse_chi_squared ichsq(nu); // Implicitly scaled to default scale = 1/df.
  34. // Try degrees of freedom estimator
  35. //double df = chi_squared::find_degrees_of_freedom(-diff, alpha[i], alpha[i], variance);
  36. cout << "ichsq.degrees_of_freedom() = " << ichsq.degrees_of_freedom() << endl;
  37. double diff = 0.5; // difference from variance to detect (delta).
  38. double variance = 1.; // true variance
  39. double alpha = 0.9;
  40. double beta = 0.9;
  41. cout << "diff = " << diff
  42. << ", variance = " << variance << ", ratio = " << diff/variance
  43. << ", alpha = " << alpha << ", beta = " << beta << endl;
  44. using boost::math::detail::inverse_chi_square_df_estimator;
  45. using boost::math::policies::default_policy;
  46. inverse_chi_square_df_estimator<> a_df(alpha, beta, variance, diff);
  47. cout << "df est" << endl;
  48. for (double df = 1; df < 3; df += 0.1)
  49. {
  50. double est_df = a_df(1);
  51. cout << df << " " << a_df(df) << endl;
  52. }
  53. //template <class F, class T, class Tol, class Policy>std::pair<T, T>
  54. // bracket_and_solve_root(F f, const T& guess, T factor, bool rising, Tol tol, boost::uintmax_t& max_iter, const Policy& pol)
  55. //double df = inverse_chi_squared_distribution<>::find_degrees_of_freedom(diff, alpha, beta, variance, 0);
  56. double df = inverse_chi_squared::find_degrees_of_freedom(diff, alpha, beta, variance, 100);
  57. cout << df << endl;
  58. }
  59. catch(const std::exception& e)
  60. { // Always useful to include try & catch blocks because default policies
  61. // are to throw exceptions on arguments that cause errors like underflow, overflow.
  62. // Lacking try & catch blocks, the program will abort without a message below,
  63. // which may give some helpful clues as to the cause of the exception.
  64. std::cout <<
  65. "\n""Message from thrown exception was:\n " << e.what() << std::endl;
  66. }
  67. return 0;
  68. } // int main()
  69. /*
  70. Output is:
  71. Example using Inverse chi squared distribution to find df.
  72. Show all potentially significant decimal digits std::numeric_limits<double>::max_digits10 = 17
  73. 10
  74. Message from thrown exception was:
  75. Error in function boost::math::inverse_chi_squared_distribution<double>::inverse_chi_squared_distribution: Degrees of freedom argument is 1.#INF, but must be > 0 !
  76. diff = 0.5, variance = 1, ratio = 0.5, alpha = 0.1, beta = 0.1
  77. df est
  78. 1 1
  79. ratio+1 = 1.5, quantile(0.1) = 0.00618, cdf = 6.5e-037, result = -0.1
  80. 1.1 -0.1
  81. ratio+1 = 1.5, quantile(0.1) = 0.00903, cdf = 1.2e-025, result = -0.1
  82. 1.2 -0.1
  83. ratio+1 = 1.5, quantile(0.1) = 0.0125, cdf = 8.25e-019, result = -0.1
  84. 1.3 -0.1
  85. ratio+1 = 1.5, quantile(0.1) = 0.0166, cdf = 2.17e-014, result = -0.1
  86. 1.4 -0.1
  87. ratio+1 = 1.5, quantile(0.1) = 0.0212, cdf = 2.2e-011, result = -0.1
  88. 1.5 -0.1
  89. ratio+1 = 1.5, quantile(0.1) = 0.0265, cdf = 3e-009, result = -0.1
  90. 1.6 -0.1
  91. ratio+1 = 1.5, quantile(0.1) = 0.0323, cdf = 1.11e-007, result = -0.1
  92. 1.7 -0.1
  93. ratio+1 = 1.5, quantile(0.1) = 0.0386, cdf = 1.7e-006, result = -0.1
  94. 1.8 -0.1
  95. ratio+1 = 1.5, quantile(0.1) = 0.0454, cdf = 1.41e-005, result = -0.1
  96. 1.9 -0.1
  97. ratio+1 = 1.5, quantile(0.1) = 0.0527, cdf = 7.55e-005, result = -0.1
  98. 2 -0.1
  99. ratio+1 = 1.5, quantile(0.1) = 0.0604, cdf = 0.000291, result = -0.1
  100. 2.1 -0.1
  101. ratio+1 = 1.5, quantile(0.1) = 0.0685, cdf = 0.00088, result = -0.1
  102. 2.2 -0.1
  103. ratio+1 = 1.5, quantile(0.1) = 0.0771, cdf = 0.0022, result = -0.0999
  104. 2.3 -0.0999
  105. ratio+1 = 1.5, quantile(0.1) = 0.0859, cdf = 0.00475, result = -0.0997
  106. 2.4 -0.0997
  107. ratio+1 = 1.5, quantile(0.1) = 0.0952, cdf = 0.00911, result = -0.0993
  108. 2.5 -0.0993
  109. ratio+1 = 1.5, quantile(0.1) = 0.105, cdf = 0.0159, result = -0.0984
  110. 2.6 -0.0984
  111. ratio+1 = 1.5, quantile(0.1) = 0.115, cdf = 0.0257, result = -0.0967
  112. 2.7 -0.0967
  113. ratio+1 = 1.5, quantile(0.1) = 0.125, cdf = 0.039, result = -0.094
  114. 2.8 -0.094
  115. ratio+1 = 1.5, quantile(0.1) = 0.135, cdf = 0.056, result = -0.0897
  116. 2.9 -0.0897
  117. ratio+1 = 1.5, quantile(0.1) = 20.6, cdf = 1, result = 0.9
  118. ichsq.degrees_of_freedom() = 10
  119. diff = 0.5, variance = 1, ratio = 0.5, alpha = 0.9, beta = 0.9
  120. df est
  121. 1 1
  122. ratio+1 = 1.5, quantile(0.9) = 0.729, cdf = 0.269, result = -0.729
  123. 1.1 -0.729
  124. ratio+1 = 1.5, quantile(0.9) = 0.78, cdf = 0.314, result = -0.693
  125. 1.2 -0.693
  126. ratio+1 = 1.5, quantile(0.9) = 0.83, cdf = 0.36, result = -0.655
  127. 1.3 -0.655
  128. ratio+1 = 1.5, quantile(0.9) = 0.879, cdf = 0.405, result = -0.615
  129. 1.4 -0.615
  130. ratio+1 = 1.5, quantile(0.9) = 0.926, cdf = 0.449, result = -0.575
  131. 1.5 -0.575
  132. ratio+1 = 1.5, quantile(0.9) = 0.973, cdf = 0.492, result = -0.535
  133. 1.6 -0.535
  134. ratio+1 = 1.5, quantile(0.9) = 1.02, cdf = 0.534, result = -0.495
  135. 1.7 -0.495
  136. ratio+1 = 1.5, quantile(0.9) = 1.06, cdf = 0.574, result = -0.455
  137. 1.8 -0.455
  138. ratio+1 = 1.5, quantile(0.9) = 1.11, cdf = 0.612, result = -0.417
  139. 1.9 -0.417
  140. ratio+1 = 1.5, quantile(0.9) = 1.15, cdf = 0.648, result = -0.379
  141. 2 -0.379
  142. ratio+1 = 1.5, quantile(0.9) = 1.19, cdf = 0.681, result = -0.342
  143. 2.1 -0.342
  144. ratio+1 = 1.5, quantile(0.9) = 1.24, cdf = 0.713, result = -0.307
  145. 2.2 -0.307
  146. ratio+1 = 1.5, quantile(0.9) = 1.28, cdf = 0.742, result = -0.274
  147. 2.3 -0.274
  148. ratio+1 = 1.5, quantile(0.9) = 1.32, cdf = 0.769, result = -0.242
  149. 2.4 -0.242
  150. ratio+1 = 1.5, quantile(0.9) = 1.36, cdf = 0.793, result = -0.212
  151. 2.5 -0.212
  152. ratio+1 = 1.5, quantile(0.9) = 1.4, cdf = 0.816, result = -0.184
  153. 2.6 -0.184
  154. ratio+1 = 1.5, quantile(0.9) = 1.44, cdf = 0.836, result = -0.157
  155. 2.7 -0.157
  156. ratio+1 = 1.5, quantile(0.9) = 1.48, cdf = 0.855, result = -0.133
  157. 2.8 -0.133
  158. ratio+1 = 1.5, quantile(0.9) = 1.52, cdf = 0.872, result = -0.11
  159. 2.9 -0.11
  160. ratio+1 = 1.5, quantile(0.9) = 29.6, cdf = 1, result = 0.1
  161. */