• https://me.yahoo.com
COST (GBP)
0.30
0.50
0

Covariance

viewed 2780 times and licensed 42 times
Calculates the covariance of a given set of data
Controller: CodeCogs
Contents

C++

Covariance

 template doublecovariance( int n T* data T* data1 )
The covariance of two random variables $\inline&space;X_1$ and $\inline&space;X_2$ with mean $\inline&space;\overline{X_1}$ and $\inline&space;\overline{X_2}$ respectively is defined as
$cov(X_1,X_2)=E[(X_1-\overline{X_1})(X_2-\overline{X_2})$

The covariance of a random variable $\inline&space;X$ with itself is simply the variance
$E[X-\overline{X}^2]$

Covariance captures a measure of the correlation of two variables.

Positive covariance indicates that as $\inline&space;X_1$ increases, so does $\inline&space;X_2$ . Negative covariance indicates $\inline&space;X_1$ decreases as $\inline&space;X_2$ increases and vice versa. Zero covariance can indicate that $\inline&space;X_1$ and $\inline&space;X_2$ are uncorrelated. Covariance is defined as:
$cov(x,y)=\frac{1}{N}\sum_{i=1}^Nx_iy_i-\overline{x_i}\overline{y_i}$

In the example below the covariance of two random variables is calculated, yielding the result: <em> -1.64 </em>. These two variables are also displayed in the following graphs.

Example 1

#include <codecogs/statistics/moments/covariance.h>
#include <iostream>
int main()
{
int x[5] = {2 , 4 , 8 , 9 , 3};
int y[5] = {3 , 5 , 7 , 2 , 9};
double cov = Stats::Moments::covariance<int>(5, x , y);
std::cout << "The covariance of x and y is: " << cov << std::endl;
return 0;
}
Output:
The covariance of x and y is: -1.64

Parameters

 n the size of the first array and of the second array data the actual population data given as the first array data1 the second array

Returns

the covariance of a given population

Authors

Anca Filibiu (August 2005)
Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.