# Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world.

## Bivariate Statistics

#### Synopsis

```#include <boost/math/statistics/bivariate_statistics.hpp>

namespace boost{ namespace math{ namespace statistics {

template<class Container>
auto covariance(Container const & u, Container const & v);

template<class Container>
auto means_and_covariance(Container const & u, Container const & v);

template<class Container>
auto correlation_coefficient(Container const & u, Container const & v);

}}}
```

#### Description

This file provides functions for computing bivariate statistics.

#### Covariance

Computes the population covariance of two datasets:

```std::vector<double> u{1,2,3,4,5};
std::vector<double> v{1,2,3,4,5};
double cov_uv = boost::math::statistics::covariance(u, v);
```

The implementation follows Bennet et al. The data is not modified. Requires a random-access container. Works with real-valued inputs and does not work with complex-valued inputs.

The algorithm used herein simultaneously generates the mean values of the input data u and v. For certain applications, it might be useful to get them in a single pass through the data. As such, we provide `means_and_covariance`:

```std::vector<double> u{1,2,3,4,5};
std::vector<double> v{1,2,3,4,5};
auto [mu_u, mu_v, cov_uv] = boost::math::statistics::means_and_covariance(u, v);
```

#### Correlation Coefficient

Computes the Pearson correlation coefficient of two datasets u and v:

```std::vector<double> u{1,2,3,4,5};
std::vector<double> v{1,2,3,4,5};
double rho_uv = boost::math::statistics::correlation_coefficient(u, v);
// rho_uv = 1.
```

The data must be random access and cannot be complex.

If one or both of the datasets is constant, the correlation coefficient is an indeterminant form (0/0) and definitions must be introduced to assign it a value. We use the following: If both datasets are constant, then the correlation coefficient is 1. If one dataset is constant, and the other is not, then the correlation coefficient is zero.

#### References

• Bennett, Janine, et al. Numerically stable, single-pass, parallel statistics algorithms. Cluster Computing and Workshops, 2009. CLUSTER'09. IEEE International Conference on. IEEE, 2009.
 Copyright © 2006-2019 Nikhar Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle Walker and Xiaogang Zhang Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)