public class Covariance extends Object implements Statistic
Pass equally sized arrays in matching observation order. Covariance uses the sample denominator; correlation removes the units. Both arrays must have nonzero variance for a meaningful correlation.
import dev.nm.stat.descriptive.covariance.Covariance;
Covariance paired = new Covariance(new double[]{1, 2, 3}, new double[]{2, 4, 6});
assert paired.N() == 3;
assert Math.abs(paired.value() - 2.0) < 1e-12;
assert Math.abs(paired.correlation() - 1.0) < 1e-12;
| 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. |
| 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.
|
public Covariance()
Covariance calculator.public Covariance(double[] data1,
double[] data2)
Covariance calculator, initialized with two samples.
The size of the two samples must be equal.data1 - the first sampledata2 - the second samplepublic Covariance(Covariance that)
that - a Covariance instancepublic double correlation()
public void addData(double... data)
double[],
we concatenate the two arrays into one.
For example, suppose we want to do
addData(new double[][]{
{1, 2, 3},
{4, 5, 6}
});
We can also write
addData(new double[]{
{1, 2, 3, 4, 5, 6}
});
In the latter case, there must be an even number of data points.public void addData(double[] data1,
double[] data2)
data1 - the first new sampledata2 - the second new samplepublic double value()
Statisticpublic long N()
StatisticCopyright © 2010-2026 NM Group. All Rights Reserved.