def test_logprob(self):
        y = self._build_placeholder([1.0, 1.3, 1.9, 2.9, 2.1])

        ssm = LocalLevelStateSpaceModel(
            num_timesteps=5,
            level_scale=0.5,
            initial_state_prior=tfd.MultivariateNormalDiag(
                scale_diag=self._build_placeholder([1.])))

        lp = ssm.log_prob(y[..., np.newaxis])
        expected_lp = -6.5021
        self.assertAllClose(self.evaluate(lp), expected_lp)
    def testEqualsLocalLevel(self):
        # An AR1 process with coef 1 is just a random walk, equivalent to a local
        # level model. Test that both models define the same distribution
        # (log-prob).
        num_timesteps = 10
        observed_time_series = self._build_placeholder(
            np.random.randn(num_timesteps, 1))

        level_scale = self._build_placeholder(0.1)

        # We'll test an AR1 process, and also (just for kicks) that the trivial
        # embedding as an AR2 process gives the same model.
        coefficients_order1 = np.array([1.]).astype(self.dtype)
        coefficients_order2 = np.array([1., 0.]).astype(self.dtype)

        ar1_ssm = AutoregressiveStateSpaceModel(
            num_timesteps=num_timesteps,
            coefficients=coefficients_order1,
            level_scale=level_scale,
            initial_state_prior=tfd.MultivariateNormalDiag(
                scale_diag=[level_scale]))
        ar2_ssm = AutoregressiveStateSpaceModel(
            num_timesteps=num_timesteps,
            coefficients=coefficients_order2,
            level_scale=level_scale,
            initial_state_prior=tfd.MultivariateNormalDiag(
                scale_diag=[level_scale, 1.]))

        local_level_ssm = LocalLevelStateSpaceModel(
            num_timesteps=num_timesteps,
            level_scale=level_scale,
            initial_state_prior=tfd.MultivariateNormalDiag(
                scale_diag=[level_scale]))

        ar1_lp, ar2_lp, ll_lp = self.evaluate(
            (ar1_ssm.log_prob(observed_time_series),
             ar2_ssm.log_prob(observed_time_series),
             local_level_ssm.log_prob(observed_time_series)))
        self.assertAllClose(ar1_lp, ll_lp)
        self.assertAllClose(ar2_lp, ll_lp)