clear
close all


% trasferimento in Matlab degli input presenti su excel

[DATASET LABELS]=xlsread('File_excel.xlsx','Serie Storiche','B2:K27')

% Stima dei rendimenti medi della asset class
REND_MEDI=mean(DATASET)


figure(1)
barh(REND_MEDI,'b')
grid on
title('Rend. Medi Annui dei Mercati')
xlabel('Rendimenti Medi')
ylabel('Asset Classes')
set(gca,'YTickLabel',LABELS)

% Il portafoglio 
[PESI]=xlsread('File_excel.xlsx','Serie Storiche','B30:K30')

figure(2)
pie(PESI)
title('Composizione del portafoglio')
legenda= legend(LABELS,'Location','SouthOutside')

% Stima del Rendimento Medio del Portafoglio
REND_MEDIO_PORT=PESI*REND_MEDI'


figure(3)
plot(DATASET(:,7))
hold on
plot(DATASET(:,10),'r')
hold off
title('Rend. annuali Az. Europa e Az. EM')
xlabel('Anni')
ylabel('Rendimenti')
legenda= legend({'Az Europa' , 'Az EM'},'Location','SouthOutside')
grid on




% Stima delle deviazioni standard delle Asset Class
SIGMA=std(DATASET)

% Confronto grafico dei sigma dei diversi mercati
figure(4)
barh(SIGMA,'r')
grid on
title('SIGMA of Asset Classes')
xlabel('SIGMA')
ylabel('Asset Classes')
set(gca,'YTickLabel',LABELS)


% Grafico a dispersione che mostra le combinazioni rendimento medio /
% Rischio delle Asset Class

figure(5)
scatter(SIGMA, REND_MEDI, 'filled')
grid on
title('Sigma-Rend Medi')
xlabel('Sigma')
ylabel('Rend Medi')
lsline
% Alcuni grafici delle correlazioni tra coppie di mercati

figure(6)
subplot(2,2,1)
scatter(DATASET(:,7), DATASET(:,8))
lsline
xlabel('Az Europa')
ylabel('Az Nord America')
grid on

subplot(2,2,2)
scatter(DATASET(:,9), DATASET(:,10))
lsline
xlabel('Az Pacifico')
ylabel('Az EM')
grid on

subplot(2,2,3)
scatter(DATASET(:,8), DATASET(:,5))
lsline
xlabel('Az NA')
ylabel('Obbl Corp HY')
grid on

subplot(2,2,4)
scatter(DATASET(:,7), DATASET(:,3))
lsline
xlabel('Az Europa')
ylabel('Obbl € Tutte le scad.')
grid on


% Stima correlazioni e covarianze
CORR=corr(DATASET)

figure(7)
h = heatmap(CORR);
title('Matrice delle Correlazioni')


COV=cov(DATASET)

PESO_RISCHIO=PESI.*SIGMA

% Calcolo del sigma di portafoglio
SIGMA_PORT1=sqrt(PESO_RISCHIO*CORR*PESO_RISCHIO')
SIGMA_PORT2=sqrt(PESI*COV*PESI')

VAR_ASSET_95=REND_MEDI-norminv(0.95,0,1)*SIGMA
VAR_ASSET_99=REND_MEDI-norminv(0.99,0,1)*SIGMA
VAR_ASSET_84=REND_MEDI-norminv(0.84,0,1)*SIGMA

figure(8)
barh([VAR_ASSET_95', VAR_ASSET_99',VAR_ASSET_84'])
grid on
title('VaR annuali delle Asset class (liv.conf=95%)')
xlabel('VaR')
ylabel('Mercati')
set(gca,'YTickLabel',LABELS)
legenda= legend({'Liv conf. 95%','Liv conf. 99%','Liv conf. 84%'},'Location','SouthOutside')


VAR_PORT_95=REND_MEDIO_PORT-norminv(0.95,0,1)*SIGMA_PORT1
