Parameters Estimation — Riskfolio-Lib 0.1 documentation

# Parameters Estimation

This module containts functions that allows us to estimate the vector of means and covariance matrix using several methods:

- Historical estimates.

- Estimates using exponencial weighted moving averages (EWMA).

- Robust estimates of the covariance matrix like shrinkage or oracle method.

- The Black Litterman model.

- Factors models to estimate the vector of means and covariance matrix.

## Module Functions

`ParamsEstimation.` `mean_vector`(X, method='hist', d=0.94)

  Calculate the expected returns vector using the selected method.

  Parameters

    - X (DataFrame of shape (n_samples, n_features)) – Features matrix, where n_samples is the number of samples and n_features is the number of features.

    - method (str, can be {'hist', 'ewma1' or 'ewma2'}) –

      The method used to estimate the expected returns. The default value is ‘hist’.

      - ’hist’: use historical estimates.

      - ’ewma1’’: use ewma with adjust=True, see EWM for more details.

      - ’ewma2’: use ewma with adjust=False, see EWM for more details.

    - d (scalar) – The smoothing factor of ewma methods. The default is 0.94.

  Returns

    mu – The estimation of expected returns.

  Return type

    1d-array

  Raises

    ValueError – When the value cannot be calculated.

`ParamsEstimation.` `covar_matrix`(X, method='hist', d=0.94, **kwargs)

  Calculate the covariance matrix using the selected method.

  Parameters

    - X (DataFrame of shape (n_samples, n_features)) – Features matrix, where n_samples is the number of samples and n_features is the number of features.

    - method (str, can be {'hist', 'ewma1', 'ewma2', 'ledoit', 'oas' or 'shrunk'}) –

      The default is ‘hist’. The method used to estimate the covariance matrix:

      - ’hist’: use historical estimates.

      - ’ewma1’’: use ewma with adjust=True, see EWM for more details.

      - ’ewma2’: use ewma with adjust=False, see EWM for more details.

      - ’ledoit’: use the Ledoit and Wolf Shrinkage method.

      - ’oas’: use the Oracle Approximation Shrinkage method.

      - ’shrunk’: use the basic Shrunk Covariance method.

    - d (scalar) – The smoothing factor of ewma methods. The default is 0.94.

    - **kwargs – Other variables related to covariance estimation. See Scikit Learn for more details.

  Returns

    cov – The estimation of covariance matrix.

  Return type

    nd-array

  Raises

    ValueError – When the value cannot be calculated.

`ParamsEstimation.` `forward_regression`(X, y, threshold=0.05, verbose=False)

  Select the variables that estimate the best model using stepwise forward regression.

  Parameters

    - X (DataFrame of shape (n_samples, n_features)) – Features matrix, where n_samples is the number of samples and n_features is the number of features.

    - y (Series of shape (n_samples, 1)) – Target vector, where n_samples in the number of samples.

    - thresholdt (scalar, optional) – Is the maximum p-value for each variable that will be accepted in the model. The default is 0.05.

    - verbose (bool, optional) – Enable verbose output. The default is False.

  Returns

    value – A list of the variables that produce the best model.

  Return type

    list

  Raises

    ValueError – When the value cannot be calculated.

`ParamsEstimation.` `backward_regression`(X, y, threshold=0.05, verbose=False)

  Select the variables that estimate the best model using stepwise backward regression.

  Parameters

    - X (DataFrame of shape (n_samples, n_features)) – Features matrix, where n_samples is the number of samples and n_features is the number of features.

    - y (Series of shape (n_samples, 1)) – Target vector, where n_samples in the number of samples.

    - threshold (scalar, optional) – Is the maximum p-value for each variable that will be accepted in the model. The default is 0.05.

    - verbose (bool, optional) – Enable verbose output. The default is False.

  Returns

    value – A list of the variables that produce the best model.

  Return type

    list

  Raises

    ValueError – When the value cannot be calculated.

`ParamsEstimation.` `loadings_matrix`(X, Y, threshold=0.05, verbose=False, stepwise='Forward')

  Estimate the loadings matrix using stepwise regression.

  Parameters

    - X (DataFrame of shape (n_samples, n_features)) – Features matrix, where n_samples is the number of samples and n_features is the number of features.

    - Y (DataFrame of shape (n_samples, n_assets)) – Target matrix, where n_samples in the number of samples and n_assets is the number of assets.

    - thresholdt (scalar, optional) – Is the maximum p-value for each variable that will be accepted in the model. The default is 0.05.

    - verbose (bool, optional) – Enable verbose output. The default is False.

    - steepwise (str 'Forward' or 'Backward', optional) – Indicate the method used for stepwise regression. The default is ‘Forward’.

  Returns

    loadings – A DataFrame with the loadings matrix.

  Return type

    DataFrame

  Raises

    ValueError – When the value cannot be calculated.

