%% ECON 715, Visualizing Quasiconcave Functions
% Name: Michael Bar
% Table of Contents
% Sum of Quasiconcave Functions May not be Quasiconcave
% Summary of Main Results About Quasiconcavity
%% Sum of Quasiconcave Functions May not be Quasiconcave
% We proved in the lecture notes that the sum of concave functions must be concave. The same is not true however for quasiconcave functions - a sum of quasiconcave functions does not have to be quasiconcave. In these note we provide an example of two quasiconcave functions that add up to a function which is not quasiconcave.
% Consider two quasiconcave functions:
% f\left(x\right)=x^3 ,\;\;\;\;\;\textrm{and}\;\;\;\;\;g\left(x\right)=-4x.
% You should be able to verify that these functions are quasiconcave. First, we can express f\left(x\right) as a monotone increasing transformation of a concave function:
% f\left(x\right)={\left(\sqrt{\;x}\right)}^6,
% where \sqrt{\;x} is concave (strictly) and the 6-th power is monotone increasing function on R_+. Recall that any monotone increasing transformation of a concave function is quasiconcave.
% The function g\left(x\right)=-4x is linear, and therefore both concave and convex, and concave functions must be quasiconcave.
% Another useful result for recognizing quasiconcave functions is that any monotone function is both quasiconcave and quasiconvex, and therefore both f and g given above are quasiconcave (f is monotone increasing, and g is monotone decreasing).
% Yet another way to recognize that both f and g are quasiconcave is by plotting them and idenitifying their upper contour sets. This way of recognizing quasiconcave functions is not practical for functions of several variables or complicated functions.
% The next figure plots the function f\left(x\right)=x^3, and highlights the upper contour set for level \alpha =2 (bold red segment on the x-axis).

syms x %Declaring symbolic variable
f(x) = x^3; %Declaring symbolic function
a = 2; %Level for Upper Contour Sets
fplot(f, [-2,2], "LineWidth",2, "Color", [0 0.4470 0.7410]) %Plot of x^3
hold on
p = fplot(0, [a^(1/3), 2], "LineWidth", 3, "Color", "red");

xline(a^(1/3), 'k--', 'LineWidth',1) %Plots the y-coordinate
yline(a, 'k--', 'LineWidth',1) %Level of upper contour sets
yline(0, 'k--', 'LineWidth',1) %Plots the x-coordinate
legend(p, ...
    ['Upper Contour Set at level \alpha = ', num2str(a)], ...
    'Location','northwest')
title('f(x) = x^3')
xlabel('x')
ylabel('y', "Rotation", 0)
grid on
grid minor
hold off

% Mathematically, the above upper contour set is described as:
% U\left(\alpha =2;f\left(x\right)=x^3 \right)=\left\lbrace x|x^3 \ge 2\right\rbrace
% In words, this is the set of all x values such that the value of the given function is above 2.
% We can find the upper contour set:
% \begin{array}{l}
% x^3 \ge 2\\
% x\ge 2^{\frac{1}{3}} \approx 1\ldotp 2599
% \end{array}
% This is exactly the line segment highlighted in red on the x-axis in the above graph. We can see from the above graph that the upper contour set is convex set, and this is the case for any level \alpha \;.
% Similarly, we can graph the function g\left(x\right)=-4x, and identify its upper contour sets. In the next figure, the upper contour set for level \alpha =2 is highlighted on the x-axis in bold red.

g(x) = -4*x; %Declaring symbolic function
fplot(g, [-2,2], "LineWidth",2, "Color", [0.8500 0.3250 0.0980]) %Plot of x^3
hold on
p = fplot(0, [-2, -1/2], "LineWidth", 3, "Color", "red");
xline(-1/2, 'k--', 'LineWidth',1) %Plots the y-coordinate
yline(0, 'k--', 'LineWidth',1) %Plots the x-coordinate
yline(a, 'k--', 'LineWidth',1) %Level of upper contour sets
legend(p, ...
    ['Upper Contour Set at level \alpha = ', num2str(a)], ...
    'Location','northeast')
title('g(x) = -4x')
xlabel('x')
ylabel('y', "Rotation", 0)
grid on
grid minor
hold off

% Mathematically, the above upper contour set is described as:
% U\left(\alpha =2;g\left(x\right)=-4x\right)=\left\lbrace x|-4x\ge 2\right\rbrace
% We can find the upper contour set:
% \begin{array}{l}
% -4x\ge 2\\
% x\le -\frac{1}{2}
% \end{array}
% Notice that again that the upper contour set is convex.
% Next, we add the two quaisiconcave functions,
% h\left(x\right)=f\left(x\right)+g\left(x\right)=x^3 -4x
% and observe that the sum is not quasiconcave. In the next graph, notice that the upper contour set consists of two separate segments (bold red segments on the x-axis. Thus, the upper contour is not a convex set, and therefore the function h\left(x\right)=f\left(x\right)+g\left(x\right) is not quasiconcave.

h(x) = f(x) + g(x); %Sum of two quasiconcave functions
S = double(vpasolve(f(x) + g(x) == a)) %Solving for upper contour set

fplot(h, [-3, 5], "LineWidth",2, "Color", [0.4940 0.1840 0.5560]) %Plot of x^3
ylim([-4, 5])
hold on
p = fplot(0, [S(1), S(2)], "LineWidth", 3, "Color", "red");
fplot(0, [S(3), 5], "LineWidth", 3, "Color", "red");
xline(S(1), 'k--', 'LineWidth',1) %Plots the y-coordinate
xline(S(2), 'k--', 'LineWidth',1) %Plots the y-coordinate
xline(S(3), 'k--', 'LineWidth',1) %Plots the y-coordinate

yline(0, 'k--', 'LineWidth',1) %Plots the x-coordinate
yline(a, 'k--', 'LineWidth',1) %Level of upper contour sets
legend(p, ...
    ['Upper Contour Set at level \alpha = ', num2str(a)], ...
    'Location','northwest')
title('h(x) = x^3 - 4x')
xlabel('x')
ylabel('y', "Rotation", 0)
grid on
grid minor
hold off

% Remark. With this function x^3 -4x, finding the upper contor set analytically is hard, and I used Matlab's numerical solver for that.
%% Summary of Main Results About Quasiconcavity
% Any monotone increasing transformation of a concave function must be quasiconcave.
% Any quasiconcave function must arise as a monotone increasing transformation of some concave function.
% Any monotone (incresing or decresing) function is quasiconcave.
% Not every quasiconcave function is monotone. Remember the bell-shaped pdf of N\left(\mu ,\sigma {\;}^2 \right).
% Weighted sum of quasiconcave functions does not have to be quasiconcave.
% The last property was illustrated in this note.