# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.stats.descriptive.covariance

## Class Covariance

• java.lang.Object
• com.numericalmethod.suanshu.stats.descriptive.covariance.Covariance
• All Implemented Interfaces:
Statistic

public class Covariance
extends Object
implements Statistic
Covariance is a measure of how much two variables change together. This implementation uses the Pearson method. That is,
Cov(X, Y) = E[(X - E(X)) * (Y - E(Y))]
Note that this implementation uses N - 1 as the denominator to give an unbiased estimator of the covariance for i.i.d. observations. This implementation uses Pébay's update formula to incrementally compute the new statistic.
• ### Constructor Summary

Constructors
Constructor and Description
Covariance()
Construct an empty Covariance calculator.
Covariance(Covariance that)
Copy constructor.
Covariance(double[] data1, double[] data2)
Construct a Covariance calculator, initialized with two samples.
• ### Method Summary

All Methods
Modifier and Type Method and Description
void addData(double... data)
Update the covariance statistic with more data.
void addData(double[] data1, double[] data2)
Update the covariance statistic with more data.
double correlation()
Get the correlation, i.e., Pearson's correlation coefficient.
long N()
Get the size of the sample.
String toString()
double value()
Get the value of the statistic.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
• ### Constructor Detail

• #### Covariance

public Covariance()
Construct an empty Covariance calculator.
• #### Covariance

public Covariance(double[] data1,
double[] data2)
Construct a Covariance calculator, initialized with two samples. The size of the two samples must be equal.
Parameters:
data1 - the first sample
data2 - the second sample
• #### Covariance

public Covariance(Covariance that)
Copy constructor.
Parameters:
that - a Covariance instance
• ### Method Detail

• #### correlation

public double correlation()
Get the correlation, i.e., Pearson's correlation coefficient.
Returns:
the correlation

public void addData(double... data)
Update the covariance statistic with more data. Since this signature takes only a single array double[], we concatenate the two arrays into one.

For example, suppose we want to do

 
{1, 2, 3},
{4, 5, 6}
});


We can also write
 
{1, 2, 3, 4, 5, 6}
});


In the latter case, there must be an even number of data points.
Specified by:
addData in interface Statistic
Parameters:
data - a data array concatenating two samples

public void addData(double[] data1,
double[] data2)
Update the covariance statistic with more data.
Parameters:
data1 - the first new sample
data2 - the second new sample
"Pébay, Philippe, "Formulas for Robust, One-Pass Parallel Computation of Covariances and Arbitrary-Order Statistical Moments," Technical Report SAND2008-6212, Sandia National Laboratories, 2008."
• #### value

public double value()
Description copied from interface: Statistic
Get the value of the statistic.
Specified by:
value in interface Statistic
Returns:
the statistic
• #### N

public long N()
Description copied from interface: Statistic
Get the size of the sample.
Specified by:
N in interface Statistic
Returns:
the sample size
• #### toString

public String toString()
Overrides:
toString in class Object