`ParamsEstimation.` `risk_factors`(X, Y, B=None, method_mu='hist', method_cov='hist', threshold=0.05, stepwise='Forward', error=True, **kwargs)

  Estimate the expected returns vector and covariance matrix based on risk factors models [B1] [B2].

  \[R = \alpha + B F + \epsilon\]
  \[\mu_{f} = \alpha +BE(F)\]
  \[\Sigma_{f} = B \Sigma_{F} B^{T} + \Sigma_{\epsilon}\]

  where:

  \(R\) is the series returns.

  \(\alpha\) is the intercept.

  \(B\) is the loadings matrix.

  \(F\) is the expected returns vector of the risk factors.

  \(\Sigma_{F}\) is the covariance matrix of the risk factors.

  \(\Sigma_{\epsilon}\) is the covariance matrix of error terms.

  \(\mu_{f}\) is the expected returns vector obtained with the risk factor model.

  \(\Sigma_{f}\) is the covariance matrix obtained with the risk factor model.

  Parameters

    - X (DataFrame of shape (n_samples, n_features)) – Features matrix, where n_samples is the number of samples and n_features is the number of features.

    - Y (DataFrame of shape (n_samples, n_assets)) – Target matrix, where n_samples in the number of samples and n_assets is the number of assets.

    - B (DataFrame of shape (n_assets, n_features), optional) – Loadings matrix. If is not specified, is estimated using stepwise regression. The default is None.

    - steepwise (str 'Forward' or 'Backward') – Indicate the method used for stepwise regression. The default is ‘Forward’.

    - error (bool) – Indicate if diagonal covariance matrix of errors is included (only when B is estimated through a regression).

    - **kwargs (dict) – Other variables related to the expected returns and covariance estimation.

  Returns

    - mu (DataFrame) – The mean vector of risk factors model.

    - cov (DataFrame) – The covariance matrix of risk factors model.

    - returns (DataFrame) – The returns based on a risk factor model.

    - nav (DataFrame) – The cumulated uncompound returns based on a risk factor model.

  Raises

    ValueError – When the value cannot be calculated.

`ParamsEstimation.` `black_litterman`(X, w, P, Q, delta=1, rf=0, eq=True, method_mu='hist', method_cov='hist', **kwargs)

  Estimate the expected returns vector and covariance matrix based on the black litterman model [B3] [B4].

  \[\Pi = \delta \Sigma w\]
  \[\Pi_{bl} = \left[(\tau\Sigma)^{-1}+ P \Omega^{-1}P \right]^{-1} \left[(\tau\Sigma)^{-1} \Pi + P \Omega^{-1}Q \right]\]
  \[M = \left((\tau\Sigma)^{-1} + P'\Omega^{-1} P \right)^{-1}\]
  \[\mu_{bl} = \Pi_{bl} + rf\]
  \[\Sigma_{bl} = \Sigma + M\]

  where:

  \(rf\) is the risk free rate.

  \(\delta\) is the risk aversion factor.

  \(\Pi\) is the equilibrium excess returns.

  \(\Sigma\) is the covariance matrix.

  \(P\) is the views matrix.

  \(Q\) is the views returns matrix.

  \(\Omega\) is covariance matrix of the error views.

  \(\mu_{bl}\) is the mean vector obtained with the black litterman model.

  \(\Sigma_{bl}\) is the covariance matrix obtained with the black litterman model.

  Parameters

    - X (DataFrame of shape (n_samples, n_features)) – Assets matrix, where n_samples is the number of samples and n_features is the number of features.

    - w (DataFrame of shape (n_assets, 1)) – Weights matrix, where n_assets is the number of assets.

    - P (DataFrame of shape (n_views, n_assets)) – Analyst’s views matrix, can be relative or absolute.

    - Q (DataFrame of shape (n_views, 1)) – Expected returns of analyst’s views.

    - delta (float, optional) – Risk aversion factor. The default value is 1.

    - rf (scalar, optional) – Risk free rate. The default is 0.

    - eq (bool, optional) – Indicate if use equilibrum or historical excess returns. The default is True.

    - **kwargs (dict) – Other variables related to the expected returns and covariance estimation.

  Returns

    - mu (DataFrame) – The mean vector of black litterman model.

    - cov (DataFrame) – The covariance matrix of black litterman model.

  Raises

    ValueError – When the value cannot be calculated.

## Bibliography

B1

  Stephen A. Ross. The arbitrage theory of capital asset pricing. Journal of Economic Theory, 13(3):341–360, December 1976. URL: https://ideas.repec.org/a/eee/jetheo/v13y1976i3p341-360.html, doi:.

B2

  Jianqing Fan, Yingying Fan, and Jinchi Lv. High dimensional covariance matrix estimation using a factor model. Journal of Econometrics, 147(1):186–197, November 2008. URL: https://ideas.repec.org/a/eee/econom/v147y2008i1p186-197.html, doi:.

B3

  Fischer Black and Robert Litterman. Global portfolio optimization. Financial Analysts Journal, 48(5):28–43, 1992. URL: http://www.jstor.org/stable/4479577.

B4

  Jay Walters. The black-litterman model in detail. SSRN Electronic Journal, pages, 07 2011. doi:10.2139/ssrn.1314585.