Вы находитесь на странице: 1из 16

# Final Project 4/29/18, 5*19 PM

Final Project

## Econ 4360 Kevin Dong (jd8nb)

Contents
Import and select data
Post-Crisis Sharpe ratio for each factor
Plot cumulative returns
Portfolio Optimization Constraints & Options
Optimize monthly using 36 month moving window, saving statistics (weights,returns,risk analysis)
Monthly Returns Attribution - select factors
Monthly estimates through 36 month moving window regressions (betas, tstats)
Calculate Returns Attribution across factors: Rp = a + b1X1 + ... + bkXk + residual
Plot time series of beta over time
Efficient Frontier
Risk Parity
Monte Carlo

## Import and select data

clc;clear
date = datetime(country.Date);
M = [country.EU, country.japan,country.us, country.china, country.brazil, country.india];
selectedVariables = {'EU';'Japan';'US';'China';'Brazil';'India'};

[T,k] = size(M);
mean_return = 100*(prod(1+M/100).^(1/T)-1);
annual_return = (mean_return.^12)-1;
s = std(M);
annual_stdev = s*sqrt(12);

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 1 of 16
Final Project 4/29/18, 5*19 PM

sharpe = annual_return./annual_stdev;
skew = skewness(M);
kt=kurtosis(M);

Table = table(annual_return',annual_stdev',sharpe',skew',kt','VariableNames',{'mean_return','risk','Sharpe','Skew','Kurtosis'
})

Table =

6×5 table

## mean_return risk Sharpe Skew Kurtosis

___________ ______ _________ ________ ________

## -0.99999 15.102 -0.066217 -0.50431 4.0196

-1 17.886 -0.055911 -0.30726 3.8786
-0.99981 14.546 -0.068733 -0.56487 4.1565
-0.98368 29.222 -0.033662 0.40298 6.7293
-0.88605 36.633 -0.024187 -0.02194 3.927
8.4815 25.111 0.33776 -0.11275 4.1202

## Post-Crisis Sharpe ratio for each factor

post_M = M(127:end,:);
mean_return = 100*(prod(1+post_M/100).^(1/T)-1);
annual_return = (mean_return.^12)-1;
s = std(post_M);
annual_stdev = s*sqrt(12);
sharpe = annual_return./annual_stdev;
skew = skewness(post_M);
kt=kurtosis(post_M);

Table = table(annual_return',annual_stdev',sharpe',skew',kt','VariableNames',{'mean_return','risk','Sharpe','Skew','Kurtosis'
})

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 2 of 16
Final Project 4/29/18, 5*19 PM

Table =

6×5 table

## mean_return risk Sharpe Skew Kurtosis

___________ ______ _________ ________ ________

## -0.99999 12.323 -0.081146 -0.33006 3.438

-1 17.438 -0.057345 -0.17052 2.9896
-0.99928 12.075 -0.082755 -0.18796 3.4268
-1 19.776 -0.050566 -0.2773 3.7198
-1 30.221 -0.033089 0.37186 3.5592
-0.99998 15.962 -0.062647 0.045354 2.9159

## Plot cumulative returns

date = datetime(factors.Date);
post_date = date(127:end,:);
BR = [factors.RealRates,factors.Inflation,factors.Credit,factors.Economic,factors.EmergingMarkets,factors.Liquidity];
post_BR = BR(127:end,:);

