code: def test_weighted_sampler(frac_zero_weights: float) -> None:
      """
      We test the weighted sampler with weights that have varying fraction of
      zeros where positions for zero are randomly selected. Indices/Positions
      with zero weight should never be sampled.
  
      For the special case `frac_zero_weights = 1.0`, we make all the weights
      zero and expect that each of the indices is sampled with equal probability.
  
      Parameters
      ----------
      frac_zero_weights
          fraction of weights that are zeros.
      """
  
      # random unnormalized weights
      num_weights = 1000
      weights = np.random.random(num_weights)
  
      # randomly make some fraction of the weights zeros to test that they are
      # never sampled.
      if frac_zero_weights != 1.0:
          zeros_ix = np.random.randint(
              low=0,
              high=num_weights,
              size=int(num_weights * frac_zero_weights),
          )
      else:
          # For the special case `frac_zero_weights` we select all indices.
          zeros_ix = np.arange(num_weights)
      weights[zeros_ix] = 0.0
  
      num_samples = 100_000
      samples_ix = WeightedSampler.sample(weights, num_samples)
  
      # empirical probabilities for each index
      counts_ix, _ = np.histogram(
          samples_ix, bins=range(num_weights + 1)
      )
      probs_ix = counts_ix / sum(counts_ix)
  
      true_prob_ix = (
          weights / sum(weights)
          if frac_zero_weights != 1.0
          else np.ones_like(weights) / num_weights
      )
  
      np.testing.assert_almost_equal(
          probs_ix,
          true_prob_ix,
          2,
          "Empirical distribution does not match sampling distribution",
      )
  
      if frac_zero_weights != 1.0:
          assert all(
              probs_ix[zeros_ix] == 0.0
          ), "Indices with sampling weight zero are sampled!"
  
file_path: gluonts/test/model/npts/test_npts.py
imports: from typing import Optional
  import numpy
  import pandas
  import pytest
  from gluonts.dataset.common import DataEntry, Dataset
  from gluonts.exceptions import GluonTSDataError
  from gluonts.model.npts import KernelType, NPTSPredictor
  from gluonts.model.npts._weighted_sampler import WeightedSampler
label_desc: Use image node_id 7 for calling a global function with example usage: test_weighted_sampler(frac_zero_weights) without return types