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 [B2], Gerber statistic [B3] and Denoise [B1] estimators.

  • Factors models to estimate the vector of means, covariance matrix, coskewness tensor and cokurtosis square matrix [B4].

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

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

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

  • The Entropy Pooling model that allows to incorporate analyst’s views on assets’ returns in estimates of vector of means, covariance matrix, coskewness tensor and cokurtosis matrix [B9], [B10].

  • 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_assets)

Assets returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

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 [B11] and [B12].

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

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

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, alpha=0.1, bWidth=0.01, detone=False, mkt_comp=1, threshold=0.5)[source]

Calculate the covariance matrix using the selected method.

Parameters:
X : DataFrame of shape (n_samples, n_assets)

Assets returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

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: [B2].

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

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

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

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

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

d : scalar

The smoothing factor of ewma methods. The default is 0.94.

alpha : scalar

The shrfactor of shrunk and shrink method. The default is 0.1.

bWidth : float

The bandwidth of the kernel for ‘fixed’, ‘spectral’ and ‘shrink’ methods.

detone : bool, optional

If remove the first mkt_comp of correlation matrix for ‘fixed’, ‘spectral’ and ‘shrink’ methods. The detone correlation matrix is singular, so it cannot be inverted.

mkt_comp : int, optional

Number of first components that will be removed using the detone method.

threshold : float

Threshold for ‘gerber1’ and ‘gerber2’ methods is between 0 and 1.

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', alpha=0.1, bWidth=0.01, detone=False, mkt_comp=1)[source]

Calculate the cokurtosis square matrix using the selected method.

Parameters:
X : DataFrame of shape (n_samples, n_assets)

Assets returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

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 [B1].

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

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

bWidth : float

The bandwidth of the kernel for ‘fixed’, ‘spectral’ and ‘shrink’ methods.

detone : bool, optional

If remove the first mkt_comp of correlation matrix for ‘fixed’, ‘spectral’ and ‘shrink’ methods. The detone correlation matrix is singular, so it cannot be inverted.

mkt_comp : int, optional

Number of first components that will be removed using the detone method.

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_factors)

Risk factors returns matrix, where n_samples is the number of samples and n_factors is the number of risk factors.

y : Series of shape (n_samples, 1)

Asset returns column DataFrame or Series, where n_samples is 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.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_factors)

Risk factors returns matrix, where n_samples is the number of samples and n_factors is the number of risk factors.

y : Series of shape (n_samples, 1)

Asset returns column DataFrame or Series, where n_samples is 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_factors)

Risk factors returns matrix, where n_samples is the number of samples and n_factors is the number of risk factors.

y : DataFrame or Series of shape (n_samples, 1)

Asset returns column DataFrame or Series, where n_samples is 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_factors)

Risk factors returns matrix, where n_samples is the number of samples and n_factors is the number of risk factors.

Y : DataFrame of shape (n_samples, n_assets)

Assets returns DataFrame, where n_samples is the number of observations 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 – Loadings matrix.

Return type:

DataFrame

Raises:

ValueError – When the value cannot be calculated.

ParamsEstimation.risk_factors(X, Y, B=None, const=True, method_mu='hist', method_cov='hist', method_kurt='hist', feature_selection='stepwise', stepwise='Forward', criterion='pvalue', threshold=0.05, n_components=0.95, higher_comoments=False, dict_mu={}, dict_cov={}, dict_kurt={})[source]

Estimate the expected returns vector, covariance matrix, coskewness tensor and cokurtosis square matrix based on risk factors models [B15] [B16] [B4].

\[\begin{split}\begin{aligned} R & = \alpha + B F + \epsilon \\ \mu_{f} & = \alpha + B \mu_{F} \\ \Sigma_{f} & = B \Sigma_{F} B^{\prime} + \Sigma_{\epsilon} \\ \Phi_{f} & = B \Phi_{F} \left ( B^{\prime} \otimes B^{\prime} \right )+ \Phi_{\epsilon} \\ \Psi_{f} & = \left ( B \otimes B \right ) \Psi_{F} \left ( B^{\prime} \otimes B^{\prime} \right ) + \Psi_{\epsilon} \\ \end{aligned}\end{split}\]

where:

\(R\) is the series returns.

\(\alpha\) is the intercept.

\(B\) is the loadings matrix.

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

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

\(\Phi_{F}\) is the coskewness tensor of the risk factors.

