1 // Copyright John Maddock 2008
2 // Copyright Paul A. Bristow 2010, 2013
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 // Caution: this file contains Quickbook markup as well as code
9 // and comments, don't change any of the special comment markups!
10
11 //[nccs_eg
12
13 /*`
14
15 This example computes a table of the power of the [chi][super 2]
16 test at the 5% significance level, for various degrees of freedom
17 and non-centrality parameters. The table is deliberately the same
18 as Table 6 from "The Non-Central [chi][super 2] and F-Distributions and
19 their applications.", P. B. Patnaik, Biometrika, Vol. 36, No. 1/2 (June 1949),
20 202-232.
21
22 First we need some includes to access the non-central chi squared distribution
23 (and some basic std output of course).
24
25 */
26
27 #include <boost/math/distributions/non_central_chi_squared.hpp>
28 using boost::math::chi_squared;
29 using boost::math::non_central_chi_squared;
30
31 #include <iostream>
32 using std::cout; using std::endl;
33 using std::setprecision;
34
main()35 int main()
36 {
37 /*`
38 Create a table of the power of the [chi][super 2] test at
39 5% significance level, start with a table header:
40 */
41 cout << "[table\n[[[nu]]";
42 for(int lam = 2; lam <= 20; lam += 2)
43 {
44 cout << "[[lambda]=" << lam << "]";
45 }
46 cout << "]\n";
47
48 /*`
49 (Note: the enclosing [] brackets are to format as a table in Boost.Quickbook).
50
51 Enumerate the rows and columns and print the power of the test
52 for each table cell:
53 */
54
55 for(int n = 2; n <= 20; ++n)
56 {
57 cout << "[[" << n << "]";
58 for(int lam = 2; lam <= 20; lam += 2)
59 {
60 /*`
61 Calculate the [chi][super 2] statistic for a 5% significance:
62 */
63 double cs = quantile(complement(chi_squared(n), 0.05));
64 /*`
65 The power of the test is given by the complement of the CDF
66 of the non-central [chi][super 2] distribution:
67 */
68 double beta = cdf(complement(non_central_chi_squared(n, lam), cs));
69 /*`
70 Then output the cell value:
71 */
72 cout << "[" << setprecision(3) << beta << "]";
73 }
74 cout << "]" << endl;
75 }
76 cout << "]" << endl;
77 }
78
79 /*`
80 The output from this program is a table in Boost.Quickbook format as shown below.
81
82 We can interpret this as follows - for example if [nu]=10 and [lambda]=10
83 then the power of the test is 0.542 - so we have only a 54% chance of
84 correctly detecting that our null hypothesis is false, and a 46% chance
85 of incurring a type II error (failing to reject the null hypothesis when
86 it is in fact false):
87
88 [table
89 [[[nu]][[lambda]=2][[lambda]=4][[lambda]=6][[lambda]=8][[lambda]=10][[lambda]=12][[lambda]=14][[lambda]=16][[lambda]=18][[lambda]=20]]
90 [[2][0.226][0.415][0.584][0.718][0.815][0.883][0.928][0.957][0.974][0.985]]
91 [[3][0.192][0.359][0.518][0.654][0.761][0.84][0.896][0.934][0.959][0.975]]
92 [[4][0.171][0.32][0.47][0.605][0.716][0.802][0.866][0.912][0.943][0.964]]
93 [[5][0.157][0.292][0.433][0.564][0.677][0.769][0.839][0.89][0.927][0.952]]
94 [[6][0.146][0.27][0.403][0.531][0.644][0.738][0.813][0.869][0.911][0.94]]
95 [[7][0.138][0.252][0.378][0.502][0.614][0.71][0.788][0.849][0.895][0.928]]
96 [[8][0.131][0.238][0.357][0.477][0.588][0.685][0.765][0.829][0.879][0.915]]
97 [[9][0.125][0.225][0.339][0.454][0.564][0.661][0.744][0.811][0.863][0.903]]
98 [[10][0.121][0.215][0.323][0.435][0.542][0.64][0.723][0.793][0.848][0.891]]
99 [[11][0.117][0.206][0.309][0.417][0.523][0.62][0.704][0.775][0.833][0.878]]
100 [[12][0.113][0.198][0.297][0.402][0.505][0.601][0.686][0.759][0.818][0.866]]
101 [[13][0.11][0.191][0.286][0.387][0.488][0.584][0.669][0.743][0.804][0.854]]
102 [[14][0.108][0.185][0.276][0.374][0.473][0.567][0.653][0.728][0.791][0.842]]
103 [[15][0.105][0.179][0.267][0.362][0.459][0.552][0.638][0.713][0.777][0.83]]
104 [[16][0.103][0.174][0.259][0.351][0.446][0.538][0.623][0.699][0.764][0.819]]
105 [[17][0.101][0.169][0.251][0.341][0.434][0.525][0.609][0.686][0.752][0.807]]
106 [[18][0.0992][0.165][0.244][0.332][0.423][0.512][0.596][0.673][0.74][0.796]]
107 [[19][0.0976][0.161][0.238][0.323][0.412][0.5][0.584][0.66][0.728][0.786]]
108 [[20][0.0961][0.158][0.232][0.315][0.402][0.489][0.572][0.648][0.716][0.775]]
109 ]
110
111 See [@../../example/nc_chi_sq_example.cpp nc_chi_sq_example.cpp] for the full C++ source code.
112
113 */
114
115 //]
116