コード例 #1
0
    def test_matrices_from_component(self):
        num_timesteps = 4
        drift_scale = 1.23
        period = 12
        frequency_multipliers = [1, 3]

        component = SmoothSeasonal(period=period,
                                   frequency_multipliers=frequency_multipliers)

        ssm = component.make_state_space_model(num_timesteps, [drift_scale])

        frequency_0 = 2 * np.pi * frequency_multipliers[0] / period
        frequency_1 = 2 * np.pi * frequency_multipliers[1] / period

        first_frequency_transition = tf.linalg.LinearOperatorFullMatrix(
            [[tf.cos(frequency_0), tf.sin(frequency_0)],
             [-tf.sin(frequency_0), tf.cos(frequency_0)]])

        second_frequency_transition = tf.linalg.LinearOperatorFullMatrix(
            [[tf.cos(frequency_1), tf.sin(frequency_1)],
             [-tf.sin(frequency_1), tf.cos(frequency_1)]])

        latents_transition = self.evaluate(
            tf.linalg.LinearOperatorBlockDiag(
                [first_frequency_transition,
                 second_frequency_transition]).to_dense())

        for t in range(num_timesteps):
            observation_matrix = self.evaluate(
                ssm.get_observation_matrix_for_timestep(t).to_dense())

            self.assertAllClose([[1.0, 0.0, 1.0, 0.0]], observation_matrix)

            observation_noise_mean = self.evaluate(
                ssm.get_observation_noise_for_timestep(t).mean())
            observation_noise_covariance = self.evaluate(
                ssm.get_observation_noise_for_timestep(t).covariance())

            self.assertAllClose([0.0], observation_noise_mean)
            self.assertAllClose([[0.0]], observation_noise_covariance)

            transition_matrix = self.evaluate(
                ssm.get_transition_matrix_for_timestep(t).to_dense())

            self.assertAllClose(latents_transition, transition_matrix)

            transition_noise_mean = self.evaluate(
                ssm.get_transition_noise_for_timestep(t).mean())
            transition_noise_covariance = self.evaluate(
                ssm.get_transition_noise_for_timestep(t).covariance())

            self.assertAllClose(np.zeros([4]), transition_noise_mean)
            self.assertAllClose(
                np.square(drift_scale) * np.eye(4),
                transition_noise_covariance)
コード例 #2
0
  def test_accepts_tensor_valued_period_and_frequency_multipliers(self):

    period = tf.constant(100.)
    frequency_multipliers = tf.constant([1., 3.])
    drift_scale = tf.constant([2.])

    component = SmoothSeasonal(
        period=period, frequency_multipliers=frequency_multipliers)

    ssm = component.make_state_space_model(
        num_timesteps=3, param_vals=[drift_scale])

    self.assertAllEqual(component.latent_size, 4)
    self.assertAllEqual(ssm.latent_size, 4)