boost-react-native-bundle
Version:
Boost library as in https://sourceforge.net/projects/boost/files/boost/1.57.0/
550 lines (536 loc) • 774 kB
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>User's Guide</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../accumulators.html" title="Chapter 1. Boost.Accumulators">
<link rel="prev" href="../accumulators.html" title="Chapter 1. Boost.Accumulators">
<link rel="next" href="acknowledgements.html" title="Acknowledgements">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../accumulators.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../accumulators.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="acknowledgements.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="accumulators.user_s_guide"></a><a class="link" href="user_s_guide.html" title="User's Guide">User's Guide</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework">The
Accumulators Framework</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_statistical_accumulators_library">The
Statistical Accumulators Library</a></span></dt>
</dl></div>
<p>
This section describes how to use the Boost.Accumulators framework to create
new accumulators and how to use the existing statistical accumulators to perform
incremental statistical computation. For detailed information regarding specific
components in Boost.Accumulators, check the <a class="link" href="reference.html#accumulators_framework_reference" title="Accumulators Framework Reference">Reference</a>
section.
</p>
<h3>
<a name="accumulators.user_s_guide.h0"></a>
<span class="phrase"><a name="accumulators.user_s_guide.hello__world_"></a></span><a class="link" href="user_s_guide.html#accumulators.user_s_guide.hello__world_">Hello,
World!</a>
</h3>
<p>
Below is a complete example of how to use the Accumulators Framework and the
Statistical Accumulators to perform an incremental statistical calculation.
It calculates the mean and 2nd moment of a sequence of doubles.
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">accumulators</span><span class="special">/</span><span class="identifier">accumulators</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">accumulators</span><span class="special">/</span><span class="identifier">statistics</span><span class="special">/</span><span class="identifier">stats</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">accumulators</span><span class="special">/</span><span class="identifier">statistics</span><span class="special">/</span><span class="identifier">mean</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">accumulators</span><span class="special">/</span><span class="identifier">statistics</span><span class="special">/</span><span class="identifier">moment</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">accumulators</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<span class="comment">// Define an accumulator set for calculating the mean and the</span>
<span class="comment">// 2nd moment ...</span>
<span class="identifier">accumulator_set</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">stats</span><span class="special"><</span><span class="identifier">tag</span><span class="special">::</span><span class="identifier">mean</span><span class="special">,</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">moment</span><span class="special"><</span><span class="number">2</span><span class="special">></span> <span class="special">></span> <span class="special">></span> <span class="identifier">acc</span><span class="special">;</span>
<span class="comment">// push in some data ...</span>
<span class="identifier">acc</span><span class="special">(</span><span class="number">1.2</span><span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span><span class="number">2.3</span><span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span><span class="number">3.4</span><span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span><span class="number">4.5</span><span class="special">);</span>
<span class="comment">// Display the results ...</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Mean: "</span> <span class="special"><<</span> <span class="identifier">mean</span><span class="special">(</span><span class="identifier">acc</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Moment: "</span> <span class="special"><<</span> <span class="identifier">accumulators</span><span class="special">::</span><span class="identifier">moment</span><span class="special"><</span><span class="number">2</span><span class="special">>(</span><span class="identifier">acc</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
This program displays the following:
</p>
<pre class="programlisting">Mean: 2.85
Moment: 9.635
</pre>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="accumulators.user_s_guide.the_accumulators_framework"></a><a class="link" href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework" title="The Accumulators Framework">The
Accumulators Framework</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.using___accumulator_set___">Using
<code class="literal">accumulator_set<></code></a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.extracting_results">Extracting
Results</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.passing_optional_parameters">Passing
Optional Parameters</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.weighted_samples">Weighted
Samples</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.numeric_operators_sub_library">Numeric
Operators Sub-Library</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.extending_the_accumulators_framework">Extending
the Accumulators Framework</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.concepts">Concepts</a></span></dt>
</dl></div>
<p>
The Accumulators Framework is framework for performing incremental calculations.
Usage of the framework follows the following pattern:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Users build a computational object, called an <span class="emphasis"><em><code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code></em></span>,
by selecting the computations in which they are interested, or authoring
their own computational primitives which fit within the framework.
</li>
<li class="listitem">
Users push data into the <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
object one sample at a time.
</li>
<li class="listitem">
The <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
computes the requested quantities in the most efficient method possible,
resolving dependencies between requested calculations, possibly caching
intermediate results.
</li>
</ul></div>
<p>
The Accumulators Framework defines the utilities needed for defining primitive
computational elements, called <span class="emphasis"><em>accumulators</em></span>. It also
provides the <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
type, described above.
</p>
<h3>
<a name="accumulators.user_s_guide.the_accumulators_framework.h0"></a>
<span class="phrase"><a name="accumulators.user_s_guide.the_accumulators_framework.terminology"></a></span><a class="link" href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.terminology">Terminology</a>
</h3>
<p>
The following terms are used in the rest of the documentation.
</p>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Sample</span></dt>
<dd><p>
<a name="sample_type"></a>A datum that is pushed into an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>. The type of the
sample is the <span class="emphasis"><em>sample type</em></span>.
</p></dd>
<dt><span class="term">Weight</span></dt>
<dd><p>
<a name="weight_type"></a>An optional scalar value passed along with
the sample specifying the weight of the sample. Conceptually, each
sample is multiplied with its weight. The type of the weight is the
<span class="emphasis"><em>weight type</em></span>.
</p></dd>
<dt><span class="term">Feature</span></dt>
<dd><p>
An abstract primitive computational entity. When defining an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>, users specify
the features in which they are interested, and the <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
figures out which <span class="emphasis"><em>accumulators</em></span> would best provide
those features. Features may depend on other features. If they do,
the accumulator set figures out which accumulators to add to satisfy
the dependencies.
</p></dd>
<dt><span class="term">Accumulator</span></dt>
<dd><p>
A concrete primitive computational entity. An accumulator is a concrete
implementation of a feature. It satisfies exactly one abstract feature.
Several different accumulators may provide the same feature, but may
represent different implementation strategies.
</p></dd>
<dt><span class="term">Accumulator Set</span></dt>
<dd><p>
A collection of accumulators. An accumulator set is specified with
a sample type and a list of features. The accumulator set uses this
information to generate an ordered set of accumulators depending on
the feature dependency graph. An accumulator set accepts samples one
datum at a time, propagating them to each accumulator in order. At
any point, results can be extracted from the accumulator set.
</p></dd>
<dt><span class="term">Extractor</span></dt>
<dd><p>
A function or function object that can be used to extract a result
from an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>.
</p></dd>
</dl>
</div>
<h3>
<a name="accumulators.user_s_guide.the_accumulators_framework.h1"></a>
<span class="phrase"><a name="accumulators.user_s_guide.the_accumulators_framework.overview"></a></span><a class="link" href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.overview">Overview</a>
</h3>
<p>
Here is a list of the important types and functions in the Accumulator Framework
and a brief description of each.
</p>
<div class="table">
<a name="accumulators.user_s_guide.the_accumulators_framework.t0"></a><p class="title"><b>Table 1.1. Accumulators Toolbox</b></p>
<div class="table-contents"><table class="table" summary="Accumulators Toolbox">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Tool
</p>
</th>
<th>
<p>
Description
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
</p>
</td>
<td>
<p>
This is the most important type in the Accumulators Framework.
It is a collection of accumulators. A datum pushed into an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code> is forwarded
to each accumulator, in an order determined by the dependency relationships
between the accumulators. Computational results can be extracted
from an accumulator at any time.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><a class="link" href="../boost/accumulators/depends_on.html" title="Struct template depends_on">depends_on<></a></code>
</p>
</td>
<td>
<p>
Used to specify which other features a feature depends on.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><a class="link" href="../boost/accumulators/feature_of.html" title="Struct template feature_of">feature_of<></a></code>
</p>
</td>
<td>
<p>
Trait used to tell the Accumulators Framework that, for the purpose
of feature-based dependency resolution, one feature should be treated
the same as another.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><a class="link" href="../boost/accumulators/as_feature.html" title="Struct template as_feature">as_feature<></a></code>
</p>
</td>
<td>
<p>
Used to create an alias for a feature. For example, if there are
two features, fast_X and accurate_X, they can be mapped to X(fast)
and X(accurate) with <code class="computeroutput"><a class="link" href="../boost/accumulators/as_feature.html" title="Struct template as_feature">as_feature<></a></code>.
This is just syntactic sugar.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><a class="link" href="../boost/accumulators/features.html" title="Struct template features">features<></a></code>
</p>
</td>
<td>
<p>
An <a href="../../../libs/mpl/index.html" target="_top">MPL</a> sequence.
We can use <code class="computeroutput"><a class="link" href="../boost/accumulators/features.html" title="Struct template features">features<></a></code>
as the second template parameter when declaring an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput">external<></code>
</p>
</td>
<td>
<p>
Used when declaring an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>.
If the weight type is specified with <code class="computeroutput">external<></code>,
then the weight accumulators are assumed to reside in a separate
accumulator set which will be passed in with a named parameter.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput">extractor<></code>
</p>
</td>
<td>
<p>
A class template useful for creating an extractor function object.
It is parameterized on a feature, and it has member functions for
extracting from an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
the result corresponding to that feature.
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="accumulators.user_s_guide.the_accumulators_framework.using___accumulator_set___"></a><a class="link" href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.using___accumulator_set___" title="Using accumulator_set<>">Using
<code class="literal">accumulator_set<></code></a>
</h4></div></div></div>
<p>
Our tour of the <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
class template begins with the forward declaration:
</p>
<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span> <span class="keyword">typename</span> <span class="identifier">Sample</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Features</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Weight</span> <span class="special">=</span> <span class="keyword">void</span> <span class="special">></span>
<span class="keyword">struct</span> <span class="identifier">accumulator_set</span><span class="special">;</span>
</pre>
<p>
The template parameters have the following meaning:
</p>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="identifier">Sample</span></code></span></dt>
<dd><p>
The type of the data that will be accumulated.
</p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">Features</span></code></span></dt>
<dd><p>
An <a href="../../../libs/mpl/index.html" target="_top">MPL</a> sequence of
features to be calculated.
</p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">Weight</span></code></span></dt>
<dd><p>
The type of the (optional) weight paramter.
</p></dd>
</dl>
</div>
<p>
For example, the following line declares an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
that will accept a sequence of doubles one at a time and calculate the
min and mean:
</p>
<pre class="programlisting"><span class="identifier">accumulator_set</span><span class="special"><</span> <span class="keyword">double</span><span class="special">,</span> <span class="identifier">features</span><span class="special"><</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">min</span><span class="special">,</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">mean</span> <span class="special">></span> <span class="special">></span> <span class="identifier">acc</span><span class="special">;</span>
</pre>
<p>
Notice that we use the <code class="computeroutput"><a class="link" href="../boost/accumulators/features.html" title="Struct template features">features<></a></code>
template to specify a list of features to be calculated. <code class="computeroutput"><a class="link" href="../boost/accumulators/features.html" title="Struct template features">features<></a></code>
is an MPL sequence of features.
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
<code class="computeroutput"><a class="link" href="../boost/accumulators/features.html" title="Struct template features">features<></a></code> is a synonym of
<code class="computeroutput"><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><></span></code>.
In fact, we could use <code class="computeroutput"><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><></span></code> or any MPL sequence if we prefer,
and the meaning would be the same.
</p></td></tr>
</table></div>
<p>
Once we have defined an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>,
we can then push data into it, and it will calculate the quantities you
requested, as shown below.
</p>
<pre class="programlisting"><span class="comment">// push some data into the accumulator_set ...</span>
<span class="identifier">acc</span><span class="special">(</span><span class="number">1.2</span><span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span><span class="number">2.3</span><span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span><span class="number">3.4</span><span class="special">);</span>
</pre>
<p>
Since <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
defines its accumulate function to be the function call operator, we might
be tempted to use an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
as a UnaryFunction to a standard algorithm such as <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span></code>.
That's fine as long as we keep in mind that the standard algorithms take
UnaryFunction objects by value, which involves making a copy of the <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code> object. Consider the
following:
</p>
<pre class="programlisting"><span class="comment">// The data for which we wish to calculate statistical properties:</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="keyword">double</span> <span class="special">></span> <span class="identifier">data</span><span class="special">(</span> <span class="comment">/* stuff */</span> <span class="special">);</span>
<span class="comment">// The accumulator set which will calculate the properties for us: </span>
<span class="identifier">accumulator_set</span><span class="special"><</span> <span class="keyword">double</span><span class="special">,</span> <span class="identifier">features</span><span class="special"><</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">min</span><span class="special">,</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">mean</span> <span class="special">></span> <span class="special">></span> <span class="identifier">acc</span><span class="special">;</span>
<span class="comment">// Use std::for_each to accumulate the statistical properties:</span>
<span class="identifier">acc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">acc</span> <span class="special">);</span>
</pre>
<p>
Notice how we must assign the return value of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span></code>
back to the <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>.
This works, but some accumulators are not cheap to copy. For example, the
<code class="computeroutput"><a class="link" href="../boost/accumulators/tag/tail.html" title="Struct template tail">tail</a></code>
and <code class="computeroutput"><a class="link" href="../boost/accumulators/tag/tail_variate.html" title="Struct template tail_variate">tail_variate<></a></code>
accumulators must store a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><></span></code>, so copying these accumulators
involves a dynamic allocation. We might be better off in this case passing
the accumulator by reference, with the help of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span><span class="special">()</span></code>. See below:
</p>
<pre class="programlisting"><span class="comment">// The data for which we wish to calculate statistical properties:</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="keyword">double</span> <span class="special">></span> <span class="identifier">data</span><span class="special">(</span> <span class="comment">/* stuff */</span> <span class="special">);</span>
<span class="comment">// The accumulator set which will calculate the properties for us:</span>
<span class="identifier">accumulator_set</span><span class="special"><</span> <span class="keyword">double</span><span class="special">,</span> <span class="identifier">features</span><span class="special"><</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">tail</span><span class="special"><</span><span class="identifier">left</span><span class="special">></span> <span class="special">></span> <span class="special">></span> <span class="identifier">acc</span><span class="special">(</span>
<span class="identifier">tag</span><span class="special">::</span><span class="identifier">tail</span><span class="special"><</span><span class="identifier">left</span><span class="special">>::</span><span class="identifier">cache_size</span> <span class="special">=</span> <span class="number">4</span> <span class="special">);</span>
<span class="comment">// Use std::for_each to accumulate the statistical properties:</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">data</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">bind</span><span class="special"><</span><span class="keyword">void</span><span class="special">>(</span> <span class="identifier">ref</span><span class="special">(</span><span class="identifier">acc</span><span class="special">),</span> <span class="identifier">_1</span> <span class="special">)</span> <span class="special">);</span>
</pre>
<p>
Notice now that we don't care about the return value of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">()</span></code> anymore because <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">()</span></code> is modifying <code class="computeroutput"><span class="identifier">acc</span></code>
directly.
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
To use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">()</span></code>
and <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span><span class="special">()</span></code>,
you must <code class="computeroutput"><span class="preprocessor">#include</span></code>
<code class="literal"><boost/bind.hpp></code> and <code class="literal"><boost/ref.hpp></code>
</p></td></tr>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="accumulators.user_s_guide.the_accumulators_framework.extracting_results"></a><a class="link" href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.extracting_results" title="Extracting Results">Extracting
Results</a>
</h4></div></div></div>
<p>
Once we have declared an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
and pushed data into it, we need to be able to extract results from it.
For each feature we can add to an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>,
there is a corresponding extractor for fetching its result. Usually, the
extractor has the same name as the feature, but in a different namespace.
For example, if we accumulate the <code class="computeroutput"><span class="identifier">tag</span><span class="special">::</span><span class="identifier">min</span></code>
and <code class="computeroutput"><span class="identifier">tag</span><span class="special">::</span><span class="identifier">max</span></code> features, we can extract the results
with the <code class="computeroutput"><span class="identifier">min</span></code> and <code class="computeroutput"><span class="identifier">max</span></code> extractors, as follows:
</p>
<pre class="programlisting"><span class="comment">// Calculate the minimum and maximum for a sequence of integers.</span>
<span class="identifier">accumulator_set</span><span class="special"><</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">features</span><span class="special"><</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">min</span><span class="special">,</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">max</span> <span class="special">></span> <span class="special">></span> <span class="identifier">acc</span><span class="special">;</span>
<span class="identifier">acc</span><span class="special">(</span> <span class="number">2</span> <span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span> <span class="special">-</span><span class="number">1</span> <span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span> <span class="number">1</span> <span class="special">);</span>
<span class="comment">// This displays "(-1, 2)"</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="char">'('</span> <span class="special"><<</span> <span class="identifier">min</span><span class="special">(</span> <span class="identifier">acc</span> <span class="special">)</span> <span class="special"><<</span> <span class="string">", "</span> <span class="special"><<</span> <span class="identifier">max</span><span class="special">(</span> <span class="identifier">acc</span> <span class="special">)</span> <span class="special"><<</span> <span class="string">")\n"</span><span class="special">;</span>
</pre>
<p>
The extractors are all declared in the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">accumulators</span><span class="special">::</span><span class="identifier">extract</span></code>
namespace, but they are brought into the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">accumulators</span></code>
namespace with a <code class="computeroutput"><span class="keyword">using</span></code> declaration.
</p>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../doc/src/images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top"><p>
On the Windows platform, <code class="computeroutput"><span class="identifier">min</span></code>
and <code class="computeroutput"><span class="identifier">max</span></code> are preprocessor
macros defined in <code class="literal">WinDef.h</code>. To use the <code class="computeroutput"><span class="identifier">min</span></code> and <code class="computeroutput"><span class="identifier">max</span></code>
extractors, you should either compile with <code class="computeroutput"><span class="identifier">NOMINMAX</span></code>
defined, or you should invoke the extractors like: <code class="computeroutput"><span class="special">(</span><span class="identifier">min</span><span class="special">)(</span> <span class="identifier">acc</span> <span class="special">)</span></code>
and <code class="computeroutput"><span class="special">(</span><span class="identifier">max</span><span class="special">)(</span> <span class="identifier">acc</span> <span class="special">)</span></code>. The parentheses keep the macro from
being invoked.
</p></td></tr>
</table></div>
<p>
Another way to extract a result from an <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
is with the <code class="computeroutput"><span class="identifier">extract_result</span><span class="special">()</span></code> function. This can be more convenient
if there isn't an extractor object handy for a certain feature. The line
above which displays results could equally be written as:
</p>
<pre class="programlisting"><span class="comment">// This displays "(-1, 2)"</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="char">'('</span> <span class="special"><<</span> <span class="identifier">extract_result</span><span class="special"><</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">min</span> <span class="special">>(</span> <span class="identifier">acc</span> <span class="special">)</span>
<span class="special"><<</span> <span class="string">", "</span> <span class="special"><<</span> <span class="identifier">extract_result</span><span class="special"><</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">max</span> <span class="special">>(</span> <span class="identifier">acc</span> <span class="special">)</span> <span class="special"><<</span> <span class="string">")\n"</span><span class="special">;</span>
</pre>
<p>
Finally, we can define our own extractor using the <code class="computeroutput">extractor<></code>
class template. For instance, another way to avoid the <code class="computeroutput"><span class="identifier">min</span></code>
/ <code class="computeroutput"><span class="identifier">max</span></code> macro business would
be to define extractors with names that don't conflict with the macros,
like this:
</p>
<pre class="programlisting"><span class="identifier">extractor</span><span class="special"><</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">min</span> <span class="special">></span> <span class="identifier">min_</span><span class="special">;</span>
<span class="identifier">extractor</span><span class="special"><</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">min</span> <span class="special">></span> <span class="identifier">max_</span><span class="special">;</span>
<span class="comment">// This displays "(-1, 2)"</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="char">'('</span> <span class="special"><<</span> <span class="identifier">min_</span><span class="special">(</span> <span class="identifier">acc</span> <span class="special">)</span> <span class="special"><<</span> <span class="string">", "</span> <span class="special"><<</span> <span class="identifier">max_</span><span class="special">(</span> <span class="identifier">acc</span> <span class="special">)</span> <span class="special"><<</span> <span class="string">")\n"</span><span class="special">;</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="accumulators.user_s_guide.the_accumulators_framework.passing_optional_parameters"></a><a class="link" href="user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.passing_optional_parameters" title="Passing Optional Parameters">Passing
Optional Parameters</a>
</h4></div></div></div>
<p>
Some accumulators need initialization parameters. In addition, perhaps
some auxiliary information needs to be passed into the <code class="computeroutput"><a class="link" href="../boost/accumulators/accumulator_set.html" title="Struct template accumulator_set">accumulator_set<></a></code>
along with each sample. Boost.Accumulators handles these cases with named
parameters from the <a href="../../../libs/parameter/index.html" target="_top">Boost.Parameter</a>
library.
</p>
<p>
For example, consider the <code class="computeroutput"><a class="link" href="../boost/accumulators/tag/tail.html" title="Struct template tail">tail</a></code> and <code class="computeroutput"><a class="link" href="../boost/accumulators/tag/tail_variate.html" title="Struct template tail_variate">tail_variate<></a></code>
features. <code class="computeroutput"><a class="link" href="../boost/accumulators/tag/tail.html" title="Struct template tail">tail</a></code> keeps an ordered list
of the largest <code class="literal"><span class="emphasis"><em>N</em></span></code> samples, where
<code class="literal"><span class="emphasis"><em>N</em></span></code> can be specified at construction
time. Also, the <code class="computeroutput"><a class="link" href="../boost/accumulators/tag/tail_variate.html" title="Struct template tail_variate">tail_variate<></a></code>
feature, which depends on <code class="computeroutput"><a class="link" href="../boost/accumulators/tag/tail.html" title="Struct template tail">tail</a></code>, keeps track of some
data that is covariate with the <code class="literal"><span class="emphasis"><em>N</em></span></code>
samples tracked by <code class="computeroutput"><a class="link" href="../boost/accumulators/tag/tail.html" title="Struct template tail">tail</a></code>. The code below shows
how this all works, and is described in more detail below.
</p>
<pre class="programlisting"><span class="comment">// Define a feature for tracking covariate data</span>
<span class="keyword">typedef</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">tail_variate</span><span class="special"><</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">tag</span><span class="special">::</span><span class="identifier">covariate1</span><span class="special">,</span> <span class="identifier">left</span> <span class="special">></span> <span class="identifier">my_tail_variate_tag</span><span class="special">;</span>
<span class="comment">// This will calculate the left tail and my_tail_variate_tag for N == 2</span>
<span class="comment">// using the tag::tail<left>::cache_size named parameter</span>
<span class="identifier">accumulator_set</span><span class="special"><</span> <span class="keyword">double</span><span class="special">,</span> <span class="identifier">features</span><span class="special"><</span> <span class="identifier">my_tail_variate_tag</span> <span class="special">></span> <span class="special">></span> <span class="identifier">acc</span><span class="special">(</span>
<span class="identifier">tag</span><span class="special">::</span><span class="identifier">tail</span><span class="special"><</span><span class="identifier">left</span><span class="special">>::</span><span class="identifier">cache_size</span> <span class="special">=</span> <span class="number">2</span> <span class="special">);</span>
<span class="comment">// push in some samples and some covariates by using </span>
<span class="comment">// the covariate1 named parameter</span>
<span class="identifier">acc</span><span class="special">(</span> <span class="number">1.2</span><span class="special">,</span> <span class="identifier">covariate1</span> <span class="special">=</span> <span class="number">12</span> <span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span> <span class="number">2.3</span><span class="special">,</span> <span class="identifier">covariate1</span> <span class="special">=</span> <span class="special">-</span><span class="number">23</span> <span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span> <span class="number">3.4</span><span class="special">,</span> <span class="identifier">covariate1</span> <span class="special">=</span> <span class="number">34</span> <span class="special">);</span>
<span class="identifier">acc</span><span class="special">(</span> <span class="number">4.5</span><span class="special">,</span> <span class="identifier">covariate1</span> <span class="special">=</span> <span class="special">-</span><span class="number">45</span> <span class="special">);</span>
<span class="comment">// Define an extractor for the my_tail_variate_tag feature</span>
<span class="identifier">extractor</span><span class="special"><</span> <span class="identifier">my_tail_variate_tag</span> <span class="special">></span> <span class="identifier">my_tail_variate</span><span class="special">;</span>
<span class="comment">// Write the tail statistic to std::cout. This will print "4.5, 3.4, "</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream_iterator</span><span class="special"><</span> <span class="keyword">double</span> <span class="special">></span> <span class="identifier">dout</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">,</span> <span class="string">", "</span> <span class="special">);</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span> <span class="identifier">tail</span><span