• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright John Maddock 2006
2 // Copyright Paul A. Bristow 2007, 2008, 2010
3 
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 
9 #ifdef _MSC_VER
10 #  pragma warning(disable: 4512) // assignment operator could not be generated.
11 #  pragma warning(disable: 4510) // default constructor could not be generated.
12 #  pragma warning(disable: 4610) // can never be instantiated - user defined constructor required.
13 #  pragma warning(disable: 4180) // qualifier has no effect (in Fusion).
14 #endif
15 
16 #include <iostream>
17 using std::cout; using std::endl;
18 using std::left; using std::fixed; using std::right; using std::scientific;
19 #include <iomanip>
20 using std::setw;
21 using std::setprecision;
22 
23 #include <boost/math/distributions/fisher_f.hpp>
24 
f_test(double sd1,double sd2,double N1,double N2,double alpha)25 void f_test(
26        double sd1,     // Sample 1 std deviation
27        double sd2,     // Sample 2 std deviation
28        double N1,      // Sample 1 size
29        double N2,      // Sample 2 size
30        double alpha)  // Significance level
31 {
32    //
33    // An F test applied to two sets of data.
34    // We are testing the null hypothesis that the
35    // standard deviation of the samples is equal, and
36    // that any variation is down to chance.  We can
37    // also test the alternative hypothesis that any
38    // difference is not down to chance.
39    // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda359.htm
40    //
41    // Avoid "using namespace boost::math;" because of potential name ambiguity.
42    using boost::math::fisher_f;
43 
44    // Print header:
45    cout <<
46       "____________________________________\n"
47       "F test for equal standard deviations\n"
48       "____________________________________\n\n";
49    cout << setprecision(5);
50    cout << "Sample 1:\n";
51    cout << setw(55) << left << "Number of Observations" << "=  " << N1 << "\n";
52    cout << setw(55) << left << "Sample Standard Deviation" << "=  " << sd1 << "\n\n";
53    cout << "Sample 2:\n";
54    cout << setw(55) << left << "Number of Observations" << "=  " << N2 << "\n";
55    cout << setw(55) << left << "Sample Standard Deviation" << "=  " << sd2 << "\n\n";
56    //
57    // Now we can calculate and output some stats:
58    //
59    // F-statistic:
60    double F = (sd1 / sd2);
61    F *= F;
62    cout << setw(55) << left << "Test Statistic" << "=  " << F << "\n\n";
63    //
64    // Finally define our distribution, and get the probability:
65    //
66    fisher_f dist(N1 - 1, N2 - 1);
67    double p = cdf(dist, F);
68    cout << setw(55) << left << "CDF of test statistic: " << "=  "
69       << setprecision(3) << scientific << p << "\n";
70    double ucv = quantile(complement(dist, alpha));
71    double ucv2 = quantile(complement(dist, alpha / 2));
72    double lcv = quantile(dist, alpha);
73    double lcv2 = quantile(dist, alpha / 2);
74    cout << setw(55) << left << "Upper Critical Value at alpha: " << "=  "
75       << setprecision(3) << scientific << ucv << "\n";
76    cout << setw(55) << left << "Upper Critical Value at alpha/2: " << "=  "
77       << setprecision(3) << scientific << ucv2 << "\n";
78    cout << setw(55) << left << "Lower Critical Value at alpha: " << "=  "
79       << setprecision(3) << scientific << lcv << "\n";
80    cout << setw(55) << left << "Lower Critical Value at alpha/2: " << "=  "
81       << setprecision(3) << scientific << lcv2 << "\n\n";
82    //
83    // Finally print out results of null and alternative hypothesis:
84    //
85    cout << setw(55) << left <<
86       "Results for Alternative Hypothesis and alpha" << "=  "
87       << setprecision(4) << fixed << alpha << "\n\n";
88    cout << "Alternative Hypothesis                                    Conclusion\n";
89    cout << "Standard deviations are unequal (two sided test)          ";
90    if((ucv2 < F) || (lcv2 > F))
91       cout << "NOT REJECTED\n";
92    else
93       cout << "REJECTED\n";
94    cout << "Standard deviation 1 is less than standard deviation 2    ";
95    if(lcv > F)
96       cout << "NOT REJECTED\n";
97    else
98       cout << "REJECTED\n";
99    cout << "Standard deviation 1 is greater than standard deviation 2 ";
100    if(ucv < F)
101       cout << "NOT REJECTED\n";
102    else
103       cout << "REJECTED\n";
104    cout << endl << endl;
105 }
106 
main()107 int main()
108 {
109    //
110    // Run tests for ceramic strength data:
111    // see http://www.itl.nist.gov/div898/handbook/eda/section4/eda42a1.htm
112    // The data for this case study were collected by Said Jahanmir of the
113    // NIST Ceramics Division in 1996 in connection with a NIST/industry
114    // ceramics consortium for strength optimization of ceramic strength.
115    //
116    f_test(65.54909, 61.85425, 240, 240, 0.05);
117    //
118    // And again for the process change comparison:
119    // see http://www.itl.nist.gov/div898/handbook/prc/section3/prc32.htm
120    // A new procedure to assemble a device is introduced and tested for
121    // possible improvement in time of assembly. The question being addressed
122    // is whether the standard deviation of the new assembly process (sample 2) is
123    // better (i.e., smaller) than the standard deviation for the old assembly
124    // process (sample 1).
125    //
126    f_test(4.9082, 2.5874, 11, 9, 0.05);
127    return 0;
128 }
129 
130 /*
131 
132 Output:
133 
134   f_test.cpp
135   F-test_example1.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Debug\F_test_example1.exe
136   ____________________________________
137   F test for equal standard deviations
138   ____________________________________
139 
140   Sample 1:
141   Number of Observations                                 =  240
142   Sample Standard Deviation                              =  65.549
143 
144   Sample 2:
145   Number of Observations                                 =  240
146   Sample Standard Deviation                              =  61.854
147 
148   Test Statistic                                         =  1.123
149 
150   CDF of test statistic:                                 =  8.148e-001
151   Upper Critical Value at alpha:                         =  1.238e+000
152   Upper Critical Value at alpha/2:                       =  1.289e+000
153   Lower Critical Value at alpha:                         =  8.080e-001
154   Lower Critical Value at alpha/2:                       =  7.756e-001
155 
156   Results for Alternative Hypothesis and alpha           =  0.0500
157 
158   Alternative Hypothesis                                    Conclusion
159   Standard deviations are unequal (two sided test)          REJECTED
160   Standard deviation 1 is less than standard deviation 2    REJECTED
161   Standard deviation 1 is greater than standard deviation 2 REJECTED
162 
163 
164   ____________________________________
165   F test for equal standard deviations
166   ____________________________________
167 
168   Sample 1:
169   Number of Observations                                 =  11.00000
170   Sample Standard Deviation                              =  4.90820
171 
172   Sample 2:
173   Number of Observations                                 =  9.00000
174   Sample Standard Deviation                              =  2.58740
175 
176   Test Statistic                                         =  3.59847
177 
178   CDF of test statistic:                                 =  9.589e-001
179   Upper Critical Value at alpha:                         =  3.347e+000
180   Upper Critical Value at alpha/2:                       =  4.295e+000
181   Lower Critical Value at alpha:                         =  3.256e-001
182   Lower Critical Value at alpha/2:                       =  2.594e-001
183 
184   Results for Alternative Hypothesis and alpha           =  0.0500
185 
186   Alternative Hypothesis                                    Conclusion
187   Standard deviations are unequal (two sided test)          REJECTED
188   Standard deviation 1 is less than standard deviation 2    REJECTED
189   Standard deviation 1 is greater than standard deviation 2 NOT REJECTED
190 
191 
192   ____________________________________
193   F test for equal standard deviations
194   ____________________________________
195 
196   Sample 1:
197   Number of Observations                                 =  240
198   Sample Standard Deviation                              =  65.549
199 
200   Sample 2:
201   Number of Observations                                 =  240
202   Sample Standard Deviation                              =  61.854
203 
204   Test Statistic                                         =  1.123
205 
206   CDF of test statistic:                                 =  8.148e-001
207   Upper Critical Value at alpha:                         =  1.238e+000
208   Upper Critical Value at alpha/2:                       =  1.289e+000
209   Lower Critical Value at alpha:                         =  8.080e-001
210   Lower Critical Value at alpha/2:                       =  7.756e-001
211 
212   Results for Alternative Hypothesis and alpha           =  0.0500
213 
214   Alternative Hypothesis                                    Conclusion
215   Standard deviations are unequal (two sided test)          REJECTED
216   Standard deviation 1 is less than standard deviation 2    REJECTED
217   Standard deviation 1 is greater than standard deviation 2 REJECTED
218 
219 
220   ____________________________________
221   F test for equal standard deviations
222   ____________________________________
223 
224   Sample 1:
225   Number of Observations                                 =  11.00000
226   Sample Standard Deviation                              =  4.90820
227 
228   Sample 2:
229   Number of Observations                                 =  9.00000
230   Sample Standard Deviation                              =  2.58740
231 
232   Test Statistic                                         =  3.59847
233 
234   CDF of test statistic:                                 =  9.589e-001
235   Upper Critical Value at alpha:                         =  3.347e+000
236   Upper Critical Value at alpha/2:                       =  4.295e+000
237   Lower Critical Value at alpha:                         =  3.256e-001
238   Lower Critical Value at alpha/2:                       =  2.594e-001
239 
240   Results for Alternative Hypothesis and alpha           =  0.0500
241 
242   Alternative Hypothesis                                    Conclusion
243   Standard deviations are unequal (two sided test)          REJECTED
244   Standard deviation 1 is less than standard deviation 2    REJECTED
245   Standard deviation 1 is greater than standard deviation 2 NOT REJECTED
246 
247 
248 
249 */
250 
251