...one of the most highly
regarded and expertly designed C++ library projects in the
world.

— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards

We use this test to determine whether the standard deviation of a sample differs from a specified value. Typically this occurs in process change situations where we wish to compare the standard deviation of a new process to an established one.

The code for this example is contained in chi_square_std_dev_test.cpp, and we'll begin by defining the procedure that will print out the test statistics:

void chi_squared_test( double Sd, // Sample std deviation double D, // True std deviation unsigned N, // Sample size double alpha) // Significance level {

The procedure begins by printing a summary of the input data:

using namespace std; using namespace boost::math; // Print header: cout << "______________________________________________\n" "Chi Squared test for sample standard deviation\n" "______________________________________________\n\n"; cout << setprecision(5); cout << setw(55) << left << "Number of Observations" << "= " << N << "\n"; cout << setw(55) << left << "Sample Standard Deviation" << "= " << Sd << "\n"; cout << setw(55) << left << "Expected True Standard Deviation" << "= " << D << "\n\n";

The test statistic (T) is simply the ratio of the sample and "true" standard deviations squared, multiplied by the number of degrees of freedom (the sample size less one):

double t_stat = (N - 1) * (Sd / D) * (Sd / D); cout << setw(55) << left << "Test Statistic" << "= " << t_stat << "\n";

The distribution we need to use, is a Chi Squared distribution with N-1 degrees of freedom:

chi_squared dist(N - 1);

The various hypothesis that can be tested are summarised in the following table:

Hypothesis |
Test |
---|---|

The null-hypothesis: there is no difference in standard deviation from the specified value |
Reject if T < χ |

The alternative hypothesis: there is a difference in standard deviation from the specified value |
Reject if χ |

The alternative hypothesis: the standard deviation is less than the specified value |
Reject if χ |

The alternative hypothesis: the standard deviation is greater than the specified value |
Reject if χ |

Where χ^{2}_{(alpha; N-1)} is the upper critical value of the Chi Squared distribution,
and χ^{2}_{(1-alpha; N-1)} is the lower critical value.

Recall that the lower critical value is the same as the quantile, and the upper critical value is the same as the quantile from the complement of the probability, that gives us the following code to calculate the critical values:

double ucv = quantile(complement(dist, alpha)); double ucv2 = quantile(complement(dist, alpha / 2)); double lcv = quantile(dist, alpha); double lcv2 = quantile(dist, alpha / 2); cout << setw(55) << left << "Upper Critical Value at alpha: " << "= " << setprecision(3) << scientific << ucv << "\n"; cout << setw(55) << left << "Upper Critical Value at alpha/2: " << "= " << setprecision(3) << scientific << ucv2 << "\n"; cout << setw(55) << left << "Lower Critical Value at alpha: " << "= " << setprecision(3) << scientific << lcv << "\n"; cout << setw(55) << left << "Lower Critical Value at alpha/2: " << "= " << setprecision(3) << scientific << lcv2 << "\n\n";

Now that we have the critical values, we can compare these to our test statistic, and print out the result of each hypothesis and test:

cout << setw(55) << left << "Results for Alternative Hypothesis and alpha" << "= " << setprecision(4) << fixed << alpha << "\n\n"; cout << "Alternative Hypothesis Conclusion\n"; cout << "Standard Deviation != " << setprecision(3) << fixed << D << " "; if((ucv2 < t_stat) || (lcv2 > t_stat)) cout << "ACCEPTED\n"; else cout << "REJECTED\n"; cout << "Standard Deviation < " << setprecision(3) << fixed << D << " "; if(lcv > t_stat) cout << "ACCEPTED\n"; else cout << "REJECTED\n"; cout << "Standard Deviation > " << setprecision(3) << fixed << D << " "; if(ucv < t_stat) cout << "ACCEPTED\n"; else cout << "REJECTED\n"; cout << endl << endl;

To see some example output we'll use the gear data from the NIST/SEMATECH e-Handbook of Statistical Methods.. The data represents measurements of gear diameter from a manufacturing process. The program output is deliberately designed to mirror the DATAPLOT output shown in the NIST Handbook Example.

______________________________________________ Chi Squared test for sample standard deviation ______________________________________________ Number of Observations = 100 Sample Standard Deviation = 0.00628 Expected True Standard Deviation = 0.10000 Test Statistic = 0.39030 CDF of test statistic: = 1.438e-099 Upper Critical Value at alpha: = 1.232e+002 Upper Critical Value at alpha/2: = 1.284e+002 Lower Critical Value at alpha: = 7.705e+001 Lower Critical Value at alpha/2: = 7.336e+001 Results for Alternative Hypothesis and alpha = 0.0500 Alternative Hypothesis Conclusion Standard Deviation != 0.100 ACCEPTED Standard Deviation < 0.100 ACCEPTED Standard Deviation > 0.100 REJECTED

In this case we are testing whether the sample standard deviation is
0.1, and the null-hypothesis is rejected, so we conclude that the standard
deviation *is not* 0.1.

For an alternative example, consider the silicon wafer data again from the NIST/SEMATECH e-Handbook of Statistical Methods.. In this scenario a supplier of 100 ohm.cm silicon wafers claims that his fabrication process can produce wafers with sufficient consistency so that the standard deviation of resistivity for the lot does not exceed 10 ohm.cm. A sample of N = 10 wafers taken from the lot has a standard deviation of 13.97 ohm.cm, and the question we ask ourselves is "Is the suppliers claim correct?".

The program output now looks like this:

______________________________________________ Chi Squared test for sample standard deviation ______________________________________________ Number of Observations = 10 Sample Standard Deviation = 13.97000 Expected True Standard Deviation = 10.00000 Test Statistic = 17.56448 CDF of test statistic: = 9.594e-001 Upper Critical Value at alpha: = 1.692e+001 Upper Critical Value at alpha/2: = 1.902e+001 Lower Critical Value at alpha: = 3.325e+000 Lower Critical Value at alpha/2: = 2.700e+000 Results for Alternative Hypothesis and alpha = 0.0500 Alternative Hypothesis Conclusion Standard Deviation != 10.000 REJECTED Standard Deviation < 10.000 REJECTED Standard Deviation > 10.000 ACCEPTED

In this case, our null-hypothesis is that the standard deviation of the sample is less than 10: this hypothesis is rejected in the analysis above, and so we reject the manufacturers claim.