CumulativeReturn = cumprod(1+post_BR/100);
plot(post_date(:,1),CumulativeReturn(:,1:6));legend('realRates','inflation','credit','economic','EM','liquidity');title("Post
-Crsis Returns for Blackrock Factors");

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 3 of 16
Final Project 4/29/18, 5*19 PM

## nAssets = numel(mean_return); r = mean(mean_return)*2;

Aeq = ones(1,nAssets); beq = 1;
Aineq = -mean_return; bineq = -r;
lb = zeros(nAssets,1); ub = ones(nAssets,1);
c = zeros(nAssets,1);
options = optimset('Algorithm','interior-point-convex');
options = optimset(options,'Display','off','TolFun',1e-10);

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 4 of 16
Final Project 4/29/18, 5*19 PM

Optimize monthly using 36 month moving window, saving statistics (weights,returns,risk analysis)

for t = 36:T
V = cov(M(t-35:t,:));
wrp = rsk_parity(mean_return',V,lb,ub,0);

wtarg(:,t) = wtemp1;
wmv(:,t) = wtemp2;
wrp(:,t) = wrp;
Rp_mv(t) = M(t,:)*wmv(:,t);
Rp_targ(t) = M(t,:)*wtarg(:,t);
Rp_rp(t) = M(t,:)*wrp(:,t);
risk_wtarg(t) = sqrt(wtarg(:,t)'*V*wtarg(:,t));
risk_wmv(t) = sqrt(wmv(:,t)'*V*wmv(:,t));
risk_rp(t) = sqrt(wrp(:,t)'*V*wrp(:,t));
rho(t) = sqrt(wtarg(:,t)'*V*wmv(:,t))/(risk_wtarg(t)*risk_wmv(t));
mctr_targ(:,t) = V*wtarg(:,t)/risk_wtarg(t);
mctr_mv(:,t) = V*wmv(:,t)/risk_wmv(t);
mctr_rp(:,t) = V*wrp(:,t)/risk_rp(t);
riskBud_targ(:,t) = mctr_targ(:,t).*wtarg(:,t);
riskBud_mv(:,t) = mctr_mv(:,t).*wmv(:,t);
riskBud_rp(:,t) = mctr_rp(:,t).*wrp(:,t);
end
% Make a Table for time t (e.g, t = 117 is Lehman Bros. collapse 9/2008)
% So 117 + 36 = 153 (adding in the 36 month look-back window).
t = 153

Performance = table(round(wmv(:,t),3),round(wtarg(:,t),3),round(mctr_targ(:,t),3),round(mctr_mv(:,t),3),...
round(riskBud_targ(:,t),3),round(riskBud_mv(:,t),3),...
'VariableNames',{'wmv','wtarg','mctr_targ','mctr_mv','riskBud_targ','riskBud_mv'},'RowNames',selectedVariables)
MiscStats = table(risk_wmv(t), risk_wtarg(t),rho(t),'VariableNames',{'risk_mv','risk_targ','correlation'})

t =

153

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 5 of 16
Final Project 4/29/18, 5*19 PM

Performance =

6×6 table

## wmv wtarg mctr_targ mctr_mv riskBud_targ riskBud_mv

_____ _____ _________ _______ ____________ __________

## EU 0.728 0.001 4.755 5.389 0.006 3.925

Japan 0.229 0 4.637 5.389 0 1.236
US 0 1.074 5.998 5.451 6.441 0
China 0.042 0 6.361 5.389 0 0.228
Brazil 0 0 9.083 7.897 0 0
India 0 0.295 7.677 6.416 2.261 0

MiscStats =

1×3 table

## risk_mv risk_targ correlation

_______ _________ ___________

## Monthly Returns Attribution - select factors

BR = [factors.RealRates,factors.Inflation,factors.Credit,factors.Economic,factors.EmergingMarkets,factors.Liquidity];
AQR = [factors.BAB, factors.MKT,factors.SMB,factors.HML,factors.UMD];
% Select regressors (BR or AQR)
X = [ones(T,1),BR]; Y = Rp_targ';

## Monthly estimates through 36 month moving window regressions (betas, tstats)

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 6 of 16
Final Project 4/29/18, 5*19 PM

for t = 36:T
b(:,t) = inv(X(t-35:t,:)'*X(t-35:t,:))*X(t-35:t,:)'*Y(t-35:t);
yhat = X(t-35:t,:)*b(:,t);
ESS = sum((yhat-Y(t-35:t)).^2);
SSb = ESS/(T-k)*inv(X(t-35:t,:)'*X(t-35:t,:));
sb = sqrt(diag(SSb));
tstats(:,t) = b(:,t)./sb;
end

## temp = b'.*X; % Individual factor returns

alpha = temp(:,1) + (Y-sum(temp,2)); % intercept plus residual (specific) return
ReturnsAtt = table(date,Y,alpha,temp(:,2),temp(:,3),temp(:,4),temp(:,5),temp(:,6),temp(:,7),'VariableNames',...
{'Date','Rport','alpha','realRates','inflation','credit','economic','EM','liquidity'});
ReturnsAtt = ReturnsAtt(36:end,:);

## subplot(2,3,1); plot(ReturnsAtt.Date,ReturnsAtt.realRates); title('Real Rates'); grid on;

subplot(2,3,2); plot(ReturnsAtt.Date,ReturnsAtt.inflation); title('Inflation'); grid on;
subplot(2,3,3); plot(ReturnsAtt.Date,ReturnsAtt.credit); title('Credit'); grid on;
subplot(2,3,4); plot(ReturnsAtt.Date,ReturnsAtt.economic); title('Economic'); grid on;
subplot(2,3,5); plot(ReturnsAtt.Date,ReturnsAtt.EM); title('EM-DM'); grid on;
subplot(2,3,6); plot(ReturnsAtt.Date,ReturnsAtt.liquidity); title('Liquidity'); grid on;

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 7 of 16
Final Project 4/29/18, 5*19 PM

## subplot(2,3,1); plot(date(36:end),b(2,36:end)); title('Real Rates'); grid on;

subplot(2,3,2); plot(date(36:end),b(3,36:end)); title('Inflation'); grid on;
subplot(2,3,3); plot(date(36:end),b(4,36:end)); title('Credit'); grid on;
subplot(2,3,4); plot(date(36:end),b(5,36:end)); title('Economic'); grid on;
subplot(2,3,5); plot(date(36:end),b(6,36:end)); title('EM-DM'); grid on;
subplot(2,3,6); plot(date(36:end),b(7,36:end)); title('Liquidity'); grid on;

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 8 of 16
Final Project 4/29/18, 5*19 PM

Efficient Frontier

V = cov(M);
[wtarg,fval1] = quadprog(V,c,Aineq,bineq,Aeq,beq,lb,ub,[],options); %targeted return port
[wmv,fval2] = quadprog(V,c,[],[],Aeq,beq,lb,ub,[],options); %Min Var portfolio

sigma_targ = sqrt(wtarg'*V*wtarg);
sigma_mv = sqrt(wmv'*V*wmv);
Rmv = mean_return*wmv;
Rtarg = mean_return*wtarg;
rho = wtarg'*V*wmv/(sigma_mv*sigma_targ);

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 9 of 16
Final Project 4/29/18, 5*19 PM

a = -1:.01:1;
a = a';

Risk Parity

wrp = rsk_parity(mean_return',V,lb,ub,0);
Rrp = mean_return*wrp;
sigma_rp = sqrt(wrp'*V*wrp);
mctr_rp = V*wrp/sigma_rp;
attr_rp = mctr_rp.*wrp
ret_risk = [Rmv sigma_mv; Rtarg sigma_targ; Rrp sigma_rp]; names = {'minVar';'targRet';'riskParity'};
compareTable = table(ret_risk(:,1),ret_risk(:,2),'VariableNames',{'Return','Risk'},'RowNames',names)

## subplot(2,1,1); plot(date(36:end),risk_rp(36:end)); title('Predicted Return - Risk Parity Portfolio'); grid on

subplot(2,1,2); plot(date(36:end),Rp_targ(36:end)); title('Predicted Return - Targ Ret Port'); grid on

attr_rp =

0.7818
0.7818
0.7818
0.7818
0.7818
0.7818

compareTable =

3×2 table

Return Risk
_______ ______

## minVar 0.46367 3.998

targRet 0.70741 5.8635
riskParity 0.37572 4.6908

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 10 of 16
Final Project 4/29/18, 5*19 PM

Monte Carlo

clc;clear;
date = datetime(country.Date);
M1 = [country.EU, country.japan,country.us, country.china, country.brazil, country.india];
M1 = M1/100;
selectedVariables = {'EU';'Japan';'US';'China';'Brazil';'India'};

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 11 of 16
Final Project 4/29/18, 5*19 PM

[T,k1] = size(M1);
mean_return = (prod(1+M1).^(1/T) -1);
Arithmean = mean_return + diag(cov(M1))'/2;

## nAssets = numel(mean_return); r = mean(Arithmean);

Aeq = ones(1,nAssets); beq = 1;
Aineq = -mean_return; bineq = -r;
lb = zeros(nAssets,1); ub = ones(nAssets,1);
c = zeros(nAssets,1);

options = optimset('Algorithm','interior-point-convex');
options = optimset(options,'Display','off','TolFun',1e-10);

for t = 36:T
V = cov(M1(t-35:t,:));
Vcov(:,:,t) = V; Vcond(t) = cond(V);
end

V = Vcov(:,:,end);
V1 = Vcov(:,:,117);

%Base Case
for n = 1:1000
R = repmat(mean_return - .5*diag(V)',60,1) + randn(60,6)*chol(V);
wrp = rsk_parity(mean_return',V,lb,ub,0);

wtarg = wtemp1;
wmv = wtemp2;
wrp = wrp;
Rp_mv(n) = prod(1+R*wmv).^(12/60)-1;
Rp_targ(n) = prod(1+R*wtarg).^(12/60)-1;
Rp_rp(n) = prod(1+R*wrp).^(12/60)-1;
end

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 12 of 16
Final Project 4/29/18, 5*19 PM

## Base = [Rp_mv', Rp_targ', Rp_rp'];

for n = 1:1000
R = repmat(mean_return - .5*diag(V1)',60,1) + randn(60,6)*chol(V1);
wrp = rsk_parity(mean_return',V1,lb,ub,0);

wtarg = wtemp1;
wmv = wtemp2;
wrp = wrp;
Rp_mv(n) = prod(1+R*wmv).^(12/60)-1;
Rp_targ(n) = prod(1+R*wtarg).^(12/60)-1;
Rp_rp(n) = prod(1+R*wrp).^(12/60)-1;
end

## % Column and row name

BaseVaR = [prctile(Base,[1,5,10])];
StressVaR = [prctile(Stress,[1,5,10])];
BaseVaRTable = table(BaseVaR(:,1),BaseVaR(:,2),BaseVaR(:,3),'Variablenames',{'MinimumVariance','TargetedReturn','RiskParity'}
,'Rownames',{'Prct1','Prct5','Prct10'});
StressVaRTable = table(StressVaR(:,1),StressVaR(:,2),StressVaR(:,3),'Variablenames',{'MinimumVariance','TargetedReturn','Risk
Parity'},'Rownames',{'Prct1','Prct5','Prct10'});

Table1 = table(BaseVaRTable,StressVaRTable)

clear; clc;
s = [0.05,0.1,0.15,0.2];
T = 1; S = 100; r = .1;Kput = 100; Kcall = 100;

for n = 1:length(s)
for t = 1:10000
P(n,t) = S*exp(r-0.5*s(n)^2*T + s(n)*norminv(rand)*sqrt(T));
call(n,t) = max(P(n,t)-S,0)/1+r;
put(n,t)= max(S-P(n,t),0)/1+r;
end
mean_call(n,:) = mean(call(n,:));

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 13 of 16
Final Project 4/29/18, 5*19 PM

std_call(n,:) = std(call(n,:));
skew_call(n,:) = skewness(call(n,:));
mean_put(n,:) = mean(put(n,:));
std_put(n,:) = std(put(n,:));
skew_put(n,:) = skewness(put(n,:));
end

Option_table = table(mean_call,std_call,skew_call,mean_put,std_put,skew_put)

clc;clear;
% Parameters input
for n =1:100
Kp = 100;
Kc = 115;
u = [0,07,0.06];
sigma = 0.18;
sigma_index = 0.15;
rho = rand();
r = 0.02;
C = [1,rho;rho,1];
S = [sigma,0;0,sigma_index];
Vcov = S*C*S;
Chol = [sqrt(Vcov(1,1)),0;sqrt(Vcov(1,2).^2/sqrt(Vcov(1,1))), sqrt(Vcov(2,2)-(sqrt(Vcov(1,2).^2/sqrt(Vcov(1,1)))).^2)];
means= [0.0538,0.04875];
Week = [0:52]';
Time = (repmat(52,length(Week),1)-Week)/52;
Time(end) = (52-51.5)/52;

## % Convert into matrix

M =[100,100; zeros(52,2)];
for t = 2:length(Time)
M(t,:) = M(t-1,:).*exp(means/52 + randn(1,2)*Chol/sqrt(52));
end
d1c = (log(M(:,2)/Kc)+repmat(r,length(Time),1)+0.5*repmat(sigma_index^2,length(Time),1).*Time)./(sigma_index*sqrt(Time));
d2c = d1c-sigma_index*sqrt(Time);
d1p = (log(M(:,2)/Kp)+repmat(r,length(Time),1)+0.5*repmat(sigma_index^2,length(Time),1).*Time)./(sigma_index*sqrt(Time));
d2p = d1p-sigma_index*sqrt(Time);
Put = repmat(Kp,length(Time),1).*exp(-r*Time).*normcdf(-d2p)-M(:,1).*normcdf(-d1p);
Call = M(:,1).*normcdf(d1c)-exp(-r*Time)*Kc.*normcdf(d1c-sigma_index*sqrt(Time));

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 14 of 16
Final Project 4/29/18, 5*19 PM

delta = normcdf(d1c);

## %Delta Hedge Accounting

X = [delta(1), 100*delta(1), 100*delta(1)*r/52; zeros(52,3)];
for t = 2:length(Time)
X(t,1) = delta(t)-delta(t-1);
X(t,2) = X(t-1,2)+X(t,1)*M(t,1)+X(t-1,3);
X(t,3) = X(t,2)*r/52;
end

DeltaHedgeAcct = table(Week,Time,M(:,1),M(:,2),d1c,d2c,d1p,d2p,Put,Call,delta,X(:,1),X(:,2),X(:,3),...
'VariableNames',{'Week','Time','Index','StockA','d1c','d2c','d1p','d2o','Put','Call','Delta','Shares_Purch','Cost','Inter
est_Cost'});

## %Evaluate the call and put value

Putvalue = max(Kp-M(end,1),0)-Put(1);
Callvalue = (M(end,1)>Kc)*Kc - X(end,2)+Call(1);
PnL= (Putvalue+Callvalue+M(end,2)-M(1,2))/100;

## DeltaHedge(n,:) = [Putvalue, Callvalue, PnL];

end

table(mean(DeltaHedge(:,1)),mean(DeltaHedge(:,2)),mean(DeltaHedge(:,3)),'Variablenames',{'Putvalue','Callvalue','PnL'})

Table1 =

3×2 table

BaseVaRTable StressVaRTable
MinimumVariance TargetedReturn RiskParity MinimumVariance TargetedReturn RiskParity
_________________________________________________________ _________________________________________________________

## Prct1 -0.021219 -0.0002665 -0.05292 Prct1 -0.073203 -0.14394 -0.13998

Prct5 0.0026172 0.023587 -0.024148 Prct5 -0.041955 -0.083908 -0.089777
Prct10 0.01659 0.042703 -0.0068031 Prct10 -0.021846 -0.054388 -0.066972

Option_table =

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 15 of 16
Final Project 4/29/18, 5*19 PM

4×6 table

## mean_call std_call skew_call mean_put std_put skew_put

_________ ________ _________ ________ _______ ________

## 10.57 5.4406 0.26167 0.14991 0.41719 11.656

11.324 9.638 0.8073 0.98701 2.6174 3.7432
12.929 13.746 1.1976 2.4374 5.1371 2.6372
14.5 17.707 1.5386 4.2614 7.6703 2.0652

ans =

1×3 table

## Putvalue Callvalue PnL

________ _________ _______

## Published with MATLAB® R2018a

file:///Users/tristan/Documents/MATLAB/html/Kevindong.html Page 16 of 16