code: def test_climatological_forecaster(
      freq: str,
      history_length: int,
      num_seasons: int,
      use_seasonal_model: bool,
      context_length_frac: float,
  ) -> None:
      """
      Here we test Climatological forecaster both seasonal and non-seasonal
      variants for various frequencies.
  
      We further parametrize the test with `context_length_frac` parameter which
      indicates what fraction of the history should actually be used including
      the case where `context_length` is larger than the history length.
  
      In particular, we check that:
  
      1. predictions are uniformly sampled over the entire past for non-seasonal
         variant and only over the past seasons for the seasonal variant;
      2. 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?
      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 Climatological
      # 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,
          use_seasonal_model=use_seasonal_model,
          kernel_type=KernelType.uniform,
      )
  
      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):
          targets_prev_seasons = train_targets[
              range(t, len(train_targets), num_seasons)
          ]
  
          # Predictions must have come from the past seasons only
          assert set(forecast.samples[:, t]).issubset(
              targets_prev_seasons
          ), (
              f"Predictions for {seasonal_str} Climatological forecaster are "
              f"not generated from the target values of past {targets_str}.\n"
              f"Past {targets_str}: {targets_prev_seasons}\n"
              f"Predictions: {set(forecast.samples[:, t])}"
          )
  
          # Predictions must have been uniformly sampled from the targets in
          # the previous seasons
          prediction_dist, _ = np.histogram(
              forecast.samples[:, t],
              np.append(targets_prev_seasons, np.inf),
          )
          prediction_dist = prediction_dist / sum(prediction_dist)
  
          expected_dist = np.ones_like(targets_prev_seasons) / len(
              targets_prev_seasons
          )
  
          np.testing.assert_almost_equal(
              prediction_dist,
              expected_dist,
              1,
              f"Predictions of {seasonal_str} Climatological forecaster are not "
              f"uniformly sampled from past {targets_str}\n"
              f"Expected distribution over the past "
              f"{targets_str}: {expected_dist}\n"
              f"Prediction distribution: {prediction_dist}\n",
          )
  
          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),
              ), (
                  f"Predictions of Climatological forecaster are sampled from "
                  f"targets outside the context length.\n"
                  f"Targets outside the context length: "
                  f"{targets_outside_context}\n"
                  f"Predictions: {set(forecast.samples[:, t])}\n"
              )
  
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 2 for calling a global function with example usage: test_climatological_forecaster(freq, history_length, num_seasons, use_seasonal_model, context_length_frac) without return types