Parameters Estimation

https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86
Buy Me a Coffee at ko-fi.com

This module has functions that allows us to estimate the vector of means, covariance matrix and cokurtosis square matrix using several methods:

  • Historical estimates.

  • Estimates using exponencial weighted moving averages (EWMA).

  • Robust estimates of the covariance matrix like Ledoit and Wolf, Oracle, Shrinkage and Graphical Lasso, j-LoGo [B1], Gerber statistic [B2] and Denoise [B3] estimators.

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

  • The Black Litterman model that allows to incorporate analyst’s views on returns in estimates of vector of means and covariance matrix [B4] [B5].

  • The Augmented Black Litterman model that allows to incorporate analyst’s views on risk factors in estimates of vector of means and covariance matrix [B6].

  • The Black Litterman Bayesian model that allows to incorporate analyst’s views on risk factors in estimates of vector of means and covariance matrix [B7].

  • Bootstrapping methods to estimate the input parameters of the uncertainty sets on mean vector and covariance matrix for worst case optimization models.

Module Functions

ParamsEstimation.mean_vector(X, method='hist', d=0.94, target='b1')[source]

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, optional) –

    The method used to estimate the expected returns. The default value is ‘hist’. Possible values are:

    • ’hist’: use historical estimator.

    • ’ewma1’: use ewma with adjust=True. For more information see EWM.

    • ’ewma2’: use ewma with adjust=False. For more information see EWM.

    • ’JS’: James-Stein estimator. For more information see [B8] and [B9].

    • ’BS’: Bayes-Stein estimator. For more information see [B10].

    • ’BOP’: BOP estimator. For more information see [B11].

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

  • target (str, optional) –

    The target mean vector. The default value is ‘b1’. Possible values are:

    • ’b1’: grand mean.

    • ’b2’: volatility weighted grand mean.

    • ’b3’: mean square error of sample mean.

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)[source]

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, optional) –

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

    • ’hist’: use historical estimates.

    • ’semi’: use semi lower covariance matrix.

    • ’ewma1’: use ewma with adjust=True. For more information see EWM.

    • ’ewma2’: use ewma with adjust=False. For more information see EWM.

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

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

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

    • ’gl’: use the basic Graphical Lasso Covariance method.

    • ’jlogo’: use the j-LoGo Covariance method. For more information see: [B1].

    • ’fixed’: denoise using fixed method. For more information see chapter 2 of [B3].

    • ’spectral’: denoise using spectral method. For more information see chapter 2 of [B3].

    • ’shrink’: denoise using shrink method. For more information see chapter 2 of [B3].

    • ’gerber1’: use the Gerber statistic 1. For more information see: [B2].

    • ’gerber2’: use the Gerber statistic 2. For more information see: [B2].

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

  • **kwargs – Other variables related to covariance estimation. See Scikit Learn and chapter 2 of [B3] for more details.

Returns:

cov – The estimation of covariance matrix.

Return type:

nd-array

Raises:

ValueError – When the value cannot be calculated.

ParamsEstimation.cokurt_matrix(X, method='hist', **kwargs)[source]

Calculate the cokurtosis square 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, optional) –

    The method used to estimate the cokurtosis square matrix: The default is ‘hist’. Possible values are:

    • ’hist’: use historical estimates.

    • ’semi’: use semi lower cokurtosis square matrix.

    • ’fixed’: denoise using fixed method. For more information see chapter 2 of [B3].

    • ’spectral’: denoise using spectral method. For more information see chapter 2 of [B3].

    • ’shrink’: denoise using shrink method. For more information see chapter 2 of [B3].

  • **kwargs – Other variables related to covariance estimation. See chapter 2 of [B3] for more details.

Returns:

kurt – The estimation of cokurtosis square matrix.

Return type:

nd-array

Raises:

ValueError – When the value cannot be calculated.

ParamsEstimation.forward_regression(X, y, criterion='pvalue', threshold=0.05, verbose=False)[source]

