% second_exercise2
clear
close all
clc
%%
warning off all;
opt = optimset('Display','off');
%%
[Data Date] = xlsread('Libro1.xls','Hoja2','A2:G448'); % Load Data into and the date from the file Libro1.xls.
[a IndexName] = xlsread('Libro1.xls','Hoja2','B1:G1'); % Load the names of the indexs from the file Libro1.xls.
%% a) Descriptive Statistics
m = mean(Data)';
S = cov(Data);

disp(' ');
disp('  The mean rate of returns of index are');
disp(' ');
disp(IndexName);
disp(['   ' num2str(m',6)]);

disp(' ');
disp('  The mean rate of returns of index are');
disp(' ');
disp(IndexName);
disp(S);

%% b) Let's assume that, according to the mean returns, we don't want to loose money, so that Rp = 0;
Rp = 0;
W0 = ones(6,1)*(1/6); % Starting point being the equi-weight composition (one fourth for each asset).
% Now, find the W that minimizes the variance (standard deviation), subject
% to: 
% Rp = w1*m1 + w2*m2 + w3*m3 + w4*m4 + w5*m5 + w6*m6 = W'*m = 0.
% Remember, we have an extra equality constraint given by
% w1 + w2 + w3 + w4 + w5 + w6 = 1.
% Alternative 1: With short-sale operations, we don't have any other restriction, so that: 

[W, Sp] = fmincon(@(w)sigmamin(w,Data),W0,[],[],[ones(1,6);m'],[1;Rp],...
[],[],[],opt);
% Check the solution is ok:
sum(W)
W'*m
STp = sqrt(Sp) % Minimum portfolio standard deviation ("risk") for Rp = 0;
% Alternative 2: Without short-sale operations, we need to resctrict the
% W's to be nonnegative.

[W1, Sp1] = fmincon(@(w)sigmamin(w,Data),W0,[],[],[ones(1,6);m'],[1;Rp],...
[zeros(6,1)],[],[],opt);
% Check the solution is ok:
sum(W1)
W1'*m
W1>=0
STp1 = sqrt(Sp1) % Minimum portfolio standard deviation ("risk") for Rp = 0;

%% c) For different values of Rp:

Rp2 = -0.1:0.0075:0.1;
for i=1:length(Rp2),
    [W2(:,i) Sp2(i)] = fmincon(@(x)sigmamin(x,Data),W0,[],[],[ones(1,6);m'],[1;Rp2(i)],...
        [],[],[],opt);
end
STp2 = sqrt(Sp2);
%% d) Plotting the efficient frontier (minimum risk frontier).
figure
plot(STp2,Rp2,'r','LineWidth',2.5), % Plotting the minimun risk curve
set(gcf,'Color','w');
hold on, % Hold on the figure
plot([min(STp2) max(STp2)],[0.01 0.01],'--','LineWidth',2.5), % Adding the free-risk return line at rf = 0.01
plot([min(STp2) max(STp2)],[0 0],':k','LineWidth',2.5), % Adding the zero-line 
axis tight % To restrict the view to the domain and image of the plots
hold off
aa = axis;
axis([1 aa(2:4)]);
print -dpng 'd:\Docencia\Curso Matlab\Tutorial\fig1.png'
%% e) Minimizing the risk
for i=1:size(Data,1)-39,
    [W3(:,i) Sp3(i)] = fmincon(@(x)sigmamin(x,Data(i:39+i,:)),W0,[],[],[ones(1,6);m'],[1;Rp],...
        zeros(1,6),ones(1,6),[],opt);
end
STp3 = sqrt(Sp3);
%%
figure
plot(datenum(Date(40:end,1),'dd/mm/yyyy'),W3');
legend(IndexName);
set(gcf,'Color','w');
datetick('x',12)
axis tight
a = axis;
axis([a(1:2) -0.05 1]);
print -dpng 'd:\Docencia\Curso Matlab\Tutorial\fig2.png'
%%
figure
set(gcf,'Color','w');
plot(datenum(Date(40:end,1),'dd/mm/yyyy'),STp3,'r','LineWidth',2.5);
datetick('x',12)
axis tight
print -dpng 'd:\Docencia\Curso Matlab\Tutorial\fig3.png'
%%
!texify.exe --pdf --run-viewer sef.tex
