code: def test_stable(Reparam, shape):
      stability = torch.empty(shape).uniform_(1.5, 2.0).requires_grad_()
      skew = torch.empty(shape).uniform_(-0.5, 0.5).requires_grad_()
      # test edge case when skew is 0
      if skew.dim() > 0 and skew.shape[-1] > 0:
          skew.data[..., 0] = 0.0
      scale = torch.empty(shape).uniform_(0.5, 1.0).requires_grad_()
      loc = torch.empty(shape).uniform_(-1.0, 1.0).requires_grad_()
      params = [stability, skew, scale, loc]
  
      def model():
          with pyro.plate_stack("plates", shape):
              with pyro.plate("particles", 200000):
                  return pyro.sample(
                      "x", dist.Stable(stability, skew, scale, loc)
                  )
  
      value = model()
      expected_moments = get_moments(value)
  
      reparam_model = poutine.reparam(model, {"x": Reparam()})
      trace = poutine.trace(reparam_model).get_trace()
      if Reparam is LatentStableReparam:
          assert isinstance(trace.nodes["x"]["fn"], MaskedDistribution)
          assert isinstance(
              trace.nodes["x"]["fn"].base_dist, dist.Delta
          )
      else:
          assert isinstance(trace.nodes["x"]["fn"], dist.Normal)
      trace.compute_log_prob()  # smoke test only
      value = trace.nodes["x"]["value"]
      actual_moments = get_moments(value)
      assert_close(actual_moments, expected_moments, atol=0.07)
  
      for actual_m, expected_m in zip(actual_moments, expected_moments):
          expected_grads = grad(
              expected_m.sum(), params, retain_graph=True
          )
          actual_grads = grad(actual_m.sum(), params, retain_graph=True)
          assert_close(actual_grads[0], expected_grads[0], atol=0.3)
          assert_close(
              actual_grads[1][skew != 0],
              expected_grads[1][skew != 0],
              atol=0.1,
          )
          assert_close(
              actual_grads[1][skew == 0],
              expected_grads[1][skew == 0],
              atol=0.3,
          )
          assert_close(actual_grads[2], expected_grads[2], atol=0.1)
          assert_close(actual_grads[3], expected_grads[3], atol=0.1)
  
file_path: pyro/tests/infer/reparam/test_stable.py
imports: import pytest
  import torch
  from scipy.stats import ks_2samp
  from torch.autograd import grad
  import pyro
  import pyro.distributions
  from pyro import poutine
  from pyro.distributions.torch_distribution import MaskedDistribution
  from pyro.infer import Trace_ELBO
  from pyro.infer.autoguide import AutoNormal
  from pyro.infer.reparam import LatentStableReparam, StableReparam, SymmetricStableReparam
  from tests.common import assert_close, xfail_param
  from .util import check_init_reparam
label_desc: Use image node_id 2 for calling a global function with example usage: test_stable(Reparam, shape) and returns: pyro