⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chi_squared_examples.qbk

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 QBK
📖 第 1 页 / 共 2 页
字号:
   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 teststatistic, 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[@http://www.itl.nist.gov/div898/handbook/eda/section3/eda3581.htm gear data] from the __handbook.The data represents measurements of gear diameter from a manufacturingprocess.  The program output is deliberately designed to mirror the DATAPLOT output shown in the [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda358.htm NIST Handbook Example].[pre'''______________________________________________Chi Squared test for sample standard deviation______________________________________________Number of Observations                                 =  100Sample Standard Deviation                              =  0.00628Expected True Standard Deviation                       =  0.10000Test Statistic                                         =  0.39030CDF of test statistic:                                 =  1.438e-099Upper Critical Value at alpha:                         =  1.232e+002Upper Critical Value at alpha/2:                       =  1.284e+002Lower Critical Value at alpha:                         =  7.705e+001Lower Critical Value at alpha/2:                       =  7.336e+001Results for Alternative Hypothesis and alpha           =  0.0500Alternative Hypothesis              Conclusion'''Standard Deviation != 0.100            ACCEPTEDStandard Deviation  < 0.100            ACCEPTEDStandard 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 standarddeviation ['is not] 0.1.For an alternative example, consider the [@http://www.itl.nist.gov/div898/handbook/prc/section2/prc23.htm silicon wafer data] again from the __handbook.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 questionwe ask ourselves is "Is the suppliers claim correct?".The program output now looks like this:[pre'''______________________________________________Chi Squared test for sample standard deviation______________________________________________Number of Observations                                 =  10Sample Standard Deviation                              =  13.97000Expected True Standard Deviation                       =  10.00000Test Statistic                                         =  17.56448CDF of test statistic:                                 =  9.594e-001Upper Critical Value at alpha:                         =  1.692e+001Upper Critical Value at alpha/2:                       =  1.902e+001Lower Critical Value at alpha:                         =  3.325e+000Lower Critical Value at alpha/2:                       =  2.700e+000Results for Alternative Hypothesis and alpha           =  0.0500Alternative Hypothesis              Conclusion'''Standard Deviation != 10.000            REJECTEDStandard Deviation  < 10.000            REJECTEDStandard 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 analysisabove, and so we reject the manufacturers claim.[endsect][/section:chi_sq_test Chi-Square Test for the Standard Deviation][section:chi_sq_size Estimating the Required Sample Sizes for a Chi-Square Test for the Standard Deviation]Suppose we conduct a Chi Squared test for standard deviation and the resultis borderline, a legitimate question to ask is "How large would the sample sizehave to be in order to produce a definitive result?"The class template [link math_toolkit.dist.dist_ref.dists.chi_squared_dist chi_squared_distribution] has a static method `find_degrees_of_freedom` that will calculate this value forsome acceptable risk of type I failure /alpha/, type II failure /beta/, and difference from the standard deviation /diff/.  Pleasenote that the method used works on variance, and not standard deviationas is usual for the Chi Squared Test.The code for this example is located in [@../../../example/chi_square_std_dev_test.cpp chi_square_std_dev_test.cpp].We begin by defining a procedure to print out the sample sizes requiredfor various risk levels:   void chi_squared_sample_sized(        double diff,      // difference from variance to detect        double variance)  // true variance   {The procedure begins by printing out the input data:   using namespace std;   using namespace boost::math;   // Print out general info:   cout <<      "_____________________________________________________________\n"      "Estimated sample sizes required for various confidence levels\n"      "_____________________________________________________________\n\n";   cout << setprecision(5);   cout << setw(40) << left << "True Variance" << "=  " << variance << "\n";   cout << setw(40) << left << "Difference to detect" << "=  " << diff << "\n";And defines a table of significance levels for which we'll calculate sample sizes:   double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };For each value of alpha we can calculate two sample sizes: one where thesample variance is less than the true value by /diff/ and onewhere it is greater than the true value by /diff/.  Thanks to theasymmetric nature of the Chi Squared distribution these two values willnot be the same, the difference in their calculation differs only in thesign of /diff/ that's passed to `find_degrees_of_freedom`.  Finallyin this example we'll simply things, and let risk level /beta/ be the same as /alpha/:   cout << "\n\n"           "_______________________________________________________________\n"           "Confidence       Estimated          Estimated\n"           " Value (%)      Sample Size        Sample Size\n"           "                (lower one         (upper one\n"           "                 sided test)        sided test)\n"           "_______________________________________________________________\n";   //   // Now print out the data for the table rows.   //   for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)   {      // Confidence value:      cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);      // calculate df for a lower single sided test:      double df = chi_squared::find_degrees_of_freedom(         -diff, alpha[i], alpha[i], variance);      // convert to sample size:      double size = ceil(df) + 1;      // Print size:      cout << fixed << setprecision(0) << setw(16) << right << size;      // calculate df for an upper single sided test:      df = chi_squared::find_degrees_of_freedom(         diff, alpha[i], alpha[i], variance);      // convert to sample size:      size = ceil(df) + 1;      // Print size:      cout << fixed << setprecision(0) << setw(16) << right << size << endl;   }   cout << endl;For some example output, consider the [@http://www.itl.nist.gov/div898/handbook/prc/section2/prc23.htm silicon wafer data] from the __handbook.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 questionwe ask ourselves is "How large would our sample have to be to reliablydetect this difference?".To use our procedure above, we have to convert thestandard deviations to variance (square them), after which the program output looks like this:[pre'''_____________________________________________________________Estimated sample sizes required for various confidence levels_____________________________________________________________True Variance                           =  100.00000Difference to detect                    =  95.16090_______________________________________________________________Confidence       Estimated          Estimated Value (%)      Sample Size        Sample Size                (lower one         (upper one                 sided test)        sided test)_______________________________________________________________    50.000               2               2    75.000               2              10    90.000               4              32    95.000               5              51    99.000               7              99    99.900              11             174    99.990              15             251    99.999              20             330''']In this case we are interested in a upper single sided test.  So for example, if the maximum acceptable risk of falsely rejectingthe null-hypothesis is 0.05 (Type I error), and the maximum acceptable risk of failing to reject the null-hypothesis is also 0.05 (Type II error), we estimate that we would need a sample size of 51.[endsect][/section:chi_sq_size Estimating the Required Sample Sizes for a Chi-Square Test for the Standard Deviation][endsect][/section:cs_eg Chi Squared Distribution][/   Copyright 2006 John Maddock and Paul A. Bristow.  Distributed under the Boost Software License, Version 1.0.  (See accompanying file LICENSE_1_0.txt or copy at  http://www.boost.org/LICENSE_1_0.txt).]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -