docstr: regression model is deterministic (default) but the forecasting models are
  probabilistic and they can be sampled to train the regression model.
docstr_tok: ["regression","model","is","deterministic","(","default",")","but","the","forecasting","models","are","probabilistic","and","they","can","be","sampled","to","train","the","regression","model","."]
code: def test_stochastic_training_regression_ensemble_model(self):
      """
      regression model is deterministic (default) but the forecasting models are
      probabilistic and they can be sampled to train the regression model.
      """
      quantiles = [0.25, 0.5, 0.75]
  
      # cannot sample deterministic forecasting models
      with pytest.raises(ValueError):
          RegressionEnsembleModel(
              forecasting_models=[
                  self.get_deterministic_global_model(lags=[-1, -3]),
                  self.get_deterministic_global_model(lags=[-2, -4]),
              ],
              regression_train_n_points=50,
              regression_train_num_samples=500,
          )
  
      # must use apprioriate reduction method
      with pytest.raises(ValueError):
          RegressionEnsembleModel(
              forecasting_models=[
                  self.get_probabilistic_global_model(
                      lags=[-1, -3], quantiles=quantiles
                  ),
                  self.get_probabilistic_global_model(
                      lags=[-2, -4], quantiles=quantiles
                  ),
              ],
              regression_train_n_points=50,
              regression_train_num_samples=500,
              regression_train_samples_reduction="wrong",
          )
  
      # by default, does not reduce samples and convert them to components
      ensemble_model_mean = RegressionEnsembleModel(
          forecasting_models=[
              self.get_probabilistic_global_model(
                  lags=[-1, -3], quantiles=quantiles
              ),
              self.get_probabilistic_global_model(
                  lags=[-2, -4], quantiles=quantiles
              ),
          ],
          regression_train_n_points=50,
          regression_train_num_samples=500,
          regression_train_samples_reduction="mean",
      )
  
      ensemble_model_median = RegressionEnsembleModel(
          forecasting_models=[
              self.get_probabilistic_global_model(
                  lags=[-1, -3], quantiles=quantiles
              ),
              self.get_probabilistic_global_model(
                  lags=[-2, -4], quantiles=quantiles
              ),
          ],
          regression_train_n_points=50,
          regression_train_num_samples=500,
      )
      assert ensemble_model_median.train_samples_reduction == "median"
  
      ensemble_model_0_5_quantile = RegressionEnsembleModel(
          forecasting_models=[
              self.get_probabilistic_global_model(
                  lags=[-1, -3], quantiles=quantiles
              ),
              self.get_probabilistic_global_model(
                  lags=[-2, -4], quantiles=quantiles
              ),
          ],
          regression_train_n_points=50,
          regression_train_num_samples=500,
          regression_train_samples_reduction=0.5,
      )
  
      train, val = self.ts_sum1.split_after(0.9)
      ensemble_model_mean.fit(train)
      ensemble_model_median.fit(train)
      ensemble_model_0_5_quantile.fit(train)
  
      pred_mean_training = ensemble_model_mean.predict(len(val))
      pred_median_training = ensemble_model_median.predict(len(val))
      pred_0_5_qt_training = ensemble_model_0_5_quantile.predict(
          len(val)
      )
  
      assert pred_median_training == pred_0_5_qt_training
      assert (
          pred_mean_training.all_values().shape
          == pred_median_training.all_values().shape
      )
  
      # deterministic regression model -> deterministic ensemble
      with pytest.raises(ValueError):
          ensemble_model_mean.predict(len(val), num_samples=100)
      with pytest.raises(ValueError):
          ensemble_model_median.predict(len(val), num_samples=100)
      with pytest.raises(ValueError):
          ensemble_model_0_5_quantile.predict(len(val), num_samples=100)
  
      # possible to use very small regression_train_num_samples
      ensemble_model_mean_1_sample = RegressionEnsembleModel(
          forecasting_models=[
              self.get_probabilistic_global_model(
                  lags=[-1, -3], quantiles=quantiles
              ),
              self.get_probabilistic_global_model(
                  lags=[-2, -4], quantiles=quantiles
              ),
          ],
          regression_train_n_points=50,
          regression_train_num_samples=1,
      )
      ensemble_model_mean_1_sample.fit(train)
      ensemble_model_mean_1_sample.predict(len(val))
  
      # multi-series support
      ensemble_model_median.fit([train, train + 100])
      ensemble_model_mean.predict(len(val), series=train)
  
imports: from typing import List, Union
  import numpy
  import pandas
  import pytest
  from sklearn.ensemble import RandomForestRegressor
  from sklearn.linear_model import LinearRegression
  from darts import TimeSeries
  from darts.logging import get_logger
  from darts.metrics import mape, rmse
  from darts.models import LinearRegressionModel, NaiveDrift, NaiveSeasonal, RandomForest, RegressionEnsembleModel, RegressionModel, Theta
  from darts.tests.conftest import tfm_kwargs
  from darts.tests.models.forecasting.test_ensemble_models import _make_ts
  from darts.tests.models.forecasting.test_regression_models import train_test_split
  from darts.utils import timeseries_generation
label_desc: Use image node_id 23 for calling the TestRegressionEnsembleModels obj's underlying member method code with example usage: obj.test_stochastic_training_regression_ensemble_model() without return types