t_test.qbk 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. [/
  2. Copyright (c) 2019 Nick Thompson
  3. Use, modification and distribution are subject to the
  4. Boost Software License, Version 1.0. (See accompanying file
  5. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ]
  7. [section:t_test /t/-tests]
  8. [heading Synopsis]
  9. ```
  10. #include <boost/math/statistics/t_test.hpp>
  11. namespace boost::math::statistics {
  12. template<typename Real>
  13. std::pair<Real, Real> one_sample_t_test(Real sample_mean, Real sample_variance, Real num_samples, Real assumed_mean);
  14. template<class ForwardIterator>
  15. auto one_sample_t_test(ForwardIterator begin, ForwardIterator end, typename std::iterator_traits<ForwardIterator>::value_type assumed_mean);
  16. template<class Container>
  17. auto one_sample_t_test(Container const & v, typename Container::value_type assumed_mean);
  18. }}}
  19. ```
  20. [heading Background]
  21. A one-sample /t/-test attempts to answer the question "given a sample mean, is it likely that the population mean of my data is a certain value?"
  22. The test statistic is
  23. [$../graphs/one_sample_t_test_statistic.svg]
  24. where µ[sub 0] is the assumed mean, /s/[super 2] is the sample variance, and /n/ is the number of samples.
  25. If the absolute value of the test statistic is large, then we have low confidence that the population mean is equal to µ[sub 0], and if the absolute value of the test statistic is small, we have high confidence.
  26. We now ask the question "what constitutes large and small in this context?"
  27. Under reasonable assumptions, the test statistic /t/ can be assumed to come from a Student's /t/-distribution.
  28. Since we wish to know if the sample mean deviates from the true mean in either direction, the test is two-tailed.
  29. Hence the /p/-value is straightforward to calculate from the Student's /t/-distribution on /n/ - 1 degrees of freedom, but nonetheless it is convenient to have it computed here.
  30. An example usage is as follows:
  31. ```
  32. #include <vector>
  33. #include <random>
  34. #include <boost/math/statistics/t_test.hpp>
  35. std::random_device rd;
  36. std::mt19937 gen{rd()};
  37. std::normal_distribution<double> dis{0,1};
  38. std::vector<double> v(1024);
  39. for (auto & x : v) {
  40. x = dis(gen);
  41. }
  42. auto [t, p] = boost::math::statistics::one_sample_t_test(v, 0.0);
  43. ```
  44. The test statistic is the first element of the pair, and the /p/-value is the second element.
  45. [heading Performance]
  46. There are two cases: Where the mean and sample variance have already been computed, and the case where the mean and sample variance must be computed on the fly.
  47. ```
  48. ----------------------------------------------
  49. Benchmark Time
  50. ----------------------------------------------
  51. OneSampleTTest<double>/8 291 ns bytes_per_second=210.058M/s
  52. OneSampleTTest<double>/16 1064 ns bytes_per_second=114.697M/s
  53. OneSampleTTest<double>/32 407 ns bytes_per_second=599.213M/s
  54. OneSampleTTest<double>/64 595 ns bytes_per_second=821.086M/s
  55. OneSampleTTest<double>/128 1475 ns bytes_per_second=662.071M/s
  56. OneSampleTTest<double>/256 1746 ns bytes_per_second=1118.85M/s
  57. OneSampleTTest<double>/512 3303 ns bytes_per_second=1.15492G/s
  58. OneSampleTTest<double>/1024 6404 ns bytes_per_second=1.19139G/s
  59. OneSampleTTest<double>/2048 12461 ns bytes_per_second=1.2245G/s
  60. OneSampleTTest<double>/4096 24805 ns bytes_per_second=1.23029G/s
  61. OneSampleTTest<double>/8192 49639 ns bytes_per_second=1.22956G/s
  62. OneSampleTTest<double>/16384 98685 ns bytes_per_second=1.23698G/s
  63. OneSampleTTest<double>/32768 197434 ns bytes_per_second=1.23656G/s
  64. OneSampleTTest<double>/65536 393929 ns bytes_per_second=1.23952G/s
  65. OneSampleTTest<double>/131072 790967 ns bytes_per_second=1.23466G/s
  66. OneSampleTTest<double>/262144 1582366 ns bytes_per_second=1.23434G/s
  67. OneSampleTTest<double>/524288 3141112 ns bytes_per_second=1.24358G/s
  68. OneSampleTTest<double>/1048576 6260407 ns bytes_per_second=1.24792G/s
  69. OneSampleTTest<double>/2097152 12521811 ns bytes_per_second=1.24784G/s
  70. OneSampleTTest<double>/4194304 25076257 ns bytes_per_second=1.24619G/s
  71. OneSampleTTest<double>/8388608 50226183 ns bytes_per_second=1.2444G/s
  72. OneSampleTTest<double>/16777216 100522789 ns bytes_per_second=1.24353G/s
  73. OneSampleTTest<double>_BigO 5.99 N
  74. OneSampleTTest<double>_RMS 0 %
  75. OneSampleTTestKnownMeanAndVariance<double> 207 ns
  76. ```
  77. [endsect]
  78. [/section:t_test]