code: def test_weighted_mean_var(event_shape, dtype, batch_shape):
      data = [(1, 0.5), (0, 1.5), (1, 0.5), (0, 1.5)]
      samples, weights = [], []
      for sample, weight in data:
          samples.append(sample * torch.ones(event_shape, dtype=dtype))
          weight_dtype = dtype if dtype is not torch.long else None
          weights.append(torch.tensor(weight, dtype=weight_dtype).log())
      samples = torch.stack(samples).expand(
          batch_shape + [4] + event_shape
      )
      weights = torch.stack(weights).expand(batch_shape + [4])
      empirical_dist = Empirical(samples, weights)
      if dtype in (torch.float32, torch.float64):
          true_mean = (
              torch.ones(batch_shape + event_shape, dtype=dtype) * 0.25
          )
          true_var = (
              torch.ones(batch_shape + event_shape, dtype=dtype)
              * 0.1875
          )
          assert_equal(empirical_dist.mean, true_mean)
          assert_equal(empirical_dist.variance, true_var)
      else:
          with pytest.raises(ValueError):
              empirical_dist.mean
              empirical_dist.variance
  
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 6 for calling a global function with example usage: test_weighted_mean_var(event_shape, dtype, batch_shape) without return types