%%设置初始表格
Asset = { ‘Bonds’, ‘Large-Cap Equities’, ‘Small-Cap Equities’, ‘Emerging Equities’ };
Price = [ 52.4; 122.7; 35.2; 46.9 ];
Holding = [ 42938; 24449; 42612; 15991 ];
UnitCost = [ 0.001; 0.001; 0.001; 0.004 ];
Blotter = table(‘RowNames’, Asset);
Blotter.Price = Price;
Blotter.InitHolding = Holding;
Wealth = sum(Blotter.Price .* Blotter.InitHolding);
Blotter.InitPort = (1/Wealth)(Blotter.Price . Blotter.InitHolding);
Blotter.UnitCost = UnitCost;
Blotter
%% 模拟价格
AssetMean = [ 0.05; 0.1; 0.12; 0.18 ];
AssetCovar = [ 0.0064 0.00408 0.00192 0;
0.00408 0.0289 0.0204 0.0119;
0.00192 0.0204 0.0576 0.0336;
0 0.0119 0.0336 0.1225 ];
X = portsim(AssetMean’/12, AssetCovar/12, 60); % monthly total returns for 5 years (60 months)
[Y, T] = ret2tick(X, [], 1/12); % form total return prices
%%不包含手续费---------------------------------------------------
pp = Portfolio(‘Name’, ‘Asset Allocation Portfolio’, …
‘AssetList’, Asset, ‘InitPort’, Blotter.InitPort);
%%设置交易条件
p = setDefaultConstraints§;
p = setGroups(p, [ 0, 1, 1, 1 ], [], 0.85);
p = addGroups(p, [ 0, 0, 0, 1 ], [], 0.35);
p = setAssetMoments(p, AssetMean/12, AssetCovar/12);
p = estimateAssetMoments(p, Y, ‘DataFormat’, ‘Prices’);%%获得均值和方差
p.AssetMean = 12p.AssetMean;
p.AssetCovar = 12p.AssetCovar;
display§;
%% 看组合是否能够优化
[lb,ub] = estimateBounds§’
disp([lb,ub]);
%%画出有效前沿
plotFrontier(p,40);
%%包含手续费-----------------------------------------------------
q = setCost(p,unitcost,unicost);
%%计算初始资产的风险和收益
[risk,ret] = estimatePortMoments(p,p.initPort);
%%计算有效前沿上最优组合
pret = estimatePortReturn(p, p.estimateFrontierLimits);
qret = estimatePortReturn(q, q.estimateFrontierLimits);