3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Discrete Quantile Policies
</title>
5 <link rel=
"stylesheet" href=
"../../math.css" type=
"text/css">
6 <meta name=
"generator" content=
"DocBook XSL Stylesheets V1.77.1">
7 <link rel=
"home" href=
"../../index.html" title=
"Math Toolkit 2.5.1">
8 <link rel=
"up" href=
"../pol_ref.html" title=
"Policy Reference">
9 <link rel=
"prev" href=
"assert_undefined.html" title=
"Mathematically Undefined Function Policies">
10 <link rel=
"next" href=
"precision_pol.html" title=
"Precision Policies">
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=
"assert_undefined.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../pol_ref.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=
"precision_pol.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h3 class=
"title">
27 <a name=
"math_toolkit.pol_ref.discrete_quant_ref"></a><a class=
"link" href=
"discrete_quant_ref.html" title=
"Discrete Quantile Policies">Discrete Quantile
29 </h3></div></div></div>
31 If a statistical distribution is
<span class=
"emphasis"><em>discrete
</em></span> then the random
32 variable can only have integer values - this leaves us with a problem when
33 calculating quantiles - we can either ignore the discreteness of the distribution
34 and return a real value, or we can round to an integer. As it happens, computing
35 integer values can be substantially faster than calculating a real value,
36 so there are definite advantages to returning an integer, but we do then
37 need to decide how best to round the result. The
<code class=
"computeroutput"><span class=
"identifier">discrete_quantile
</span></code>
38 policy defines how discrete quantiles work, and how integer results are rounded:
40 <pre class=
"programlisting"><span class=
"keyword">enum
</span> <span class=
"identifier">discrete_quantile_policy_type
</span>
41 <span class=
"special">{
</span>
42 <span class=
"identifier">real
</span><span class=
"special">,
</span>
43 <span class=
"identifier">integer_round_outwards
</span><span class=
"special">,
</span> <span class=
"comment">// default
</span>
44 <span class=
"identifier">integer_round_inwards
</span><span class=
"special">,
</span>
45 <span class=
"identifier">integer_round_down
</span><span class=
"special">,
</span>
46 <span class=
"identifier">integer_round_up
</span><span class=
"special">,
</span>
47 <span class=
"identifier">integer_round_nearest
</span>
48 <span class=
"special">};
</span>
50 <span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"identifier">discrete_quantile_policy_type
</span><span class=
"special">></span>
51 <span class=
"keyword">struct
</span> <span class=
"identifier">discrete_quantile
</span><span class=
"special">;
</span>
54 The values that
<code class=
"computeroutput"><span class=
"identifier">discrete_quantile
</span></code>
55 can take have the following meanings:
58 <a name=
"math_toolkit.pol_ref.discrete_quant_ref.h0"></a>
59 <span class=
"phrase"><a name=
"math_toolkit.pol_ref.discrete_quant_ref.real"></a></span><a class=
"link" href=
"discrete_quant_ref.html#math_toolkit.pol_ref.discrete_quant_ref.real">real
</a>
62 Ignores the discreteness of the distribution, and returns a real-valued result.
65 <pre class=
"programlisting"><span class=
"preprocessor">#include
</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">distributions
</span><span class=
"special">/
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span>
66 <span class=
"keyword">using
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">negative_binomial_distribution
</span><span class=
"special">;
</span>
68 <span class=
"keyword">using
</span> <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">policies
</span><span class=
"special">;
</span>
70 <span class=
"keyword">typedef
</span> <span class=
"identifier">negative_binomial_distribution
</span><span class=
"special"><</span>
71 <span class=
"keyword">double
</span><span class=
"special">,
</span>
72 <span class=
"identifier">policy
</span><span class=
"special"><</span><span class=
"identifier">discrete_quantile
</span><span class=
"special"><</span><span class=
"identifier">real
</span><span class=
"special">></span> <span class=
"special">></span>
73 <span class=
"special">></span> <span class=
"identifier">dist_type
</span><span class=
"special">;
</span>
75 <span class=
"comment">// Lower
5% quantile:
</span>
76 <span class=
"keyword">double
</span> <span class=
"identifier">x
</span> <span class=
"special">=
</span> <span class=
"identifier">quantile
</span><span class=
"special">(
</span><span class=
"identifier">dist_type
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">,
</span> <span class=
"number">0.3</span><span class=
"special">),
</span> <span class=
"number">0.05</span><span class=
"special">);
</span>
77 <span class=
"comment">// Upper
95% quantile:
</span>
78 <span class=
"keyword">double
</span> <span class=
"identifier">y
</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_type
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">,
</span> <span class=
"number">0.3</span><span class=
"special">),
</span> <span class=
"number">0.05</span><span class=
"special">));
</span>
81 Results in
<code class=
"computeroutput"><span class=
"identifier">x
</span> <span class=
"special">=
</span>
82 <span class=
"number">27.3898</span></code> and
<code class=
"computeroutput"><span class=
"identifier">y
</span>
83 <span class=
"special">=
</span> <span class=
"number">68.1584</span></code>.
86 <a name=
"math_toolkit.pol_ref.discrete_quant_ref.h1"></a>
87 <span class=
"phrase"><a name=
"math_toolkit.pol_ref.discrete_quant_ref.integer_round_outwards"></a></span><a class=
"link" href=
"discrete_quant_ref.html#math_toolkit.pol_ref.discrete_quant_ref.integer_round_outwards">integer_round_outwards
</a>
90 This is the default policy: an integer value is returned so that:
92 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
94 Lower quantiles (where the probability is less than
0.5) are rounded
98 Upper quantiles (where the probability is greater than
0.5) are rounded
103 This is normally the safest rounding policy, since it ensures that both one
104 and two sided intervals are guaranteed to have
<span class=
"emphasis"><em>at least
</em></span>
105 the requested coverage. For example:
107 <pre class=
"programlisting"><span class=
"preprocessor">#include
</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">distributions
</span><span class=
"special">/
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span>
108 <span class=
"keyword">using
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">;
</span>
110 <span class=
"comment">// Use the default rounding policy integer_round_outwards.
</span>
111 <span class=
"comment">// Lower quantile rounded down:
</span>
112 <span class=
"keyword">double
</span> <span class=
"identifier">x
</span> <span class=
"special">=
</span> <span class=
"identifier">quantile
</span><span class=
"special">(
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">,
</span> <span class=
"number">0.3</span><span class=
"special">),
</span> <span class=
"number">0.05</span><span class=
"special">);
</span> <span class=
"comment">// rounded up
27 from
27.3898</span>
113 <span class=
"comment">// Upper quantile rounded up:
</span>
114 <span class=
"keyword">double
</span> <span class=
"identifier">y
</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">negative_binomial
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">,
</span> <span class=
"number">0.3</span><span class=
"special">),
</span> <span class=
"number">0.05</span><span class=
"special">));
</span> <span class=
"comment">// rounded down to
69 from
68.1584</span>
117 Results in
<code class=
"computeroutput"><span class=
"identifier">x
</span> <span class=
"special">=
</span>
118 <span class=
"number">27</span></code> (rounded down from
27.3898) and
119 <code class=
"computeroutput"><span class=
"identifier">y
</span> <span class=
"special">=
</span>
120 <span class=
"number">69</span></code> (rounded up from
68.1584).
123 The variables x and y are now defined so that:
125 <pre class=
"programlisting"><span class=
"identifier">cdf
</span><span class=
"special">(
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">),
</span> <span class=
"identifier">x
</span><span class=
"special">)
</span> <span class=
"special"><=
</span> <span class=
"number">0.05</span>
126 <span class=
"identifier">cdf
</span><span class=
"special">(
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">),
</span> <span class=
"identifier">y
</span><span class=
"special">)
</span> <span class=
"special">>=
</span> <span class=
"number">0.95</span>
129 In other words we guarantee
<span class=
"emphasis"><em>at least
90% coverage in the central
130 region overall
</em></span>, and also
<span class=
"emphasis"><em>no more than
5% coverage in
131 each tail
</em></span>.
134 <a name=
"math_toolkit.pol_ref.discrete_quant_ref.h2"></a>
135 <span class=
"phrase"><a name=
"math_toolkit.pol_ref.discrete_quant_ref.integer_round_inwards"></a></span><a class=
"link" href=
"discrete_quant_ref.html#math_toolkit.pol_ref.discrete_quant_ref.integer_round_inwards">integer_round_inwards
</a>
138 This is the opposite of
<span class=
"emphasis"><em>integer_round_outwards
</em></span>: an integer
139 value is returned so that:
141 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
142 <li class=
"listitem">
143 Lower quantiles (where the probability is less than
0.5) are rounded
144 <span class=
"emphasis"><em>up
</em></span>.
146 <li class=
"listitem">
147 Upper quantiles (where the probability is greater than
0.5) are rounded
148 <span class=
"emphasis"><em>down
</em></span>.
154 <pre class=
"programlisting"><span class=
"preprocessor">#include
</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">distributions
</span><span class=
"special">/
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span>
155 <span class=
"keyword">using
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">negative_binomial_distribution
</span><span class=
"special">;
</span>
157 <span class=
"keyword">using
</span> <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">policies
</span><span class=
"special">;
</span>
159 <span class=
"keyword">typedef
</span> <span class=
"identifier">negative_binomial_distribution
</span><span class=
"special"><</span>
160 <span class=
"keyword">double
</span><span class=
"special">,
</span>
161 <span class=
"identifier">policy
</span><span class=
"special"><</span><span class=
"identifier">discrete_quantile
</span><span class=
"special"><</span><span class=
"identifier">integer_round_inwards
</span><span class=
"special">></span> <span class=
"special">></span>
162 <span class=
"special">></span> <span class=
"identifier">dist_type
</span><span class=
"special">;
</span>
164 <span class=
"comment">// Lower quantile rounded up:
</span>
165 <span class=
"keyword">double
</span> <span class=
"identifier">x
</span> <span class=
"special">=
</span> <span class=
"identifier">quantile
</span><span class=
"special">(
</span><span class=
"identifier">dist_type
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">,
</span> <span class=
"number">0.3</span><span class=
"special">),
</span> <span class=
"number">0.05</span><span class=
"special">);
</span> <span class=
"comment">//
28 rounded up from
27.3898</span>
166 <span class=
"comment">// Upper quantile rounded down:
</span>
167 <span class=
"keyword">double
</span> <span class=
"identifier">y
</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_type
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">,
</span> <span class=
"number">0.3</span><span class=
"special">),
</span> <span class=
"number">0.05</span><span class=
"special">));
</span> <span class=
"comment">//
68 rounded down from
68.1584</span>
170 Results in
<code class=
"computeroutput"><span class=
"identifier">x
</span> <span class=
"special">=
</span>
171 <span class=
"number">28</span></code> (rounded up from
27.3898) and
<code class=
"computeroutput"><span class=
"identifier">y
</span> <span class=
"special">=
</span> <span class=
"number">68</span></code>
172 (rounded down from
68.1584).
175 The variables x and y are now defined so that:
177 <pre class=
"programlisting"><span class=
"identifier">cdf
</span><span class=
"special">(
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">),
</span> <span class=
"identifier">x
</span><span class=
"special">)
</span> <span class=
"special">>=
</span> <span class=
"number">0.05</span>
178 <span class=
"identifier">cdf
</span><span class=
"special">(
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">),
</span> <span class=
"identifier">y
</span><span class=
"special">)
</span> <span class=
"special"><=
</span> <span class=
"number">0.95</span>
181 In other words we guarantee
<span class=
"emphasis"><em>at no more than
90% coverage in the
182 central region overall
</em></span>, and also
<span class=
"emphasis"><em>at least
5% coverage
183 in each tail
</em></span>.
186 <a name=
"math_toolkit.pol_ref.discrete_quant_ref.h3"></a>
187 <span class=
"phrase"><a name=
"math_toolkit.pol_ref.discrete_quant_ref.integer_round_down"></a></span><a class=
"link" href=
"discrete_quant_ref.html#math_toolkit.pol_ref.discrete_quant_ref.integer_round_down">integer_round_down
</a>
190 Always rounds down to an integer value, no matter whether it's an upper or
194 <a name=
"math_toolkit.pol_ref.discrete_quant_ref.h4"></a>
195 <span class=
"phrase"><a name=
"math_toolkit.pol_ref.discrete_quant_ref.integer_round_up"></a></span><a class=
"link" href=
"discrete_quant_ref.html#math_toolkit.pol_ref.discrete_quant_ref.integer_round_up">integer_round_up
</a>
198 Always rounds up to an integer value, no matter whether it's an upper or
202 <a name=
"math_toolkit.pol_ref.discrete_quant_ref.h5"></a>
203 <span class=
"phrase"><a name=
"math_toolkit.pol_ref.discrete_quant_ref.integer_round_nearest"></a></span><a class=
"link" href=
"discrete_quant_ref.html#math_toolkit.pol_ref.discrete_quant_ref.integer_round_nearest">integer_round_nearest
</a>
206 Always rounds to the nearest integer value, no matter whether it's an upper
207 or a lower quantile. This will produce the requested coverage
<span class=
"emphasis"><em>in
208 the average case
</em></span>, but for any specific example may results in
209 either significantly more or less coverage than the requested amount. For
215 <pre class=
"programlisting"><span class=
"preprocessor">#include
</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">distributions
</span><span class=
"special">/
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span>
216 <span class=
"keyword">using
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">negative_binomial_distribution
</span><span class=
"special">;
</span>
218 <span class=
"keyword">using
</span> <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">policies
</span><span class=
"special">;
</span>
220 <span class=
"keyword">typedef
</span> <span class=
"identifier">negative_binomial_distribution
</span><span class=
"special"><</span>
221 <span class=
"keyword">double
</span><span class=
"special">,
</span>
222 <span class=
"identifier">policy
</span><span class=
"special"><</span><span class=
"identifier">discrete_quantile
</span><span class=
"special"><</span><span class=
"identifier">integer_round_nearest
</span><span class=
"special">></span> <span class=
"special">></span>
223 <span class=
"special">></span> <span class=
"identifier">dist_type
</span><span class=
"special">;
</span>
225 <span class=
"comment">// Lower quantile rounded (down) to nearest:
</span>
226 <span class=
"keyword">double
</span> <span class=
"identifier">x
</span> <span class=
"special">=
</span> <span class=
"identifier">quantile
</span><span class=
"special">(
</span><span class=
"identifier">dist_type
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">,
</span> <span class=
"number">0.3</span><span class=
"special">),
</span> <span class=
"number">0.05</span><span class=
"special">);
</span> <span class=
"comment">//
27</span>
227 <span class=
"comment">// Upper quantile rounded (down) to nearest:
</span>
228 <span class=
"keyword">double
</span> <span class=
"identifier">y
</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_type
</span><span class=
"special">(
</span><span class=
"number">20</span><span class=
"special">,
</span> <span class=
"number">0.3</span><span class=
"special">),
</span> <span class=
"number">0.05</span><span class=
"special">));
</span> <span class=
"comment">//
68</span>
231 Results in
<code class=
"computeroutput"><span class=
"identifier">x
</span> <span class=
"special">=
</span>
232 <span class=
"number">27</span></code> (rounded from
27.3898) and
<code class=
"computeroutput"><span class=
"identifier">y
</span> <span class=
"special">=
</span> <span class=
"number">68</span></code>
233 (rounded from
68.1584).
236 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
237 <td align=
"left"></td>
238 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2006-
2010,
2012-
2014 Nikhar Agrawal,
239 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
240 Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R
åde, Gautam Sewani,
241 Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang
<p>
242 Distributed under the Boost Software License, Version
1.0. (See accompanying
243 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>)
248 <div class=
"spirit-nav">
249 <a accesskey=
"p" href=
"assert_undefined.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../pol_ref.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=
"precision_pol.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>