Skip to content

Instantly share code, notes, and snippets.

@pims
Created June 18, 2010 05:04
Show Gist options
  • Save pims/443265 to your computer and use it in GitHub Desktop.
Save pims/443265 to your computer and use it in GitHub Desktop.

Revisions

  1. pims revised this gist Jun 18, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion runningCorrelation.py
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ def __online_variance(self,x,y):
    self.variance_y = self.m2_y/(self.n - 1)

    def __online_covariance(self,x,y):
    self.cov = self.cov + ((y - self.old_mean_y) * (x - self.old_mean_x)) * (self.n - 1) / self.n
    self.cov = self.cov + ((y - self.old_mean_y) * (x - self.old_mean_x)) * (self.n - 1.0) / self.n

    def push(self,x,y):
    self.n = self.n + 1
  2. pims created this gist Jun 18, 2010.
    66 changes: 66 additions & 0 deletions runningCorrelation.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    #/usr/bin/env python
    import math

    class RunningCorrelation:
    def __init__(self):
    self.n = 0.0
    self.mean_x = 0.0
    self.mean_y = 0.0
    self.m2_x = 0.0
    self.m2_y = 0.0
    self.cov = 0.0
    self.variance_x = 0.0
    self.variance_y = 0.0
    self.old_mean_x = 0.0
    self.old_mean_y = 0.0

    def __online_variance(self,x,y):
    delta_x = x - self.mean_x
    delta_y = y - self.mean_y

    self.mean_x = self.mean_x + delta_x / self.n
    self.mean_y = self.mean_y + delta_y / self.n

    self.m2_x = self.m2_x + delta_x * (x - self.mean_x) # This expression uses the new value of mean
    self.m2_y = self.m2_y + delta_y * (y - self.mean_y) # This expression uses the new value of mean

    self.variance_x = self.m2_x/(self.n - 1)
    self.variance_y = self.m2_y/(self.n - 1)

    def __online_covariance(self,x,y):
    self.cov = self.cov + ((y - self.old_mean_y) * (x - self.old_mean_x)) * (self.n - 1) / self.n

    def push(self,x,y):
    self.n = self.n + 1

    if self.n == 1:
    self.mean_x = x
    self.mean_y = y
    else:
    self.old_mean_x = self.mean_x
    self.old_mean_y = self.mean_y

    self.__online_variance(x,y)
    self.__online_covariance(x,y)

    @property
    def r(self):
    if self.cov > 0:
    return self.cov / (math.sqrt(self.variance_x) * math.sqrt(self.variance_y))
    return 0

    @property
    def variance(self):
    return (self.variance_x,self.variance_y)

    def main():
    rc = RunningCorrelation()
    v1 = [2.5,3.5,3.0,3.5,2.5,3.0]
    v2 = [3.0,3.5,1.5,5.0,3.5,3.0]

    for x,y in zip(v1,v2):
    rc.push(x,y)
    print rc.r #value of r is wrong, and is twice the correct value on the first pass > 1

    if __name__ == '__main__':
    main()