def test_bayes_evidence(self): k = pb.KalmanFilter(**self.setup_2) y = np.array([[4.1], [-0.2], [1.4], [-2.1]]) u = np.array([[4.8], [-0.3], [1.1], [-1.8]]) exp_mu = np.array([ [ 3.62004716, -0.46320771], [-0.16638519, 3.58787721], [ 1.21108425, 0.0224309 ], [-1.87141692, 0.98517451] ]) exp_var = np.array([ [ 0.00999960, 40.3342872], [ 0.00999029, 0.20999610], [ 0.00963301, 0.20962422], [ 0.00963191, 0.20930431] ]) # in: y_t -1, y_t, y_t + 1 exp_evidences_log = np.array([ [ -3.68958564, -3.68287128, -3.68015356], [ -3.16749303, -2.75744797, -2.44453198], [ -2.69696927, -8.75357053, -18.48011903], [-10.17228316, -3.47352413, -0.45566728] ]) for i in range(4): k.bayes(y[i], u[i]) post = k.posterior() self.assertApproxEqual(post.mean(), exp_mu[i]) self.assertApproxEqual(post.variance(), exp_var[i]) evidences = np.array([k.evidence_log(y[i] - 1.), k.evidence_log(y[i]), k.evidence_log(y[i] + 1.)]) self.assertApproxEqual(evidences, exp_evidences_log[i])
def test_bayes(self): k = pb.KalmanFilter(**self.setup_2) y = np.array([[4.1], [-0.2], [1.4], [-2.1]]) u = np.array([[4.8], [-0.3], [1.1], [-1.8]]) exp_mu = np.array([[3.62004716, -0.46320771], [-0.16638519, 3.58787721], [1.21108425, 0.0224309], [-1.87141692, 0.98517451]]) for i in range(4): k.bayes(y[i], u[i]) mu = k.posterior().mu self.assertApproxEqual(mu, exp_mu[i])
def run_kalman_on_mat_data(input_file, output_file, timer): # this should be here so that only this stress fails when scipy is not installed loadmat = scipy.io.loadmat savemat = scipy.io.savemat d = loadmat(input_file, struct_as_record=True, mat_dtype=True) mu0 = np.reshape(d.pop('mu0'), (-1, )) # otherwise we would get 2D array of shape (1xN) P0 = d.pop('P0') y = d.pop('y').T u = d.pop('u').T #x = d.pop('x').T x = None gauss = pb.GaussPdf(mu0, P0) kalman = pb.KalmanFilter(d['A'], d['B'], d['C'], d['D'], d['Q'], d['R'], gauss) N = y.shape[0] n = mu0.shape[0] mean = np.zeros((N, n)) var = np.zeros((N, n)) timer.start() for t in range(1, N): # the 1 start offset is intentional kalman.bayes(y[t], u[t]) mean[t] = kalman.posterior().mean() #var[t] = kalman.posterior().variance() timer.stop() var = np.sqrt(var) # to get standard deviation plt = None # turn off plotting for now if plt: axis = np.arange(N) plt.plot(axis, x[:, 0], 'k-', label='x_1') plt.plot(axis, x[:, 1], 'k--', label='x_2') plt.errorbar(axis, mean[:, 0], fmt='s', label='mu_1') # yerror=var[:,0] plt.errorbar(axis, mean[:, 1], fmt='D', label='mu_2') # yerror=var[:,1] plt.legend() plt.show() savemat(output_file, { "Mu_py": mean.T, "exec_time_pybayes": timer.spent[0] }, oned_as='row')
def test_deepcopy(self): """Test that deep copying KF works as expected""" o = pb.KalmanFilter(**self.setup_2) # original c = deepcopy(o) # copy self.assertEqual(type(c), type(o)) self.assertTrue(id(o) != id(c)) for (a, b) in [(o.A, c.A), (o.B, c.B), (o.C, c.C), (o.D, c.D), (o.Q, c.Q), (o.R, c.R)]: self.assertArraysEqualNotSame(a, b) # n, k, j do not need to be different as they are immutable self.assertEqual(o.n, c.n) self.assertEqual(o.k, c.k) self.assertEqual(o.j, c.j) self.assertTrue(id(o.P) != id(c.P)) self.assertArraysEqualNotSame(o.P.mu, c.P.mu) # this is better tested in self.assertArraysEqualNotSame(o.P.R, c.P.R) # GaussPdf deepcopy test, but wont hurt here self.assertTrue(id(o.S) != id(c.S))
def test_copy(self): """Test that copying KF works as expected""" o = pb.KalmanFilter(**self.setup_1) # original c = copy(o) # copy self.assertEqual(type(c), type(o)) self.assertTrue(id(o) != id(c)) self.assertTrue(id(o.A) == id(c.A)) self.assertTrue(id(o.B) == id(c.B)) self.assertTrue(id(o.C) == id(c.C)) self.assertTrue(id(o.D) == id(c.D)) self.assertTrue(id(o.Q) == id(c.Q)) self.assertTrue(id(o.R) == id(c.R)) self.assertTrue(id(o.n) == id(c.n)) self.assertTrue(id(o.k) == id(c.k)) self.assertTrue(id(o.j) == id(c.j)) self.assertTrue(id(o.P) == id(c.P)) self.assertTrue(id(o.S) == id(c.S))
def test_copy(self): """Test that copying KF works as expected""" o = pb.KalmanFilter(**self.setup_1) # original c = copy(o) # copy self.assertEqual(type(c), type(o)) self.assertNotEqual(id(o), id(c)) self.assertArraysSame(o.A, c.A) self.assertArraysSame(o.B, c.B) self.assertArraysSame(o.C, c.C) self.assertArraysSame(o.D, c.D) self.assertArraysSame(o.Q, c.Q) self.assertArraysSame(o.R, c.R) self.assertEqual(o.n, c.n) self.assertEqual(o.k, c.k) self.assertEqual(o.j, c.j) self.assertEqual(id(o.P), id(c.P)) self.assertEqual(id(o.S), id(c.S))
def test_init(self): k = pb.KalmanFilter(**self.setup_1) self.assertEqual(type(k), pb.KalmanFilter) l = pb.KalmanFilter(**self.setup_2) self.assertEqual(type(l), pb.KalmanFilter)
try: from scipy.io import loadmat, savemat except ImportError, e: raise StopIteration("Kalman filter stress needs scipy installed: " + str(e)) d = loadmat(input_file, struct_as_record=True, mat_dtype=True) mu0 = np.reshape(d.pop('mu0'), (-1,)) # otherwise we would get 2D array of shape (1xN) P0 = d.pop('P0') y = d.pop('y').T u = d.pop('u').T #x = d.pop('x').T x = None gauss = pb.GaussPdf(mu0, P0) kalman = pb.KalmanFilter(d['A'], d['B'], d['C'], d['D'], d['Q'], d['R'], gauss) N = y.shape[0] n = mu0.shape[0] mean = np.zeros((N, n)) var = np.zeros((N, n)) timer.start() for t in xrange(1, N): # the 1 start offset is intentional kalman.bayes(y[t], u[t]) mean[t] = kalman.posterior().mean() #var[t] = kalman.posterior().variance() timer.stop() var = np.sqrt(var) # to get standard deviation plt = None # turn off plotting for now