BlackLitterman
- class qf_lib.portfolio_construction.black_litterman.black_litterman.BlackLitterman(hist_cov: QFDataFrame, weights: QFSeries, sample_size: int, sharpe: float = 0.5)[source]
Bases:
object
Creates an object allowing calculation of the distribution of the returns using Black Litterman model
- Parameters:
hist_cov (QFDataFrame) – covariance matrix of historical excess returns of assets; should be annualized
weights (QFSeries) – weights of assets in the market cap index
sample_size (int) – number of simple returns used to estimate the historical covariance
sharpe (float) – average sharpe ratio of the market (by default value of 0.5 is used)
Attributes:
Matrix of variance of the views Matrix is square and diagonal.
Matrix of views P.
Vector of expected performance Q.
Methods:
add_absolute_view
(asset_index, ...)Adds a new view that will be taken into account in calculating the posterior
add_relative_view
(outperforming_asset_index, ...)Adds a new view that will be taken into account in calculating the posterior The investor believs that outperforming_asset will outperform the underperforming_asset by outperformance with the volatility of the view of view_vol
Calculates lambda corresponding to risk aversion in the formula: max(w): w'R - lambda/2 * w' * Cov * w:
Calculate shape of posterior distribution including the views.
Function calculates the prior for the BL model: - prior_mean = lambda * hist_cov * market_cap_weights - prior_cov = tau * hist_cov
- property Omega: array
Matrix of variance of the views Matrix is square and diagonal. Each value Omega[i,i] corresponds to the variance of a specific view nr of rows = number of different views nr of columns = number of different views For example: - (0.0001, 0.0000) means we expect volatility of the first view to be sqrt(0.0001) = 0.01 = 1% - (0.0000, 0.0025) means we expect volatility of the first view to be sqrt(0.0025) = 0.05 = 5% NOTE: self._omega is stored as horizontal vector. Values are copied on diagonal
- property P: array
Matrix of views P. nr of rows = number of different views nr of columns = number of assets Example for 4 assets and 2 views:
(0, 1, 0, 0) means go long second asset
(0, 1, 0, -1) means go long second asset while shorting first asset
- property Q: array
Vector of expected performance Q. Also sometimes refered as vector v nr of rows = number of different views nr of columns = 1 For example: - [0.10] - [0.03] means that we have two views and we expect to realise 10% above risk free rate on first view and 3% on second
- add_absolute_view(asset_index: int, outperformance: float, view_vol: float)[source]
Adds a new view that will be taken into account in calculating the posterior
- Parameters:
asset_index (int) – index of the asset that we expect to outperform the market (indexing starts at 0)
outperformance (float) – how much are we expecting one asset to outperform the market
view_vol (float) – volatility of the view. This is the measure of a standard deviation of the outperformance value. For example: 0.02 means that the real outperformance will be provided outperformance +- 2% within one standard deviation confidence interval.
- add_relative_view(outperforming_asset_index: int, underperforming_asset_index: int, outperformance: float, view_vol: float)[source]
Adds a new view that will be taken into account in calculating the posterior The investor believs that outperforming_asset will outperform the underperforming_asset by outperformance with the volatility of the view of view_vol
- Parameters:
outperforming_asset_index (int) – index of the asset that you believe will outperform (indexing starts at 0)
underperforming_asset_index (int) – index of the asset that you believe will underperform (indexing starts at 0)
outperformance (float) – how much are we expecting one asset to outperform the other
view_vol (float) – volatility of the outperformance. This is the measure of a standard deviation of the outperformance value. For example: 0.02 means that the real outperformance will be provided outperformance +- 2% within one standard deviation confidence interval.
- calculate_lambda()[source]
Calculates lambda corresponding to risk aversion in the formula: max(w): w’R - lambda/2 * w’ * Cov * w:
lambda is calculated in the followign way: lambda = sharpe_ratio_of_market / vol_of_market.
- calculate_posterior() Tuple[QFSeries, QFDataFrame] [source]
Calculate shape of posterior distribution including the views. It is calculated using a numerically stable formula posterior_mean = prior_mean + tau * hist_cov * P’ * ( tau * P * hist_cov * P’ + Omega)^(-1) * (Q - P * prior_mean) posterior_cov = (1 + tau) * hist_cov - tau^2 * hist_cov * P’ * (tau * P * hist_cov * P’ + Omega)^(-1) * P * hist_cov
- Return type:
Tuple[QFSeries, QFDataFrame]
- calculate_prior() Tuple[QFSeries, QFDataFrame] [source]
Function calculates the prior for the BL model: - prior_mean = lambda * hist_cov * market_cap_weights - prior_cov = tau * hist_cov
- Returns:
Tuple(prior_mean, prior_cov)
- Return type:
Tuple[QFSeries, QFDataFrame]