% Portfolio Selection
% PortSelect1
clear
clc

%Information Asset 1
r1 = 0.07;
sig1 = 3;

%Information Asset 2
r2 = 0.3;
sig2 = 5;

% Relation each other
ro = 0.1;

% Initial resources
w1 = 0.5;
w2 = 1-w1;
wieghts = [0.7 0.3];

retu = [r1 r2];
cova = [sig1^2 sig1*sig2*ro;sig1*sig2*ro sig2^2];
NPor = 100;
[PortRisk, PortReturn, PortWts] = frontera(retu,cova,NPor);

W1 = 0:0.01:1;
y = [];
x = [];
for i = 1:length(W1)
x(i,1) = profit([W1(i) 1-W1(i)],r1,r2);
y(i,1) = risk([W1(i) 1-W1(i)],sig1,sig2,ro);
end
set(gcf,'Color',[1,1,1]);
plot1 = plot(y,x);
hold on
plot2 = plot(PortRisk,PortReturn);
hold off
title('Efficient combination between Risk and Return','FontWeight','bold','FontSize',12);
set(plot1,'LineStyle','-','LineWidth',2,'Color','r');
set(plot2,'LineStyle','-','LineWidth',2.5,'Color','b');
miny = min(y);
xgiveny = x((miny==y));
ylabel('Return','FontWeight','bold','FontSize',10);
xlabel('Risk ( \sigma )','FontWeight','bold','FontSize',10);
xlim([min(y)*0.85,max(y)*1.05]);
ylim([min(x)*0.85,max(x)*1.05]);

% Minimization of the risk given the curretnt condition of market using 
% the command fmincon from the optimization toolbox

opt = optimset('Display','iter','MaxFunEvals','100*numberofvariables','MaxIter',400,...
    'TolFun',1.0000e-006,'TolX',1.0000e-006,'FunValCheck','off','OutputFcn',[],...
    'ActiveConstrTol',[],'BranchStrategy',[],'DerivativeCheck','off','Diagnostics',...
    'off','DiffMaxChange',0.1000,'DiffMinChange',1.0000e-008,'GoalsExactAchieve',[],...
    'GradConstr',[],'GradObj','off','Hessian','off','HessMult',[],'HessPattern',...
    'sparse(ones(numberofvariables))','HessUpdate','bfgs','InitialHessType','scaled-identity',...
    'InitialHessMatrix',[],'Jacobian',[],'JacobMult',[],'JacobPattern',[],'LargeScale','off',...
    'LevenbergMarquardt',[],'LineSearchType',[],'MaxNodes',[],'MaxPCGIter',...
    'max(1,floor(numberofvariables/2))','MaxRLPIter',[],'MaxSQPIter',[],'MaxTime',[],...
    'MeritFunction',[],'MinAbsMax',[],'NodeDisplayInterval',[],'NodeSearchStrategy',[],...
    'NonlEqnAlgorithm',[],'NoStopIfFlatInfeas',[],'PhaseOneTotalScaling',[],'Preconditioner',...
    [],'PrecondBandWidth',0,'RelLineSrchBnd',[],'RelLineSrchBndDuration',[],...
    'ShowStatusWindow',[],'Simplex',[],'TolCon',[],'TolPCG',0.1000,'TolRLPFun',[],...
    'TolXInteger',[],'TypicalX','ones(numberofvariables,1)');

w = 10;
ini_guess = [rand rand]; % We, just, initiate the algorithm by two random number between zero and one
[opt_wieghts,fval,exitflag,output,lambda] = fmincon('risk',ini_guess,[],[],[1 1],1,[0 0],[1 1],[],opt,sig1,sig2,ro);
text(miny,xgiveny,['                     Min-Risk point ',sprintf('\n'),'\leftarrow   (Risk,Return) = ',...
    '(',num2str(miny),' ; ',num2str(xgiveny),')',sprintf('\n'),'         (W{_1},W{_2}) = ',...
    '(',num2str(opt_wieghts(1)),' ; ',num2str(opt_wieghts(2)),')'],'HorizontalAlignment','left',...
    'FontAngle','italic'); % We add text to the graph
R0 = profit(opt_wieghts,r1,r2);
disp(output); % The command disp shows on the screen message
disp(' ');
disp('        Solution');
disp(' ');
disp('        Wieghts');
disp('      w1        w2');
disp(opt_wieghts);
disp(' ');
disp('     Risk     Return');
disp([fval R0]);