Select the variables that estimate the best model using stepwise forward regression. In case none of the variables has a p-value lower than threshold, the algorithm will select the variable with lowest p-value.

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.

  • criterion (str, optional) –

    The default is ‘pvalue’. Possible values of the criterion used to select the best features are:

    • ’pvalue’: select the features based on p-values.

    • ’AIC’: select the features based on lowest Akaike Information Criterion.

    • ’SIC’: select the features based on lowest Schwarz Information Criterion.

    • ’R2’: select the features based on highest R Squared.

    • ’R2_A’: select the features based on highest Adjusted R Squared.

  • 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, criterion='pvalue', threshold=0.05, verbose=False)[source]

Select the variables that estimate the best model using stepwise backward regression. In case none of the variables has a p-value lower than threshold, the algorithm will select the variable with lowest p-value.

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.

  • criterion (str, optional) –

    The default is ‘pvalue’. Possible values of the criterion used to select the best features are:

    • ’pvalue’: select the features based on p-values.

    • ’AIC’: select the features based on lowest Akaike Information Criterion.

    • ’SIC’: select the features based on lowest Schwarz Information Criterion.

    • ’R2’: select the features based on highest R Squared.

    • ’R2_A’: select the features based on highest Adjusted R Squared.

  • 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.PCR(X, y, n_components=0.95)[source]

Estimate the coefficients using Principal Components Regression (PCR).

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.

  • n_components (int, float, None or str, optional) – if 1 < n_components (int), it represents the number of components that will be keep. if 0 < n_components < 1 (float), it represents the percentage of variance that the is explained by the components kept. See PCA for more details. The default is 0.95.

Returns:

value – An array with the coefficients of the model calculated using PCR.

Return type:

nd-array

Raises:

ValueError – When the value cannot be calculated.

ParamsEstimation.loadings_matrix(X, Y, feature_selection='stepwise', stepwise='Forward', criterion='pvalue', threshold=0.05, n_components=0.95, verbose=False)[source]

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.

  • feature_selection (str, 'stepwise' or 'PCR', optional) –

    Indicate the method used to estimate the loadings matrix. The default is ‘stepwise’. Possible values are:

    • ’stepwise’: use stepwise regression to select the best factors and estimate coefficients.

    • ’PCR’: use principal components regression to estimate coefficients.

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

  • criterion (str, optional) –

    The default is ‘pvalue’. Possible values of the criterion used to select the best features are:

    • ’pvalue’: select the features based on p-values.

    • ’AIC’: select the features based on lowest Akaike Information Criterion.

    • ’SIC’: select the features based on lowest Schwarz Information Criterion.

    • ’R2’: select the features based on highest R Squared.

    • ’R2_A’: select the features based on highest Adjusted R Squared.

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

  • n_components (int, float, None or str, optional) –

    if 1 < n_components (int), it represents the number of components that will be keep. if 0 < n_components < 1 (float), it represents the percentage of variance that the is explained by the components kept. See PCA for more details. The default is 0.95.

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

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, const=False, method_mu='hist', method_cov='hist', feature_selection='stepwise', stepwise='Forward', criterion='pvalue', threshold=0.05, n_components=0.95, error=True, **kwargs)[source]

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

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

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.

  • const (bool, optional) – Indicate if the loadings matrix has a constant. The default is False.

  • method_mu (str, optional) –

    The method used to estimate the expected returns of factors. The default value is ‘hist’. Possible values are:

    • ’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.

    • ’JS’: James-Stein estimator. For more information see [B8] and [B9].

    • ’BS’: Bayes-Stein estimator. For more information see [B10].

    • ’BOP’: BOP estimator. For more information see [B11].

  • method_cov (str, optional) –

    The method used to estimate the covariance matrix of factors. The default is ‘hist’. Possible values are:

    • ’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.

    • ’gl’: use the basic Graphical Lasso Covariance method.

    • ’jlogo’: use the j-LoGo Covariance method. For more information see: [B1].

    • ’fixed’: denoise using fixed method. For more information see chapter 2 of [B3].

    • ’spectral’: denoise using spectral method. For more information see chapter 2 of [B3].

    • ’shrink’: denoise using shrink method. For more information see chapter 2 of [B3].

    • ’gerber1’: use the Gerber statistic 1. For more information see: [B2].

    • ’gerber2’: use the Gerber statistic 2. For more information see: [B2].

  • feature_selection (str, 'stepwise' or 'PCR', optional) –

    Indicate the method used to estimate the loadings matrix. The default is ‘stepwise’. Possible values are:

    • ’stepwise’: use stepwise regression to select the best factors and estimate coefficients.

    • ’PCR’: use principal components regression to estimate coefficients.

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

  • criterion (str, optional) –

    The default is ‘pvalue’. Possible values of the criterion used to select the best features are:

    • ’pvalue’: select the features based on p-values.

    • ’AIC’: select the features based on lowest Akaike Information Criterion.

    • ’SIC’: select the features based on lowest Schwarz Information Criterion.

    • ’R2’: select the features based on highest R Squared.

    • ’R2_A’: select the features based on highest Adjusted R Squared.

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

  • n_components (int, float, None or str, optional) –

    if 1 < n_components (int), it represents the number of components that will be keep. if 0 < n_components < 1 (float), it represents the percentage of variance that the is explained by the components kept. See PCA for more details. The default is 0.95.

  • 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 uncompounded 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)[source]

