Can somebody help with the below as i have been struggling for sometime.
Marginal contribution to risk= To find the marginal contribution of each asset, take the cross-product of the weights vector and the covariance matrix divided by the portfolio standard deviation.
Now multiply the marginal contribution of each asset by the weights vector to get total contribution. We can then sum the asset contributions and make sure it’s equal to the total portfolio standard deviation.
Relativeweights = np.array([0.02, -0.025, -0.015, 0.0, 0.02,0,0,0])
cov_matrix_a= 8x8 matrix
Port_volatility= 0.05882615906289199
So i have tried the following code;
MCTAR=(np.dot(Relativeweights,cov_matrix_a))/Port_volatility
TCTPR=MCTAR*Relativeweights
np.sum(TCTPR, axis=0)
This doesn't sum to the portfolio volatility so not sure what is wrong?
Any help appreciated. Thanks
Full code below;
Pull in returns from csv file
import numpy as np
import ST as STPL
Assetreturns = STPL.get_Asset_returns()
Construct a covariance matrix for the portfolio's daily returns with the .cov() method
cov_matrix_Assetreturns = Assetreturns.cov()
cov_matrix_Assetreturns
Annualise the daily covariance matrix with the standard 252 trading days
cov_matrix_a = cov_matrix_Assetreturns * 252
cov_matrix_a
Assign relative weights
Relativeweights = np.array([0.02, -0.025, -0.015, 0.0, 0.02,0,0,0])
Relativeweights
Assign portfolio weights
Portweights = np.array([0.49, 0.15, 0.125, 0.215, 0.02, 0, 0,0])
Portweights
The standard deviation of a portfolio is just a square root of its variance
Port_volatility = np.sqrt(np.dot(Portweights.T, np.dot(cov_matrix_a, Portweights)))
Port_volatility
Marginal contribution of each asset, take the cross-product of the weights vector and the covariance matrix divided by the portfolio standard deviation.
MCTAR=(np.dot(Relativeweights,cov_matrix_a))/Port_volatility
Now multiply the marginal contribution of each asset by the weights vector to get total contribution. We can then sum the asset contributions and make sure it’s equal to the total portfolio standard deviation.
TCTPR=MCTAR*Relativeweights
np.sum(TCTPR, axis=0)