def test_quantiles_from_model(self, config):
        """Tests residuals from quantile regression model works for both direct likelihood parameter prediction or
        sampled prediction by giving the correct metrics kwargs."""
        metric, lpo = config

        is_interval_metric = metric.__name__ == "iw"

        # multi-quantile metrics yield more components
        q = [0.05, 0.50, 0.95]
        q_interval = [(0.05, 0.50), (0.50, 0.95), (0.05, 0.95)]

        y = lt(length=20)
        y = y.stack(y + 1.0)
        if not is_interval_metric:
            q_comp_names_expected = pd.Index(
                likelihood_component_names(
                    components=y.components,
                    parameter_names=quantile_names(q),
                )
            )
        else:
            q_comp_names_expected = pd.Index(
                likelihood_component_names(
                    components=y.components,
                    parameter_names=quantile_interval_names(q_interval),
                )
            )
        y = [y, y]
        metric_kwargs = {"component_reduction": None}
        if not is_interval_metric:
            metric_kwargs["q"] = q
        else:
            metric_kwargs["q_interval"] = q_interval

        icl = 3
        model = LinearRegressionModel(
            lags=icl, output_chunk_length=1, likelihood="quantile", quantiles=q
        )
        model.fit(y)

        # quantile forecasts
        bts = model.residuals(
            series=y,
            forecast_horizon=1,
            metric=metric,
            last_points_only=lpo,
            metric_kwargs=metric_kwargs,
            predict_likelihood_parameters=True,
            retrain=False,
        )
        assert isinstance(bts, list) and len(bts) == 2
        if not lpo:
            bts = [concatenate(bt, axis=0) for bt in bts]

        # `ae` with time and component reduction is equal to `mae` with component reduction
        shape_expected = (len(y[0]) - icl, len(q) * y[0].n_components)
        for bt in bts:
            assert bt.shape[:2] == shape_expected
            assert bt.components.equals(q_comp_names_expected)

        # probabilistic forecasts
        bts_prob = model.residuals(
            series=y,
            forecast_horizon=1,
            metric=metric,
            last_points_only=lpo,
            metric_kwargs=metric_kwargs,
            predict_likelihood_parameters=False,
            num_samples=1000,
            retrain=False,
        )
        assert isinstance(bts_prob, list) and len(bts_prob) == 2
        if not lpo:
            bts_prob = [concatenate(bt, axis=0) for bt in bts_prob]
        for bt_p, bt_q in zip(bts_prob, bts):
            assert bt_p.shape == bt_q.shape
            assert bt_p.components.equals(bt_q.components)
            # check that the results are similar
            assert np.abs(bt_p.all_values() - bt_q.all_values()).max() < 0.1

        # single quantile
        q_single = 0.05
        q_interval_single = (0.05, 0.50)
        metric_kwargs = {"component_reduction": None}
        if not is_interval_metric:
            metric_kwargs["q"] = q_single
        else:
            metric_kwargs["q_interval"] = q_interval_single
        bts = model.residuals(
            series=y,
            forecast_horizon=1,
            metric=metric,
            last_points_only=lpo,
            metric_kwargs=metric_kwargs,
            predict_likelihood_parameters=True,
            retrain=False,
        )
        assert isinstance(bts, list) and len(bts) == 2
        if not lpo:
            bts = [concatenate(bt, axis=0) for bt in bts]

        # `ae` with time and component reduction is equal to `mae` with component reduction
        shape_expected = (len(y[0]) - icl, y[0].n_components)
        for bt in bts:
            assert bt.shape[:2] == shape_expected
            assert bt.components.equals(
                pd.Index(
                    likelihood_component_names(
                        y[0].components,
                        parameter_names=(
                            quantile_names([q_single])
                            if not is_interval_metric
                            else quantile_interval_names([q_interval_single])
                        ),
                    )
                )
            )

        # wrong quantile
        q_wrong = [0.99]
        q_interval_wrong = (0.05, 0.99)
        metric_kwargs = {"component_reduction": None}
        if not is_interval_metric:
            metric_kwargs["q"] = q_wrong
        else:
            metric_kwargs["q_interval"] = q_interval_wrong
        with pytest.raises(ValueError) as exc:
            _ = model.residuals(
                series=y,
                forecast_horizon=1,
                metric=metric,
                last_points_only=lpo,
                metric_kwargs=metric_kwargs,
                predict_likelihood_parameters=True,
                retrain=False,
            )
        assert str(exc.value).startswith(
            f"Computing a metric with quantile(s) "
            f"`q={'[0.99]' if not is_interval_metric else '[0.05 0.99]'}` is only supported"
        )
Tests residuals from quantile regression model works for both direct likelihood parameter prediction or
        sampled prediction by giving the correct metrics kwargs.
darts/tests/models/forecasting/test_residuals.py