Estimate the expected returns vector and covariance matrix based on the Black Litterman model [B4] [B5].

\[\begin{split}\begin{aligned} \Pi & = \delta \Sigma w \\ \Pi_{BL} & = \left [ (\tau\Sigma)^{-1}+ P^{T} \Omega^{-1}P \right]^{-1} \left[(\tau\Sigma)^{-1} \Pi + P^{T} \Omega^{-1} Q \right] \\ M & = \left((\tau\Sigma)^{-1} + P^{T}\Omega^{-1} P \right)^{-1} \\ \mu_{BL} & = \Pi_{BL} + r_{f} \\ \Sigma_{BL} & = \Sigma + M \\ \end{aligned}\end{split}\]

where:

\(r_{f}\) 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 the 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_assets)) – Assets matrix, where n_samples is the number of samples and n_assets is the number of assets.

  • 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 equilibrium or historical excess returns. The default is True.

  • method_mu (str, optional) –

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

    • ’hist’: use historical estimates.

    • ’ewma1’: use ewma with adjust=True. For more information see EWM.

    • ’ewma2’: use ewma with adjust=False. For more information see EWM.

    • ’JS’: James-Stein estimator. For more information see [B8] and [B9].

    • ’BS’: Bayes-Stein estimator. For more information see [B10].

    • ’BOP’: BOP estimator. For more information see [B11].

  • method_cov (str, optional) –

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

    • ’hist’: use historical estimates.

    • ’ewma1’: use ewma with adjust=True. For more information see EWM.

    • ’ewma2’: use ewma with adjust=False. For more information see EWM.

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

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

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

    • ’gl’: use the basic Graphical Lasso Covariance method.

    • ’jlogo’: use the j-LoGo Covariance method. For more information see: [B1].

    • ’fixed’: denoise using fixed method. For more information see chapter 2 of [B3].

    • ’spectral’: denoise using spectral method. For more information see chapter 2 of [B3].

    • ’shrink’: denoise using shrink method. For more information see chapter 2 of [B3].

    • ’gerber1’: use the Gerber statistic 1. For more information see: [B2].

    • ’gerber2’: use the Gerber statistic 2. For more information see: [B2].

  • **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.

  • w (DataFrame) – The equilibrium weights of Black Litterman model, without constraints.

Raises:

ValueError – When the value cannot be calculated.

ParamsEstimation.augmented_black_litterman(X, w, F=None, B=None, P=None, Q=None, P_f=None, Q_f=None, delta=1, rf=0, eq=True, const=True, method_mu='hist', method_cov='hist', **kwargs)[source]

Estimate the expected returns vector and covariance matrix based on the Augmented Black Litterman model [B6].