\(\Psi_{F}\) is the cokurtosis square matrix of the risk factors.

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

\(\Phi_{\epsilon}\) is the coskewness tensor of error terms.

\(\Psi_{\epsilon}\) is the cokurtosis square 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.

\(\Phi_{f}\) is the coskewness tensor obtained with the risk factor model.

\(\Psi_{f}\) is the cokurtosis square matrix obtained with the risk factor model.

Parameters:
X : DataFrame of shape (n_samples, n_factors)

Risk factors returns matrix, where n_samples is the number of samples and n_factors is the number of risk factors.

Y : DataFrame of shape (n_samples, n_assets)

Assets returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

B : DataFrame of shape (n_assets, n_factors), optional

Loadings matrix, where n_assets is the number assets and n_factors is the number of risk factors. 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 [B11] and [B12].

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

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

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: [B2].

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

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

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

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

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

method_kurt : 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 [B1].

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

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

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.

dict_mu : dict

Other variables related to the expected returns.

dict_cov : dict

Other variables related to the covariance estimation.

dict_kurt : dict

Other variables related to the cokurtosis estimation.

Returns:

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

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

  • skew (DataFrame) – The coskewness tensor of risk factors model.

  • kurt (DataFrame) – The cokurtosis square matrix of risk factors model.

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

  • B (DataFrame) – Loadings matrix.

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', dict_mu={}, dict_cov={})[source]

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

\[\begin{split}\begin{aligned} \Pi & = \delta \Sigma w \\ \Pi_{BL} & = \left [ (\tau\Sigma)^{-1}+ P^{\prime} \Omega^{-1}P \right]^{-1} \left[(\tau\Sigma)^{-1} \Pi + P^{\prime} \Omega^{-1} Q \right] \\ M & = \left((\tau\Sigma)^{-1} + P^{\prime}\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 returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

w : DataFrame or Series of shape (n_assets, 1)

Portfolio weights, 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 [B11] and [B12].

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

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

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: [B2].

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

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

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

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

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

dict_mu : dict

Other variables related to the mean vector estimation method.

dict_cov : dict

Other variables related to the covariance estimation method.

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, B, P=None, Q=None, P_f=None, Q_f=None, delta=1, rf=0, eq=True, const=True, method_mu='hist', method_cov='hist', dict_mu={}, dict_cov={})[source]

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

\[\begin{split}\begin{aligned} \Pi^{a} & = \delta \left [ \begin{array}{c} \Sigma \\ \Sigma_{F} B^{\prime} \\ \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^{\prime} & \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})^{\prime} (\Omega^{a})^{-1} P^{a} \right ]^{-1} \left [ (\tau\Sigma^{a})^{-1} \Pi^{a} + (P^{a})^{\prime} (\Omega^{a})^{-1} Q^{a} \right ] \\ M^{a} & = \left ( (\tau\Sigma^{a})^{-1} + (P^{a})^{\prime} (\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 returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

w : DataFrame or Series of shape (n_assets, 1)

Portfolio weights, where n_assets is the number of assets.

F : DataFrame of shape (n_samples, n_factors)

Risk factors returns DataFrame, where n_samples is the number of samples and n_factors is the number of risk factors.

B : DataFrame of shape (n_assets, n_factors), optional

Loadings matrix, where n_assets is the number assets and n_factors is the number of risk factors.

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_factors)

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.

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 [B11] and [B12].

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

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

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: [B2].

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

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

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

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

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

dict_mu : dict

Other variables related to the mean vector estimation method.

dict_cov : dict

Other variables related to the covariance estimation method.

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, const=True, method_mu='hist', method_cov='hist', dict_mu={}, dict_cov={})[source]

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

