Example #1
0
 def setUp(self):
     """The basic model defined above, with unit batches."""
     self.kalman_filter = kalman_filter.KalmanFilter()
     self.transition_fn, self.power_sum_fn = (
         _powers_and_sums_from_transition_matrix(
             state_transition=STATE_TRANSITION,
             state_transition_noise_covariance=STATE_TRANSITION_NOISE,
             state_noise_transform=STATE_NOISE_TRANSFORM,
             max_gap=5))
Example #2
0
    def test_do_filter_batch(self):
        """Tests do_filter, in batch mode.

    Tests that correct values have high probability and incorrect values
    have low probability when there is low uncertainty.
    """
        with self.cached_session():
            state = constant_op.constant([[4., 2.], [5., 3.], [6., 4.]])
            state_var = constant_op.constant(3 *
                                             [[[0.0001, 0.], [0., 0.0001]]])
            observation = constant_op.constant([
                [
                    .5 * (
                        4.  # Base
                        + 2.),  # State transition
                    2.
                ],
                [
                    .5 * (
                        5.  # Base
                        + 3.),  # State transition
                    3.
                ],
                [3.14, 2.71]
            ])  # Low probability observation
            kf = kalman_filter.KalmanFilter()
            transition_fn, power_sum_fn = _powers_and_sums_from_transition_matrix(
                state_transition=STATE_TRANSITION,
                state_transition_noise_covariance=STATE_TRANSITION_NOISE,
                state_noise_transform=STATE_NOISE_TRANSFORM,
                max_gap=2)
            estimated_state = kf.predict_state_mean(state,
                                                    transition_fn(3 * [1]))
            estimated_state_covariance = kf.predict_state_var(
                state_var, transition_fn(3 * [1]), power_sum_fn(3 * [1]))
            observation_model = array_ops.tile(OBSERVATION_MODEL, [3, 1, 1])
            (predicted_observation,
             predicted_observation_covariance) = (kf.observed_from_state(
                 estimated_state,
                 estimated_state_covariance,
                 observation_model=observation_model,
                 observation_noise=OBSERVATION_NOISE))
            (state, state_var, log_prob) = kf.do_filter(
                estimated_state=estimated_state,
                estimated_state_covariance=estimated_state_covariance,
                predicted_observation=predicted_observation,
                predicted_observation_covariance=
                predicted_observation_covariance,
                observation=observation,
                observation_model=observation_model,
                observation_noise=OBSERVATION_NOISE)
            first_log_prob, second_log_prob, third_log_prob = log_prob.eval()
            self.assertGreater(first_log_prob.sum(), numpy.log(0.99))
            self.assertGreater(second_log_prob.sum(), numpy.log(0.99))
            self.assertLess(third_log_prob.sum(), numpy.log(0.01))