\[\begin{split}\begin{aligned} \Pi^{a} & = \delta \left [ \begin{array}{c} \Sigma \\ \Sigma_{F} B^{T} \\ \end{array} \right ] w \\ P^{a} & = \left [ \begin{array}{cc} P & 0 \\ 0 & P_{F} \\ \end{array} \right ] \\ Q^{a} & = \left [ \begin{array}{c} Q \\ Q_{F} \\ \end{array} \right ] \\ \Sigma^{a} & = \left [ \begin{array}{cc} \Sigma & B \Sigma_{F}\\ \Sigma_{F} B^{T} & \Sigma_{F} \\ \end{array} \right ] \\ \Omega^{a} & = \left [ \begin{array}{cc} \Omega & 0 \\ 0 & \Omega_{F} \\ \end{array} \right ] \\ \Pi^{a}_{BL} & = \left [ (\tau \Sigma^{a})^{-1} + (P^{a})^{T} (\Omega^{a})^{-1} P^{a} \right ]^{-1} \left [ (\tau\Sigma^{a})^{-1} \Pi^{a} + (P^{a})^{T} (\Omega^{a})^{-1} Q^{a} \right ] \\ M^{a} & = \left ( (\tau\Sigma^{a})^{-1} + (P^{a})^{T} (\Omega^{a})^{-1} P^{a} \right )^{-1} \\ \mu^{a}_{BL} & = \Pi^{a}_{BL} + r_{f} \\ \Sigma^{a}_{BL} & = \Sigma^{a} + M^{a} \\ \end{aligned}\end{split}\]

where:

\(r_{f}\) is the risk free rate.

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

\(B\) is the loadings matrix.

\(\Sigma\) is the covariance matrix of assets.

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

\(\Sigma^{a}\) is the augmented covariance matrix.

\(P\) is the assets views matrix.

\(Q\) is the assets views returns matrix.

\(P_{F}\) is the factors views matrix.

\(Q_{F}\) is the factors views returns matrix.

\(P^{a}\) is the augmented views matrix.

\(Q^{a}\) is the augmented views returns matrix.

\(\Pi^{a}\) is the augmented equilibrium excess returns.

\(\Omega\) is the covariance matrix of errors of assets views.

\(\Omega_{F}\) is the covariance matrix of errors of factors views.

\(\Omega^{a}\) is the covariance matrix of errors of augmented views.

\(\mu^{a}_{BL}\) is the mean vector obtained with the Augmented Black Litterman model.

\(\Sigma^{a}_{BL}\) is the covariance matrix obtained with the Augmented Black Litterman model.

