Class 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 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
      • addData

        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

         
         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.
        Specified by:
        addData in interface Statistic
        Parameters:
        data - a data array concatenating two samples
      • addData

        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
      • 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