%% Comparative Statics: General Non-Linear Systems
% Michael Bar
% Table of Contents
% One equation (one endogenous, three exogenous vars)
% Two equations (two endogenous, one exogenous vars)
% Two equations (two endogenous, 2 exogenous vars)
%% One equation (one endogenous, three exogenous vars)
% Consider the equation: {\textrm{ax}}^{0\ldotp 4} +\textrm{bx}+c=0. Here x is endogenous variable, depending on the parameters a,b,c (exogenous variables). This equation does not have analytical solution. Suppose that we are able to solve this equation for a=2,b=-1,c=10, and we would like to see how the solution x changes as the parameters a,b,c change. This analysis is called comparative statics.
% One way would be to solve the given system from scratch for any given parameters. For example, suppose that c increases by \textrm{dc}=1, and we have c=11. The solution for initial and new parameters is:

clear %Clearing memory
close all %Closing figures
syms x a b c %Declaring symbolic variables (x endogenous, [a b c] exogenous)
F(x,a,b,c) = a*x^0.4 + b*x + c

% Solving the equation numerically
x1 = vpasolve( F(x,2,-1,10) ) %Initial solution
x2 = vpasolve( F(x,2,-1,11) ) %New solution
dx_exact = x2 - x1 %Exact change in x

% Instead of solving the equation again, we can use the implicit function theorem and differential, to approximate the changes in x due to changes in parameters, near the initial point with parameters: a=2,b=-1,c=10.

% Comparative statics
J_x_sym(x,a,b,c) = jacobian(F,x); %Symbolic Jacobian w.r.t. x
J_p_sym(x,a,b,c) = jacobian(F,[a,b,c]); %Symbolic Jacobian w.r.t. [a b c]
J_x = J_x_sym(x1,2,-1,10); %Jacobian w.r.t. x, evaluated at the initial point
J_p = J_p_sym(x1,2,-1,10); %Jacobian w.r.t. [a b c], evaluated at the initial point

dp = [0 0 1]'; %Increments in parameters
dx_dp = -inv(J_x)*J_p; %Derivatives (Implicit Function Theorem) 
dx_approx = dx_dp*dp %Approximate change is solution

% We can quickly find approximate changes in the solution, without solving the original equation, when changes in parameters are "small", so the new solution is not far from the original point, or when the solution x is well approximated by linear function of the parameters.
% For example, the approximate change in solution due to \textrm{da}=0\ldotp 1 is:

dp = [0.1 0 0]'; %Change in parameters
dx_approx = dx_dp*dp %Approximate change is solution

x2 = vpasolve( F(x,2.1,-1,10) ); %New solution
dx_exact = x2 - x1 %Exact change in x

% and the approximate change in the solution due to \textrm{db}=0\ldotp 1 is:

dp = [0 0.1 0]'; %Change in parameters
dx_approx = dx_dp*dp %Approximate change is solution

x2 = vpasolve( F(x,2,-0.9,10) ); %New solution
dx_exact = x2 - x1 %Exact change in x

%
%% Two equations (two endogenous, one exogenous vars)
% Consider the system with \left(x,y\right) endogenous variables and a is exogenous variable:
% \begin{array}{l}
% 2\textrm{xa}+\textrm{xy}+a-2\sqrt{\;a}-11=0\\
% \textrm{xya}=6
% \end{array}
% It is easy to verify that \left(x=3,\;y=2\right) is a solution to the system when a=1. We would like to perfrom comparative statics analysis of impact on \left(x,y\right) of small changes in a.

clear %Clearing memory
close all %Closing figures
syms x y a %Declaring symbolic variables (x,y endogenous, a exogenous)
F1(x,y,a) = 2*x*a + x*y + a - 2*sqrt(a) - 11; %First implicit function
F2(x,y,a) = x*y*a - 6;                       %Second implicit function
F(x,y,a) = [F1(x,y,a); F2(x,y,a)]; %Declaring symbolic system
F(3,2,1) %Evaluating the system at given the point (3,2,1)

% Jacobians at initial solution.

J_xy_sym(x,y,a) = jacobian(F, [x, y]); %Jacobian w.r.t. endogenous variables
J_xy = J_xy_sym(3,2,1); %Evaluating the Jacobian at given the initial point
det_J = det(J_xy) %Determinant of Jcobian (making sure IFT works)

J_a_sym = jacobian(F,a); %Jacobian w.r.t. exogenous variable
J_a = J_a_sym(3,2,1); %Evaluating the Jacobian at given the initial point

% Comparative statics and derivatives.

da = 0.01; %Increment of a
dxy_da = -inv(J_xy)*J_a %Derivatives of x,y w.r.t. a (Implicit Function Theorem)
dxy_approx = double(dxy_da*da) %Approximate change in endogenous variables

% Exact changes.

[x0, y0] = vpasolve(F(x,y,1),[x,y]);
[x1, y1] = vpasolve(F(x,y,1+da),[x,y]);
d_xy_exact = [x1,y1]' - [x0,y0]'

%
%% Two equations (two endogenous, 2 exogenous vars)
% Cosider the system with \left(x,y\right) endogenous variables and \left(u,v\right) exogenous variables.
% \begin{array}{l}
% y^2 +2u^2 +v^2 -\textrm{xy}-15=0\\
% 2y^2 +u^2 +v^2 +\textrm{xy}-38=0
% \end{array}
% It is easy to verify that \left(x=1,y=4\right) is a solution to the system when \left(u=1,v=-1\right).

clear %Clearing memory
close all %Closing figure
syms x y u v %Declaring symbolic variables (x,y endogenous, u,v exogenous)
F1(x,y,u,v) = y^2 + 2*u^2 + v^2 - x*y - 15; %Declaring symbolic function
F2(x,y,u,v) = 2*y^2 + u^2 + v^2 + x*y - 38; %Declaring symbolic function
F(x,y,u,v) = [F1(x,y,u,v); F2(x,y,u,v)]; %Declaring symbolic system
F(1,4,1,-1) %Evaluating the system at given the initial point

% Jacobians at initial solution.

J_xy_sym(x,y,u,v) = jacobian(F, [x, y]); %Jacobian w.r.t. endogenous variables
J_xy = J_xy_sym(1, 4, 1, -1); %Evaluating the Jacobian at given the initial point
det_J = det(J_xy); %Determinant of Jcobian

J_uv_sym = jacobian(F,[u,v]); %Jacobian w.r.t. exogenous variable
J_uv = J_uv_sym(1, 4, 1, -1); %Evaluating the Jacobian at given the initial point

% Comparative statics and derivatives.

% Comparative statics
dxy_duv = -inv(J_xy)*J_uv; %Derivatives of x,y w.r.t. u,v (Implicit Function Theorem)

du = 0.1; dv = 0; %Increments in u,v
dxy_approx = double(dxy_duv*[du dv]') %Approximate changes in endogenous variables

% Exact changes.

[x0, y0] = vpasolve(F(x,y,1,-1),[x,y]);
[x1, y1] = vpasolve(F(x,y,1+du, 1+dv),[x,y]);
d_xy_exact = [x1,y1]' - [x0,y0]'

%