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">
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>
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>
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>
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.
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>
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:
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"><<</span> <span class="string">"CDF at t = 1 is "</span> <span class="special"><<</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"><<</span> <span class="identifier">endl</span><span class="special">;</span>
47 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Complement of CDF at t = 1 is "</span> <span class="special"><<</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"><<</span> <span class="identifier">endl</span><span class="special">;</span>
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>,
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>
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"><</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"><<</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"><<</span> <span class="identifier">i</span> <span class="special"><<</span> <span class="string">" chance is "</span> <span class="special"><<</span> <span class="identifier">t</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span>
64 <span class="special">}</span>
66 <div class="tip"><table border="0" summary="Tip">
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>
71 <tr><td align="left" valign="top">
73 <span class="bold"><strong>Critical values are just quantiles</strong></span>
76 Some texts talk about quantiles, or percentiles or fractiles, others
77 about critical values, the basic rule is:
80 <span class="emphasis"><em>Lower critical values</em></span> are the same as the quantile.
83 <span class="emphasis"><em>Upper critical values</em></span> are the same as the quantile
84 from the complement of the probability.
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
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>
96 and the <span class="emphasis"><em>upper critical value</em></span> is given by:
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>
102 which return 4.82 and 14.63 respectively.
106 <a name="why_complements"></a><div class="tip"><table border="0" summary="Tip">
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>
111 <tr><td align="left" valign="top">
113 <span class="bold"><strong>Why bother with complements anyway?</strong></span>
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!
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:
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>
138 returns the expected t-statistic <code class="computeroutput"><span class="number">6.00336</span></code>,
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>
145 raises an overflow error, since it is the same as:
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>
151 Which has no finite result.
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
161 So always avoid, for example, using a probability near to unity like
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>
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>
176 since 1 - 0.99999 is not exactly equal to 0.00001 when using floating-point
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.
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 © 2006-2010 John Maddock, Paul A. Bristow, Hubert Holin, Xiaogang Zhang, Bruno
189 Lalande, Johan Rå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>)
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>