<a id="online-learning"></a>

<a id="id1"></a>

# Online Learning

`skfolio` provides dedicated online utilities for estimators that support
`partial_fit`. By updating a single stateful estimator incrementally rather than
refitting from scratch at every split, online evaluation is significantly faster than
standard cross-validation. These utilities cover stateful walk-forward evaluation,
online covariance forecast diagnostics, and online hyper-parameter tuning.

Examples of supported estimators include
[`EWMu`](https://skfolio.org/generated/skfolio.moments.EWMu.html.md#skfolio.moments.EWMu), [`EWCovariance`](https://skfolio.org/generated/skfolio.moments.EWCovariance.html.md#skfolio.moments.EWCovariance),
[`RegimeAdjustedEWCovariance`](https://skfolio.org/generated/skfolio.moments.RegimeAdjustedEWCovariance.html.md#skfolio.moments.RegimeAdjustedEWCovariance),
[`EmpiricalPrior`](https://skfolio.org/generated/skfolio.prior.EmpiricalPrior.html.md#skfolio.prior.EmpiricalPrior) and portfolio
optimizers such as [`MeanRisk`](https://skfolio.org/generated/skfolio.optimization.MeanRisk.html.md#skfolio.optimization.MeanRisk) when they embed
incremental moment estimators through a prior estimator.

Online learning is also where native NaN-aware estimators are especially useful:
they can update from available observations while preserving estimator state. Pipeline
based pre-selection and imputation are not currently available in `skfolio` online
learning workflows. See [Missing Data and Changing Universes](https://skfolio.org/user_guide/data_representation.html.md#missing-data)
for details.

<a id="how-online-evaluation-works"></a>

## How Online Evaluation Works

The online utilities all follow the same stateful evaluation pattern:

1. Clone the estimator once, starting from a clean unfitted state.
2. Initialize it on the first `warmup_size` observations with `partial_fit`.
3. Evaluate on the next test window out-of-sample, with optional purging
   between the data seen by the estimator and the test window.
4. Update the same estimator with the newly observed data.
5. Repeat until the end of the sample.

This differs from standard cross-validation, where each split fits an
independent estimator clone. In the online setting, the estimator state is
carried forward through time.

<a id="non-predictor-estimators-versus-portfolio-optimizers"></a>

## Non-Predictor Estimators Versus Portfolio Optimizers

The online API distinguishes between non-predictor estimators and portfolio
optimizers.

* **Non-predictor estimators** such as covariance, expected-return, and prior
  estimators do not implement `predict`. Their scores are computed from the
  fitted estimator and the current test window using callables such as
  `scorer(estimator, X_test)`. When using
  [`make_scorer`](https://skfolio.org/generated/skfolio.metrics.make_scorer.html.md#skfolio.metrics.make_scorer), the appropriate form is
  `make_scorer(..., response_method=None)`.
* **Portfolio optimization estimators** such as
  [`MeanRisk`](https://skfolio.org/generated/skfolio.optimization.MeanRisk.html.md#skfolio.optimization.MeanRisk) are evaluated by collecting the
  out-of-sample predictions into a
  [`MultiPeriodPortfolio`](https://skfolio.org/generated/skfolio.portfolio.MultiPeriodPortfolio.html.md#skfolio.portfolio.MultiPeriodPortfolio). Measures are then computed
  on that aggregate portfolio. In that case, scoring uses
  [`BaseMeasure`](https://skfolio.org/generated/skfolio.measures.BaseMeasure.html.md#skfolio.measures.BaseMeasure) enums directly rather than
  [`make_scorer`](https://skfolio.org/generated/skfolio.metrics.make_scorer.html.md#skfolio.metrics.make_scorer).

Accordingly, [`online_predict`](https://skfolio.org/generated/skfolio.model_selection.online_predict.html.md#skfolio.model_selection.online_predict) is restricted to
portfolio optimizers, while [`online_score`](https://skfolio.org/generated/skfolio.model_selection.online_score.html.md#skfolio.model_selection.online_score) and
[`OnlineGridSearch`](https://skfolio.org/generated/skfolio.model_selection.OnlineGridSearch.html.md#skfolio.model_selection.OnlineGridSearch) /
[`OnlineRandomizedSearch`](https://skfolio.org/generated/skfolio.model_selection.OnlineRandomizedSearch.html.md#skfolio.model_selection.OnlineRandomizedSearch) accept both categories.

<a id="online-versus-standard-cross-validation"></a>

## Online Versus Standard Cross-Validation

The standard [`cross_val_predict`](https://skfolio.org/generated/skfolio.model_selection.cross_val_predict.html.md#skfolio.model_selection.cross_val_predict) and its online
counterpart [`online_predict`](https://skfolio.org/generated/skfolio.model_selection.online_predict.html.md#skfolio.model_selection.online_predict) are both designed
exclusively for **portfolio optimization** estimators. The key differences are:

* **Fitting strategy**: standard cross-validation clones and refits the estimator from
  scratch at every fold, while online evaluation maintains a single stateful estimator
  updated incrementally via `partial_fit`, which is significantly faster.
* **Scoring methodology**: standard cross-validation scores each test fold independently
  and averages the results, which can be unreliable when test folds are short (e.g. the
  Sharpe ratio is undefined on a single observation). Online evaluation instead collects
  all out-of-sample predictions into a single
  [`MultiPeriodPortfolio`](https://skfolio.org/generated/skfolio.portfolio.MultiPeriodPortfolio.html.md#skfolio.portfolio.MultiPeriodPortfolio) and computes the metric on the full
  out-of-sample path, which is generally preferred for short rebalancing horizons.

[`online_score`](https://skfolio.org/generated/skfolio.model_selection.online_score.html.md#skfolio.model_selection.online_score) extends online evaluation to both
portfolio optimizers and non-predictor estimators (covariance, expected-return, and
prior estimators). For non-predictor estimators, scores are computed per test window
and averaged by default.

Because the scoring methodology differs for portfolio optimizers, the online utilities
are complementary to the existing cross-validation tools rather than replacements.

<a id="online-covariance-forecast-evaluation"></a>

## Online Covariance Forecast Evaluation

[`online_covariance_forecast_evaluation`](https://skfolio.org/generated/skfolio.model_selection.online_covariance_forecast_evaluation.html.md#skfolio.model_selection.online_covariance_forecast_evaluation)
evaluates the quality of covariance forecasts out-of-sample. It is intended for
covariance estimators rather than portfolio optimizers, which should instead be
evaluated with [`online_predict`](https://skfolio.org/generated/skfolio.model_selection.online_predict.html.md#skfolio.model_selection.online_predict) or
[`online_score`](https://skfolio.org/generated/skfolio.model_selection.online_score.html.md#skfolio.model_selection.online_score).

At each step, the covariance forecast produced after `partial_fit` is compared
to the realized returns over the next test window. The resulting
[`CovarianceForecastEvaluation`](https://skfolio.org/generated/skfolio.model_selection.CovarianceForecastEvaluation.html.md#skfolio.model_selection.CovarianceForecastEvaluation) provides
diagnostics such as:

* Mahalanobis calibration ratio for the full covariance structure across all
  eigenvalue directions.
* Diagonal calibration ratio for asset-level variance calibration.
* Portfolio standardized returns and the associated bias statistic for
  calibration along one portfolio direction by default, or multiple portfolio
  directions when explicit test portfolios are provided.
* Portfolio QLIKE for portfolio variance forecast quality along one or more
  portfolio directions.

When `portfolio_weights=None`, the portfolio diagnostics use a single dynamic
inverse-volatility portfolio direction by default. Passing explicit portfolio
weights extends the evaluation to multiple selected traded directions.

See the example
[Online Covariance Forecast Evaluation](https://skfolio.org/auto_examples/online_learning/plot_1_online_covariance_forecast_evaluation.html.md#sphx-glr-auto-examples-online-learning-plot-1-online-covariance-forecast-evaluation-py)
for the complete workflow.

<a id="online-hyper-parameter-tuning"></a>

## Online Hyper-Parameter Tuning

[`OnlineGridSearch`](https://skfolio.org/generated/skfolio.model_selection.OnlineGridSearch.html.md#skfolio.model_selection.OnlineGridSearch) and
[`OnlineRandomizedSearch`](https://skfolio.org/generated/skfolio.model_selection.OnlineRandomizedSearch.html.md#skfolio.model_selection.OnlineRandomizedSearch) extend the online
workflow to hyper-parameter selection.

Conceptually, this is the online counterpart of combining
`GridSearchCV` or
`RandomizedSearchCV` with
[`WalkForward`](https://skfolio.org/generated/skfolio.model_selection.WalkForward.html.md#skfolio.model_selection.WalkForward) using `expand_train=True`.
The key difference is that online search updates each candidate
incrementally via `partial_fit` along one sequential path instead of
refitting it from scratch at every split.

Each candidate parameter configuration is evaluated on one full online
walk-forward path. When `refit=True`, `best_estimator_` exposes the selected
fitted candidate without an additional fit after model selection because it
has already been updated through the full sample during evaluation.

For non-predictor estimators, online tuning typically uses callable scorers
such as QLIKE or calibration losses, typically wrapped with
[`make_scorer`](https://skfolio.org/generated/skfolio.metrics.make_scorer.html.md#skfolio.metrics.make_scorer) using `response_method=None`. For
multi-metric searches, `refit` should be set explicitly to the name of the
metric used to select the best candidate.

See the example
[Online Covariance Hyperparameter Tuning](https://skfolio.org/auto_examples/online_learning/plot_2_online_hyperparameter_tuning.html.md#sphx-glr-auto-examples-online-learning-plot-2-online-hyperparameter-tuning-py)
for covariance tuning with both
[`OnlineGridSearch`](https://skfolio.org/generated/skfolio.model_selection.OnlineGridSearch.html.md#skfolio.model_selection.OnlineGridSearch) and
[`OnlineRandomizedSearch`](https://skfolio.org/generated/skfolio.model_selection.OnlineRandomizedSearch.html.md#skfolio.model_selection.OnlineRandomizedSearch).

<a id="online-evaluation-of-portfolio-optimization"></a>

## Online Evaluation of Portfolio Optimization

For portfolio optimizers, the main entry points are
[`online_predict`](https://skfolio.org/generated/skfolio.model_selection.online_predict.html.md#skfolio.model_selection.online_predict) and
[`online_score`](https://skfolio.org/generated/skfolio.model_selection.online_score.html.md#skfolio.model_selection.online_score).

* [`online_predict`](https://skfolio.org/generated/skfolio.model_selection.online_predict.html.md#skfolio.model_selection.online_predict) returns a
  [`MultiPeriodPortfolio`](https://skfolio.org/generated/skfolio.portfolio.MultiPeriodPortfolio.html.md#skfolio.portfolio.MultiPeriodPortfolio) built from the sequence of
  out-of-sample portfolio predictions.
* [`online_score`](https://skfolio.org/generated/skfolio.model_selection.online_score.html.md#skfolio.model_selection.online_score) returns a scalar measure, or a
  dict of measures, computed on the aggregate online evaluation.

This is useful when a portfolio estimator embeds incremental moment estimators such as
[`EWMu`](https://skfolio.org/generated/skfolio.moments.EWMu.html.md#skfolio.moments.EWMu) and
[`RegimeAdjustedEWCovariance`](https://skfolio.org/generated/skfolio.moments.RegimeAdjustedEWCovariance.html.md#skfolio.moments.RegimeAdjustedEWCovariance).

During online portfolio evaluation, each rebalance is solved after the estimator has
incorporated the observations available at that date. If the optimization problem cannot
be solved and `raise_on_failure=False`, `online_predict` records that rebalance as a
[`FailedPortfolio`](https://skfolio.org/generated/skfolio.portfolio.FailedPortfolio.html.md#skfolio.portfolio.FailedPortfolio) and continues with the next window. When the
estimator uses previous weights, the last valid allocation remains the reference for
later rebalances.

Pass `portfolio_params={"weight_drift": True, "compounded": True}` to
`online_predict` to evaluate the path with drifted weights and compounded returns. The
`portfolio_params` of `online_predict` follow the same rules as those of
[`cross_val_predict`](https://skfolio.org/generated/skfolio.model_selection.cross_val_predict.html.md#skfolio.model_selection.cross_val_predict): the parameters shared by
[`Portfolio`](https://skfolio.org/generated/skfolio.portfolio.Portfolio.html.md#skfolio.portfolio.Portfolio) and
[`MultiPeriodPortfolio`](https://skfolio.org/generated/skfolio.portfolio.MultiPeriodPortfolio.html.md#skfolio.portfolio.MultiPeriodPortfolio) are applied to the returned
`MultiPeriodPortfolio` and to each `Portfolio` it contains, take precedence over the
optimizer’s `portfolio_params` and are inherited from it when omitted. `weight_drift`
applies to each `Portfolio` of the path. See [Cross-Validation Prediction](https://skfolio.org/user_guide/model_selection.html.md#cross-validation) for the complete
rules.

When the estimator needs previous weights, the `ending_weights` of the last successful
portfolio become the `previous_weights` of the next rebalancing. They equal its target
weights with `weight_drift=False` and its weights after the final observation with
`weight_drift=True`.

`online_score`, `OnlineGridSearch` and `OnlineRandomizedSearch` accept the same
`portfolio_params` when evaluating a portfolio optimizer. Because they score the
resulting `MultiPeriodPortfolio`, these parameters can change the scores and the
ranking of the parameter sets. When refitting is enabled, `weight_drift` is retained in
`best_estimator_` because prediction requires it. The other parameters are not.

To hold the last allocation instead of producing a failed rebalance, configure
`fallback="previous_weights"`. Other fallback estimators are not available with
`partial_fit`, because they would not have learned from the same sequence of past
observations.

See the example
[Online Evaluation of Portfolio Optimization](https://skfolio.org/auto_examples/online_learning/plot_3_online_portfolio_optimization_evaluation.html.md#sphx-glr-auto-examples-online-learning-plot-3-online-portfolio-optimization-evaluation-py)
for an end-to-end online evaluation of
[`MeanRisk`](https://skfolio.org/generated/skfolio.optimization.MeanRisk.html.md#skfolio.optimization.MeanRisk).