Example #3
0
 def _multivariate_symmetric_covariance_test_template(
         self, dtype, simplified_posterior_variance_computation):
     """Check that errors aren't building up asymmetries in covariances."""
     kf = kalman_filter.KalmanFilter(dtype=dtype)
     observation_noise_covariance = constant_op.constant(
         [[1., 0.5], [0.5, 1.]], dtype=dtype)
     observation_model = constant_op.constant(
         [[[1., 0., 0., 0.], [0., 0., 1., 0.]]], dtype=dtype)
     state = array_ops.placeholder(shape=[1, 4], dtype=dtype)
     state_var = array_ops.placeholder(shape=[1, 4, 4], dtype=dtype)
     observation = array_ops.placeholder(shape=[1, 2], dtype=dtype)
     transition_fn, power_sum_fn = _powers_and_sums_from_transition_matrix(
         state_transition=constant_op.constant(
             [[1., 1., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 1.],
              [0., 0., 0., 1.]],
             dtype=dtype),
         state_noise_transform=linalg_ops.eye(4, dtype=dtype),
         state_transition_noise_covariance=constant_op.constant(
             [[1., 0., 0.5, 0.], [0., 1., 0., 0.5], [0.5, 0., 1., 0.],
              [0., 0.5, 0., 1.]],
             dtype=dtype))
     pred_state = kf.predict_state_mean(prior_state=state,
                                        transition_matrices=transition_fn(
                                            [1]))
     pred_state_var = kf.predict_state_var(
         prior_state_var=state_var,
         transition_matrices=transition_fn([1]),
         transition_noise_sums=power_sum_fn([1]))
     observed_mean, observed_var = kf.observed_from_state(
         state_mean=pred_state,
         state_var=pred_state_var,
         observation_model=observation_model,
         observation_noise=observation_noise_covariance)
     post_state, post_state_var = kf.posterior_from_prior_state(
         prior_state=pred_state,
         prior_state_var=pred_state_var,
         observation=observation,
         observation_model=observation_model,
         predicted_observations=(observed_mean, observed_var),
         observation_noise=observation_noise_covariance)
     with self.cached_session() as session:
         evaled_state = numpy.array([[1., 1., 1., 1.]])
         evaled_state_var = numpy.eye(4)[None]
         for i in range(500):
             evaled_state, evaled_state_var, evaled_observed_var = session.run(
                 [post_state, post_state_var, observed_var],
                 feed_dict={
                     state: evaled_state,
                     state_var: evaled_state_var,
                     observation: [[float(i), float(i)]]
                 })
             self.assertAllClose(evaled_observed_var[0],
                                 evaled_observed_var[0].T)
             self.assertAllClose(evaled_state_var[0], evaled_state_var[0].T)
Example #4
0
 def test_predict_n_ahead_mean(self):
     with self.cached_session():
         kf = kalman_filter.KalmanFilter()
         transition_fn, _ = _powers_and_sums_from_transition_matrix(
             state_transition=STATE_TRANSITION,
             state_transition_noise_covariance=STATE_TRANSITION_NOISE,
             state_noise_transform=STATE_NOISE_TRANSFORM,
             max_gap=2)
         original_state = constant_op.constant([[4., 2.], [3., 1.],
                                                [6., 2.]])
         state0 = original_state
         state1 = kf.predict_state_mean(state0, transition_fn(3 * [1]))
         state2 = kf.predict_state_mean(state1, transition_fn(3 * [1]))
         batch_eval = kf.predict_state_mean(original_state,
                                            transition_fn([1, 0,
                                                           2])).eval()
         self.assertAllClose(state0.eval()[1], batch_eval[1])
         self.assertAllClose(state1.eval()[0], batch_eval[0])
         self.assertAllClose(state2.eval()[2], batch_eval[2])
Example #5
0
 def test_predict_n_ahead_var(self):
     with self.cached_session():
         kf = kalman_filter.KalmanFilter()
         transition_fn, power_sum_fn = _powers_and_sums_from_transition_matrix(
             state_transition=STATE_TRANSITION,
             state_transition_noise_covariance=STATE_TRANSITION_NOISE,
             state_noise_transform=STATE_NOISE_TRANSFORM,
             max_gap=2)
         base_var = 2.0 * numpy.identity(2) + numpy.ones([2, 2])
         original_var = constant_op.constant(
             numpy.array([base_var, 2.0 * base_var, 3.0 * base_var],
                         dtype=numpy.float32))
         var0 = original_var
         var1 = kf.predict_state_var(var0, transition_fn(3 * [1]),
                                     power_sum_fn(3 * [1]))
         var2 = kf.predict_state_var(var1, transition_fn(3 * [1]),
                                     power_sum_fn(3 * [1]))
         batch_eval = kf.predict_state_var(original_var,
                                           transition_fn([1, 0, 2]),
                                           power_sum_fn([1, 0, 2])).eval()
         self.assertAllClose(var0.eval()[1], batch_eval[1])
         self.assertAllClose(var1.eval()[0], batch_eval[0])
         self.assertAllClose(var2.eval()[2], batch_eval[2])