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