code: def test_weighted_sample_coherence(event_shape, dtype):
      data = [(1.0, 0.5), (0.0, 1.5), (1.0, 0.5), (0.0, 1.5)]
      samples, weights = [], []
      for sample, weight in data:
          samples.append(sample * torch.ones(event_shape, dtype=dtype))
          weights.append(torch.tensor(weight).log())
      samples, weights = torch.stack(samples), torch.stack(weights)
      empirical_dist = Empirical(samples, weights)
      assert_equal(empirical_dist.event_shape, torch.Size(event_shape))
      assert_equal(empirical_dist.sample_size, 4)
      sample_to_score = torch.ones(event_shape, dtype=dtype) * 1.0
      assert_equal(
          empirical_dist.log_prob(sample_to_score),
          torch.tensor(0.25).log(),
      )
      samples = empirical_dist.sample(sample_shape=torch.Size((1000,)))
      zeros = torch.zeros(event_shape, dtype=dtype)
      ones = torch.ones(event_shape, dtype=dtype)
      num_zeros = (
          samples.eq(zeros)
          .contiguous()
          .view(1000, -1)
          .min(dim=-1)[0]
          .float()
          .sum()
      )
      num_ones = (
          samples.eq(ones)
          .contiguous()
          .view(1000, -1)
          .min(dim=-1)[0]
          .float()
          .sum()
      )
      assert_equal(num_zeros.item() / 1000, 0.75, prec=0.02)
      assert_equal(num_ones.item() / 1000, 0.25, prec=0.02)
  
file_path: pyro/tests/distributions/test_empirical.py
imports: import pytest
  import torch
  from pyro.distributions.empirical import Empirical
  from tests.common import assert_close, assert_equal
label_desc: Use image node_id 5 for calling a global function with example usage: test_weighted_sample_coherence(event_shape, dtype) without return types