clear all
close all

clear 
close all

SIGMA=xlsread('File_excel.xlsx','BL','D6:N16')
[W_MN LABELS]=xlsread('File_excel.xlsx','BL','A6:B16')


RISK_FREE=xlsread('File_excel.xlsx','BL','B19:B19')
LAMBDA=xlsread('File_excel.xlsx','BL','B20:B20')
EXP_RET_MN=RISK_FREE+LAMBDA*SIGMA*W_MN

TAU=xlsread('File_excel.xlsx','BL','Z21:Z21')
TAU_SIGMA=TAU*SIGMA

P=xlsread('File_excel.xlsx','BL','U37:AE38')
Q=xlsread('File_excel.xlsx','BL','U40:U41')
C=xlsread('File_excel.xlsx','BL','U43:U44')


OMEGA=zeros(2,2);
for f=1:2
for g=1:2
if f==g
OMEGA(f,g)=((1/C(f,1)-1)*P(f,:)*(TAU_SIGMA)*P(f,:)');
end
end
end


REND_BL=inv(inv(TAU_SIGMA)+P'*inv(OMEGA)*P)*(inv(TAU_SIGMA)*EXP_RET_MN+P'*inv(OMEGA)*Q)


EXP_RET=REND_BL;

% Inputs trasferred on Matlab
[NNN LABELS]=xlsread('File_excel.xlsx','Mark opt','A2:B12')
COV=xlsread('File_excel.xlsx','Mark opt','H2:R12')


[ASSET NNN]=size(COV);
SIZE=xlsread('File_excel.xlsx','Resampling_TM','A17');

SIM=xlsread('File_excel.xlsx','Resampling_TM','A14');



% frontiera efficiente semplice
[RISK2,ROR2,WTS2]=portopt(EXP_RET,COV,100);



STORE_WTS=zeros(100,ASSET,SIM);  




for i = 1:SIM
i
SIM_RET= mvnrnd(EXP_RET, COV,SIZE);
EXP_RET_SIM=mean(SIM_RET);
COV_SIM=cov(SIM_RET);

[RISK,ROR,WTS]=portopt(EXP_RET_SIM,COV_SIM,100);


STORE_WTS(:,:,i)= WTS;
end 


RESAPL_WEIGHTS=mean(STORE_WTS,3);



EXP_RET_RESAMPL= RESAPL_WEIGHTS*EXP_RET;

RISK_RESAMPL = zeros(100,1);

for i = 1 :100
RISK_RESAMPL(i,1) = sqrt(RESAPL_WEIGHTS(i,:)*COV*RESAPL_WEIGHTS(i,:)');
end 


figure(2)
subplot(2,2,[1 2])
plot (RISK2,ROR2,'R')
hold on 
plot (RISK_RESAMPL,EXP_RET_RESAMPL,'B')
hold off
title('Efficient Frontier versus Resampled Frontier')
legenda= legend({'Eff Front','RESAMPLED Front'},'Location','SouthOutside')

subplot(2,2,3)
area(WTS2)
legenda= legend(LABELS,'Location','EastOutside')
title('Composition of Efficient Portfolios')
ylim([0 1]);
xlim([1 100]);
subplot(2,2,4)
area(RESAPL_WEIGHTS)
legenda= legend(LABELS,'Location','EastOutside')
title('Composition of Resampled Portfolios')
ylim([0 1]);
xlim([1 100]);





