SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.stats.descriptive
Class Covariance

java.lang.Object
  extended by com.numericalmethod.suanshu.stats.descriptive.Covariance
All Implemented Interfaces:
Statistic

public class Covariance
extends java.lang.Object
implements Statistic

Covariance is a measure of how much two variables change together.

In this implementation, we compute covariance using the Pearson method.

 Cov(X, Y) = E[(X - E(X)) * (Y - E(Y))]
 

Note that we use N - 1 as the denominator to give an unbiased estimator of the covariance for i.i.d. observations.

See Also:

Constructor Summary
Covariance()
          Construct an empty Covariance calculator.
Covariance(Covariance that)
          Copy constructor.
Covariance(double[][] data)
          Construct a Covariance calculator, initialized with two samples.
 
Method Summary
 void addData(double... data)
          Recompute the statistic, incrementally if possible.
 void addData(double[][] data)
          Update the covariance statistic with more data.
 double correlation()
          Get the correlation.
 long N()
          Get the size of the sample.
 java.lang.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[][] data)
Construct a Covariance calculator, initialized with two samples.

For example,

Covariance = new Covariance(new double[][]{ {1, 2, 3}, {4, 5, 6} });
The size of the two double[]s must be equal.


Covariance

public Covariance(Covariance that)
Copy constructor.

Parameters:
that - a Covariance instance
Method Detail

addData

public void addData(double... data)
Recompute the statistic, incrementally if possible. Update the covariance statistic with more data.

Since this function takes only a single double array, we concatenate two arrays into one.

For example, suppose we want to

addData(new double[][]{ {1, 2, 3}, {4, 5, 6} });
We can instead write
addData(new double[]{ {1, 2, 3, 4, 5, 6} });
There must be an even number of data points.

Specified by:
addData in interface Statistic
Parameters:
data - a data array made by concatenating two samples

addData

public void addData(double[][] data)
Update the covariance statistic with more data.

Parameters:
data - two new samples

value

public double value()
Description copied from interface: Statistic
Get the value of the statistic.

Specified by:
value in interface Statistic
Returns:
the statistic

correlation

public double correlation()
Get the correlation.

Returns:
the correlation

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 java.lang.String toString()
Overrides:
toString in class java.lang.Object

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.