code: def plot_prediction(
      val_ts_tensor: torch.Tensor,
      predictions: torch.Tensor,
      context_length: int,
      lead_time: int = 1,
      start: int = 0,
      end: int = 500,
      fig: Optional[matplotlib.figure.Figure] = None,
  ):
      """
      Arguments
      ----------
      val_ts_tensor: Time series
      predictions: Prediction series
      context_length: Number of time steps to input
      lead_time: Number of time steps to predict ahead
      start: Index of time series at which to start plotting
      end: Index of time series at which to end plotting
      """
      end = np.minimum(end, len(predictions["lower"]))
  
      for key in predictions.keys():
          try:
              predictions[key] = np.array(
                  list(
                      map(
                          lambda x: x.detach().cpu().numpy(),
                          predictions[key],
                      )
                  )
              ).ravel()
          except Exception:
              # TODO: handle error
              pass
  
      if fig is None:
          fig = plt.figure(figsize=(16, 7))
      plt.plot(
          val_ts_tensor.cpu().flatten()[
              context_length + lead_time - 1 :
          ][start:end],
          "ko",
          markersize=2,
          label=r"$x_t$",
      )
      plt.plot(
          predictions["up_upper"][start:end],
          "r-",
          alpha=0.3,
          label=r"$z_{0.01}(t)|x_{t-1}$",
      )
      plt.plot(
          predictions["upper"][start:end],
          "y-",
          alpha=0.3,
          label=r"$z_{0.05}(t)|x_{t-1} \rightarrow \tau_t^{\rm{upper}}$",
      )
      plt.fill_between(
          np.arange(start, end),
          predictions["lower"][start:end],
          predictions["upper"][start:end],
          facecolor="y",
          alpha=0.3,
          label=None,
      )
      plt.plot(
          predictions["median"][start:end],
          "g",
          label=r"$z_{0.5\,}(t)|x_{t-1}$",
      )
      plt.plot(
          predictions["lower"][start:end],
          "y-",
          alpha=0.3,
          label=r"$z_{0.95}(t)|x_{t-1} \rightarrow \tau_t^{\rm{lower}}$",
      )
      plt.plot(
          predictions["low_lower"][start:end],
          "r-",
          alpha=0.3,
          label=r"$z_{0.99}(t)|x_{t-1}$",
      )
  
      plt.legend(loc="upper right")
  
      median = np.array(predictions["median"][start:end])
      true_val = np.array(
          val_ts_tensor.cpu().flatten()[
              context_length + lead_time - 1 :
          ][start:end]
      )
      MAE = np.mean(np.abs(median - true_val))
      plt.title(str(MAE))
  
      return fig
  
file_path: gluonts/src/gluonts/nursery/spliced_binned_pareto/training_functions.py
imports: from typing import Optional
  import matplotlib
  import matplotlib.pyplot
  import numpy
  import pandas
  import torch.nn
  import torch.optim
  from .distr_tcn import DistributionalTCN
label_desc: Use image node_id 3 for calling a global function with example usage: plot_prediction(val_ts_tensor, predictions, context_length, lead_time, start, end, fig) and returns: fig