<a id="skfolio-moments-empiricalvariance"></a>

# skfolio.moments.EmpiricalVariance

<a id="skfolio.moments.EmpiricalVariance"></a>

### *class* skfolio.moments.EmpiricalVariance(window_size=None, ddof=1, assume_centered=False)

Empirical Variance estimator.

This is the variance-only counterpart of
`EmpiricalCovariance`, computing only
the diagonal elements (variances) and assuming zero correlation. This is
appropriate when:

* Estimating **idiosyncratic (specific) risk** in factor models, where residual
  returns are uncorrelated by construction
* Working with **orthogonalized** or **uncorrelated** return series
* The full covariance structure is not needed or is constructed separately

* **Parameters:**
  **window_size** *int, optional*
  : Window size. The model is fitted on the last `window_size` observations.
    The default (`None`) is to use all the data.

  **ddof** *int, default=1*
  : Normalization is by `(n_observations - ddof)`.
    Note that `ddof=1` will return the unbiased estimate, and `ddof=0`
    will return the simple average. The default value is `1`.

  **assume_centered** *bool, default=False*
  : If False (default), the data are mean-centered before computing the variance.
    This is the standard behavior when working with raw returns where the mean is
    not guaranteed to be zero.
    If True, the estimator assumes the input data are already centered. Use this
    when you know the returns have zero mean, such as pre-demeaned data or
    regression residuals.
* **Attributes:**
  **variance_** *ndarray of shape (n_assets,)*
  : Estimated variance vector.

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

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

### Methods

| [`fit`](#skfolio.moments.EmpiricalVariance.fit)(X[, y])            | Fit the empirical variance estimator.   |
|-------------------------------------------------------------------------|-----------------------------------------|
| [`get_metadata_routing`](#skfolio.moments.EmpiricalVariance.get_metadata_routing)() | Get metadata routing of this object.    |
| [`get_params`](#skfolio.moments.EmpiricalVariance.get_params)([deep])     | Get parameters for this estimator.      |
| [`set_params`](#skfolio.moments.EmpiricalVariance.set_params)(\*\*params) | Set the parameters of this estimator.   |

### Examples

```pycon
>>> from skfolio.datasets import load_sp500_dataset
>>> from skfolio.moments import EmpiricalVariance
>>> from skfolio.preprocessing import prices_to_returns
>>>
>>> prices = load_sp500_dataset()
>>> X = prices_to_returns(prices)
>>> model = EmpiricalVariance()
>>> model.fit(X)
EmpiricalVariance()
>>> print(model.variance_[:5])
[0.000747... 0.00151... 0.000681... 0.00101... 0.000275...]
```

<a id="skfolio.moments.EmpiricalVariance.fit"></a>

#### fit(X, y=None)

Fit the empirical variance estimator.

* **Parameters:**
  **X** *array-like of shape (n_observations, n_assets)*
  : Price returns of the assets.

  **y** *Ignored*
  : Not used, present for API consistency by convention.
* **Returns:**
  **self** *EmpiricalVariance*
  : Fitted estimator.

<a id="skfolio.moments.EmpiricalVariance.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.moments.EmpiricalVariance.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.moments.EmpiricalVariance.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.