Parameters:
  • X (DataFrame of shape (n_samples, n_assets)) – Assets matrix, where n_samples is the number of samples and n_assets is the number of features.

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

  • F (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.

  • B (DataFrame of shape (n_assets, n_features), optional) – Loadings matrix. The default is None.

  • 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.

  • P_f (DataFrame of shape (n_views, n_features)) – Analyst’s factors views matrix, can be relative or absolute.

  • Q_f (DataFrame of shape (n_views, 1)) – Expected returns of analyst’s factors 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 equilibrium or historical excess returns. The default is True.

  • const (bool, optional) – Indicate if use equilibrium or historical excess returns. The default is True.

  • method_mu (str, optional) –

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

    • ’hist’: use historical estimates.

    • ’ewma1’: use ewma with adjust=True. For more information see EWM.

    • ’ewma2’: use ewma with adjust=False. For more information see EWM.

    • ’JS’: James-Stein estimator. For more information see [B8] and [B9].

    • ’BS’: Bayes-Stein estimator. For more information see [B10].

    • ’BOP’: BOP estimator. For more information see [B11].

  • method_cov (str, optional) –

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

    • ’hist’: use historical estimates.

    • ’ewma1’: use ewma with adjust=True. For more information see EWM.

    • ’ewma2’: use ewma with adjust=False. For more information see EWM.

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

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

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

    • ’gl’: use the basic Graphical Lasso Covariance method.

    • ’jlogo’: use the j-LoGo Covariance method. For more information see: [B1].

    • ’fixed’: denoise using fixed method. For more information see chapter 2 of [B3].

    • ’spectral’: denoise using spectral method. For more information see chapter 2 of [B3].

    • ’shrink’: denoise using shrink method. For more information see chapter 2 of [B3].

    • ’gerber1’: use the Gerber statistic 1. For more information see: [B2].

    • ’gerber2’: use the Gerber statistic 2. For more information see: [B2].

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

Returns:

  • mu (DataFrame) – The mean vector of Augmented Black Litterman model.

  • cov (DataFrame) – The covariance matrix of Augmented Black Litterman model.

  • w (DataFrame) – The equilibrium weights of Augmented Black Litterman model, without constraints.

Raises:

ValueError – When the value cannot be calculated.

ParamsEstimation.black_litterman_bayesian(X, F, B, P_f, Q_f, delta=1, rf=0, eq=True, const=True, diag=True, method_mu='hist', method_cov='hist', **kwargs)[source]

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

\[\begin{split}\begin{aligned} \Sigma_{F} & = B \Sigma_{F} B^{T} + D \\ \overline{\Pi}_{F} & = \left ( \Sigma_{F}^{-1} + P_{F}^{T}\Omega_{F}^{-1}P_{F} \right )^{-1} \left ( \Sigma_{F}^{-1}\Pi_{F} + P_{F}^{T}\Omega_{F}^{-1}Q_{F} \right) \\ \overline{\Sigma}_{F} & = \left ( \Sigma_{F}^{-1} + P_{F}^{T}\Omega_{F}^{-1}P_{F} \right )^{-1} \\ \Sigma_{BLB} & = \left( \Sigma^{-1} - \Sigma^{-1} B \left( \overline{\Sigma}_{F}^{-1} + B^{T}\Sigma^{-1}B \right)^{-1} B^{T}\Sigma^{-1} \right )^{-1} \\ \mu_{BLB} & = \Sigma_{BLB} \left ( \Sigma^{-1} B \left( \overline{\Sigma}_{F}^{-1} +B^{T}\Sigma^{-1}B \right)^{-1} \overline{\Sigma}_{F}^{-1} \overline{\Pi}_{F} \right ) + r_{f} \\ \end{aligned}\end{split}\]

where:

\(r_{f}\) is the risk free rate.

\(B\) is the loadings matrix.

\(D\) is a diagonal matrix of variance of errors of a factor model.

\(\Sigma\) is the covariance matrix obtained with a factor model.

\(\Pi_{F}\) is the equilibrium excess returns of factors.

\(\overline{\Pi}_{F}\) is the posterior excess returns of factors.

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

\(\overline{\Sigma}_{F}\) is the posterior covariance matrix of factors.

\(P_{F}\) is the factors views matrix.

\(Q_{F}\) is the factors views returns matrix.

\(\Omega_{F}\) is the covariance matrix of errors of factors views.

\(\mu_{BLB}\) is the mean vector obtained with the Black Litterman Bayesian model or posterior predictive mean.

\(\Sigma_{BLB}\) is the covariance matrix obtained with the Black Litterman Bayesian model or posterior predictive covariance.

Parameters:
  • X (DataFrame of shape (n_samples, n_assets)) – Assets matrix, where n_samples is the number of samples and n_assets is the number of assets.

  • F (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.

  • B (DataFrame of shape (n_assets, n_features), optional) – Loadings matrix. The default is None.

  • P_f (DataFrame of shape (n_views, n_features)) – Analyst’s factors views matrix, can be relative or absolute.

  • Q_f (DataFrame of shape (n_views, 1)) – Expected returns of analyst’s factors 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 equilibrium or historical excess returns. The default is True.

  • const (bool, optional) – Indicate if the loadings matrix has a constant. The default is True.

  • diag (bool, optional) – Indicate if we use the diagonal matrix to calculate covariance matrix of factor model, only useful when we work with a factor model based on a regresion model (only equity portfolio). The default is True.

  • method_mu (str, optional) –

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

    • ’hist’: use historical estimates.

    • ’ewma1’: use ewma with adjust=True. For more information see EWM.

    • ’ewma2’: use ewma with adjust=False, For more information see EWM.

    • ’JS’: James-Stein estimator. For more information see [B8] and [B9].

    • ’BS’: Bayes-Stein estimator. For more information see [B10].

    • ’BOP’: BOP estimator. For more information see [B11].

  • method_cov (str, optional) –

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

    • ’hist’: use historical estimates.

    • ’ewma1’: use ewma with adjust=True. For more information see EWM.

    • ’ewma2’: use ewma with adjust=False. For more information see EWM.

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

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

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

    • ’gl’: use the basic Graphical Lasso Covariance method.

    • ’jlogo’: use the j-LoGo Covariance method. For more information see: [B1].

    • ’fixed’: denoise using fixed method. For more information see chapter 2 of [B3].

    • ’spectral’: denoise using spectral method. For more information see chapter 2 of [B3].

    • ’shrink’: denoise using shrink method. For more information see chapter 2 of [B3].

    • ’gerber1’: use the Gerber statistic 1. For more information see: [B2].

    • ’gerber2’: use the Gerber statistic 2. For more information see: [B2].

  • **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.

  • w (DataFrame) – The equilibrium weights of Black Litterman model, without constraints.

Raises:

ValueError – When the value cannot be calculated.

ParamsEstimation.bootstrapping(X, kind='stationary', q=0.05, n_sim=6000, window=3, diag=False, threshold=1e-15, seed=0)[source]

Estimates the uncertainty sets of mean and covariance matrix through the selected bootstrapping 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.

  • kind (str) –

    The bootstrapping method. The default value is ‘stationary’. Possible values are:

  • q (scalar) – Significance level for box and elliptical constraints. The default is 0.05.

  • n_sim (scalar) – Number of simulations of the bootstrapping method. The default is 6000.

  • window (int) – Block size of the bootstrapping method. Must be greather than 1 and lower than the n_samples - n_features + 1 The default is 3.

  • diag (bool) – If consider only the main diagonal of covariance matrices of estimation errors following [B14]. The default is False.

  • threshold (float) – Parameter used to fix covariance matrices in case they are not positive semidefinite. The default is 1e-15.

  • seed (int) – Seed used to generate random numbers for bootstrapping method. The default is 0.

Returns:

  • mu_l (DataFrame) – The q/2 percentile of mean vector obtained through the selected bootstrapping method.

  • mu_u (DataFrame) – The 1-q/2 percentile of mean vector obtained through the selected bootstrapping method.

  • cov_l (DataFrame) – The q/2 percentile of covariance matrix obtained through the selected bootstrapping method.

  • cov_u (DataFrame) – The 1-q/2 percentile of covariance matrix obtained through the selected bootstrapping method.

  • cov_mu (DataFrame) – The covariance matrix of estimation errors of mean vector obtained through the selected bootstrapping method.

  • cov_sigma (DataFrame) – The covariance matrix of estimation errors of covariance matrix obtained through the selected bootstrapping method.

  • k_mu (DataFrame) – The square root of size of elliptical constraint of mean vector estimation error based on 1-q percentile.

  • k_sigma (DataFrame) – The square root of size of elliptical constraint of covariance matrix estimation error based on 1-q percentile.

Raises:

ValueError – When the value cannot be calculated.

ParamsEstimation.normal_simulation(X, q=0.05, n_sim=6000, diag=False, threshold=1e-15, seed=0)[source]

Estimates the uncertainty sets of mean and covariance matrix assuming that assets returns follows a multivariate normal distribution.

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.

  • q (scalar) – Significance level for box and elliptical constraints. The default is 0.05.

  • n_sim (scalar) – Number of simulations of the bootstrapping method. The default is 6000.

  • diag (bool) – If consider only the main diagonal of covariance matrices of estimation errors following [B14]. The default is False.

  • threshold (float) – Parameter used to fix covariance matrices in case they are not positive semidefinite. The default is 1e-10.

  • seed (int) – Seed used to generate random numbers for simulation. The default is 0.

Returns:

  • mu_l (DataFrame) – The q/2 percentile of mean vector obtained through the normal simulation.

  • mu_u (DataFrame) – The 1-q/2 percentile of mean vector obtained through the normal simulation.

  • cov_l (DataFrame) – The q/2 percentile of covariance matrix obtained through the normal simulation.

  • cov_u (DataFrame) – The 1-q/2 percentile of covariance matrix obtained through the normal simulation.

  • cov_mu (DataFrame) – The covariance matrix of estimation errors of mean vector obtained through the normal simulation.

  • cov_sigma (DataFrame) – The covariance matrix of estimation errors of covariance matrix obtained through the normal simulation.

  • k_mu (DataFrame) – The square root of size of elliptical constraint of mean vector estimation error based on 1-q percentile.

  • k_sigma (DataFrame) – The square root of size of elliptical constraint of covariance matrix estimation error based on 1-q percentile.

Raises:

ValueError – When the value cannot be calculated.

Bibliography

[B1] (1,2,3,4,5,6)

Wolfram Barfuss, Guido Previde Massara, T. Di Matteo, and Tomaso Aste. Parsimonious modeling with information filtering networks. Physical Review E, Dec 2016. URL: http://dx.doi.org/10.1103/PhysRevE.94.062306, doi:10.1103/physreve.94.062306.

[B2] (1,2,3,4,5,6,7,8,9,10,11)

Sander Gerber, Harry Markowitz, Philip Ernst, Yinsen Miao, Babak Javid, and Paul Sargen. The gerber statistic: a robust co-movement measure for portfolio optimization. SSRN Electronic Journal, 2021. URL: https://doi.org/10.2139/ssrn.3880054, doi:10.2139/ssrn.3880054.

[B3] (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)

Marcos M. López de Prado. Machine Learning for Asset Managers. Elements in Quantitative Finance. Cambridge University Press, 2020. doi:10.1017/9781108883658.

[B4] (1,2)

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

[B5] (1,2)

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

[B6] (1,2)

Wing Cheung. The augmented black-litterman model: a ranking-free approach to factor-based portfolio construction and beyond. Quantitative Finance, 13:, 08 2007. doi:10.2139/ssrn.1347648.

[B7] (1,2)

Petter Kolm and Gordon Ritter. On the bayesian interpretation of black-litterman. European Journal of Operational Research, 258:, 10 2016. doi:10.1016/j.ejor.2016.10.027.

[B8] (1,2,3,4,5)

Attilio Meucci. Risk and Asset Allocation. Springer Berlin Heidelberg, 2005. URL: https://doi.org/10.1007/978-3-540-27904-4, doi:10.1007/978-3-540-27904-4.

[B9] (1,2,3,4,5)

Yiyong Feng and Daniel P. Palomar. A signal processing perspective of financial engineering. Foundations and Trends® in Signal Processing, 9(1-2):1–231, 2016. URL: https://doi.org/10.1561/2000000072, doi:10.1561/2000000072.

[B10] (1,2,3,4,5)

Philippe Jorion. Bayes-stein estimation for portfolio analysis. The Journal of Financial and Quantitative Analysis, 21(3):279, September 1986. URL: https://doi.org/10.2307/2331042, doi:10.2307/2331042.

[B11] (1,2,3,4,5)

Taras Bodnar, Ostap Okhrin, and Nestor Parolya. Optimal shrinkage estimator for high-dimensional mean vector. Journal of Multivariate Analysis, 170:63–79, mar 2019. URL: https://doi.org/10.1016\%2Fj.jmva.2018.07.004, doi:10.1016/j.jmva.2018.07.004.

[B12]

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:.

[B13]

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:.

[B14] (1,2)

Frank Fabozzi. Robust portfolio optimization and management. John Wiley, Hoboken, N.J, 2007. ISBN 978-0-471-92122-6.