资源简介
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
评论
共有 条评论