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)




%data from the excell file
[NNN LABELS]=xlsread('File_excel.xlsx','Mark Infra Optim','A2:B12');
% DEVI UTILIZZARE I RENDIMENTI ATTESI ALLA BLACK-LITTERMAN E QUINDI
% EXP_RET DEVONO ESSERE I RENDIMENTI CALCOLATI CON LA ROUTINE SOPRA

EXP_RET=REND_BL;

COV=xlsread('File_excel.xlsx','Mark Infra Optim','H2:R12');
LB1=xlsread('File_excel.xlsx','Mark Infra Optim','U2:U12');
UB1=xlsread('File_excel.xlsx','Mark Infra Optim','W2:W12');

%setting P and the constraints for positive weights 
P=Portfolio;
P=Portfolio('AssetMean', EXP_RET,'AssetCovar',COV,'Assetlist',LABELS,'LowerBudget', 1, 'UpperBudget', 1);

LB=-zeros(1,length(EXP_RET));
b=-eye(length(EXP_RET));
P = setInequality(P,b,LB);


POSITION = eye(length(EXP_RET))  
GROUP = [1 1 1 1 1 1 0 0 0 0 0;1 1 1 1 1 1 0 0 0 0 0;1 1 1 1 1 1 0 0 0 0 0;1 1 1 1 1 1 0 0 0 0 0;1 1 1 1 1 1 0 0 0 0 0;1 1 1 1 1 1 0 0 0 0 0;0 0 0 0 0 0 1 1 1 1 1;0 0 0 0 0 0 1 1 1 1 1;0 0 0 0 0 0 1 1 1 1 1;0 0 0 0 0 0 1 1 1 1 1;0 0 0 0 0 0 1 1 1 1 1]   
P = setGroupRatio(P, POSITION, GROUP, LB1, UB1);



PORT_WEIGHT=estimateFrontier(P,100)

EXP_RET_INFRA= PORT_WEIGHT'*EXP_RET;
RISK_INFRA=zeros(100,1);

for i = 1 :100
RISK_INFRA(i,1) = sqrt(PORT_WEIGHT(:,i)'*COV*PORT_WEIGHT(:,i));
end 

[RISKPORT, RETPORT, WEIGHTS]=portopt(EXP_RET,COV,100)




figure(1)
subplot(2,2,[1 2])
scatter(RISK_INFRA, EXP_RET_INFRA, 'o', 'r')
hold on 
scatter(RISKPORT, RETPORT, 'o', 'b')
hold off
title('Infra Group Frontier versus Efficient Frontier')
ylabel('E(R)')
xlabel('Sigma')
grid on

subplot(2,2,3)
area(PORT_WEIGHT')
title('Composition of Infra-Group Portfolios')
ylabel('Weights')
xlabel('Portfolios')
legenda= legend(LABELS,'Location','EastOutside')
ylim([0 1]);
xlim([1 100]);

subplot(2,2,4)
area(WEIGHTS)
title('Composition of Efficient Portfolios')
ylabel('Weights')
xlabel('Portfolios')
legenda= legend(LABELS,'Location','EastOutside')
ylim([0 1]);
xlim([1 100]);


