%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 
%% FILE: kt20201111.m
%%
%% Sirpa Sijoittajan esimerkki kolmella paatossaannolla: opt, pess, kk.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

c = 100000;            %% Sijoitettava varallisuus
%% Palkkiomatriisi tuotoille.
R0 = [ 
  80  -50   10;
 120 -100 -100;
 -60   20  200;
-100 -100  400
];

R = c*(1+R0/100);      %% Palkkiomatriisi lopputulemille.

po1.psaanto = "opt";
po1.pmat = R;
po2.psaanto = "pess";
po2.pmat = R;
po3.psaanto = "kk";
po3.pmat = R;

pr1 = pmatriisi(po1);
pr2 = pmatriisi(po2);
pr3 = pmatriisi(po3);

%% Yhdistetty saanto
V1 = round(pr1.parvot);
V2 = round(pr2.parvot);
V3 = round(pr3.parvot);
V3 = -V3;

%% Yhdistetään skaalatut arvofunktiot V = w1*V1 + w2*V2 + (1-w1-w2)*V3 ja 
%% piirretaan kuva.
V1 = ( V1-min(V1) )/( max(V1)-min(V1) ); 
V2 = ( V2-min(V2) )/( max(V2)-min(V2) );
V3 = ( V3-min(V3) )/( max(V3)-min(V3) );

N = 500;
w1 = linspace(0,1,N);
w2 = linspace(0,1,N);

for i=1:length(w1)
   for j=1:length(w2)
      V = w1(i)*V1 +w2(j)*V2 + (1-w1(i)-w2(j))*V3; 
      [roskaa, z(j,i)] = max(V);
      if w1(i) + w2(j) > 1
          z(j,i) = 0;
      end
   end
end

contourf(w1,w2,z,0:4)
puna  = [1 0 0];
viher = [0 1 0];
valko = [1 1 1];
colormap([valko; puna; 0.5*puna; viher; 0.5*viher])
