Skip to content

check_search_refit_false_no_forecaster

yohou.testing.search.check_search_refit_false_no_forecaster(search_cv, y, X_actual=None, forecasting_horizon=3, X_future=None, X_forecast=None)

Check refit=False doesn't create best_forecaster_.

Parameters

Name Type Description Default
search_cv BaseSearchCV

Unfitted search CV instance with refit=False

required
y DataFrame

Training target data

required
X_actual DataFrame

Training features

None
forecasting_horizon int

Number of steps ahead to forecast

3

Raises

Type Description
AssertionError

If best_forecaster_ is created when refit=False

Source Code

Show/Hide source
def check_search_refit_false_no_forecaster(
    search_cv,
    y: pl.DataFrame,
    X_actual: pl.DataFrame | None = None,
    forecasting_horizon: int = 3,
    X_future: pl.DataFrame | None = None,
    X_forecast: pl.DataFrame | None = None,
) -> None:
    """Check refit=False doesn't create best_forecaster_.

    Parameters
    ----------
    search_cv : BaseSearchCV
        Unfitted search CV instance with refit=False
    y : pl.DataFrame
        Training target data
    X_actual : pl.DataFrame, optional
        Training features
    forecasting_horizon : int, default=3
        Number of steps ahead to forecast

    Raises
    ------
    AssertionError
        If best_forecaster_ is created when refit=False

    """
    search_cv_clone = clone(search_cv)

    # Temporarily set refit=False if not already
    original_refit = search_cv_clone.refit
    search_cv_clone.refit = False

    search_cv_clone.fit(y, X_actual, forecasting_horizon=forecasting_horizon, X_future=X_future, X_forecast=X_forecast)

    # Should have results but no best_forecaster_
    assert hasattr(search_cv_clone, "best_params_"), "fit() should set best_params_ even when refit=False"
    assert hasattr(search_cv_clone, "best_score_"), "fit() should set best_score_ even when refit=False"
    assert not hasattr(search_cv_clone, "best_forecaster_"), "fit() should NOT set best_forecaster_ when refit=False"
    assert not hasattr(search_cv_clone, "refit_time_"), "fit() should NOT set refit_time_ when refit=False"

    # predict() should not be available
    try:
        search_cv_clone.predict(forecasting_horizon=1)
        raise AssertionError(f"{search_cv_clone.__class__.__name__} should not have predict() when refit=False")
    except AttributeError:
        # Expected behavior
        pass

    # Restore original refit value
    search_cv_clone.refit = original_refit