資源簡介
Black-Litterman實現的Matlab代碼,配合博文【BL】Black-Litterman Portfolio Optimization使用

代碼片段和文件信息
clear?all;
clc;
close?all;
%%?數據讀入和預處理
T?=?readtable(‘dowPortfolio.xlsx‘);
names=[“AA“?“AIG“?“WMT“?“MSFT“?“BA“?“GE“?“IBM“];
benchmark=“DJI“;?%?設置道瓊斯指數為benchmark
%?數據預覽
head(T(:?[‘Dates‘?benchmark?names]))
%%?預處理,分離資產組合和benchmark
ret?=?tick2ret(T(:?2:end));
asset_ret?=?ret(:?names);
dj_ret=ret(:?benchmark);
num=size(asset_ret?2);
%%?觀點生成
v=3;?%?觀點的數量為3
P=zeros(v?num);?%?pick?matrix
q=zeros(v?1);
Omega=zeros(v);
%?view?1:?AIG
P(1?names==“AIG“)=1;
q(1)=0.05;
Omega(1?1)=1e-3;
%?view?2:?WMT
P(2?names==“WMT“)=1;
q(2)=0.03;
Omega(2?2)=1e-3;
%?view?3:?MSFT?and?IBM
P(3?names==“MSFT“)=1;
P(3?names==“IBM“)=-1;
q(3)=0.05;
Omega(3?3)=1e-5;
%%?可視化觀點矩陣
viewTable=array2table([P?q?diag(Omega)]?“VariableNames“?[names?“ViewReturn“?“ViewsUncertainty“]);
%%?數據口徑統一
factor=1/252;?%設置一年有252個交易日
q=q*factor;?%?收益率日度化
Omega=Omega*factor;?%?觀點矩陣不確定程度日度化
%%?估計方差協方差矩陣
Sigma=cov(asset_ret.Variables);
%%?對先驗分布的不確定程度C
tau=1/size(asset_ret.Variables?1);
C=tau*Sigma;
%%?計算市場隱含均衡收益
[wtsMarket?PI]=findMarketPortfolioAndImpliedReturn(asset_ret.Variables?dj_ret.Variables);
%%?混合均值和方差
mu_bl=(P‘*(Omega\P)+inv(C))\(C\PI+P‘*(Omega\q));
cov_bl=inv(P‘*(Omega\P)+inv(C));
%%?顯示結果
table(names‘?PI/factor?mu_bl/factor?‘VariableNames‘?[“Asset_Name“?“Prior_Belief_of_ER“?“BL_Blended_ER“])
%%?MV框架下模型求解
%?MV模型
port?=?Portfolio(‘NumAssets‘?num?‘lb‘?0?‘budget‘?1?‘Name‘?‘Mean?Variance‘);
port?=?setAssetMoments(port?mean(asset_ret.Variables)?Sigma);
wts=estimateMaxSharpeRatio(port);?%?目標函數為求解最大Sharpe?Ratio
%?BL模型
portBL?=?Portfolio(‘NumAssets‘?num?‘lb‘?0?‘budget‘?1?‘Name‘?‘BL?Mean?Variance‘);
portBL?=?setAssetMoments(portBL?mu_bl?Sigma+cov_bl);
wtsBL=estimateMaxSharpeRatio(portBL);?%?目標函數為求解最大Sharpe?Ratio
%%?plot?
tol=0.01;?%?只統計權重超過1%的資產
ax1=subplot(1?2?1);
idx?=?wts>tol;?
pie(ax1?wts(idx)?names(idx));
title(ax1?port.Name?‘Position‘?[-0.05?1.60?0]);
ax2=subplot(12?2);
idxBL=wtsBL>tol;
pie(ax2?wtsBL(idxBL)?names(idxBL));
title(ax2?portBL.Name?‘Position‘?[-0.05?1.60?0]);
%%?打表
table(names‘?wts?wtsBL?‘VariableNames‘?[“AssetName“?“MV“?“MV_BL“])
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2372??2020-09-09?21:13??bl.m
?????文件????????460??2020-09-09?20:24??findMarketPortfolioAndImpliedReturn.m
-----------?---------??----------?-----??----
?????????????????2832????????????????????2
評論
共有 條評論