• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>t-tests</title>
5<link rel="stylesheet" href="../math.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../index.html" title="Math Toolkit 2.12.0">
8<link rel="up" href="../statistics.html" title="Chapter 6. Statistics">
9<link rel="prev" href="anderson_darling.html" title="The Anderson-Darling Test">
10<link rel="next" href="runs_test.html" title="Runs tests">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="anderson_darling.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../statistics.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="runs_test.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h2 class="title" style="clear: both">
27<a name="math_toolkit.t_test"></a><a class="link" href="t_test.html" title="t-tests"><span class="emphasis"><em>t</em></span>-tests</a>
28</h2></div></div></div>
29<h4>
30<a name="math_toolkit.t_test.h0"></a>
31      <span class="phrase"><a name="math_toolkit.t_test.synopsis"></a></span><a class="link" href="t_test.html#math_toolkit.t_test.synopsis">Synopsis</a>
32    </h4>
33<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">statistics</span><span class="special">/</span><span class="identifier">t_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
34
35<span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">statistics</span> <span class="special">{</span>
36
37<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Real</span><span class="special">&gt;</span>
38<span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">&gt;</span> <span class="identifier">one_sample_t_test</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">sample_mean</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">sample_variance</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">num_samples</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">assumed_mean</span><span class="special">);</span>
39
40<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
41<span class="keyword">auto</span> <span class="identifier">one_sample_t_test</span><span class="special">(</span><span class="identifier">ForwardIterator</span> <span class="identifier">begin</span><span class="special">,</span> <span class="identifier">ForwardIterator</span> <span class="identifier">end</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">iterator_traits</span><span class="special">&lt;</span><span class="identifier">ForwardIterator</span><span class="special">&gt;::</span><span class="identifier">value_type</span> <span class="identifier">assumed_mean</span><span class="special">);</span>
42
43<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">&gt;</span>
44<span class="keyword">auto</span> <span class="identifier">one_sample_t_test</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Container</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">assumed_mean</span><span class="special">);</span>
45
46<span class="special">}}}</span>
47</pre>
48<h4>
49<a name="math_toolkit.t_test.h1"></a>
50      <span class="phrase"><a name="math_toolkit.t_test.background"></a></span><a class="link" href="t_test.html#math_toolkit.t_test.background">Background</a>
51    </h4>
52<p>
53      A one-sample <span class="emphasis"><em>t</em></span>-test attempts to answer the question "given
54      a sample mean, is it likely that the population mean of my data is a certain
55      value?" The test statistic is
56    </p>
57<p>
58      <span class="inlinemediaobject"><object type="image/svg+xml" data="../../graphs/one_sample_t_test_statistic.svg" width="97" height="59"></object></span>
59    </p>
60<p>
61      where µ<sub>0</sub> is the assumed mean, <span class="emphasis"><em>s</em></span><sup>2</sup> is the sample variance,
62      and <span class="emphasis"><em>n</em></span> is the number of samples. If the absolute value
63      of the test statistic is large, then we have low confidence that the population
64      mean is equal to µ<sub>0</sub>, and if the absolute value of the test statistic is small,
65      we have high confidence. We now ask the question "what constitutes large
66      and small in this context?"
67    </p>
68<p>
69      Under reasonable assumptions, the test statistic <span class="emphasis"><em>t</em></span> can
70      be assumed to come from a Student's <span class="emphasis"><em>t</em></span>-distribution. Since
71      we wish to know if the sample mean deviates from the true mean in either direction,
72      the test is two-tailed. Hence the <span class="emphasis"><em>p</em></span>-value is straightforward
73      to calculate from the Student's <span class="emphasis"><em>t</em></span>-distribution on <span class="emphasis"><em>n</em></span>
74      - 1 degrees of freedom, but nonetheless it is convenient to have it computed
75      here.
76    </p>
77<p>
78      An example usage is as follows:
79    </p>
80<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
81<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">random</span><span class="special">&gt;</span>
82<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">statistics</span><span class="special">/</span><span class="identifier">t_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
83
84<span class="identifier">std</span><span class="special">::</span><span class="identifier">random_device</span> <span class="identifier">rd</span><span class="special">;</span>
85<span class="identifier">std</span><span class="special">::</span><span class="identifier">mt19937</span> <span class="identifier">gen</span><span class="special">{</span><span class="identifier">rd</span><span class="special">()};</span>
86<span class="identifier">std</span><span class="special">::</span><span class="identifier">normal_distribution</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">dis</span><span class="special">{</span><span class="number">0</span><span class="special">,</span><span class="number">1</span><span class="special">};</span>
87<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">(</span><span class="number">1024</span><span class="special">);</span>
88<span class="keyword">for</span> <span class="special">(</span><span class="keyword">auto</span> <span class="special">&amp;</span> <span class="identifier">x</span> <span class="special">:</span> <span class="identifier">v</span><span class="special">)</span> <span class="special">{</span>
89  <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
90<span class="special">}</span>
91
92<span class="keyword">auto</span> <span class="special">[</span><span class="identifier">t</span><span class="special">,</span> <span class="identifier">p</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">statistics</span><span class="special">::</span><span class="identifier">one_sample_t_test</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="number">0.0</span><span class="special">);</span>
93</pre>
94<p>
95      The test statistic is the first element of the pair, and the <span class="emphasis"><em>p</em></span>-value
96      is the second element.
97    </p>
98<h4>
99<a name="math_toolkit.t_test.h2"></a>
100      <span class="phrase"><a name="math_toolkit.t_test.performance"></a></span><a class="link" href="t_test.html#math_toolkit.t_test.performance">Performance</a>
101    </h4>
102<p>
103      There are two cases: Where the mean and sample variance have already been computed,
104      and the case where the mean and sample variance must be computed on the fly.
105    </p>
106<pre class="programlisting"><span class="special">----------------------------------------------</span>
107<span class="identifier">Benchmark</span>                                <span class="identifier">Time</span>
108<span class="special">----------------------------------------------</span>
109<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">8</span>               <span class="number">291</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">210.058</span><span class="identifier">M</span><span class="special">/</span><span class="identifier">s</span>
110<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">16</span>             <span class="number">1064</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">114.697</span><span class="identifier">M</span><span class="special">/</span><span class="identifier">s</span>
111<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">32</span>              <span class="number">407</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">599.213</span><span class="identifier">M</span><span class="special">/</span><span class="identifier">s</span>
112<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">64</span>              <span class="number">595</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">821.086</span><span class="identifier">M</span><span class="special">/</span><span class="identifier">s</span>
113<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">128</span>            <span class="number">1475</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">662.071</span><span class="identifier">M</span><span class="special">/</span><span class="identifier">s</span>
114<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">256</span>            <span class="number">1746</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1118.85</span><span class="identifier">M</span><span class="special">/</span><span class="identifier">s</span>
115<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">512</span>            <span class="number">3303</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.15492</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
116<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">1024</span>           <span class="number">6404</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.19139</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
117<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">2048</span>          <span class="number">12461</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.2245</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
118<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">4096</span>          <span class="number">24805</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.23029</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
119<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">8192</span>          <span class="number">49639</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.22956</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
120<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">16384</span>         <span class="number">98685</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.23698</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
121<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">32768</span>        <span class="number">197434</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.23656</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
122<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">65536</span>        <span class="number">393929</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.23952</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
123<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">131072</span>       <span class="number">790967</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.23466</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
124<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">262144</span>      <span class="number">1582366</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.23434</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
125<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">524288</span>      <span class="number">3141112</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.24358</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
126<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">1048576</span>     <span class="number">6260407</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.24792</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
127<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">2097152</span>    <span class="number">12521811</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.24784</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
128<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">4194304</span>    <span class="number">25076257</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.24619</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
129<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">8388608</span>    <span class="number">50226183</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.2444</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
130<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;/</span><span class="number">16777216</span>  <span class="number">100522789</span> <span class="identifier">ns</span> <span class="identifier">bytes_per_second</span><span class="special">=</span><span class="number">1.24353</span><span class="identifier">G</span><span class="special">/</span><span class="identifier">s</span>
131<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span><span class="identifier">_BigO</span>           <span class="number">5.99</span> <span class="identifier">N</span>
132<span class="identifier">OneSampleTTest</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span><span class="identifier">_RMS</span>               <span class="number">0</span> <span class="special">%</span>
133<span class="identifier">OneSampleTTestKnownMeanAndVariance</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span>        <span class="number">207</span> <span class="identifier">ns</span>
134</pre>
135</div>
136<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
137<td align="left"></td>
138<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
139      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
140      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
141      Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
142      Daryle Walker and Xiaogang Zhang<p>
143        Distributed under the Boost Software License, Version 1.0. (See accompanying
144        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
145      </p>
146</div></td>
147</tr></table>
148<hr>
149<div class="spirit-nav">
150<a accesskey="p" href="anderson_darling.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../statistics.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="runs_test.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
151</div>
152</body>
153</html>
154