code: def test_gaussian_filter():
      dim = 4
      init_dist = dist.MultivariateNormal(
          torch.zeros(dim), scale_tril=torch.eye(dim) * 10
      )
      trans_mat = torch.eye(dim)
      trans_dist = dist.MultivariateNormal(
          torch.zeros(dim), scale_tril=torch.eye(dim)
      )
      obs_mat = torch.eye(dim)
      obs_dist = dist.MultivariateNormal(
          torch.zeros(dim), scale_tril=torch.eye(dim) * 2
      )
      hmm = dist.GaussianHMM(
          init_dist, trans_mat, trans_dist, obs_mat, obs_dist
      )
  
      class Model:
          def init(self, state):
              state["z"] = pyro.sample("z_init", init_dist)
              self.t = 0
  
          def step(self, state, datum=None):
              state["z"] = pyro.sample(
                  "z_{}".format(self.t),
                  dist.MultivariateNormal(
                      state["z"], scale_tril=trans_dist.scale_tril
                  ),
              )
              datum = pyro.sample(
                  "obs_{}".format(self.t),
                  dist.MultivariateNormal(
                      state["z"], scale_tril=obs_dist.scale_tril
                  ),
                  obs=datum,
              )
              self.t += 1
              return datum
  
      class Guide:
          def init(self, state):
              pyro.sample("z_init", init_dist)
              self.t = 0
  
          def step(self, state, datum):
              pyro.sample(
                  "z_{}".format(self.t),
                  dist.MultivariateNormal(
                      state["z"], scale_tril=trans_dist.scale_tril * 2
                  ),
              )
              self.t += 1
  
      # Generate data.
      num_steps = 20
      model = Model()
      state = {}
      model.init(state)
      data = torch.stack([model.step(state) for _ in range(num_steps)])
  
      # Perform inference.
      model = Model()
      guide = Guide()
      smc = SMCFilter(
          model, guide, num_particles=1000, max_plate_nesting=0
      )
      smc.init()
      for t, datum in enumerate(data):
          smc.step(datum)
          expected = hmm.filter(data[: 1 + t])
          actual = smc.get_empirical()["z"]
          assert_close(
              actual.variance**0.5,
              expected.variance**0.5,
              atol=0.1,
              rtol=0.5,
          )
          sigma = actual.variance.max().item() ** 0.5
          assert_close(actual.mean, expected.mean, atol=3 * sigma)
  
imports: import pytest
  import torch
  import pyro
  import pyro.distributions
  import pyro.poutine
  from pyro.infer import SMCFilter
  from pyro.infer.smcfilter import _systematic_sample
  from tests.common import assert_close
label_desc: Use image node_id 6 for calling a global function with example usage: test_gaussian_filter() and returns: datum