【代码】Financial Toolbox:Portfolio Optimization

对Matlab金融工具箱内的Portfolio_Optimization_Example代码的阅读笔记;

【Example】

打开文档

打开Matlab,在command中输入:

1
openExample('finance/portfolioexamples')

可打开文档

加载数据

1
2
3
4
5
6
load BlueChipStockMoments % 加载官方给的example

mret = MarketMean; % mret = market_return
mrsk = sqrt(MarketVar); % mrsk = market_risk
cret = CashMean; % cret = cash_return
crsk = sqrt(CashVar); % cret = cash_risk

初始化一个Portfolio对象

1
2
3
p = Portfolio('AssetList',AssetList,'RiskFreeRate',CashMean);
p = setAssetMoments(p,AssetMean,AssetCovar); % 加载数据
% [m, cov] = getAssetMoments(p); % 提取数据

AssetList:Asset名称的List,如果自己导入data,可采用:

1
2
data = readtable('data.xlsx');
AssetList = data.Properties.VariableNames';

【可选步骤】

1.初始化一个 equal-weight portfolio
1
2
p = setInitPort(p,1/p.NumAssets); 
[ersk,eret] = estimatePortMoments(p,p.InitPort); % 给出对应的equal-weight risk,return
2.画一个所有Asset在risk-return上的分布图
1
2
3
4
5
6
7
clf; % 删除所有figure
portfolioexamples_plot('Asset Risks and Returns', ... % title
{'scatter', mrsk, mret, {'Market'}}, ... % 点状图,x坐标,y坐标,名称
{'scatter', crsk, cret, {'Cash'}}, ...
{'scatter', ersk, eret, {'Equal'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});
% diag取对角矩阵;‘.r’图形样式

在各种约束下的投资组合优化

1.初始化一个long only portfolios

1
2
3
4
5
6
7
8
9
10
11
12
p = setDefaultConstraints(p);

pwgt = estimateFrontier(p,20); % 为Frontier输出weight,第二个值确定个数
[prsk,pret] = estimatePortMoments(p,pwgt); % 输出对应risk和return
% 或者直接使用 plotFrontier(p,20);

% Plot efficient frontier
clf;
portfolioexamples_plot('Efficient Frontier', ...
{'line', prsk, pret}, ...
{'scatter', [mrsk, crsk, ersk], [mret, cret, eret], {'Market', 'Cash', 'Equal'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});

2.初始化一个portfolios with budget constraint

1
2
3
4
5
6
7
8
9
10
11
12
q = setBudget(p, 0, 0.8); % cash in [0,80%]

qwgt = estimateFrontier(q,20);
[qrsk,qret] = estimatePortMoments(q,qwgt);

% Plot efficient frontier with tangent line (0 to 0.8 cash)
clf;
portfolioexamples_plot('Efficient Frontier with Tangent Line', ...
{'line', prsk, pret}, ...
{'line', qrsk, qret, [], 'r', 0.5}, ...
{'scatter', [mrsk, crsk, ersk], [mret, cret, eret], {'Market', 'Cash', 'Equal'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});

3.初始化一个portfolios其交易包含Transactions Costs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BuyCost = 0.0020;
SellCost = 0.0020;

q = setCosts(p,BuyCost,SellCost);

qwgt = estimateFrontier(q,20);
[qrsk,qret] = estimatePortMoments(q,qwgt);

% Plot efficient frontiers with gross and net returns
clf;
portfolioexamples_plot('Efficient Frontier with and without Transaction Costs', ...
{'line', prsk, pret, {'Gross'}, ':b'}, ...
{'line', qrsk, qret, {'Net'}}, ...
{'scatter', [mrsk, crsk, ersk], [mret, cret, eret], {'Market', 'Cash', 'Equal'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});

4.初始化一个portfolios其交易加入了Turnover Constraint

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BuyCost = 0.0020;
SellCost = 0.0020;
Turnover = 0.2;

q = setCosts(p, BuyCost,SellCost);
q = setTurnover(q,Turnover);

[qwgt,qbuy,qsell] = estimateFrontier(q,20);
[qrsk,qret] = estimatePortMoments(q,qwgt);

% Plot efficient frontier with turnover constraint

clf;
portfolioexamples_plot('Efficient Frontier with Turnover Constraint', ...
{'line', prsk, pret, {'Unconstrained'}, ':b'}, ...
{'line', qrsk, qret, {sprintf('%g%% Turnover', 100*Turnover)}}, ...
{'scatter', [mrsk, crsk, ersk], [mret, cret, eret], {'Market', 'Cash', 'Equal'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});
displaySumOfTransactions(Turnover, qbuy, qsell)

5.依照最大夏普比率得到最优组合

1
2
3
4
5
6
7
8
9
10
11
12
13
p = setInitPort(p, 0);

swgt = estimateMaxSharpeRatio(p);
[srsk,sret] = estimatePortMoments(p,swgt);

% Plot efficient frontier with portfolio that attains maximum Sharpe ratio

clf;
portfolioexamples_plot('Efficient Frontier with Maximum Sharpe Ratio Portfolio', ...
{'line', prsk, pret}, ...
{'scatter', srsk, sret, {'Sharpe'}}, ...
{'scatter', [mrsk, crsk, ersk], [mret, cret, eret], {'Market', 'Cash', 'Equal'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});

并与切线结合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
q = setBudget(p, 0, 1);

qwgt = estimateFrontier(q,20);
[qrsk,qret] = estimatePortMoments(q,qwgt);

% Plot that shows Sharpe ratio portfolio is the tangency portfolio

clf;
portfolioexamples_plot('Efficient Frontier with Maximum Sharpe Ratio Portfolio', ...
{'line', prsk, pret}, ...
{'line', qrsk, qret, [], [], 1}, ...
{'scatter', srsk, sret, {'Sharpe'}}, ...
{'scatter', [mrsk, crsk, ersk], [mret, cret, eret], {'Market', 'Cash', 'Equal'}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});

其他

1.获得risk和return的范围
1
2
3
4
[rsk,ret] = estimatePortMoments(p,estimateFrontierLimits(p));

display(rsk);
display(ret);
2.利用固定return或risk得到值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
TargetReturn = 0.20;
TargetRisk = 0.15;

awgt = estimateFrontierByReturn(p,TargetReturn/12); % 按月度数据/12
[arsk,aret] = estimatePortMoments(p,awgt);

bwgt = estimateFrontierByRisk(p,TargetRisk/sqrt(12));
[brsk,bret] = estimatePortMoments(p,bwgt);

% Plot efficient frontier with targeted portfolios
clf;
portfolioexamples_plot('Efficient Frontier with Targeted Portfolios', ...
{'line', prsk, pret}, ...
{'scatter', [mrsk, crsk, ersk], [mret, cret, eret], {'Market', 'Cash', 'Equal'}}, ...
{'scatter', arsk, aret, {sprintf('%g%% Return',100*TargetReturn)}}, ...
{'scatter', brsk, bret, {sprintf('%g%% Risk',100*TargetRisk)}}, ...
{'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList, '.r'});

3.显示portfolio的包含的Assets和weight占比
1
2
3
4
5
6
7
aBlotter = dataset({100*awgt(awgt > 0),'Weight'}, 'obsnames', p.AssetList(awgt > 0));

displayPortfolio(sprintf('Portfolio with %g%% Target Return', 100*TargetReturn), aBlotter, false);

bBlotter = dataset({100*bwgt(bwgt > 0),'Weight'}, 'obsnames', p.AssetList(bwgt > 0));

displayPortfolio(sprintf('Portfolio with %g%% Target Risk', 100*TargetRisk), bBlotter, false);

Reference

  1. MathWork - Help Center

【代码】Financial Toolbox:Portfolio Optimization
http://achlier.github.io/2021/03/11/Financial_Toolbox-Portfolio_Optimization/
Author
Hailey
Posted on
March 11, 2021
Licensed under