%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% FILE: example_shsh.m
%%
%% Sheldon Share has c euros extra money to be invested.  He contenplates
%% between the following four alternatives:
%%
%% a1 = retail stock
%% a2 = retail call-option
%% a3 = airliner stock
%% a4 = airliner call-option
%%
%% Sheldon believes the following scenarios are relevant to his alternatives:
%%
%% s_1 = Koala Ebola runs rampant.
%% s_2 = Koala Ebola is contained, but the economy is in recess.
%% s_3 = Koala Ebola is contained and the economy is booming.
%%
%% Sheldon assumes that the associated probabilities are:
%%
%% p1 = p0.
%% p2 = q0.
%% p3 = q0.
%%
%% Here p0 is an unknown sensitivity parameter and q0 = (1-p0)/2.
%%
%% Sheldon uses the CARA rule, but is uncertain of the related risk tolerance
%% r0.  Thus, he treats r0 also as a sensitivity parameter.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Calculations and defititions for visualization.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Money to be invested and the decition matrix
c = 100000;
R = [ 1.8*c   0.5*c   1.1*c ;
      2.2*c   0.0*c   0.0*c ;
      0.4*c   1.2*c   3.0*c ;
      0.0*c   0.0*c   5.0*c ];

%% N is the sampling frequency for p0 and r0.  The bigger the N the better the
%% picture, but the longer the calculations take.  For N=100 the calculations
%% take 1.5 seconds.  For N=500 the calculations take 35 seconds.
N = 500;
p0samples = linspace(0, 1, N);         %% N equidistant samples from 0...1
r0samples = linspace(1, 10*c ,N);      %% N equidistant samples from 0...10c

%% Constant fields for decision problem dp.
dp.dmat = R;
dp.drule = "cara";

%% Optimal choices with different parameter values p0 and r0 are stored in the
%% matrix z.
for i = 1:N
    x = p0samples(i);
    dp.prob = [x (1-x)/2 (1-x)/2]';
    for j = 1:N
        y = r0samples(j);
        dp.rtol = y;
        dr = dmatrix(dp);
        z(j,i) = dr.doptim;
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Plotting the sensitivity picture
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Contour picture of z
contourf(p0samples, r0samples, z);

%% Names for axes and their labels
set(gca, "fontsize", 22);
xlabel("p_0");
ylabel("r_0");

%% Change the colors of the picture
red  = [1 0 0];                              %% RGB coded color
green = [0 1 0];                             %% RGB coded color
colormap([red; 0.5*red; green; 0.5*green]);  %% Colormap with four values

%% Plot the labels for the alternative (we printed z with N=10 to check which
%% color corresponded to which alternative and we noted that the y axis is
%% reverted in z compared to the picture)
text(0.05, 9.50*c, "a_4", "fontsize", 24);
text(0.30, 9.50*c, "a_3", "fontsize", 24);
text(0.60, 9.50*c, "a_1", "fontsize", 24);
text(0.85, 9.50*c, "a_2", "fontsize", 24);
%orient("landscape")
%print("sheldon_picture.pdf")

