code: def test_npts_forecaster(
      freq: str,
      history_length: int,
      num_seasons: int,
      use_seasonal_model: bool,
      feature_scale: float,
      min_frac_samples_from_seasons: float,
      context_length_frac: Optional[float],
  ) -> None:
      """
      Here we test both seasonal (num_seasons=24) and non-seasonal
      (num_seasons=1) variants of NPTS for various frequencies.
  
      We further parametrize the test with `context_length_frac` parameter which
      indicates what fraction of the history should actually be used.
  
      In particular, we check that the
  
      1. the predictions must come from past seasons exclusively with high
         probability for large value of `feature_scale`
  
      2. predictions are sampled according to exponentially decaying weights over
         the targets from past seasons or whole of training history depending on
         the flag `use_seasonal_model`
  
      3. predictions are sampled from time points that are not seasons as well
         for small value of `feature_scale`
  
      4. predictions do not come from the targets outsides of the context length,
         if `context_length` is present.
  
      Parameters
      ----------
      freq
          frequency of the time series
      history_length
          length of the time series to be generated for tests
      num_seasons
          number of seasons present in a given frequency
      use_seasonal_model
          use the seasonal variant?
      feature_scale
          scale for the seasonal features to enforce strict sampling over the
          past seasons
      min_frac_samples_from_seasons
          the minimum threshold for fraction of times the predictions should come
          exclusively from past seasons
      context_length_frac
          fraction of history length that should be used as context length
      """
  
      train_ts = get_test_data(history_length=history_length, freq=freq)
  
      # For seasonal variant we check the predictions of NPTS forecaster for
      # all seasons.
      # Non-seasonal variant is modeled in the tests as `num_seasons = 1`, and
      # the predictions are checked for only one step.
      num_seasons = num_seasons if use_seasonal_model else 1
      pred_length = num_seasons
  
      context_length = (
          int(context_length_frac * history_length)
          if context_length_frac is not None
          else None
      )
      predictor = NPTSPredictor(
          prediction_length=pred_length,
          context_length=context_length,
          kernel_type=KernelType.exponential,
          feature_scale=feature_scale,
          use_seasonal_model=use_seasonal_model,
      )
  
      dataset = [
          {
              "start": pd.Period(train_ts.index[0], freq=freq),
              "target": train_ts.values,
          }
      ]
  
      # validate that the predictor works with targets with NaNs
      _test_nans_in_target(predictor, dataset)
  
      forecast = next(predictor.predict(dataset, num_samples=2000))
  
      train_targets = (
          train_ts.values
          if context_length is None
          else train_ts.values[-min(history_length, context_length) :]
      )
      targets_outside_context = (
          None
          if context_length is None or context_length >= history_length
          else train_ts.values[:context_length]
      )
      targets_str = "seasons" if use_seasonal_model else "targets"
      seasonal_str = "seasonal" if use_seasonal_model else ""
      for t in range(pred_length):
          prev_seasons_ix = range(t, len(train_targets), num_seasons)
  
          # Prediction distribution over all the training targets
          prediction_dist, _ = np.histogram(
              forecast.samples[:, t], np.append(train_targets, np.inf)
          )
          prediction_dist = prediction_dist / sum(prediction_dist)
  
          # The fraction of times the targets from past seasons are sampled
          # exclusively should be above some threshold.
          assert (
              sum(prediction_dist[prev_seasons_ix])
              > min_frac_samples_from_seasons
          ), (
              f"Predictions of {seasonal_str} NPTS are not sampled from past "
              f"{targets_str} enough number of times.\n"
              f"Expected frequency over past {targets_str}: "
              f"{min_frac_samples_from_seasons}\n"
              f"Sampled frequency: {sum(prediction_dist[prev_seasons_ix])}"
          )
  
          if feature_scale == 1.0:
              # Time index feature and seasonal features are given equal
              # importance so we expect to see some predictions
              # coming outside of past seasons.
              non_seasons_ix = list(
                  set(range(len(train_targets))) - set(prev_seasons_ix)
              )
              if non_seasons_ix:
                  assert sum(prediction_dist[non_seasons_ix]) > 0.0, (
                      "Predictions of {seasonal_str} NPTS are expected to come "
                      "from targets not in the previous seasons"
                      "for small value of feature_scale: {feature_scale}"
                  )
  
          if feature_scale == 1000.0:
              # Here we sample mostly from past seasons. In this case, the past
              # seasons must be sampled with exponentially
              # decaying weights which depend only on the time index
              # feature: f(t) = t / (train_length + pred_length)
              distance_to_prev_seasons = np.arange(
                  len(train_targets) + 1, 1, -num_seasons
              ) / (len(train_targets) + pred_length)
              expected_dist_seasons = np.exp(-distance_to_prev_seasons)
              expected_dist_seasons /= sum(expected_dist_seasons)
  
              prediction_dist_seasons = prediction_dist[prev_seasons_ix]
              prediction_dist_seasons /= sum(prediction_dist_seasons)
  
              np.testing.assert_almost_equal(
                  prediction_dist_seasons,
                  expected_dist_seasons,
                  1,
                  f"Predictions of {seasonal_str} NPTS are not sampled with "
                  f"exponentially decaying weights over the past "
                  f"{targets_str}.\nExpected distribution over the past "
                  f"{targets_str}: {expected_dist_seasons}\n"
                  f"Prediction_dist: {prediction_dist_seasons}",
              )
  
          if targets_outside_context is not None:
              # Predictions should never be from the targets outside the context
              # length
              assert not set.intersection(
                  set(forecast.samples[:, t]),
                  set(targets_outside_context),
              ), (
                  "Predictions of NPTS forecaster are sampled from targets "
                  "outside the context length.\n"
                  f"Targets outside the context length: "
                  f"{targets_outside_context}\n"
                  f"Predictions: {set(forecast.samples[:, t])}"
              )
  
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 3 for calling a global function with example usage: test_npts_forecaster(freq, history_length, num_seasons, use_seasonal_model, feature_scale, min_frac_samples_from_seasons, context_length_frac) without return types