Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-449

Storeless covariance

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.0
    • None
    • None

    Description

      Currently there is no storeless version for computing the covariance. However, Pebay (2008) describes algorithms for on-line covariance computations, http://infoserve.sandia.gov/sand_doc/2008/086212.pdf. I have provided a simple class for implementing this algorithm. It would be nice to have this integrated into org.apache.commons.math.stat.correlation.Covariance.

      //This code is granted for inclusion in the Apache Commons under the terms of the ASL.
      
      public class StorelessCovariance{
      
          private double deltaX = 0.0;
          private double deltaY = 0.0;
          private double meanX = 0.0;
          private double meanY = 0.0;
          private double N=0;
          private Double covarianceNumerator=0.0;
          private boolean unbiased=true;
      
          public Covariance(boolean unbiased){
      	this.unbiased = unbiased;
          }
      
          public void increment(Double x, Double y){
              if(x!=null & y!=null){
                  N++;
                  deltaX = x - meanX;
                  deltaY = y - meanY;
                  meanX += deltaX/N;
                  meanY += deltaY/N;
                  covarianceNumerator += ((N-1.0)/N)*deltaX*deltaY;
              }
              
          }
      
          public Double getResult(){
              if(unbiased){
                  return covarianceNumerator/(N-1.0);
              }else{
                  return covarianceNumerator/N;
              }
          }   
      }
      

      Attachments

        1. MATH-449.patch
          20 kB
          Patrick Meyer
        2. MATH-449.patch
          5 kB
          Patrick Meyer

        Activity

          People

            tn Thomas Neidhart
            meyerjp Patrick Meyer
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 168h
                168h
                Remaining:
                Remaining Estimate - 168h
                168h
                Logged:
                Time Spent - Not Specified
                Not Specified