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