def setUp(self): dt = 0.5 # process model A = numpy.array([[1, 0, dt, 0], [0, 1, 0, dt], [0, 0, .5, 0], [0, 0, 0, .5]], dtype=numpy.float64) # observation model C = numpy.array([[1, 0, 0, 0], [0, 1, 0, 0]], dtype=numpy.float64) # process covariance Q = numpy.eye(4) Q[:2,:2] = [[ 2, .1],[.1,2]] # measurement covariance R = numpy.array([[ 1, 0.2], [0.2, 1]], dtype=numpy.float64) Q = 0.001*Q R = 0.001*R N = 1000 numpy.random.seed(4) x0 = numpy.random.randn( 4 ) P0 = numpy.random.randn( 4,4 ) process_noise = adskalman.rand_mvn( 0, Q, N ) observation_noise = adskalman.rand_mvn( 0, R, N ) X = [] Y = [] x = x0 for i in range(N): X.append( x ) Y.append( numpy.dot(C,x) + observation_noise[i] ) x = numpy.dot(A,x) + process_noise[i] # for next time step X = numpy.array(X) Y = numpy.array(Y) Abad = numpy.eye(4) #Abad[0,0] = 0 #Abad[1,1] = 100 #Abad[0,2] = .0 #Abad[1,3] = .0 self.Y = Y self.A = A self.Abad = Abad self.C = C self.Q = Q self.R = R self.x0 = x0 self.P0 = P0
def setUp(self): # process model A = numpy.array([[1, 0], [0, .5]], dtype=numpy.float64) # observation model C = numpy.array([[1, 0], [0, 1]], dtype=numpy.float64) # process covariance Q = numpy.eye(2) # measurement covariance R = numpy.eye(2) Q = 0.001*Q R = 0.001*R N = 1000 numpy.random.seed(4) x0 = numpy.random.randn( 2 ) P0 = numpy.random.randn( 2,2 ) process_noise = adskalman.rand_mvn( 0, Q, N ) observation_noise = adskalman.rand_mvn( 0, R, N ) #pylab.hist( process_noise[:,0], bins=100) #pylab.show() X = [] Y = [] x = x0 for i in range(N): X.append( x ) Y.append( numpy.dot(C,x) + observation_noise[i] ) x = numpy.dot(A,x) + process_noise[i] # for next time step X = numpy.array(X) Y = numpy.array(Y) Abad = numpy.eye(2) Abad[0,0] = .1 Abad[1,1] = 4 self.X = X self.Y = Y self.A = A self.Abad = Abad self.C = C self.Q = Q self.R = R self.x0 = x0 self.P0 = P0
def test_rand_mvn2(self): mu = numpy.array([1]) sigma = 2*numpy.eye(1) N = 10000 Y = adskalman.rand_mvn(mu,sigma,N) Y2 = numpy.random.standard_normal((N,1))*sigma[0]+mu[0] assert Y.shape == (N,1) assert Y2.shape == (N,1) Y = Y[:,0] # drop an axis Y2 = Y2[:,0] # drop an axis meanY = numpy.mean(Y) #print 'meanY',meanY meanY2 = numpy.mean(Y2) #print 'meanY2',meanY2 # according to z-test on wikipedia SE = sigma[0]/numpy.sqrt(N) SE2 = sigma[0]/numpy.sqrt(N) z = (meanY-mu[0])/SE z2 = (meanY2-mu[0])/SE2 if abs(z) > 3: raise ValueError('Sample mean was three sigma away from true mean. This error is expected rarely.') if abs(z2) > 3: raise ValueError('Sample mean was three sigma away from true mean. This error is expected rarely.')
def test_rand_mvn1(self): mu = numpy.array([1,2,3,4]) sigma = numpy.eye(4) sigma[:2,:2] = [[2.0, 0.1],[0.1,0.2]] N = 1000 Y = adskalman.rand_mvn(mu,sigma,N) assert Y.shape==(N,4) mu2 = numpy.mean(Y,axis=0) eps = .2 assert numpy.sqrt(numpy.sum((mu-mu2)**2)) < eps # expect occasional failure here sigma2 = adskalman.covar(Y) eps = .3 assert numpy.sqrt(numpy.sum((sigma-sigma2)**2)) < eps # expect occasional failure here