1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Setting Policies for Distributions on an Ad Hoc Basis</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="changing_policy_defaults.html" title="Changing the Policy Defaults"> 10<link rel="next" href="ad_hoc_sf_policies.html" title="Changing the Policy on an Ad Hoc Basis for the Special Functions"> 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="changing_policy_defaults.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="ad_hoc_sf_policies.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.ad_hoc_dist_policies"></a><a class="link" href="ad_hoc_dist_policies.html" title="Setting Policies for Distributions on an Ad Hoc Basis">Setting 28 Policies for Distributions on an Ad Hoc Basis</a> 29</h3></div></div></div> 30<p> 31 All of the statistical distributions in this library are class templates 32 that accept two template parameters: real type (float, double ...) and policy 33 (how to handle exceptional events), both with sensible defaults, for example: 34 </p> 35<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span> 36 37<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">RealType</span> <span class="special">=</span> <span class="keyword">double</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policy</span> <span class="special">=</span> <span class="identifier">policies</span><span class="special">::</span><span class="identifier">policy</span><span class="special"><></span> <span class="special">></span> 38<span class="keyword">class</span> <span class="identifier">fisher_f_distribution</span><span class="special">;</span> 39 40<span class="keyword">typedef</span> <span class="identifier">fisher_f_distribution</span><span class="special"><></span> <span class="identifier">fisher_f</span><span class="special">;</span> 41 42<span class="special">}}</span> 43</pre> 44<p> 45 This policy gets used by all the accessor functions that accept a distribution 46 as an argument, and forwarded to all the functions called by these. So if 47 you use the shorthand-typedef for the distribution, then you get <code class="computeroutput"><span class="keyword">double</span></code> precision arithmetic and all the default 48 policies. 49 </p> 50<p> 51 However, say for example we wanted to evaluate the quantile of the binomial 52 distribution at float precision, without internal promotion to double, and 53 with the result rounded to the <span class="emphasis"><em>nearest</em></span> integer, then 54 here's how it can be done: 55 </p> 56<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">binomial</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 57<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> 58 59<span class="comment">// Begin by defining a policy type, that gives the behaviour we want:</span> 60 61<span class="comment">//using namespace boost::math::policies; or explicitly</span> 62<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> 63 64<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">promote_float</span><span class="special">;</span> 65<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> 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">policies</span><span class="special">::</span><span class="identifier">integer_round_nearest</span><span class="special">;</span> 67 68<span class="keyword">typedef</span> <span class="identifier">policy</span><span class="special"><</span> 69 <span class="identifier">promote_float</span><span class="special"><</span><span class="keyword">false</span><span class="special">>,</span> <span class="comment">// Do not promote to double.</span> 70 <span class="identifier">discrete_quantile</span><span class="special"><</span><span class="identifier">integer_round_nearest</span><span class="special">></span> <span class="comment">// Round result to nearest integer.</span> 71<span class="special">></span> <span class="identifier">mypolicy</span><span class="special">;</span> 72<span class="comment">//</span> 73<span class="comment">// Then define a new distribution that uses it:</span> 74<span class="keyword">typedef</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="keyword">float</span><span class="special">,</span> <span class="identifier">mypolicy</span><span class="special">></span> <span class="identifier">mybinom</span><span class="special">;</span> 75 76<span class="comment">// And now use it to get the quantile:</span> 77 78<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 79<span class="special">{</span> 80 <span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"quantile(mybinom(200, 0.25), 0.05) is: "</span> <span class="special"><<</span> 81 <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">mybinom</span><span class="special">(</span><span class="number">200</span><span class="special">,</span> <span class="number">0.25</span><span class="special">),</span> <span class="number">0.05</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">endl</span><span class="special">;</span> 82<span class="special">}</span> 83</pre> 84<p> 85 Which outputs: 86 </p> 87<pre class="programlisting">quantile is: 40</pre> 88</div> 89<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 90<td align="left"></td> 91<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar 92 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, 93 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan 94 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, 95 Daryle Walker and Xiaogang Zhang<p> 96 Distributed under the Boost Software License, Version 1.0. (See accompanying 97 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>) 98 </p> 99</div></td> 100</tr></table> 101<hr> 102<div class="spirit-nav"> 103<a accesskey="p" href="changing_policy_defaults.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="ad_hoc_sf_policies.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 104</div> 105</body> 106</html> 107