📄 students_t_single_sample.cpp
字号:
// Copyright John Maddock 2006// Copyright Paul A. Bristow 2007// Use, modification and distribution are subject to 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)#ifdef _MSC_VER# pragma warning(disable: 4512) // assignment operator could not be generated.# pragma warning(disable: 4510) // default constructor could not be generated.# pragma warning(disable: 4610) // can never be instantiated - user defined constructor required.#endif#include <iostream>#include <iomanip>#include <boost/math/distributions/students_t.hpp>void confidence_limits_on_mean(double Sm, double Sd, unsigned Sn){ // // Sm = Sample Mean. // Sd = Sample Standard Deviation. // Sn = Sample Size. // // Calculate confidence intervals for the mean. // For example if we set the confidence limit to // 0.95, we know that if we repeat the sampling // 100 times, then we expect that the true mean // will be between out limits on 95 occations. // Note: this is not the same as saying a 95% // confidence interval means that there is a 95% // probability that the interval contains the true mean. // The interval computed from a given sample either // contains the true mean or it does not. // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm using namespace std; using namespace boost::math; // Print out general info: cout << "__________________________________\n" "2-Sided Confidence Limits For Mean\n" "__________________________________\n\n"; cout << setprecision(7); cout << setw(40) << left << "Number of Observations" << "= " << Sn << "\n"; cout << setw(40) << left << "Mean" << "= " << Sm << "\n"; cout << setw(40) << left << "Standard Deviation" << "= " << Sd << "\n"; // // Define a table of significance/risk levels: // double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 }; // // Start by declaring the distribution we'll need: // students_t dist(Sn - 1); // // Print table header: // cout << "\n\n" "_______________________________________________________________\n" "Confidence T Interval Lower Upper\n" " Value (%) Value Width Limit Limit\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 T: double T = quantile(complement(dist, alpha[i] / 2)); // Print T: cout << fixed << setprecision(3) << setw(10) << right << T; // Calculate width of interval (one sided): double w = T * Sd / sqrt(double(Sn)); // Print width: if(w < 0.01) cout << scientific << setprecision(3) << setw(17) << right << w; else cout << fixed << setprecision(3) << setw(17) << right << w; // Print Limits: cout << fixed << setprecision(5) << setw(15) << right << Sm - w; cout << fixed << setprecision(5) << setw(15) << right << Sm + w << endl; } cout << endl;}void single_sample_t_test(double M, double Sm, double Sd, unsigned Sn, double alpha){ // // M = true mean. // Sm = Sample Mean. // Sd = Sample Standard Deviation. // Sn = Sample Size. // alpha = Significance Level. // // A Students t test applied to a single set of data. // We are testing the null hypothesis that the true // mean of the sample is M, and that any variation is down // to chance. We can also test the alternative hypothesis // that any difference is not down to chance. // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda352.htm // using namespace std; using namespace boost::math; // Print header: cout << "__________________________________\n" "Student t test for a single sample\n" "__________________________________\n\n"; cout << setprecision(5); cout << setw(55) << left << "Number of Observations" << "= " << Sn << "\n"; cout << setw(55) << left << "Sample Mean" << "= " << Sm << "\n"; cout << setw(55) << left << "Sample Standard Deviation" << "= " << Sd << "\n"; cout << setw(55) << left << "Expected True Mean" << "= " << M << "\n\n"; // // Now we can calculate and output some stats: // // Difference in means: double diff = Sm - M; cout << setw(55) << left << "Sample Mean - Expected Test Mean" << "= " << diff << "\n"; // Degrees of freedom: unsigned v = Sn - 1; cout << setw(55) << left << "Degrees of Freedom" << "= " << v << "\n"; // t-statistic: double t_stat = diff * sqrt(double(Sn)) / Sd; cout << setw(55) << left << "T Statistic" << "= " << t_stat << "\n"; // // Finally define our distribution, and get the probability: // students_t dist(v); double q = cdf(complement(dist, fabs(t_stat))); cout << setw(55) << left << "Probability that difference is due to chance" << "= " << setprecision(3) << scientific << 2 * q << "\n\n"; // // Finally print out results of alternative hypothesis: // cout << setw(55) << left << "Results for Alternative Hypothesis and alpha" << "= " << setprecision(4) << fixed << alpha << "\n\n"; cout << "Alternative Hypothesis Conclusion\n"; cout << "Mean != " << setprecision(3) << fixed << M << " "; if(q < alpha / 2) cout << "NOT REJECTED\n"; else cout << "REJECTED\n"; cout << "Mean < " << setprecision(3) << fixed << M << " "; if(cdf(dist, t_stat) < alpha) cout << "NOT REJECTED\n"; else cout << "REJECTED\n"; cout << "Mean > " << setprecision(3) << fixed << M << " "; if(cdf(complement(dist, t_stat)) < alpha) cout << "NOT REJECTED\n"; else cout << "REJECTED\n"; cout << endl << endl;}void single_sample_find_df(double M, double Sm, double Sd){ // // M = true mean. // Sm = Sample Mean. // Sd = Sample Standard Deviation. // 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 Mean" << "= " << M << "\n"; cout << setw(40) << left << "Sample Mean" << "= " << Sm << "\n"; cout << setw(40) << left << "Sample Standard Deviation" << "= " << Sd << "\n"; // // Define a table of significance intervals: // double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 }; // // Print table header: // cout << "\n\n" "_______________________________________________________________\n" "Confidence Estimated Estimated\n" " Value (%) Sample Size Sample Size\n" " (one sided test) (two 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 single sided test: double df = students_t::find_degrees_of_freedom( fabs(M - Sm), alpha[i], alpha[i], Sd); // convert to sample size: double size = ceil(df) + 1; // Print size: cout << fixed << setprecision(0) << setw(16) << right << size; // calculate df for two sided test: df = students_t::find_degrees_of_freedom( fabs(M - Sm), alpha[i]/2, alpha[i], Sd); // convert to sample size: size = ceil(df) + 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -