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 emptyCovariancecalculator.Covariance(double[] data1, double[] data2)Construct aCovariancecalculator, initialized with two samples.Covariance(Covariance that)Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddData(double... data)Update the covariance statistic with more data.voidaddData(double[] data1, double[] data2)Update the covariance statistic with more data.doublecorrelation()Get the correlation, i.e., Pearson's correlation coefficient.longN()Get the size of the sample.StringtoString()doublevalue()Get the value of the statistic.
-
-
-
Constructor Detail
-
Covariance
public Covariance()
Construct an emptyCovariancecalculator.
-
Covariance
public Covariance(double[] data1, double[] data2)Construct aCovariancecalculator, 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- aCovarianceinstance
-
-
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 do
We can also writeaddData(new double[][]{ {1, 2, 3}, {4, 5, 6} });
In the latter case, there must be an even number of data points.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:StatisticGet the value of the statistic.
-
N
public long N()
Description copied from interface:StatisticGet the size of the sample.
-
-