% Portfolio Selection
% PortSelect
clear
clc

% Risk free asset
rf = 0.07;

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

%Information Asset 1
r2 = 0.35;
sig2 = 5;

% Relation each other
ro = 0.15;

retu = [r1 r2];
cova = [sig1^2 sig1*sig2*ro;sig1*sig2*ro sig2^2];
NPor = 100;
[PortRisk, PortReturn, PortWts] = frontera(retu,cova,NPor);
% Initial resources
w1 = -1.000;
w2 = 1-w1;
wieghts = [w1 w2];
R0 = input('Level of return you want: ');
%R0 = profit(wieghts,r1,r2);
%V0 = risk(wieghts,sig1,sig2,ro);

W1 = -3:0.01:3;
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);
title('Efficient combination between Risk and Return','FontWeight','bold','FontSize',12);
set(plot1,'LineStyle','-','LineWidth',2,'Color','r');
miny = min(y);
xgiveny = x((miny==y));
text(miny,xgiveny,['\leftarrow Min-Risk point ','(Risk,Return) = ','(',num2str(miny),' ; ',num2str(xgiveny),')'],...
    'HorizontalAlignment','left','FontAngle','italic');
ylabel('Return','FontWeight','bold','FontSize',10);
xlabel('Risk ( \sigma )','FontWeight','bold','FontSize',10);
xlim([min(y)*0.85,max(y)*1.05]);
if min(x)<0,
    liminfy = 0; 
else
    liminfy = min(x)*0.5;
end
ylim([liminfy,max(x)*1.05]);

% Minimization of the risk given the a level of return (R0) and 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 = [0.1 0.1];
[opt_wieghts,fval,exitflag,output,lambda] = fmincon('risk',ini_guess,[],[],[r1 r2;1 1],[R0;1],[],[],[],opt,sig1,sig2,ro);
V0 = risk(opt_wieghts,sig1,sig2,ro);
disp(' ');
disp('Press Intro ');
disp(' ');
pause;
text(V0,R0,['     \leftarrow Optim point ',sprintf('\n'),'(Risk,Return) = ','(',num2str(V0),' ; ',num2str(R0),')'],...
    'HorizontalAlignment','left','FontAngle','italic');
disp(output);
disp(' ');
disp('        Solution');
disp(' ');
disp('        Wieghts');
disp('      w1        w2');
disp(opt_wieghts+eps);
disp(' ');
disp('     Risk     Return');
disp([fval R0]);







