<a id="skfolio-prior-empiricalprior"></a>

# skfolio.prior.EmpiricalPrior

<a id="skfolio.prior.EmpiricalPrior"></a>

### *class* skfolio.prior.EmpiricalPrior(mu_estimator=None, covariance_estimator=None, is_log_normal=False, investment_horizon=None, max_history=None)

Empirical Prior estimator.

The Empirical Prior estimates the [`ReturnDistribution`](https://skfolio.org/generated/skfolio.prior.ReturnDistribution.html.md#skfolio.prior.ReturnDistribution) by
fitting a `mu_estimator` and a `covariance_estimator` separately.

**NaN handling:**

Missing data (NaN returns) caused by late listings, delistings and holidays is
accepted when both `mu_estimator` and `covariance_estimator` support it (for
example [`EWMu`](https://skfolio.org/generated/skfolio.moments.EWMu.html.md#skfolio.moments.EWMu) and
[`EWCovariance`](https://skfolio.org/generated/skfolio.moments.EWCovariance.html.md#skfolio.moments.EWCovariance)). The moment estimators receive the data
unchanged and apply their own NaN treatment.

In `return_distribution_.returns`, the scenario columns of non-investable
assets (NaN in the estimated `mu` and/or covariance diagonal) are left
unchanged and are removed downstream by
[`investable_subset`](https://skfolio.org/generated/skfolio.prior.ReturnDistribution.html.md#skfolio.prior.ReturnDistribution.investable_subset). Missing
observations of investable assets are replaced by zero.

Zero-filling long gaps, such as the pre-listing history of a late-listed
asset, understates its risk in scenario-based measures (CVaR, EVaR, CDaR,
worst realization, …). A `UserWarning` is emitted when more than 5% of an
investable asset’s scenario history is zero-filled. The moments estimation is
not affected. To reduce the zero-filled share, set `max_history` or use a
factor model prior such as [`CharacteristicsFactorModel`](https://skfolio.org/generated/skfolio.prior.CharacteristicsFactorModel.html.md#skfolio.prior.CharacteristicsFactorModel).

* **Parameters:**
  **mu_estimator** *BaseMu, optional*
  : The assets [expected returns estimator](https://skfolio.org/user_guide/expected_returns.html.md#mu-estimator).
    The default (`None`) is to use [`EmpiricalMu`](https://skfolio.org/generated/skfolio.moments.EmpiricalMu.html.md#skfolio.moments.EmpiricalMu).

  **covariance_estimator** *BaseCovariance , optional*
  : The assets [covariance matrix estimator](https://skfolio.org/user_guide/covariance.html.md#covariance-estimator).
    The default (`None`) is to use  [`EmpiricalCovariance`](https://skfolio.org/generated/skfolio.moments.EmpiricalCovariance.html.md#skfolio.moments.EmpiricalCovariance).

  **is_log_normal** *bool, default=False*
  : If this is set to True, the moments are estimated on the logarithmic returns
    as opposed to the linear returns. Then the moments estimations of the
    logarithmic returns are projected to the investment horizon and transformed
    to obtain the moments estimation of the linear returns at the investment
    horizon. If True, `investment_horizon` must be provided. The input `X` must be
    **linear returns**. They will be converted into logarithmic returns only for the
    moments estimation.
    <br/>
    #### SEE ALSO
    [data preparation](https://skfolio.org/user_guide/data_preparation.html.md#data-preparation)

  **investment_horizon** *float, optional*
  : The investment horizon used for the moments estimation of the linear returns
    when `is_log_normal` is `True`.

  **max_history** *int, optional*
  : Maximum number of observations to keep in `return_distribution_.returns`.
    This is useful for controlling memory usage during incremental learning
    with [`partial_fit`](#skfolio.prior.EmpiricalPrior.partial_fit).
    * If `None` (default), all returns are accumulated.
    * If an integer, only the last `max_history` observations are kept
      (rolling window).
* **Attributes:**
  **return_distribution_** *ReturnDistribution*
  : Fitted [`ReturnDistribution`](https://skfolio.org/generated/skfolio.prior.ReturnDistribution.html.md#skfolio.prior.ReturnDistribution) to be used by the optimization
    estimators, containing the asset returns distribution and moments estimation.

  **mu_estimator_** *BaseMu*
  : Fitted `mu_estimator`.

  **covariance_estimator_** *BaseCovariance*
  : Fitted `covariance_estimator`.

  **n_features_in_** *int*
  : Number of assets seen during `fit`.

  **feature_names_in_** *ndarray of shape (`n_features_in_`,)*
  : Names of features seen during `fit`. Defined only when `X`
    has feature names that are all strings.

### Methods

| [`fit`](#skfolio.prior.EmpiricalPrior.fit)(X[, y])            | Fit the Empirical Prior estimator.               |
|-------------------------------------------------------------------------|--------------------------------------------------|
| [`get_metadata_routing`](#skfolio.prior.EmpiricalPrior.get_metadata_routing)() | Get metadata routing of this object.             |
| [`get_params`](#skfolio.prior.EmpiricalPrior.get_params)([deep])     | Get parameters for this estimator.               |
| [`partial_fit`](#skfolio.prior.EmpiricalPrior.partial_fit)(X[, y])    | Incrementally fit the Empirical Prior estimator. |
| [`set_params`](#skfolio.prior.EmpiricalPrior.set_params)(\*\*params) | Set the parameters of this estimator.            |

### References

* <a id='r2751d933e841-1'>**[1]**</a> “Linear vs. Compounded Returns - Common Pitfalls in Portfolio Management”. GARP Risk Professional. Attilio Meucci (2010).

<a id="skfolio.prior.EmpiricalPrior.fit"></a>

#### fit(X, y=None, \*\*fit_params)

Fit the Empirical Prior estimator.

* **Parameters:**
  **X** *array-like of shape (n_observations, n_assets)*
  : Price returns of the assets. May contain NaN (holidays, late
    listings, delistings) when both `mu_estimator` and
    `covariance_estimator` handle missing data.

  **y** *Ignored*
  : Not used, present for API consistency by convention.

  **\*\*fit_params** *dict*
  : Parameters to pass to the underlying estimators.
    Only available if `enable_metadata_routing=True`, which can be
    set by using `sklearn.set_config(enable_metadata_routing=True)`.
    See [Metadata Routing User Guide](https://skfolio.org/user_guide/metadata_routing.html.md#metadata-routing) for
    more details.
* **Returns:**
  **self** *EmpiricalPrior*
  : Fitted estimator.

<a id="skfolio.prior.EmpiricalPrior.get_metadata_routing"></a>

#### get_metadata_routing()

Get metadata routing of this object.

Please check [User Guide](https://skfolio.org/user_guide/metadata_routing.html.md#metadata-routing) on how the routing
mechanism works.

* **Returns:**
  **routing** *MetadataRequest*
  : A `MetadataRequest` encapsulating
    routing information.

<a id="skfolio.prior.EmpiricalPrior.get_params"></a>

#### get_params(deep=True)

Get parameters for this estimator.

* **Parameters:**
  **deep** *bool, default=True*
  : If True, will return the parameters for this estimator and
    contained subobjects that are estimators.
* **Returns:**
  **params** *dict*
  : Parameter names mapped to their values.

<a id="skfolio.prior.EmpiricalPrior.partial_fit"></a>

#### partial_fit(X, y=None, \*\*fit_params)

Incrementally fit the Empirical Prior estimator.

This method allows for streaming/online updates to the prior estimate.
Each call updates the internal state with new observations.

Both `mu_estimator` and `covariance_estimator` must implement
`partial_fit` for this method to work.

* **Parameters:**
  **X** *array-like of shape (n_observations, n_assets)*
  : Price returns of the assets. May contain NaN (holidays, late
    listings, delistings) when both `mu_estimator` and
    `covariance_estimator` handle missing data.

  **y** *Ignored*
  : Not used, present for API consistency by convention.

  **\*\*fit_params** *dict*
  : Parameters to pass to the underlying estimators.
    Only available if `enable_metadata_routing=True`, which can be
    set by using `sklearn.set_config(enable_metadata_routing=True)`.
    See [Metadata Routing User Guide](https://skfolio.org/user_guide/metadata_routing.html.md#metadata-routing) for
    more details.
* **Returns:**
  **self** *EmpiricalPrior*
  : Fitted estimator.

<a id="skfolio.prior.EmpiricalPrior.set_params"></a>

#### set_params(\*\*params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects
(such as `Pipeline`). The latter have
parameters of the form `<component>__<parameter>` so that it’s
possible to update each component of a nested object.

* **Parameters:**
  **\*\*params** *dict*
  : Estimator parameters.
* **Returns:**
  **self** *estimator instance*
  : Estimator instance.

