Class Covariance
- java.lang.Object
-
- dev.nm.stat.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.- See Also:
-
-
Constructor Summary
Constructors Constructor Description Covariance()
Construct an emptyCovariance
calculator.Covariance(double[] data1, double[] data2)
Construct aCovariance
calculator, initialized with two samples.Covariance(Covariance that)
Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Constructor Detail
-
Covariance
public Covariance()
Construct an emptyCovariance
calculator.
-
Covariance
public Covariance(double[] data1, double[] data2)
Construct aCovariance
calculator, initialized with two samples. The size of the two samples must be equal.- Parameters:
data1
- the first sampledata2
- the second sample
-
Covariance
public Covariance(Covariance that)
Copy constructor.- Parameters:
that
- aCovariance
instance
-
-
Method Detail
-
correlation
public double correlation()
Get the correlation, i.e., Pearson's correlation coefficient.- Returns:
- the correlation
-
addData
public void addData(double... data)
Update the covariance statistic with more data. Since this signature takes only a single arraydouble[]
, we concatenate the two arrays into one. For example, suppose we want to doaddData(new double[][]{ {1, 2, 3}, {4, 5, 6} });
addData(new double[]{ {1, 2, 3, 4, 5, 6} });
-
addData
public void addData(double[] data1, double[] data2)
Update the covariance statistic with more data.- Parameters:
data1
- the first new sampledata2
- the second new sample
-
value
public double value()
Description copied from interface:Statistic
Get the value of the statistic.
-
N
public long N()
Description copied from interface:Statistic
Get the size of the sample.
-
-