|
用matlab求解black-litterman模型
不加资产比例限制的时候表现正常
加入资产比例限制后输出资产比例变为[1,0,0,0,0,0,0,0,0,0]
代码如下:求教各位大神!
matlab代码.zip
(5.07 KB)
function [er, ps, w, posteriorSigma] = BL_constrained(delta, weq, sigma, tau, P, Q, Omega,CashRet)%#codegen
% hlblacklitterman
% This function performs the Black-Litterman blending of the prior
% and the views into a new posterior estimate of the returns as
% described in the paper by He and Litterman.
% Inputs
% delta - Risk tolerance from the equilibrium portfolio
% weq - Weights of the assets in the equilibrium portfolio
% sigma - Prior covariance matrix
% tau - Coefficiet of uncertainty in the prior estimate of the mean (pi)
% P - Pick matrix for the view(s)
% Q - Vector of view returns
% Omega - Matrix of variance of the views (diagonal)
% Outputs
% Er - Posterior estimate of the mean returns
% w - constrained weights computed given the Posterior estimates
% of the mean and covariance of returns.
% lambda - A measure of the impact of each view on the posterior estimates.
% theta - A measure of the share of the prior and sample information in the
% posterior precision.
% Reverse optimize and back out the equilibrium returns
% This is formula (12) page 6.
pi = weq * sigma * delta;
% We use tau * sigma many places so just compute it once
ts = tau * sigma;
% Compute posterior estimate of the mean
% This is a simplified version of formula (8) on page 4.
er = pi' + ts * P' * inv(P * ts * P' + Omega) * (Q - P * pi');
% We can also do it the long way to illustrate that d1 + d2 = I
d = inv(inv(ts) + P' * inv(Omega) * P);
d1 = d * inv(ts);
d2 = d * P' * inv(Omega) * P;
er2 = d1 * pi' + d2 * pinv(P) * Q;
% Compute posterior estimate of the uncertainty in the mean
% This is a simplified and combined version of formulas (9) and (15)
ps = ts - ts * P' * inv(P * ts * P' + Omega) * P * ts;
posteriorSigma = sigma + ps;
%用Portfolio函数计算约束条件下的权重
p = Portfolio('RiskFreeRate', CashRet);
p = p.setAssetMoments(er,posteriorSigma);
p = p.setDefaultConstraints;
w = estimateMaxSharpeRatio(p);
|