%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% FILE: example_su.m
%%
%% Sophie Sophomore's spouse Susan Suit contemplates what to do next Fall.
%% Susan's alterantives and scenarios are the same as Sophie's:
%%
%% Alternatives:
%%
%% a1 = Student exchange to China.
%% a2 = Concentrate on the studies full-time.
%% a3 = Mostly studies and working for a food delivery company in the evenings.
%% a4 = Little studies and working full-time for a multinational corporation.
%%
%% Scenarios:
%%
%% s1 = Koala Ebola is pandemic, the university is in distant teaching,
%%      and the world economy is in recess.
%% s2 = Koala Ebola is pandemic, the university is in distant teaching,
%%      and the world economy is recovering.
%% s3 = Koala Ebola is pandemic, the university is in normal teaching,
%%      and the world economy is in recess.
%% s4 = Koala Ebola is pandemic, the university is in normal teaching,
%%      and the world economy is recovering.
%% s5 = Koala Ebola has been defeated, and the world economy is in recess.
%% s6 = Koala Ebola has been defeated, and the world economy is recovering.
%%
%% The probabilities for the scenarios are
%%
%% p1 = p0/4
%% p2 = p0/4
%% p3 = p0/4
%% p4 = p0/4
%% p5 = (1-p0)/2
%% p6 = (1-p0)/2
%%
%% The Koala Ebola pandemic paramter p0 is a sensitivity parameter.
%%
%% Susan bases her decisions on excpectd value rule and regret aversion rule.
%% Susan's decision matrix is her expected monthy salary after three years-
%% The combination weight for expected value and regret arversion is a
%% sensitivity parameter w.
%%
%% The file uses the functions dmatrix and scaling.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Decision matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

R = [
3000  3500  2800  3500  2800  3500;
2500  2700  3300  3800  3500  4000;
3000  3200  2000  2500  2000  2500;
3000  3200  2500  4000  3000  3500
];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Visualization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Calculations.

%% Constant fields for decision problem 1 (expected value)
dp1.dmat = R;
dp1.drule = "ev";

%% Decision problem 2 (regret aversion)
dp2.dmat = R;
dp2.drule = "regret";
dr2 = dmatrix(dp2);
V2 = dr2.dvalues;
V2 = -V2;                           %% Maximization form.
V2 = scaling(V2);                   %% Affine scaling.


%% Sampling frequency.  The bigger the better, but calculations take more time.
%% If N=500 the calculations take approximately 3 seconds.
N=500;

%% Calculations.
p0samples = linspace(0,1,N);
wsamples = linspace(0,1,N);

for i=1:length(p0samples)
    p0 = p0samples(i);
    q0 = 1-p0;
    dp1.prob = [p0/4 p0/4 p0/4 p0/4 q0/2 q0/2]';
    dr1 = dmatrix(dp1);
    V1 = scaling(dr1.dvalues);
    for j=1:length(wsamples)
        w = wsamples(j);
        V = w*V1 + (1-w)*V2;
        [tmp, z(j,i)] = max(V);
    end
end

%% The picture. See m-file example_so.m for more detailed comments.
contourf(p0samples, wsamples, z)
set(gca, "fontsize", 24);
xlabel("p_0")
ylabel("w")
red   = [1 0 0];
green = [0 1 0];
colormap([red; 0.6*red; green; 0.6*green])
text(0.80,0.20, "a_1", "fontsize",24)
text(0.20,0.60, "a_2", "fontsize",24)
%% Uncomment the following lines to produce landscape PDF picture.  Note that
%% in some systems the PDF driver does not work.  In that case use, e.g., the
%% JPG driver.  This means changing the fie extension to .jpg.  You may also
%% what to leave the orient line commented in that case.
%orient("landscape")
%print("su_picture.pdf")
