docstr: Test grouped numerical predictions, with anomalies and forecast horizon > 1 
docstr_tok: ["Test","grouped","numerical","predictions",",","with","anomalies","and","forecast","horizon",">","1"]
code: def test_0_time_series_grouped_regression(self):
      """Test grouped numerical predictions, with anomalies and forecast horizon > 1"""
      data = pd.read_csv("tests/data/arrivals.csv")
      train, test = self.split_arrivals(data, grouped=True)
      target = "Traffic"
      order_by = "T"
      window = 5
  
      for horizon in (1, 2):
          jai = json_ai_from_problem(
              train,
              ProblemDefinition.from_dict(
                  {
                      "target": target,
                      "time_aim": 30,
                      "anomaly_detection": True,
                      "timeseries_settings": {
                          "use_previous_target": True,
                          "allow_incomplete_history": True,
                          "group_by": ["Country"],
                          "horizon": horizon,
                          "order_by": order_by,
                          "period_intervals": (("daily", 7),),
                          "window": window,
                      },
                  }
              ),
          )
          sktime_mixer_idx = None
          for i, mixer in enumerate(jai.model["args"]["submodels"]):
              if mixer["module"] == "SkTime":
                  sktime_mixer_idx = i
  
          if sktime_mixer_idx:
              jai.model["args"]["submodels"][sktime_mixer_idx] = {
                  "module": "SkTime",
                  "args": {
                      "stop_after": "$problem_definition.seconds_per_mixer",
                      "horizon": "$problem_definition.timeseries_settings.horizon",
                      "model_path": "'trend.TrendForecaster'",  # use a cheap forecaster
                      "hyperparam_search": False,  # disable this as it's expensive and covered in test #3
                  },
              }
  
          code = code_from_json_ai(jai)
          pred = predictor_from_code(code)
  
          # Test with a short time aim
          train_and_check_time_aim(pred, train, ignore_time_aim=True)
          preds = pred.predict(test)
          self.check_ts_prediction_df(preds, horizon, [order_by])
  
          # test allowed incomplete history
          preds = pred.predict(test[: window - 1])
          self.check_ts_prediction_df(preds, horizon, [order_by])
  
          # test inferring mode, check timestamps are further into the future than test dates
          test["__mdb_forecast_offset"] = 1
          preds = pred.predict(test)
          self.check_ts_prediction_df(preds, horizon, [order_by])
  
          latest_timestamp = (
              pd.to_datetime(test[order_by], utc=True).max().timestamp()
          )
          for idx, row in preds.iterrows():
              row[f"order_{order_by}"] = (
                  [row[f"order_{order_by}"]]
                  if horizon == 1
                  else row[f"order_{order_by}"]
              )
              for timestamp in row[f"order_{order_by}"]:
                  assert timestamp > latest_timestamp
  
          # Check custom ICP params
          test.pop("__mdb_forecast_offset")
          preds = pred.predict(
              test, {"fixed_confidence": 0.01, "anomaly_cooldown": 100}
          )
          if horizon == 1:
              assert set([v for v in preds["confidence"].values]) == {
                  0.01
              }
          else:
              assert all(
                  [
                      all([v == 0.01 for v in f])
                      for f in preds["confidence"].values
                  ]
              )
          assert pred.pred_args.anomaly_cooldown == 100
  
imports: import random
  from datetime import datetime
  from datetime import timedelta
  import unittest
  import numpy
  import pandas
  from typing import List
  from scipy import signal
  from sklearn.metrics import r2_score
  from lightwood.api.types import ProblemDefinition
  from tests.utils.timing import train_and_check_time_aim
  from sktime.forecasting.base import ForecastingHorizon
  from sktime.forecasting.statsforecast import StatsForecastAutoARIMA
  from dataprep_ml.splitters import stratify
  from lightwood.api.high_level import json_ai_from_problem, code_from_json_ai, predictor_from_code, predictor_from_problem, predictor_from_json_ai
  from lightwood.mixer.sktime import SkTime
label_desc: Use image node_id 3 for calling the TestTimeseries obj's underlying member method code with example usage: obj.test_0_time_series_grouped_regression() without return types