1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Bivariate Statistics</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="../statistics.html" title="Chapter 6. Statistics"> 9<link rel="prev" href="univariate_statistics.html" title="Univariate Statistics"> 10<link rel="next" href="signal_statistics.html" title="Signal Statistics"> 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="univariate_statistics.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../statistics.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="signal_statistics.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 27<a name="math_toolkit.bivariate_statistics"></a><a class="link" href="bivariate_statistics.html" title="Bivariate Statistics">Bivariate Statistics</a> 28</h2></div></div></div> 29<h4> 30<a name="math_toolkit.bivariate_statistics.h0"></a> 31 <span class="phrase"><a name="math_toolkit.bivariate_statistics.synopsis"></a></span><a class="link" href="bivariate_statistics.html#math_toolkit.bivariate_statistics.synopsis">Synopsis</a> 32 </h4> 33<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">statistics</span><span class="special">/</span><span class="identifier">bivariate_statistics</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 34 35<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> <span class="keyword">namespace</span> <span class="identifier">statistics</span> <span class="special">{</span> 36 37 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">></span> 38 <span class="keyword">auto</span> <span class="identifier">covariance</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">v</span><span class="special">);</span> 39 40 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">></span> 41 <span class="keyword">auto</span> <span class="identifier">means_and_covariance</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">v</span><span class="special">);</span> 42 43 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">></span> 44 <span class="keyword">auto</span> <span class="identifier">correlation_coefficient</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">v</span><span class="special">);</span> 45 46<span class="special">}}}</span> 47</pre> 48<h4> 49<a name="math_toolkit.bivariate_statistics.h1"></a> 50 <span class="phrase"><a name="math_toolkit.bivariate_statistics.description"></a></span><a class="link" href="bivariate_statistics.html#math_toolkit.bivariate_statistics.description">Description</a> 51 </h4> 52<p> 53 This file provides functions for computing bivariate statistics. 54 </p> 55<h4> 56<a name="math_toolkit.bivariate_statistics.h2"></a> 57 <span class="phrase"><a name="math_toolkit.bivariate_statistics.covariance"></a></span><a class="link" href="bivariate_statistics.html#math_toolkit.bivariate_statistics.covariance">Covariance</a> 58 </h4> 59<p> 60 Computes the population covariance of two datasets: 61 </p> 62<pre class="programlisting"><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">u</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">};</span> 63<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">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">};</span> 64<span class="keyword">double</span> <span class="identifier">cov_uv</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">statistics</span><span class="special">::</span><span class="identifier">covariance</span><span class="special">(</span><span class="identifier">u</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span> 65</pre> 66<p> 67 The implementation follows <a href="https://doi.org/10.1109/CLUSTR.2009.5289161" target="_top">Bennet 68 et al</a>. The data is not modified. Requires a random-access container. 69 Works with real-valued inputs and does not work with complex-valued inputs. 70 </p> 71<p> 72 The algorithm used herein simultaneously generates the mean values of the input 73 data <span class="emphasis"><em>u</em></span> and <span class="emphasis"><em>v</em></span>. For certain applications, 74 it might be useful to get them in a single pass through the data. As such, 75 we provide <code class="computeroutput"><span class="identifier">means_and_covariance</span></code>: 76 </p> 77<pre class="programlisting"><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">u</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">};</span> 78<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">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">};</span> 79<span class="keyword">auto</span> <span class="special">[</span><span class="identifier">mu_u</span><span class="special">,</span> <span class="identifier">mu_v</span><span class="special">,</span> <span class="identifier">cov_uv</span><span class="special">]</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">statistics</span><span class="special">::</span><span class="identifier">means_and_covariance</span><span class="special">(</span><span class="identifier">u</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span> 80</pre> 81<h4> 82<a name="math_toolkit.bivariate_statistics.h3"></a> 83 <span class="phrase"><a name="math_toolkit.bivariate_statistics.correlation_coefficient"></a></span><a class="link" href="bivariate_statistics.html#math_toolkit.bivariate_statistics.correlation_coefficient">Correlation 84 Coefficient</a> 85 </h4> 86<p> 87 Computes the <a href="https://en.wikipedia.org/wiki/Pearson_correlation_coefficient" target="_top">Pearson 88 correlation coefficient</a> of two datasets <span class="emphasis"><em>u</em></span> and 89 <span class="emphasis"><em>v</em></span>: 90 </p> 91<pre class="programlisting"><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">u</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">};</span> 92<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">v</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">};</span> 93<span class="keyword">double</span> <span class="identifier">rho_uv</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">statistics</span><span class="special">::</span><span class="identifier">correlation_coefficient</span><span class="special">(</span><span class="identifier">u</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span> 94<span class="comment">// rho_uv = 1.</span> 95</pre> 96<p> 97 The data must be random access and cannot be complex. 98 </p> 99<p> 100 If one or both of the datasets is constant, the correlation coefficient is 101 an indeterminant form (0/0) and definitions must be introduced to assign it 102 a value. We use the following: If both datasets are constant, then the correlation 103 coefficient is 1. If one dataset is constant, and the other is not, then the 104 correlation coefficient is zero. 105 </p> 106<h4> 107<a name="math_toolkit.bivariate_statistics.h4"></a> 108 <span class="phrase"><a name="math_toolkit.bivariate_statistics.references"></a></span><a class="link" href="bivariate_statistics.html#math_toolkit.bivariate_statistics.references">References</a> 109 </h4> 110<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 111 Bennett, Janine, et al. <span class="emphasis"><em>Numerically stable, single-pass, parallel 112 statistics algorithms.</em></span> Cluster Computing and Workshops, 2009. 113 CLUSTER'09. IEEE International Conference on. IEEE, 2009. 114 </li></ul></div> 115</div> 116<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 117<td align="left"></td> 118<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar 119 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, 120 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan 121 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, 122 Daryle Walker and Xiaogang Zhang<p> 123 Distributed under the Boost Software License, Version 1.0. (See accompanying 124 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>) 125 </p> 126</div></td> 127</tr></table> 128<hr> 129<div class="spirit-nav"> 130<a accesskey="p" href="univariate_statistics.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../statistics.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="signal_statistics.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 131</div> 132</body> 133</html> 134