Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 import numpy as np
00018 import copy
00019
00020
00021 class Running_Mean_And_Variance():
00022 def __init__(self):
00023 self.mn = None
00024 self.s = 0
00025 self.k = 0
00026
00027 def update(self, x):
00028 self.k += 1
00029 if self.k == 1:
00030 self.mn = x
00031 self.s = 0.
00032 else:
00033 mn_old = self.mn
00034 self.mn = mn_old + (x - mn_old) / self.k
00035 self.s = self.s + (x - mn_old) * (x - self.mn)
00036
00037 def mean(self):
00038 return copy.copy(self.mn)
00039
00040 def std(self):
00041 return np.sqrt(self.variance())
00042
00043 def variance(self):
00044 return copy.copy(self.s/(self.k-1))
00045
00046 def biased_std(self):
00047 return np.sqrt(self.biased_variance())
00048
00049 def biased_variance(self):
00050 return copy.copy(self.s/(self.k))
00051
00052
00053
00054 if __name__ == '__main__':
00055 a = np.random.randn(4, 100)
00056
00057 print 'Mean and Std computed using numpy:'
00058 print 'Mean:', np.mean(a, 1)
00059 print 'Std:', np.std(a, 1)
00060
00061 print ''
00062 print 'Running Mean and Std:'
00063
00064 rmav = Running_Mean_And_Variance()
00065 for b in a.T:
00066 rmav.update(b)
00067
00068 print 'Mean:', rmav.mean()
00069 print 'Unbiased Std:', rmav.std()
00070 print 'Biased Std:', rmav.biased_std()
00071
00072
00073
00074
00075
hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06