docstr: For RegressionModels we create the lagged prediction data once per series using a moving window.
  With this, we can avoid having to recreate the tabular input data and call `model.predict()` for each
  forecastable index and series.
  Additionally, there is a dedicated subroutines for `last_points_only=True` and `last_points_only=False`.
  
  TODO: support forecast_horizon > output_chunk_length (auto-regression)
docstr_tok: ["For","RegressionModels","we","create","the","lagged","prediction","data","once","per","series","using","a","moving","window",".","With","this",",","we","can","avoid","having","to","recreate","the","tabular","input","data","and","call","`","model.predict","(",")","`","for","each","forecastable","index","and","series",".","Additionally",",","there","is","a","dedicated","subroutines","for","`","last_points_only=True","`","and","`","last_points_only=False","`",".","TODO",":","support","forecast_horizon",">","output_chunk_length","(","auto-regression",")"]
code: def _optimized_historical_forecasts(
      self,
      series: Optional[Sequence[TimeSeries]],
      past_covariates: Optional[Sequence[TimeSeries]] = None,
      future_covariates: Optional[Sequence[TimeSeries]] = None,
      num_samples: int = 1,
      start: Optional[Union[pd.Timestamp, float, int]] = None,
      start_format: Literal["position", "value"] = "value",
      forecast_horizon: int = 1,
      stride: int = 1,
      overlap_end: bool = False,
      last_points_only: bool = True,
      verbose: bool = False,
      show_warnings: bool = True,
      predict_likelihood_parameters: bool = False,
      **kwargs,
  ) -> Union[
      TimeSeries,
      List[TimeSeries],
      Sequence[TimeSeries],
      Sequence[List[TimeSeries]],
  ]:
      """
      For RegressionModels we create the lagged prediction data once per series using a moving window.
      With this, we can avoid having to recreate the tabular input data and call `model.predict()` for each
      forecastable index and series.
      Additionally, there is a dedicated subroutines for `last_points_only=True` and `last_points_only=False`.
  
      TODO: support forecast_horizon > output_chunk_length (auto-regression)
      """
      (
          series,
          past_covariates,
          future_covariates,
      ) = _process_historical_forecast_input(
          model=self,
          series=series,
          past_covariates=past_covariates,
          future_covariates=future_covariates,
          forecast_horizon=forecast_horizon,
          allow_autoregression=False,
      )
  
      # TODO: move the loop here instead of duplicated code in each sub-routine?
      if last_points_only:
          return _optimized_historical_forecasts_last_points_only(
              model=self,
              series=series,
              past_covariates=past_covariates,
              future_covariates=future_covariates,
              num_samples=num_samples,
              start=start,
              start_format=start_format,
              forecast_horizon=forecast_horizon,
              stride=stride,
              overlap_end=overlap_end,
              show_warnings=show_warnings,
              predict_likelihood_parameters=predict_likelihood_parameters,
              **kwargs,
          )
      else:
          return _optimized_historical_forecasts_all_points(
              model=self,
              series=series,
              past_covariates=past_covariates,
              future_covariates=future_covariates,
              num_samples=num_samples,
              start=start,
              start_format=start_format,
              forecast_horizon=forecast_horizon,
              stride=stride,
              overlap_end=overlap_end,
              show_warnings=show_warnings,
              predict_likelihood_parameters=predict_likelihood_parameters,
              **kwargs,
          )
  
imports: from collections import OrderedDict
  from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
  import numpy
  import pandas
  from sklearn.linear_model import LinearRegression
  from darts.logging import get_logger, raise_if, raise_if_not, raise_log
  from darts.models.forecasting.forecasting_model import GlobalForecastingModel
  from darts.timeseries import TimeSeries
  from darts.utils.data.tabularization import add_static_covariates_to_lagged_data, create_lagged_component_names, create_lagged_training_data
  from darts.utils.historical_forecasts import _check_optimizable_historical_forecasts_global_models, _optimized_historical_forecasts_all_points, _optimized_historical_forecasts_last_points_only, _process_historical_forecast_input
  from darts.utils.multioutput import MultiOutputRegressor
  from darts.utils.utils import _check_quantiles, get_single_series, seq2series, series2seq
label_desc: Use image node_id 22 for calling the RegressionModel obj's underlying member method code with example usage: obj._optimized_historical_forecasts(series, past_covariates, future_covariates, num_samples, start, start_format, forecast_horizon, stride, overlap_end, last_points_only, verbose, show_warnings, predict_likelihood_parameters) and returns: _optimized_historical_forecasts_last_points_only, _optimized_historical_forecasts_all_points