Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / math / doc / sf_and_dist / html / math_toolkit / dist / stat_tut / overview / complements.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Complements are supported too - and when to use them</title>
5 <link rel="stylesheet" href="../../../../../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="../../../../index.html" title="Math Toolkit">
8 <link rel="up" href="../overview.html" title="Overview of Distributions">
9 <link rel="prev" href="generic.html" title="Generic operations common to all distributions are non-member functions">
10 <link rel="next" href="parameters.html" title="Parameters can be calculated">
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="generic.html"><img src="../../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.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="parameters.html"><img src="../../../../../../../../../doc/src/images/next.png" alt="Next"></a>
24 </div>
25 <div class="section math_toolkit_dist_stat_tut_overview_complements">
26 <div class="titlepage"><div><div><h5 class="title">
27 <a name="math_toolkit.dist.stat_tut.overview.complements"></a><a name="complements"></a><a class="link" href="complements.html" title="Complements are supported too - and when to use them">Complements
28           are supported too - and when to use them</a>
29 </h5></div></div></div>
30 <p>
31             Often you don't want the value of the CDF, but its complement, which
32             is to say <code class="computeroutput"><span class="number">1</span><span class="special">-</span><span class="identifier">p</span></code> rather than <code class="computeroutput"><span class="identifier">p</span></code>.
33             It is tempting to calculate the CDF and subtract it from <code class="computeroutput"><span class="number">1</span></code>, but if <code class="computeroutput"><span class="identifier">p</span></code>
34             is very close to <code class="computeroutput"><span class="number">1</span></code> then cancellation
35             error will cause you to lose accuracy, perhaps totally.
36           </p>
37 <p>
38             <a class="link" href="complements.html#why_complements">See below <span class="emphasis"><em>"Why and when
39             to use complements?"</em></span></a>
40           </p>
41 <p>
42             In this library, whenever you want to receive a complement, just wrap
43             all the function arguments in a call to <code class="computeroutput"><span class="identifier">complement</span><span class="special">(...)</span></code>, for example:
44           </p>
45 <pre class="programlisting"><span class="identifier">students_t</span> <span class="identifier">dist</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
46 <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"CDF at t = 1 is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="number">1.0</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
47 <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Complement of CDF at t = 1 is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="number">1.0</span><span class="special">))</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
48 </pre>
49 <p>
50             But wait, now that we have a complement, we have to be able to use it
51             as well. Any function that accepts a probability as an argument can also
52             accept a complement by wrapping all of its arguments in a call to <code class="computeroutput"><span class="identifier">complement</span><span class="special">(...)</span></code>,
53             for example:
54           </p>
55 <pre class="programlisting"><span class="identifier">students_t</span> <span class="identifier">dist</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
56
57 <span class="keyword">for</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">1e10</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">*=</span> <span class="number">10</span><span class="special">)</span>
58 <span class="special">{</span>
59    <span class="comment">// Calculate the quantile for a 1 in i chance:</span>
60    <span class="keyword">double</span> <span class="identifier">t</span> <span class="special">=</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="number">1</span><span class="special">/</span><span class="identifier">i</span><span class="special">));</span>
61    <span class="comment">// Print it out:</span>
62    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Quantile of students-t with 5 degrees of freedom\n"</span>
63            <span class="string">"for a 1 in "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="string">" chance is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">t</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
64 <span class="special">}</span>
65 </pre>
66 <div class="tip"><table border="0" summary="Tip">
67 <tr>
68 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../../../../doc/src/images/tip.png"></td>
69 <th align="left">Tip</th>
70 </tr>
71 <tr><td align="left" valign="top">
72 <p>
73               <span class="bold"><strong>Critical values are just quantiles</strong></span>
74             </p>
75 <p>
76               Some texts talk about quantiles, or percentiles or fractiles, others
77               about critical values, the basic rule is:
78             </p>
79 <p>
80               <span class="emphasis"><em>Lower critical values</em></span> are the same as the quantile.
81             </p>
82 <p>
83               <span class="emphasis"><em>Upper critical values</em></span> are the same as the quantile
84               from the complement of the probability.
85             </p>
86 <p>
87               For example, suppose we have a Bernoulli process, giving rise to a
88               binomial distribution with success ratio 0.1 and 100 trials in total.
89               The <span class="emphasis"><em>lower critical value</em></span> for a probability of
90               0.05 is given by:
91             </p>
92 <p>
93               <code class="computeroutput"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="number">100</span><span class="special">,</span> <span class="number">0.1</span><span class="special">),</span> <span class="number">0.05</span><span class="special">)</span></code>
94             </p>
95 <p>
96               and the <span class="emphasis"><em>upper critical value</em></span> is given by:
97             </p>
98 <p>
99               <code class="computeroutput"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="number">100</span><span class="special">,</span> <span class="number">0.1</span><span class="special">),</span> <span class="number">0.05</span><span class="special">))</span></code>
100             </p>
101 <p>
102               which return 4.82 and 14.63 respectively.
103             </p>
104 </td></tr>
105 </table></div>
106 <a name="why_complements"></a><div class="tip"><table border="0" summary="Tip">
107 <tr>
108 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../../../../doc/src/images/tip.png"></td>
109 <th align="left">Tip</th>
110 </tr>
111 <tr><td align="left" valign="top">
112 <p>
113               <span class="bold"><strong>Why bother with complements anyway?</strong></span>
114             </p>
115 <p>
116               It's very tempting to dispense with complements, and simply subtract
117               the probability from 1 when required. However, consider what happens
118               when the probability is very close to 1: let's say the probability
119               expressed at float precision is <code class="computeroutput"><span class="number">0.999999940f</span></code>,
120               then <code class="computeroutput"><span class="number">1</span> <span class="special">-</span>
121               <span class="number">0.999999940f</span> <span class="special">=</span>
122               <span class="number">5.96046448e-008</span></code>, but the result
123               is actually accurate to just <span class="emphasis"><em>one single bit</em></span>: the
124               only bit that didn't cancel out!
125             </p>
126 <p>
127               Or to look at this another way: consider that we want the risk of falsely
128               rejecting the null-hypothesis in the Student's t test to be 1 in 1
129               billion, for a sample size of 10,000. This gives a probability of 1
130               - 10<sup>-9</sup>, which is exactly 1 when calculated at float precision. In this
131               case calculating the quantile from the complement neatly solves the
132               problem, so for example:
133             </p>
134 <p>
135               <code class="computeroutput"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">students_t</span><span class="special">(</span><span class="number">10000</span><span class="special">),</span> <span class="number">1e-9</span><span class="special">))</span></code>
136             </p>
137 <p>
138               returns the expected t-statistic <code class="computeroutput"><span class="number">6.00336</span></code>,
139               where as:
140             </p>
141 <p>
142               <code class="computeroutput"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">students_t</span><span class="special">(</span><span class="number">10000</span><span class="special">),</span> <span class="number">1</span><span class="special">-</span><span class="number">1e-9f</span><span class="special">)</span></code>
143             </p>
144 <p>
145               raises an overflow error, since it is the same as:
146             </p>
147 <p>
148               <code class="computeroutput"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">students_t</span><span class="special">(</span><span class="number">10000</span><span class="special">),</span> <span class="number">1</span><span class="special">)</span></code>
149             </p>
150 <p>
151               Which has no finite result.
152             </p>
153 <p>
154               With all distributions, even for more reasonable probability (unless
155               the value of p can be represented exactly in the floating-point type)
156               the loss of accuracy quickly becomes significant if you simply calculate
157               probability from 1 - p (because it will be mostly garbage digits for
158               p ~ 1).
159             </p>
160 <p>
161               So always avoid, for example, using a probability near to unity like
162               0.99999
163             </p>
164 <p>
165               <code class="computeroutput"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">my_distribution</span><span class="special">,</span>
166               <span class="number">0.99999</span><span class="special">)</span></code>
167             </p>
168 <p>
169               and instead use
170             </p>
171 <p>
172               <code class="computeroutput"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">my_distribution</span><span class="special">,</span>
173               <span class="number">0.00001</span><span class="special">))</span></code>
174             </p>
175 <p>
176               since 1 - 0.99999 is not exactly equal to 0.00001 when using floating-point
177               arithmetic.
178             </p>
179 <p>
180               This assumes that the 0.00001 value is either a constant, or can be
181               computed by some manner other than subtracting 0.99999 from 1.
182             </p>
183 </td></tr>
184 </table></div>
185 </div>
186 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
187 <td align="left"></td>
188 <td align="right"><div class="copyright-footer">Copyright &#169; 2006-2010 John Maddock, Paul A. Bristow, Hubert Holin, Xiaogang Zhang, Bruno
189       Lalande, Johan R&#229;de, Gautam Sewani, Thijs van den Berg and Benjamin Sobotta<p>
190         Distributed under the Boost Software License, Version 1.0. (See accompanying
191         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>)
192       </p>
193 </div></td>
194 </tr></table>
195 <hr>
196 <div class="spirit-nav">
197 <a accesskey="p" href="generic.html"><img src="../../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.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="parameters.html"><img src="../../../../../../../../../doc/src/images/next.png" alt="Next"></a>
198 </div>
199 </body>
200 </html>