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

📄 students_t_two_samples.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 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 two_samples_t_test_equal_sd(        double Sm1,        double Sd1,        unsigned Sn1,        double Sm2,        double Sd2,        unsigned Sn2,        double alpha){   //   // Sm1 = Sample Mean 1.   // Sd1 = Sample Standard Deviation 1.   // Sn1 = Sample Size 1.   // Sm2 = Sample Mean 2.   // Sd2 = Sample Standard Deviation 2.   // Sn2 = Sample Size 2.   // alpha = Significance Level.   //   // A Students t test applied to two sets of data.   // We are testing the null hypothesis that the two   // samples have the same mean and that any difference   // if due to chance.   // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm   //   using namespace std;   using namespace boost::math;   // Print header:   cout <<      "_______________________________________________\n"      "Student t test for two samples (equal variances)\n"      "_______________________________________________\n\n";   cout << setprecision(5);   cout << setw(55) << left << "Number of Observations (Sample 1)" << "=  " << Sn1 << "\n";   cout << setw(55) << left << "Sample 1 Mean" << "=  " << Sm1 << "\n";   cout << setw(55) << left << "Sample 1 Standard Deviation" << "=  " << Sd1 << "\n";   cout << setw(55) << left << "Number of Observations (Sample 2)" << "=  " << Sn2 << "\n";   cout << setw(55) << left << "Sample 2 Mean" << "=  " << Sm2 << "\n";   cout << setw(55) << left << "Sample 2 Standard Deviation" << "=  " << Sd2 << "\n";   //   // Now we can calculate and output some stats:   //   // Degrees of freedom:   double v = Sn1 + Sn2 - 2;   cout << setw(55) << left << "Degrees of Freedom" << "=  " << v << "\n";   // Pooled variance:   double sp = sqrt(((Sn1-1) * Sd1 * Sd1 + (Sn2-1) * Sd2 * Sd2) / v);   cout << setw(55) << left << "Pooled Standard Deviation" << "=  " << v << "\n";   // t-statistic:   double t_stat = (Sm1 - Sm2) / (sp * sqrt(1.0 / Sn1 + 1.0 / Sn2));   cout << setw(55) << left << "T Statistic" << "=  " << t_stat << "\n";   //   // 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 << "Sample 1 Mean != Sample 2 Mean       " ;   if(q < alpha / 2)      cout << "NOT REJECTED\n";   else      cout << "REJECTED\n";   cout << "Sample 1 Mean <  Sample 2 Mean       ";   if(cdf(dist, t_stat) < alpha)      cout << "NOT REJECTED\n";   else      cout << "REJECTED\n";   cout << "Sample 1 Mean >  Sample 2 Mean       ";   if(cdf(complement(dist, t_stat)) < alpha)      cout << "NOT REJECTED\n";   else      cout << "REJECTED\n";   cout << endl << endl;}void two_samples_t_test_unequal_sd(        double Sm1,        double Sd1,        unsigned Sn1,        double Sm2,        double Sd2,        unsigned Sn2,        double alpha){   //   // Sm1 = Sample Mean 1.   // Sd1 = Sample Standard Deviation 1.   // Sn1 = Sample Size 1.   // Sm2 = Sample Mean 2.   // Sd2 = Sample Standard Deviation 2.   // Sn2 = Sample Size 2.   // alpha = Significance Level.   //   // A Students t test applied to two sets of data.   // We are testing the null hypothesis that the two   // samples have the same mean and that any difference   // if due to chance.   // See http://www.itl.nist.gov/div898/handbook/eda/section3/eda353.htm   //   using namespace std;   using namespace boost::math;   // Print header:   cout <<      "_________________________________________________\n"      "Student t test for two samples (unequal variances)\n"      "_________________________________________________\n\n";   cout << setprecision(5);   cout << setw(55) << left << "Number of Observations (Sample 1)" << "=  " << Sn1 << "\n";   cout << setw(55) << left << "Sample 1 Mean" << "=  " << Sm1 << "\n";   cout << setw(55) << left << "Sample 1 Standard Deviation" << "=  " << Sd1 << "\n";   cout << setw(55) << left << "Number of Observations (Sample 2)" << "=  " << Sn2 << "\n";   cout << setw(55) << left << "Sample 2 Mean" << "=  " << Sm2 << "\n";   cout << setw(55) << left << "Sample 2 Standard Deviation" << "=  " << Sd2 << "\n";   //   // Now we can calculate and output some stats:   //   // Degrees of freedom:   double v = Sd1 * Sd1 / Sn1 + Sd2 * Sd2 / Sn2;   v *= v;   double t1 = Sd1 * Sd1 / Sn1;   t1 *= t1;   t1 /=  (Sn1 - 1);   double t2 = Sd2 * Sd2 / Sn2;   t2 *= t2;   t2 /= (Sn2 - 1);   v /= (t1 + t2);   cout << setw(55) << left << "Degrees of Freedom" << "=  " << v << "\n";   // t-statistic:   double t_stat = (Sm1 - Sm2) / sqrt(Sd1 * Sd1 / Sn1 + Sd2 * Sd2 / Sn2);   cout << setw(55) << left << "T Statistic" << "=  " << t_stat << "\n";   //   // 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 << "Sample 1 Mean != Sample 2 Mean       " ;   if(q < alpha / 2)      cout << "NOT REJECTED\n";   else      cout << "REJECTED\n";   cout << "Sample 1 Mean <  Sample 2 Mean       ";   if(cdf(dist, t_stat) < alpha)      cout << "NOT REJECTED\n";   else      cout << "REJECTED\n";   cout << "Sample 1 Mean >  Sample 2 Mean       ";   if(cdf(complement(dist, t_stat)) < alpha)      cout << "NOT REJECTED\n";   else      cout << "REJECTED\n";   cout << endl << endl;}int main(){   //   // Run tests for Car Mileage sample data   // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3531.htm   // from the NIST website http://www.itl.nist.gov.  The data compares   // miles per gallon of US cars with miles per gallon of Japanese cars.   //   two_samples_t_test_equal_sd(20.14458, 6.414700, 249, 30.48101, 6.107710, 79, 0.05);   two_samples_t_test_unequal_sd(20.14458, 6.414700, 249, 30.48101, 6.107710, 79, 0.05);   return 0;} // int main()/*Output is------ Build started: Project: students_t_two_samples, Configuration: Debug Win32 ------Compiling...students_t_two_samples.cppLinking...Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\students_t_two_samples.exe"_______________________________________________Student t test for two samples (equal variances)_______________________________________________Number of Observations (Sample 1)                      =  249Sample 1 Mean                                          =  20.145Sample 1 Standard Deviation                            =  6.4147Number of Observations (Sample 2)                      =  79Sample 2 Mean                                          =  30.481Sample 2 Standard Deviation                            =  6.1077Degrees of Freedom                                     =  326Pooled Standard Deviation                              =  326T Statistic                                            =  -12.621Probability that difference is due to chance           =  5.273e-030Results for Alternative Hypothesis and alpha           =  0.0500Alternative Hypothesis              ConclusionSample 1 Mean != Sample 2 Mean       NOT REJECTEDSample 1 Mean <  Sample 2 Mean       NOT REJECTEDSample 1 Mean >  Sample 2 Mean       REJECTED_________________________________________________Student t test for two samples (unequal variances)_________________________________________________Number of Observations (Sample 1)                      =  249Sample 1 Mean                                          =  20.14458Sample 1 Standard Deviation                            =  6.41470Number of Observations (Sample 2)                      =  79Sample 2 Mean                                          =  30.48101Sample 2 Standard Deviation                            =  6.10771Degrees of Freedom                                     =  136.87499T Statistic                                            =  -12.94627Probability that difference is due to chance           =  1.571e-025Results for Alternative Hypothesis and alpha           =  0.0500Alternative Hypothesis              ConclusionSample 1 Mean != Sample 2 Mean       NOT REJECTEDSample 1 Mean <  Sample 2 Mean       NOT REJECTEDSample 1 Mean >  Sample 2 Mean       REJECTEDBuild Time 0:03Build log was saved at "file://i:\boost-06-05-03-1300\libs\math\test\Math_test\students_t_two_samples\Debug\BuildLog.htm"students_t_two_samples - 0 error(s), 0 warning(s)========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========*/

⌨️ 快捷键说明

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