class KalmanTest(unittest.TestCase): def setUp(self): self.DEWMA = Ewma() def testKalmanDelta(self): self.DEWMA = Ewma(x_init=10., x_dinit=1 / 64.) sse = 0. for i in xrange(1, 2001): z = ((i / 64.) + 10.) v = self.DEWMA.filter(z, i * 1024) err = v[0] - z sse += err * err print v[0] print v[1] print sse self.assertEquals(v[0], 10. + 2000. / 64.) self.assertEquals(v[1], 1. / 64.) self.assertEquals(sse, 0.) def testKalmanDelta2(self): self.DEWMA = Ewma(x_init=10., x_dinit=1 / 64., alpha=0.01, beta=0.001) sse = 0. sse2 = 0. for i in xrange(1, 6001): z = ((i / 64.) + 10.) + random.gauss(0, 1.) v = self.DEWMA.filter(z, i * 1024) err = v[0] - z err2 = v[1] - 1 / 64. sse += err * err sse2 += err2 * err2 print v[0] print v[1] print sse print sse2 self.assertEquals(v[0], 10. + 6000. / 64.) self.assertEquals(v[1], 1. / 64.) self.assertEquals(sse, 0.)
class KalmanTest(unittest.TestCase): def setUp(self): self.DEWMA = Ewma() def testKalmanDelta(self): self.DEWMA = Ewma(x_init=10.0, x_dinit=1 / 64.0) sse = 0.0 for i in xrange(1, 2001): z = (i / 64.0) + 10.0 v = self.DEWMA.filter(z, i * 1024) err = v[0] - z sse += err * err print v[0] print v[1] print sse self.assertEquals(v[0], 10.0 + 2000.0 / 64.0) self.assertEquals(v[1], 1.0 / 64.0) self.assertEquals(sse, 0.0) def testKalmanDelta2(self): self.DEWMA = Ewma(x_init=10.0, x_dinit=1 / 64.0, alpha=0.01, beta=0.001) sse = 0.0 sse2 = 0.0 for i in xrange(1, 6001): z = ((i / 64.0) + 10.0) + random.gauss(0, 1.0) v = self.DEWMA.filter(z, i * 1024) err = v[0] - z err2 = v[1] - 1 / 64.0 sse += err * err sse2 += err2 * err2 print v[0] print v[1] print sse print sse2 self.assertEquals(v[0], 10.0 + 6000.0 / 64.0) self.assertEquals(v[1], 1.0 / 64.0) self.assertEquals(sse, 0.0)
def test(): kd = None first_time = None with open("testVOC.csv", "rb") as ff: rdr = csv.reader(ff) print "time,x,xhat,dx" for r in rdr: n = int(r[0]) dt = datetime.strptime(r[1], "%Y-%m-%d %H:%M:%S") v = float(r[2]) if kd is None: first_time = dt kd = Ewma( x_init = v, x_dinit = 0., alpha = 0.05, beta = 0.05) else: z2 = v x = kd.filter(z2, (dt - first_time).seconds) print dt.strftime("%s")+","+str(v)+","+str(x[0])+","+str(x[1])