Covariance captures a measure of the correlation of two variables.
Positive covariance indicates that as increases, so does . Negative covariance indicates decreases as
increases and vice versa. Zero covariance can indicate that and are uncorrelated. Covariance is defined as:
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;
return0;
}
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