\[\begin{split}\begin{aligned} \Sigma_{F} & = B \Sigma_{F} B^{\prime} + D \\ \overline{\Pi}_{F} & = \left ( \Sigma_{F}^{-1} + P_{F}^{\prime}\Omega_{F}^{-1}P_{F} \right )^{-1} \left ( \Sigma_{F}^{-1}\Pi_{F} + P_{F}^{\prime}\Omega_{F}^{-1}Q_{F} \right) \\ \overline{\Sigma}_{F} & = \left ( \Sigma_{F}^{-1} + P_{F}^{\prime}\Omega_{F}^{-1}P_{F} \right )^{-1} \\ \Sigma_{BLB} & = \left( \Sigma^{-1} - \Sigma^{-1} B \left( \overline{\Sigma}_{F}^{-1} + B^{\prime}\Sigma^{-1}B \right)^{-1} B^{\prime}\Sigma^{-1} \right )^{-1} \\ \mu_{BLB} & = \Sigma_{BLB} \left ( \Sigma^{-1} B \left( \overline{\Sigma}_{F}^{-1} +B^{\prime}\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 returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

F : DataFrame of shape (n_samples, n_factors)

Risk factors returns DataFrame, where n_samples is the number of samples and n_factors is the number of risk factors.

B : DataFrame of shape (n_assets, n_factors), optional

Loadings matrix, where n_assets is the number assets and n_factors is the number of risk factors. The default is None.

P_f : DataFrame of shape (n_views, n_factors)

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.

const : bool, optional

Indicate if the loadings matrix has a constant. 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 [B11] and [B12].

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

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

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: [B2].

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

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

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

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

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

dict_mu : dict

Other variables related to the mean vector estimation method.

dict_cov : dict

Other variables related to the covariance estimation method.

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.entropy_pooling(X: ndarray | DataFrame, P_eq: ndarray = None, Q_eq: ndarray = None, P_in: ndarray = None, Q_in: ndarray = None, higher_comoments: bool = False, solver: str = 'CLARABEL')[source]

Estimate the optimal scenario weights and comoments parameters using the Entropy Pooling model. The Entropy Pooling model consist in solve the following optimization problem:

\[\begin{split}\begin{align} \min_{Z} & \quad \sum^{T}_{i=1}Z_{i} \ln (Z_{i}) - Z_{i} ln \left ( \frac{1}{T} \right) \\ \text{s. t.} & \quad Z \geq 0\\ & \quad Z \leq 1\\ & \quad \mathbf{1}^{\prime}_{T} Z = 1\\ & \quad P_{eq} Z = Q_{eq}\\ & \quad P_{in} Z \geq Q_{in}\\ \end{align}\end{split}\]

Where:

\(Z\) are the scenario weights.

Parameters:
X : DataFrame of shape (n_samples, n_assets)

Assets returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

P_eq : np.array, optional

Matrix P of views that can be expressed as equality constraints.

Q_eq : np.array, optional

Matrix Q of views that can be expressed as equality constraints.

P_in : np.array, optional

Matrix P of views that can be expressed as inequality constraints.

Q_in : np.array, optional

Matrix Q of views that can be expressed as inequality constraints.

higher_comoments : bool, optional

If True calculate coskewness tensor and cokurtosis square matrix.

solver : str, optional

Solver used to solve the Entropy Pooling problem. The default is value ‘CLARABEL’.

Returns:

Z – Optimal scenario weights based on the Entropy Pooling model.

Return type:

np.array

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_assets)

Assets returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

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_factors + 1 The default is 3.

diag : bool

If consider only the main diagonal of covariance matrices of estimation errors following [B17]. 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_assets)

Assets returns DataFrame, where n_samples is the number of observations and n_assets is the number of assets.

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 [B17]. 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,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)

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

[B2] (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, 12 2016. URL: http://dx.doi.org/10.1103/PhysRevE.94.062306, doi:10.1103/physreve.94.062306.

[B3] (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.

[B4] (1,2)

Kris Boudt, Wanbo Lu, and Benedict Peeters. Higher order comoments of multifactor models and asset allocation. Finance Research Letters, 13:225–233, May 2015. URL: http://dx.doi.org/10.1016/j.frl.2014.12.008, doi:10.1016/j.frl.2014.12.008.

[B5] (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.

[B6] (1,2)

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

[B7] (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.

[B8] (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.

[B9]

Attilio Meucci. Fully flexible views: theory and practice. SSRN Electronic Journal, 2010. URL: https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1213325.

[B10]

Jan Alexandersson. The entropy pooling approach: incorporating views on forecast distributions. Master's thesis, Stockholm University, Universitetsvägen 10 A, 114 18 Stockholm, Sweden, 12 2021. URL: https://kurser.math.su.se/pluginfile.php/20130/mod_folder/content/0/Master/2021/2021_7_report.pdf.

[B11] (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.

[B12] (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.

[B13] (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.

[B14] (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, 03 2019. URL: https://doi.org/10.1016\%2Fj.jmva.2018.07.004, doi:10.1016/j.jmva.2018.07.004.

[B15]

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

[B16]

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

[B17] (1,2)

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