1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Understanding Quantiles of Discrete Distributions</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="../pol_tutorial.html" title="Policy Tutorial"> 9<link rel="prev" href="user_def_err_pol.html" title="Calling User Defined Error Handlers"> 10<link rel="next" href="../pol_ref.html" title="Policy Reference"> 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="user_def_err_pol.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../pol_tutorial.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="../pol_ref.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="math_toolkit.pol_tutorial.understand_dis_quant"></a><a class="link" href="understand_dis_quant.html" title="Understanding Quantiles of Discrete Distributions">Understanding 28 Quantiles of Discrete Distributions</a> 29</h3></div></div></div> 30<p> 31 Discrete distributions present us with a problem when calculating the quantile: 32 we are starting from a continuous real-valued variable - the probability 33 - but the result (the value of the random variable) should really be discrete. 34 </p> 35<p> 36 Consider for example a Binomial distribution, with a sample size of 50, and 37 a success fraction of 0.5. There are a variety of ways we can plot a discrete 38 distribution, but if we plot the PDF as a step-function then it looks something 39 like this: 40 </p> 41<p> 42 <span class="inlinemediaobject"><img src="../../../graphs/binomial_pdf.png"></span> 43 </p> 44<p> 45 Now lets suppose that the user asks for a the quantile that corresponds to 46 a probability of 0.05, if we zoom in on the CDF for that region here's what 47 we see: 48 </p> 49<p> 50 <span class="inlinemediaobject"><img src="../../../graphs/binomial_quantile_1.png"></span> 51 </p> 52<p> 53 As can be seen there is no random variable that corresponds to a probability 54 of exactly 0.05, so we're left with two choices as shown in the figure: 55 </p> 56<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 57<li class="listitem"> 58 We could round the result down to 18. 59 </li> 60<li class="listitem"> 61 We could round the result up to 19. 62 </li> 63</ul></div> 64<p> 65 In fact there's actually a third choice as well: we could "pretend" 66 that the distribution was continuous and return a real valued result: in 67 this case we would calculate a result of approximately 18.701 (this accurately 68 reflects the fact that the result is nearer to 19 than 18). 69 </p> 70<p> 71 By using policies we can offer any of the above as options, but that still 72 leaves the question: <span class="emphasis"><em>What is actually the right thing to do?</em></span> 73 </p> 74<p> 75 And in particular: <span class="emphasis"><em>What policy should we use by default?</em></span> 76 </p> 77<p> 78 In coming to an answer we should realise that: 79 </p> 80<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 81<li class="listitem"> 82 Calculating an integer result is often much faster than calculating a 83 real-valued result: in fact in our tests it was up to 20 times faster. 84 </li> 85<li class="listitem"> 86 Normally people calculate quantiles so that they can perform a test of 87 some kind: <span class="emphasis"><em>"If the random variable is less than N then 88 we can reject our null-hypothesis with 90% confidence."</em></span> 89 </li> 90</ul></div> 91<p> 92 So there is a genuine benefit to calculating an integer result as well as 93 it being "the right thing to do" from a philosophical point of 94 view. What's more if someone asks for a quantile at 0.05, then we can normally 95 assume that they are asking for <span class="emphasis"><em><span class="bold"><strong>at least</strong></span> 96 95% of the probability to the right of the value chosen, and <span class="bold"><strong>no 97 more than</strong></span> 5% of the probability to the left of the value chosen.</em></span> 98 </p> 99<p> 100 In the above binomial example we would therefore round the result down to 101 18. 102 </p> 103<p> 104 The converse applies to upper-quantiles: If the probability is greater than 105 0.5 we would want to round the quantile up, <span class="emphasis"><em>so that <span class="bold"><strong>at 106 least</strong></span> the requested probability is to the left of the value returned, 107 and <span class="bold"><strong>no more than</strong></span> 1 - the requested probability 108 is to the right of the value returned.</em></span> 109 </p> 110<p> 111 Likewise for two-sided intervals, we would round lower quantiles down, and 112 upper quantiles up. This ensures that we have <span class="emphasis"><em>at least the requested 113 probability in the central region</em></span> and <span class="emphasis"><em>no more than 1 114 minus the requested probability in the tail areas.</em></span> 115 </p> 116<p> 117 For example, taking our 50 sample binomial distribution with a success fraction 118 of 0.5, if we wanted a two sided 90% confidence interval, then we would ask 119 for the 0.05 and 0.95 quantiles with the results <span class="emphasis"><em>rounded outwards</em></span> 120 so that <span class="emphasis"><em>at least 90% of the probability</em></span> is in the central 121 area: 122 </p> 123<p> 124 <span class="inlinemediaobject"><img src="../../../graphs/binomial_pdf_3.png"></span> 125 </p> 126<p> 127 So far so good, but there is in fact a trap waiting for the unwary here: 128 </p> 129<pre class="programlisting"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.05</span><span class="special">);</span> 130</pre> 131<p> 132 returns 18 as the result, which is what we would expect from the graph above, 133 and indeed there is no x greater than 18 for which: 134 </p> 135<pre class="programlisting"><span class="identifier">cdf</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="identifier">x</span><span class="special">)</span> <span class="special"><=</span> <span class="number">0.05</span><span class="special">;</span> 136</pre> 137<p> 138 However: 139 </p> 140<pre class="programlisting"><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.95</span><span class="special">);</span> 141</pre> 142<p> 143 returns 31, and indeed while there is no x less than 31 for which: 144 </p> 145<pre class="programlisting"><span class="identifier">cdf</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">>=</span> <span class="number">0.95</span><span class="special">;</span> 146</pre> 147<p> 148 We might naively expect that for this symmetrical distribution the result 149 would be 32 (since 32 = 50 - 18), but we need to remember that the cdf of 150 the binomial is <span class="emphasis"><em>inclusive</em></span> of the random variable. So 151 while the left tail area <span class="emphasis"><em>includes</em></span> the quantile returned, 152 the right tail area always excludes an upper quantile value: since that "belongs" 153 to the central area. 154 </p> 155<p> 156 Look at the graph above to see what's going on here: the lower quantile of 157 18 belongs to the left tail, so any value <= 18 is in the left tail. The 158 upper quantile of 31 on the other hand belongs to the central area, so the 159 tail area actually starts at 32, so any value > 31 is in the right tail. 160 </p> 161<p> 162 Therefore if U and L are the upper and lower quantiles respectively, then 163 a random variable X is in the tail area - where we would reject the null 164 hypothesis if: 165 </p> 166<pre class="programlisting"><span class="identifier">X</span> <span class="special"><=</span> <span class="identifier">L</span> <span class="special">||</span> <span class="identifier">X</span> <span class="special">></span> <span class="identifier">U</span> 167</pre> 168<p> 169 And the a variable X is inside the central region if: 170 </p> 171<pre class="programlisting"><span class="identifier">L</span> <span class="special"><</span> <span class="identifier">X</span> <span class="special"><=</span> <span class="identifier">U</span> 172</pre> 173<p> 174 The moral here is to <span class="emphasis"><em>always be very careful with your comparisons 175 when dealing with a discrete distribution</em></span>, and if in doubt, <span class="emphasis"><em>base 176 your comparisons on CDF's instead</em></span>. 177 </p> 178<h5> 179<a name="math_toolkit.pol_tutorial.understand_dis_quant.h0"></a> 180 <span class="phrase"><a name="math_toolkit.pol_tutorial.understand_dis_quant.other_rounding_policies_are_avai"></a></span><a class="link" href="understand_dis_quant.html#math_toolkit.pol_tutorial.understand_dis_quant.other_rounding_policies_are_avai">Other 181 Rounding Policies are Available</a> 182 </h5> 183<p> 184 As you would expect from a section on policies, you won't be surprised to 185 know that other rounding options are available: 186 </p> 187<div class="variablelist"> 188<p class="title"><b></b></p> 189<dl class="variablelist"> 190<dt><span class="term">integer_round_outwards</span></dt> 191<dd> 192<p> 193 This is the default policy as described above: lower quantiles are 194 rounded down (probability < 0.5), and upper quantiles (probability 195 > 0.5) are rounded up. 196 </p> 197<p> 198 This gives <span class="emphasis"><em>no more than</em></span> the requested probability 199 in the tails, and <span class="emphasis"><em>at least</em></span> the requested probability 200 in the central area. 201 </p> 202</dd> 203<dt><span class="term">integer_round_inwards</span></dt> 204<dd> 205<p> 206 This is the exact opposite of the default policy: lower quantiles are 207 rounded up (probability < 0.5), and upper quantiles (probability 208 > 0.5) are rounded down. 209 </p> 210<p> 211 This gives <span class="emphasis"><em>at least</em></span> the requested probability 212 in the tails, and <span class="emphasis"><em>no more than</em></span> the requested probability 213 in the central area. 214 </p> 215</dd> 216<dt><span class="term">integer_round_down</span></dt> 217<dd><p> 218 This policy will always round the result down no matter whether it 219 is an upper or lower quantile 220 </p></dd> 221<dt><span class="term">integer_round_up</span></dt> 222<dd><p> 223 This policy will always round the result up no matter whether it is 224 an upper or lower quantile 225 </p></dd> 226<dt><span class="term">integer_round_nearest</span></dt> 227<dd><p> 228 This policy will always round the result to the nearest integer no 229 matter whether it is an upper or lower quantile 230 </p></dd> 231<dt><span class="term">real</span></dt> 232<dd><p> 233 This policy will return a real valued result for the quantile of a 234 discrete distribution: this is generally much slower than finding an 235 integer result but does allow for more sophisticated rounding policies. 236 </p></dd> 237</dl> 238</div> 239<p> 240 To understand how the rounding policies for the discrete distributions can 241 be used, we'll use the 50-sample binomial distribution with a success fraction 242 of 0.5 once again, and calculate all the possible quantiles at 0.05 and 0.95. 243 </p> 244<p> 245 Begin by including the needed headers (and some using statements for conciseness): 246 </p> 247<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 248<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 249<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">left</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">fixed</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">right</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">scientific</span><span class="special">;</span> 250<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iomanip</span><span class="special">></span> 251<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setw</span><span class="special">;</span> 252<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">;</span> 253 254<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">binomial</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 255</pre> 256<p> 257 Next we'll bring the needed declarations into scope, and define distribution 258 types for all the available rounding policies: 259 </p> 260<pre class="programlisting"><span class="comment">// Avoid </span> 261<span class="comment">// using namespace std; // and </span> 262<span class="comment">// using namespace boost::math;</span> 263<span class="comment">// to avoid potential ambiguity of names, like binomial.</span> 264<span class="comment">// using namespace boost::math::policies; is small risk, but</span> 265<span class="comment">// the necessary items are brought into scope thus:</span> 266 267<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">binomial_distribution</span><span class="special">;</span> 268<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">policies</span><span class="special">::</span><span class="identifier">policy</span><span class="special">;</span> 269<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">policies</span><span class="special">::</span><span class="identifier">discrete_quantile</span><span class="special">;</span> 270 271<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">policies</span><span class="special">::</span><span class="identifier">integer_round_outwards</span><span class="special">;</span> 272<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">policies</span><span class="special">::</span><span class="identifier">integer_round_down</span><span class="special">;</span> 273<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">policies</span><span class="special">::</span><span class="identifier">integer_round_up</span><span class="special">;</span> 274<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">policies</span><span class="special">::</span><span class="identifier">integer_round_nearest</span><span class="special">;</span> 275<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">policies</span><span class="special">::</span><span class="identifier">integer_round_inwards</span><span class="special">;</span> 276<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">policies</span><span class="special">::</span><span class="identifier">real</span><span class="special">;</span> 277 278<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">binomial_distribution</span><span class="special">;</span> <span class="comment">// Not std::binomial_distribution.</span> 279 280<span class="keyword">typedef</span> <span class="identifier">binomial_distribution</span><span class="special"><</span> 281 <span class="keyword">double</span><span class="special">,</span> 282 <span class="identifier">policy</span><span class="special"><</span><span class="identifier">discrete_quantile</span><span class="special"><</span><span class="identifier">integer_round_outwards</span><span class="special">></span> <span class="special">></span> <span class="special">></span> 283 <span class="identifier">binom_round_outwards</span><span class="special">;</span> 284 285<span class="keyword">typedef</span> <span class="identifier">binomial_distribution</span><span class="special"><</span> 286 <span class="keyword">double</span><span class="special">,</span> 287 <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> <span class="special">></span> 288 <span class="identifier">binom_round_inwards</span><span class="special">;</span> 289 290<span class="keyword">typedef</span> <span class="identifier">binomial_distribution</span><span class="special"><</span> 291 <span class="keyword">double</span><span class="special">,</span> 292 <span class="identifier">policy</span><span class="special"><</span><span class="identifier">discrete_quantile</span><span class="special"><</span><span class="identifier">integer_round_down</span><span class="special">></span> <span class="special">></span> <span class="special">></span> 293 <span class="identifier">binom_round_down</span><span class="special">;</span> 294 295<span class="keyword">typedef</span> <span class="identifier">binomial_distribution</span><span class="special"><</span> 296 <span class="keyword">double</span><span class="special">,</span> 297 <span class="identifier">policy</span><span class="special"><</span><span class="identifier">discrete_quantile</span><span class="special"><</span><span class="identifier">integer_round_up</span><span class="special">></span> <span class="special">></span> <span class="special">></span> 298 <span class="identifier">binom_round_up</span><span class="special">;</span> 299 300<span class="keyword">typedef</span> <span class="identifier">binomial_distribution</span><span class="special"><</span> 301 <span class="keyword">double</span><span class="special">,</span> 302 <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> <span class="special">></span> 303 <span class="identifier">binom_round_nearest</span><span class="special">;</span> 304 305<span class="keyword">typedef</span> <span class="identifier">binomial_distribution</span><span class="special"><</span> 306 <span class="keyword">double</span><span class="special">,</span> 307 <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> <span class="special">></span> 308 <span class="identifier">binom_real_quantile</span><span class="special">;</span> 309</pre> 310<p> 311 Now let's set to work calling those quantiles: 312 </p> 313<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 314<span class="special">{</span> 315 <span class="identifier">cout</span> <span class="special"><<</span> 316 <span class="string">"Testing rounding policies for a 50 sample binomial distribution,\n"</span> 317 <span class="string">"with a success fraction of 0.5.\n\n"</span> 318 <span class="string">"Lower quantiles are calculated at p = 0.05\n\n"</span> 319 <span class="string">"Upper quantiles at p = 0.95.\n\n"</span><span class="special">;</span> 320 321 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">25</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 322 <span class="special"><<</span> <span class="string">"Policy"</span><span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 323 <span class="special"><<</span> <span class="string">"Lower Quantile"</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 324 <span class="special"><<</span> <span class="string">"Upper Quantile"</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 325 326 <span class="comment">// Test integer_round_outwards:</span> 327 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">25</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 328 <span class="special"><<</span> <span class="string">"integer_round_outwards"</span> 329 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 330 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_outwards</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.05</span><span class="special">)</span> 331 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 332 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_outwards</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.95</span><span class="special">)</span> 333 <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 334 335 <span class="comment">// Test integer_round_inwards:</span> 336 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">25</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 337 <span class="special"><<</span> <span class="string">"integer_round_inwards"</span> 338 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 339 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_inwards</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.05</span><span class="special">)</span> 340 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 341 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_inwards</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.95</span><span class="special">)</span> 342 <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 343 344 <span class="comment">// Test integer_round_down:</span> 345 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">25</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 346 <span class="special"><<</span> <span class="string">"integer_round_down"</span> 347 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 348 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_down</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.05</span><span class="special">)</span> 349 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 350 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_down</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.95</span><span class="special">)</span> 351 <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 352 353 <span class="comment">// Test integer_round_up:</span> 354 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">25</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 355 <span class="special"><<</span> <span class="string">"integer_round_up"</span> 356 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 357 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_up</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.05</span><span class="special">)</span> 358 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 359 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_up</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.95</span><span class="special">)</span> 360 <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 361 362 <span class="comment">// Test integer_round_nearest:</span> 363 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">25</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 364 <span class="special"><<</span> <span class="string">"integer_round_nearest"</span> 365 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 366 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_nearest</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.05</span><span class="special">)</span> 367 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 368 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_round_nearest</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.95</span><span class="special">)</span> 369 <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 370 371 <span class="comment">// Test real:</span> 372 <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">25</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 373 <span class="special"><<</span> <span class="string">"real"</span> 374 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 375 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_real_quantile</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.05</span><span class="special">)</span> 376 <span class="special"><<</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">18</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">right</span> 377 <span class="special"><<</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">binom_real_quantile</span><span class="special">(</span><span class="number">50</span><span class="special">,</span> <span class="number">0.5</span><span class="special">),</span> <span class="number">0.95</span><span class="special">)</span> 378 <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 379<span class="special">}</span> <span class="comment">// int main()</span> 380</pre> 381<p> 382 Which produces the program output: 383 </p> 384<pre class="programlisting"> policy_eg_10.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\policy_eg_10.exe 385 Testing rounding policies for a 50 sample binomial distribution, 386 with a success fraction of 0.5. 387 388 Lower quantiles are calculated at p = 0.05 389 390 Upper quantiles at p = 0.95. 391 392 Policy Lower Quantile Upper Quantile 393 integer_round_outwards 18 31 394 integer_round_inwards 19 30 395 integer_round_down 18 30 396 integer_round_up 19 31 397 integer_round_nearest 19 30 398 real 18.701 30.299 399</pre> 400</div> 401<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 402<td align="left"></td> 403<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar 404 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, 405 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan 406 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, 407 Daryle Walker and Xiaogang Zhang<p> 408 Distributed under the Boost Software License, Version 1.0. (See accompanying 409 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>) 410 </p> 411</div></td> 412</tr></table> 413<hr> 414<div class="spirit-nav"> 415<a accesskey="p" href="user_def_err_pol.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../pol_tutorial.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="../pol_ref.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 416</div> 417</body> 418</html> 419