code: def test_backtest_forecasting(self):
      linear_series = lt(length=50)
      linear_series_int = TimeSeries.from_values(linear_series.values())
      linear_series_multi = linear_series.stack(linear_series)
  
      # univariate model + univariate series
      score = NaiveDrift().backtest(
          linear_series,
          start=pd.Timestamp("20000201"),
          forecast_horizon=3,
          metric=r2_score,
      )
      assert score == 1.0
  
      # univariate model + univariate series + historical_forecasts precalculated
      forecasts = NaiveDrift().historical_forecasts(
          linear_series,
          start=pd.Timestamp("20000201"),
          forecast_horizon=3,
          last_points_only=False,
      )
      precalculated_forecasts_score = NaiveDrift().backtest(
          linear_series,
          historical_forecasts=forecasts,
          start=pd.Timestamp("20000201"),
          forecast_horizon=3,
          metric=r2_score,
      )
      assert score == precalculated_forecasts_score
  
      # very large train length should not affect the backtest
      score = NaiveDrift().backtest(
          linear_series,
          train_length=10000,
          start=pd.Timestamp("20000201"),
          forecast_horizon=3,
          metric=r2_score,
      )
      assert score == 1.0
  
      # using several metric function should not affect the backtest
      score = NaiveDrift().backtest(
          linear_series,
          train_length=10000,
          start=pd.Timestamp("20000201"),
          forecast_horizon=3,
          metric=[r2_score, mape],
      )
      np.testing.assert_almost_equal(score, np.array([1.0, 0.0]))
  
      # window of size 2 is too small for naive drift
      with pytest.raises(ValueError):
          NaiveDrift().backtest(
              linear_series,
              train_length=2,
              start=pd.Timestamp("20000201"),
              forecast_horizon=3,
              metric=r2_score,
          )
  
      # test that it also works for time series that are not Datetime-indexed
      score = NaiveDrift().backtest(
          linear_series_int,
          start=0.7,
          forecast_horizon=3,
          metric=r2_score,
      )
      assert score == 1.0
  
      with pytest.raises(ValueError):
          NaiveDrift().backtest(
              linear_series,
              start=pd.Timestamp("20000218"),
              forecast_horizon=3,
              overlap_end=False,
          )
      NaiveDrift().backtest(
          linear_series,
          start=pd.Timestamp("20000217"),
          forecast_horizon=3,
      )
      NaiveDrift().backtest(
          linear_series,
          start=pd.Timestamp("20000218"),
          forecast_horizon=3,
          overlap_end=True,
      )
  
      # Using forecast_horizon default value
      NaiveDrift().backtest(
          linear_series, start=pd.Timestamp("20000216")
      )
      NaiveDrift().backtest(
          linear_series,
          start=pd.Timestamp("20000217"),
          overlap_end=True,
      )
  
      # Using an int or float value for start
      NaiveDrift().backtest(linear_series, start=30)
      NaiveDrift().backtest(linear_series, start=0.7, overlap_end=True)
  
      # Set custom train window length
      NaiveDrift().backtest(linear_series, train_length=10, start=30)
  
      # Using invalid start and/or forecast_horizon values
      with pytest.raises(ValueError):
          NaiveDrift().backtest(
              linear_series, start=0.7, forecast_horizon=-1
          )
      with pytest.raises(ValueError):
          NaiveDrift().backtest(
              linear_series, start=-0.7, forecast_horizon=1
          )
  
      with pytest.raises(ValueError):
          NaiveDrift().backtest(linear_series, start=100)
      with pytest.raises(ValueError):
          NaiveDrift().backtest(linear_series, start=1.2)
      with pytest.raises(TypeError):
          NaiveDrift().backtest(linear_series, start="wrong type")
      with pytest.raises(ValueError):
          NaiveDrift().backtest(
              linear_series, train_length=0, start=0.5
          )
      with pytest.raises(TypeError):
          NaiveDrift().backtest(
              linear_series, train_length=1.2, start=0.5
          )
      with pytest.raises(TypeError):
          NaiveDrift().backtest(
              linear_series, train_length="wrong type", start=0.5
          )
  
      with pytest.raises(ValueError):
          NaiveDrift().backtest(
              linear_series,
              start=49,
              forecast_horizon=2,
              overlap_end=False,
          )
  
      # univariate model + multivariate series
      with pytest.raises(ValueError):
          FFT().backtest(
              linear_series_multi,
              start=pd.Timestamp("20000201"),
              forecast_horizon=3,
          )
  
      # multivariate model + univariate series
      if TORCH_AVAILABLE:
          tcn_model = TCNModel(
              input_chunk_length=12,
              output_chunk_length=1,
              batch_size=1,
              n_epochs=1,
              **tfm_kwargs,
          )
          # cannot perform historical forecasts with `retrain=False` and untrained model
          with pytest.raises(ValueError):
              _ = tcn_model.historical_forecasts(
                  linear_series,
                  start=pd.Timestamp("20000125"),
                  forecast_horizon=3,
                  verbose=False,
                  last_points_only=True,
                  retrain=False,
              )
  
          pred = tcn_model.historical_forecasts(
              linear_series,
              start=pd.Timestamp("20000125"),
              forecast_horizon=3,
              verbose=False,
              last_points_only=True,
          )
          assert pred.width == 1
          assert pred.end_time() == linear_series.end_time()
  
          # multivariate model + multivariate series
          # historical forecasts doesn't overwrite model object -> we can use different input dimensions
          tcn_model.backtest(
              linear_series_multi,
              start=pd.Timestamp("20000125"),
              forecast_horizon=3,
              verbose=False,
          )
  
          # univariate model
          tcn_model = TCNModel(
              input_chunk_length=12,
              output_chunk_length=1,
              batch_size=1,
              n_epochs=1,
              **tfm_kwargs,
          )
          tcn_model.fit(linear_series, verbose=False)
          # univariate fitted model + multivariate series
          with pytest.raises(ValueError):
              tcn_model.backtest(
                  linear_series_multi,
                  start=pd.Timestamp("20000125"),
                  forecast_horizon=3,
                  verbose=False,
                  retrain=False,
              )
  
          tcn_model = TCNModel(
              input_chunk_length=12,
              output_chunk_length=3,
              batch_size=1,
              n_epochs=1,
              **tfm_kwargs,
          )
          pred = tcn_model.historical_forecasts(
              linear_series_multi,
              start=pd.Timestamp("20000125"),
              forecast_horizon=3,
              verbose=False,
              last_points_only=True,
          )
          assert pred.width == 2
          assert pred.end_time() == linear_series.end_time()
  
imports: import random
  from itertools import product
  import numpy
  import pandas
  import pytest
  from darts import TimeSeries
  from darts.datasets import AirPassengersDataset, MonthlyMilkDataset
  from darts.logging import get_logger
  from darts.metrics import mape, r2_score
  from darts.models import ARIMA, FFT, ExponentialSmoothing, NaiveDrift, NaiveSeasonal, Theta
  from darts.tests.conftest import tfm_kwargs
  from darts.utils.timeseries_generation import gaussian_timeseries
  from darts.utils.timeseries_generation import linear_timeseries
  from darts.utils.timeseries_generation import random_walk_timeseries
  from darts.utils.timeseries_generation import sine_timeseries
label_desc: Use image node_id 1 for calling the TestBacktesting obj's underlying member method code with example usage: obj.test_backtest_forecasting() without return types