# Dowling13A.wxmx:  Comparative Statics

load(draw)$  set_draw_defaults(line_width=2, draw_realpart=false)$ 
fpprintprec:5$   ratprint:false$     kill(all)$

load ("Econ1.mac");

## Preface

Dowling13A.wxmx uses Maxima to work some of the problems in Ch. 13 of Introduction to 
Mathematical Economics (3rd ed), by Edward T. Dowling, (Schaum's Outline Series), 
McGraw-Hill, 2012. This text is a bargain, with many complete problems worked out in detail.
You should compare Dowling's solutions, worked out "by hand", with what we do using 
Maxima here.

A code file Econ1.mac as available in the same section (of Economic Analysis with Maxima),
which defines many Maxima functions used in this worksheet.

Use load ("Econ1.mac");

We use the function killAB()$ at the start of some sections. This function is defined
in Econ1.mac, and kills all bindings except for the functions defined in Econ1.mac (and
thus avoids having to constantly reload Econ1.mac).

Edwin L. (Ted) Woollett
http://web.csulb.edu/~woollett/
July, 2021

This worksheet is one of a number of wxMaxima files available in the section
    Economic Analysis with Maxima
on my CSULB webpage.

The main subjects of Ch. 13 are Comparative Statics and Concave Programming.

We have slightly changed some of the symbols used by Dowling in particular problems.

## Comparative Static Analysis

Comparative static analysis (comparative statics) compares the different equilibrium values
of the endogenous variables induced by changes in the values of exogenous variables and
parameters (given a specific model).

Comparative statics provides tools which can predict the changes in consumer demand 
caused by changes in excise taxes, tariffs, and subsidies. The change in national income
due to changes in consumer investment, government spending, and interest rates can also
be predicted. The change in the market price of some specific commodity due to changes
in weather conditions, price of production inputs, and the availability of transport.

These kinds of predictions require the first derivatives of endogenous variables with respect
to (wrt) exogenous variables and parameters, using some plausible model.

### One Endogenous Variable [13.2]

Comparative statics can be used with both specific functions and general functions.
Example 1 illustrates a specific function illustration and Example 2 demonstrates the
method with a general function.

### Example 1

Assume the demand Qd of some specific commodity is given by

Qd : m - n*P + k*Y;

with m, n, k > 0 and P = price, Y = consumers income.
Then the rate of change of the demand (D) as price P increases is given by the
partial derivative of Qd wrt P represented here by the symbol dDdP.

dDdP : diff (Qd, P);

and since n > 0, we have dDdP < 0, as all economists believe (as the price of a normal good
increases, consumers buy less of that good).

Assume the supply Qs of that commodity is given by

Qs : a + b*P;

with the parameters a, b > 0. The equilibrium price is the solution of Qd = Qs.

soln : solve (Qd = Qs, P);

Let Ps be the market price in this model which depends in income Y and the model
parameters.

Ps : at (P, soln);

We can find the rate of change of the market price Ps wrt Y, m, n, a, and b by taking the
relevant partial derivative, for example ∂Ps/∂Y, represented here by the symbol dPdY:

dPdY : diff (Ps, Y);

Since k, n, and b > 0, dPdY > 0 and, as you would expect, the market price of a normal good
rises as consumer income increases. The actual rate of increase clearly depends on the 
parameters k, n, and b.

### Example 2, Implicit Function Theorem

Now assume a general model in which 
   Demand = D (P, Y), with the "normal good" attributes:  ∂D/∂P < 0, and ∂D/∂Y > 0.
   Supply = S (P), with the "normal good" attribute: dSdP > 0.

The market price Ps ("the price that clears the market") is the solution (in this model) of
 D(P, Y) = S (P). 

The "excess demand" is the difference D - S at any price, and setting 
this excess demand equal to zero implies the same market price Ps.

Let the  excess demand be F(P, Y) = D(P, Y) - S (P), (for any P > 0 and any Y > 0).
We  have the relations among partial derivatives of F, D, and S:
   ∂F/∂P = ∂D/∂P - ∂S/∂P
   ∂F/∂Y = ∂D/∂Y.
Ps is the solution of F(P, Y) = 0.

We now need the implicit function theorem, which we recall using Maxima's depend
function.  With F(P, Y) = 0, the total derivative of F(P,Y) wrt Y, dF(P,Y)/dY, taking into 
account the dependence of P on Y, will have two terms.

Since F(P,Y) = 0,  dF(P,Y)/dY = 0 and we use this second equation to find an 
expression for dPdY in terms of the partial derivatives of F.

depends (F, [P, Y]);

depends (P, Y);

The left hand side of dF(P,Y)/dY = 0 is now

diff (F, Y);

So we have dF(P,Y)/dY = ∂F(P,Y)/∂Y + ∂F(P,Y)/∂P dP/dY = 0, and solving for dP/dY we get
   dP/dY = - ∂F(P,Y)/∂Y /  ∂F(P,Y)/∂P
which is the "implicit function theorem".  

We are assuming that ∂F(P,Y)/∂P # 0.

We then have dP/dY = - ∂D/∂Y/(∂D/∂P - ∂S/∂P) = - (+) / ((-) - (+))  >  0, using our
"normal good" assumptions above about the signs of the first partial derivatives of D and S.
For a normal good, the market price rises with rising consumer income.

If we are considering a "Giffen good", ∂D/∂Y < 0 and ∂D/∂P > 0, and  no such general
conclusion can be drawn about the sign of dP/dY; the sign will depend on the sign of
the denominator.

### More Than One Endogenous Variable, Matrix Solution

### Example 3 : Two Endogenous, Two Exogenous Variables

Let (y1, y2) be a pair of endogenous variables (dependent variables), (x1, x2) a pair of 
exogenous variables (independent variables). We assume each of the endogenous variables 
depends on the pair of exogenous variables.

Let market equilibrium (y1s, y2s) be the solution of the pair of equations:

       F (y1, y2; x1, x2) = 0,        G (y1, y2; x1, x2) = 0

We can solve for the signs of the derivatives (dy1/dx1, dy2/dx1) while simultaneouslly
respecting the equilibrium conditions by forming the two (total derivative) equations:  
         dF/dx1 = 0,                        dG/dx1 = 0. 
 
When evaluated at the equilibrium point, all the partial derivatives will have fixed values.

In this example, the functional dependence is implicit (rather than explicit) and we need to
tell Maxima what depends on what.

killAB()$
depends ([F, G], [y1,y2, x1, x2]);

depends ([y1,y2], [x1, x2]);

Now that Maxima has been told that F depends on (y1, y2, x1, x2) and both y1 and y2
depend on x1 and x2, diff (F, x1) will produce

   diff (F, x1) ==> ∂F/∂x1 + ∂F/∂y1 * ∂y1/∂x1  + ∂F/∂y2 * ∂y2/∂x1

in the next cell:

diff (F,x1) = 0;

diff (G,x1) = 0;

In these two equations, all factors are to be interpreted as partial derivatives.
If we transfer the last term of the left hand side (∂F/∂x1 or ∂G/∂x1)  to the right hand side 
(for each equation), we can write these two equations in terms of two element (matrix) 
column vectors  X and B, and a 2 x 2 matrix J,  as the matrix equation:
            J . X  = B. 

Let X be the two element matrix column vector whose elements are the (unknown)rates of 
change of the endogenous variables (y1, y2) wrt the exogenous variable x1.

X : cvec ( ["∂y1/∂x1", "∂y2/∂x1"] );

Let B be the two element matrix column vector of the negative of the partial
derivatives of F and G wrt x1; only any explicit dependence of F and G on x1
contributes to B (with a negative sign).

B : cvec ( ["- ∂F/∂x1", "- ∂G/∂x1"] );

Let J be the 2x2 matrix whose elements are the partial derivatives of F and G wrt the
pair of endogenous variables (y1, y2). The first row the partial derivatives of F, the
second row the partial derivatives of G (we will later use the Maxima function jacobian
to calculate this, using the syntax jacobian ( [F, G], [y1, y2] ); )

J : matrix ( ["∂F/∂y1",  "∂F/∂y2"],
                 ["∂G/∂y1", "∂G/∂y2"] );

The list of the first row elements can be extracted using J[1]:

J[1];

Likewise, a list of the elements of the second row of the matrix J:

J[2];

Finally, a single element, say row i and column j, using J[i, j] = J[row, col].
Here, row 1 and column 2:

J[1,2];

If determinant (J) # 0, then one can use Cramer's rule (for example) to solve for
the unknowns, the elements of the matrix column vector X.

Assuming a solution for X exists, we will normally use the simplest Maxima route:
   X : invert(J) . B.
The Maxima function invert (M) calculates the matrix inverse of the matrix M, so
that once found, you can check that invert (M) . M = ident (2)  (if M is 2x2),
and, of course, M . ident (M) = ident (2) as well.

Don't try this with the above definitions of J and X, which involve strings (stuff in
double quotes). By using the Maxima function grind, you can check the actual
code without the pretty printing (and surpression of double quotes, etc) used by
default in wxMaxima interface (which sets display2d to true).

grind(J[1,1])$

display2d;

display2d : false;

J;

display2d : true;
J;

### Example 4:  Two Endogenous Variables Y, i

In this example, Dowling assumes equilibrium in both  the goods and services market 
(IS curve) and also the money market (LM curve), using a simple model, to asses the 
effect of an increase in autonomous comsumption C0 on the equilibrium values of income 
Y and interest  rate i.

The equilibrium of the goods and services market, national income(Y) = consumption,
requires Y = C0 + C(Y,i), where C0 is the "autonomous consumption" and the C (Y, i) 
is the added consumption in terms of an implicit function of income Y and interest rate 
on money i. 

We assume 0 < ∂C(Y,i)/∂Y < 1 and ∂C(Y,i)/∂i < 0 (consumption of goods and services 
 increases when national income increases, and decreases when the interest rate on 
money increases).

We later use the symbols Cy and Ci to stand for the partial derivatives ∂C(Y,i)/∂Y and 
∂C(Y,i)/∂i respectively.

The equilibrium of the money market requires

     demand for money ("liquidity preference") L(Y,i) = supply of "real money" M0/P, 

where M0 (>0) = supply of money,  P (>0) = the price level. For simplicity we will hold 
P constant.  We assume Li =  ∂L(Y,i)/∂i < 0, and Ly =  ∂L(Y,i)/∂Y > 0 (demand for money
decreases as the money interest rate i increases, demand for money increases as 
national income increases.)

We bring all terms of the equilibrium equations to the lefthand side to write:

       F(Y, i; C0, M0, P) = Y - C0 - C (Y, i) = 0,

       G(Y, i; C0, M0, P) = L (Y, i) - M0/P = 0

Beginning with two equilibrium conditions, F = 0 and G = 0, which in principle can
be solved for unique(?)  equilibrium values Ys =  Y(C0, M0, P) and is = i(C0, M0, P), we 
can determine the response of Ys and is to increases in C0 (for example) by setting the
total derivatives of F and G wrt C0 equal to zero:

     dF/dC0 = 0, dG/dC0 = 0 

to solve for

   yc0 = ∂Y/∂C0 and  ic0 = ∂i/∂C0. 

Find the effect on the equilibrium levels of Y and i of an increase in the 
autonomous consumption C0. In this example, we can ignore the dependence of (Y,i)
on M0 and P in declaring dependencies:

killAB()$
F : Y - C0 - C;
G : L - M0/P;
depends ([Y,i], C0);
depends ( [C, L], [Y, i]);
diff (F, C0);
diff (G, C0);

Use symbols: Cy = ∂C(Y,i)/∂Y, Ci = ∂C(Y,i)/∂i,  Ly =  ∂L(Y,i)/∂Y,  Li = ∂L(Y,i)/∂i, 
Yc0 = ∂Y/∂C0, ic0 = ∂i/∂C0. Write the pair of equations dF/dC0 = 0 and dG/dC0 = 0 
in matrix form J . X = B as follows:

X : cvec ([Yc0, ic0]);
B : cvec ([1, 0]);
J : matrix ( [1 - Cy, - Ci],
                 [Ly,  Li] );
J . X = B;

determinant (J);

Using our assumptions that Cy > 0, Cy < 1, Ci < 0, Ly > 0, Li < 0, the two terms of
the determinant(J) have the sign structure  (-)(+) + (+)(-) = (-) + (-) < 0, so |J| # 0,
and a solution Xs should exist.

Note two things.
1. We can infer the form of the needed matrix J by using jacobian ([F,G], [Y,i]) (after
declaring dependency of the impllicit functions C and L on Y and i as we have done above).

jacobian ([F, G], [Y, i]);

Then, using the symbols Cy, Ci, Ly, Li, we can define the 2x2 matrix J immediately by
comparison with the above structure.

2.  If you remove the dependencies declared above (using remove, see below), you
can create the needed matrix column vector B using
       - jacobian ([F, G], [C0]);
which will only take into account explicit factors of C0 in F and G.

remove ([Y,i,C,L], dependency);

-jacobian ([F, G], [C0]);

We haven't done anything dangerous to our definitions of B and J:

B;
J;

Solve for the unknown values of X by multiplying both sides of the matrix equation
X = J . B by invert(J), using the property invert(J) . J = ident(2) = the 2x2 identity matrix 
with 1's on the diagonal and 0's on the off-diagonals. We are assuming determinant (J) is
not equal to zero.

Call the resulting solution (matrix) column vector Xs.

Xs : invert (J) . B;

Yc0 : Xs[1,1];

ic0 : Xs[2,1];

The denominator of Yc0 is the same as determinant(J), which we found above is negative,
given our assumptions about the signs of the relevant pieces. And since Li < 0 also, 
we have sign (Yc0) = (-)/(-) = (+) > 0, and national income Y has a positive response
to an increase in autonomous consumption C0.

The denominator of ic0 is the same as the denominator of Yc0 and is negative. Since Ly > 0,
sign (ic0) = - (+) /(-) > 0 and this model (which includes the sign assumptions of the 
implicit functions C(Y,i) and L(Y,i) ) predicts that interest rate i also has a positive response
to an increase in autonomous consumption C0.

The Maxima function sign(A) attempts to return the sign of A, based on the facts in the 
current data base. We use assume (...) to add facts about our symbols to the current
 data  base. facts (a); returns what is assumed about a. facts(); returns all data in the
current context.

assume (Cy > 0, Cy < 1, Ci < 0, Ly > 0, Li < 0 );

facts( );

sign (Yc0);

sign (ic0);

The ability of Maxima to use the assume data base to infer a sign of an expression is weak
and should not be trusted. Always check signs "by hand".

We assumed 0 < Cy < 1, Ci < 0, Ly > 0, Li < 0. 
We conclude that
A.  an increase in autonomous consumption C0 will lead to an increase in the equilibrium 
level of national income Y,
B. an increase in autonomous consumption C0 will also lead to an increase in the 
   equilibrium level of the money interest rate i.

The effect on Y and i of a increase in money M0 is treated in problem 13.10.

### Problem 13.10: Continuation of Example 4

We continue with the same model used in Example 4 (above), and analyze the 
effects of an increase in the money supply M0 (>0) on national income Y and 
money interest rate i, holding P (>0)  constant. 

Beginning with two equilibrium conditions, F = 0 and G = 0, we 
 form the two (total derivative) equations 
     dF/dM0 = 0, dG/dM0 = 0 
to solve for the pair of unknown quantities:
   ym0 = ∂Y/∂M0 and  im0 = ∂i/∂M0. 

We use a speedier route to the solution, taking advantage of (1) and (2) above.

killAB()$
facts( );

We first find B, before declaring any dependencies. In that situation diff (F,M0) will act like
a partial derivative ∂F/∂M0 --> 0  and diff (G, M0) will act like a partial derivative 
 ∂G/∂M0 --> -1/P

F : Y - C0 - C;
G : L - M0/P;
B : - jacobian ( [F, G], [M0] );

To find the matrix J, the matrix of partial derivatives of (F,G) wrt (Y,i), we need to tell 
Maxima that the implicit functions C and L both depend on Y and i in some undefined 
manner.

depends ( [C, L], [Y, i]);
jacobian ([F, G], [Y, i]);

Now define J using symbols for the partial derivatives.

J : matrix ( [1 - Cy,  - Ci ],
                 [Ly, Li] );

Note that this is the same J as used in Example 4 above. Only B is different.

Xs : invert (J) . B;

Ym0 : Xs [1,1];

im0 : Xs [2,1];

With D = determinant(J) < 0, and P > 0, Ym0 = Ci/ (D*P), sign (Ym0) = (-)/[(-)(+)] = (-)/(-) > 0,
and im0 = (1-Cy)/ [D*P], sign(im0) = (+)/[(-)(+)] < 0.

assume (Cy > 0, Cy < 1, Ci < 0, Ly > 0, Li < 0, P > 0 );
matrix ( ["Ym0", "im0" ], [sign (Ym0), sign (im0)] );

With the assumptions: 0 < Cy < 1, Ci < 0, Ly > 0, Li < 0, P > 0, we conclude that
A. an increase in money supply M0 will lead to an increase in the equilibrium level of 
national income Y (income will rise).

B. an increase in money supply M0 will lead to a decrease in the equilibrium interest rate i
 (the interest rate will fall).

### Problem 13.11 Three Endogenous Variables Y, C, T, Statics

Consider a three-sector income determination model [see Ch. 2, sec 3, Ch. 6, sec.2]
in which Y = national income (GDP), C = consumption, T = taxation, t = tax rate, 
T0 = autonomous taxation, C0 = autonomous consumption, G0 = government spending, 
I0 = autonomous (capital) investment, and b = propensity to consume after-tax income, 
the equilibrium values of Y, C, and T are assumed to satisfy the following three equations.

 Y = C + I0 + G0,
C = C0 + b (Y - T)
T  = T0 + t Y

We rewrite these three equations by bringing all terms over to the lefthand side and
defining an equilibrium as the triplet of equations:  F = 0, G = 0, H = 0:

F = Y - C - I0 - G0 = 0,
G = C - C0 - b (Y - T) = 0,
H = T - T0 - t Y =  0

How are Y, C, and T affected by an increase in government spending G0?
Assume C0, I0, G0, T0 > 0, and assume 0 < b, t < 1.

There are no implicit functions of the dependent variables (endogenous variables) 
Y, C, and T, such as we had above, so we can use Statics ([F, G, H], [Y,C,T], G0), where
Statics (funcList, endogenousList, param) is defined in Econ1.mac.

killAB()$
F : Y - C - I0 - G0;
G : C - C0 - b*(Y - T);
H  : T - T0 - t*Y;
M : Statics ( [F, G, H], [Y, C, T], G0 );

Our solution (matrix) column vector Xs is the second column of M:

Xs : col (M, 2);

The denominator D of each element is the same, which can be written in the form
D = 1 - b (1 - t). We have 0 < b,t < 1, so 0 < (1-t) < 1, so b (1-t) < 1 and D > 0.
Hence all three elements of Xs are positive.

An increase in autonomous government spending G0, is predicted to induce
an increase in the equilibrium income Y, in  the equilibrium consumption C, and in 
the equilibrium taxation T, according to this model.

This example will show the weakness of Maxima's sign function in logical deductions based
on the assume data base.

facts ();

assume (b > 0, b < 1, t > 0, t < 1);

facts ();

sign (b*t - b + 1);

A pnz return from sign stands for positive, negative, or zero; ie., no information!!

assume ( (b*t - b + 1) > 0 );

for j thru 3 do print (M[j,1],"  ", sign (M[j,2]) )$

### Problem 13.12, Continuation of Prob. 13.11, Statics

Same as previous problem, but what about an independent increase in autonomous 
taxation T0?

We haven't used killAB()$, so the definitions of F,G,H should still be known.

F;

M : Statics ( [F, G, H], [Y, C, T], T0 );

for j thru 3 do print (M[j, 1],"  ", sign (M[j, 2]) )$

An increase in autonomous taxation T0 will induce a decrease in income Y and a decrease
in consumption C, and a increase in overall taxation T, according to this model.

### Problem 13.13, Continuation of Prob 13.11, Statics

Same problem, effects of an increase in the tax rate t.

M : Statics ( [F, G, H], [Y, C, T], t );

We haven't bothered to include Y in our assume database so far.

assume (Y > 0);

for j thru 3 do print (M[j, 1],"  ", sign (M[j, 2]) )$

An increase in the tax rate t will induce a decrease in income Y and consumption C, but
an increase to total taxation T, according to this model.

### Problem 13.14:  Y, C, Z  in an Open Economy, Statics

Given a national income determination model which includes trade:

    Y = C + I0 + G0 + X0 - Z,    C = C0 + b Y,    Z = Z0 + z Y, 

where X0 = exports (domestically produced goods and services purchased by foreigners),
Z = imports (domestically purchased goods and services produced in other countries), and
where a 0 subscript indicates an exogenously fixed variable,  find the effects on 
Y, C, and Z of an increase in autonomous exports X0.

The reason for the minus sign in front of imports Z is that national income (GDP) is meant
as an estimate of the value of all goods and services produced in the U.S. If you buy a
Volvo for $80,000 from a Swedish car manufacturer (an imported car) that transaction
increases "consumption" (in the national accounting) by $80,000, but should not be
counted finally, because it does not represent goods and services produced in the U.S. 
Hence, as an accounting maneuver, that transaction is added to the import category also, 
and in the final accounting cancels out the consumption number.

We rewrite the three model equations by bringing all terms over to the lefthand side and
defining the equilibrium as:  F = 0, G = 0, H = 0:

F = Y - C - I0 - G0 - X0 + Z = 0,
G = C - C0 - b Y = 0,
H = Z - Z0 - z Y =  0

Assume C0, I0, G0, Z0 > 0, and assume 0 < b, z < 1. (b is the marginal propensity to
consume, z is the marginal propensity to import.)

There are no implicit functions of the dependent variables (endogenous variables) 
Y, C, and Z, so we can use Statics ([F, G, H], [Y, C,  Z], X0), where
Statics (funcList, endogenousList, param) is defined in Econ1.mac.

killAB()$
F : Y - C - I0 - G0 - X0 + Z;
G : C - C0 - b*Y;
H  : Z - Z0 - z*Y;
M : Statics ( [F, G, H], [Y, C, Z], X0 );

Each denominator is z - b + 1 = z + (1 - b) = (+) + (+) = (+) > 0, since we assume 0 < b, z < 1.

assume (b > 0, b < 1, z > 0, z < 1);

sign (z - b + 1);

assume ( (z - b + 1) > 0);

for j thru 3 do print (M[j, 1],"  ", sign (M[j, 2]) )$

This model predicts that an increase in exports X0 will produce an increase in national 
income Y, an increase in consumption C, and an increase in the purchase of imports Z.

### Problem 13.15 Continuation of Prob. 13.14, Statics

Continuing with the model of Prob. 13.14, find the effects of an increase in the propensity to
consume b on Y, C, and Z.

M : Statics ( [F, G, H], [Y, C, Z], b );

assume (Y > 0);

for j thru 3 do print (M[j, 1],"  ", sign (M[j, 2]) )$

Increasing the parameter b has a positive effect of the equilibrium values of Y, C, and Z.

### Problem 13.16 Continuation of Prob. 13.14, Statics

Continuing with the model of Prob. 13.14, find the effects of an increase in the propensity to
import z on Y, C, and Z.

M : Statics ( [F, G, H], [Y, C, Z], z );

for j thru 3 do print (M[j, 1],"  ", sign (M[j, 2]) )$

An increase in the propensity to import will produce an increase in the equilibrium value of
imports Z, but a decrease in the equilibrium values of national income Y and consumption C.

### Problem 13.17, Implicit Functions of (Y, i) in an Open Economy

For an open economy, national income Y = C + I + G + NX, where NX = X - Z = net exports
= exports (X) - imports (Z).

Let "national savings"  S = Y - C - G = (national income minus consumer spending minus
government spending).  Then we have
                      I + X = S + Z. 

We assume exports X = X0, domestic capital investment I = I(i), national savings S = S (Y,i), 
imports Z = Z(Y,i). The  equilibrium values of (Y,i) must satisfy

                   I(i) + X0 = S(Y,i) + Z(Y,i).

Domestic capital investment I(i) (investment for future production) is assumed to change with 
interest rate i according to Ii =  ∂I/∂i < 0 (increasing interest rate induces decreased capital 
investment). We also assume that Si =  ∂S/∂i > 0  (an increase in interest rate induces a 
larger value of national savings S. We also assume an increase in national income is 
accompanied by an increase in national savings S. 
Let Sy = ∂S/∂Y.  Then the assumption is that  0 <  Sy < 1.

We assume imports Z increase with national income Y, so Zy = ∂Z/∂Y > 0, and we assume 
imports Z decrease with interest rates, so Zi = ∂Z/∂i < 0.

We consider simultaneous equilibrium in the goods market of an open economy and in 
the money market. For the latter, we need money supply M0 = money demand = L(Y,I).

                  L(Y, i) = M0

L(Y,i) = "liquidity preference" = money demand as a function of national income Y and
interest rate i, is assumed to have a positive response to an increase in national income:
Ly = ∂L/∂Y > 0, and a negative response to an increase in the interest rate i: Li =  ∂L/∂i <  0. 

We look for the effects of an increase in money supply M0 on the equilibrium values of 
Y and i. That is, we want to predict the signs of  Ym0 =  ∂Y/∂M0 and im0 =  ∂i/∂M0.

killAB()$
facts();

Before we define J (which needs dependencies declared), we define B which looks for
explicit occurrence of M0 only, not implicit.

F : I + X0 - S - Z;
G : L - M0;
B : - jacobian ( [F, G], [M0] );

Now we want to define the 2x2 matrix J symbolically.

depends (I, i);
depends ( [S, Z, L], [Y, i]);
jacobian ([F, G], [Y, i]);

If you have questions about where matrix elements start and end, you can use grind.

grind(%)$

Now we define the matrix J, using the symbols Sy, Si, Zy, Zi, Ly, Li, and Ii = ∂I(i)/∂i, 
being careful to match the structure of the jacobian matrix above.

J : matrix ( [ - Zy - Sy , - Zi - Si + Ii ],
                 [Ly, Li] );

grind(%)$

In the version of Maxima I am using, the display2d = false output of grind includes 
unnecessary parentheses, returning:
           matrix ( [ (-Zy) - Sy, (-Zi) - Si + Ii], [ Ly,  Li]  ).
These superflous parentheses do not interfere with Maxima's ability to do algegra, etc.
and are a bug in the version I am using.

Let D be the determinant of the matrix J. We are interested in its sign, since the solutions
Ym0 and im0 include this determinant.

D : determinant (J);

We assume: Ii < 0, Si > 0, Zi < 0, Li < 0, Sy > 0, Zy > 0, Ly > 0.
The sign of the first term of D is 
   (-) [ - (+) - (+) ] = (-) (-) = (+).
The sign of the second term of D is
  - (+) [ - (-) - (+) + (-) ] = (-) [ (+) - (+) + (-) ] = (-) + (+) - (-) = (-) + (+) = undetermined sign.
If we write Zi = - |Zi|, Ii = - |Ii|, the second term is positive if (Si + |Ii|) > |Zi|, in which case
D > 0.  We will assume in the following that D > 0. (There are clearly other ways D can
be positive: even if the second term is negative, the first (pos) term could be greater than
the magnitude of the second term.)

Solve for the solution vector Xs

Xs : invert (J) . B;

Ym0 : Xs[1,1];

im0 : Xs[2,1];

assume (Sy > 0, Si > 0, Ii  < 0, Zi < 0, Zy > 0, Li < 0, Ly > 0);

D : denom (Ym0);

determinant (J);

assume (% > 0);

sign (D);

sign(Ym0);

The numerator of Ym0 is the same combination whose sign was in question when we
looked at the sign of D.

num (Ym0);

sign(%);

Let's go ahead and make an assumption that gets us to D > 0 and Ym0 > 0.

assume (Zi + Si - Ii > 0);

matrix (["Ym0", "im0"], 
           [sign (Ym0), sign (im0)] );

This model (plus our assumptions) predicts that national income Y has a positive response
to an increase in money supply M0, and interest rate i has a negative response.

### Problem 13.18, Continuation of Prob. 13.17

Using the above model and assumptions to find the responses of Y and i to an increase in
exports X0. Let Yx0 = ∂Y/∂X0 and ix0 = ∂i/∂X0.

We retain our definition of the 2x2 matrix J and the sign assumptions made in 13.17 above.
All we need is a new vector B corresponding to the partial derivative of F and G wrt X0.

remove ([I, Z, S, L], dependency);

B : - jacobian ( [F, G], [X0] );

Xs : invert (J) . B;

Yx0 : Xs[1,1];

ix0 : Xs[2,1];

matrix (["Yx0", "ix0"], 
           [sign (Yx0), sign (ix0)] );

This model (plus our assumptions) predicts that both the national income Y 
and the interest rate i have a positive response to an increase in autonomous exports
X0.

### Comparative Statics for Unconstrained Optimization Problems

What will be the effects of increases in the values of exogenous (independent) variables on
the solution values of optimization problems?

Answers can be found by applying comparative statics techniques to the first order conditions
from which the initial optimal values are determined.

Since first order conditions involve first order derivatives, comparative static analysis of 
optimization problems will involve second order derivatives and Hessian determinants.

The general approach is described in Example 5 following.

### Example 5: Profit function π (K, L)

A price-taking firm has a "strictly concave" production function Q (K, L) in which
K = units of capital, L = units of labor.

As defined in Dowling, Ch. 4, this implies that  ∂²Q/∂K² < 0 for all K > 0 and 
likewise  ∂²Q/∂L² < 0 for all L > 0.

Given P = output price, r = rental rate of capital (interest rate), and w = wage, 
the firm's profit function is revenue - cost:
     π = P Q (K, L) - r K - w L.

The optimum values (Ko,Lo) are solutions of the pair of equations (first order conditions FOC)
  ∂π/∂K = 0 and ∂π/∂L = 0. We write the left hand sides as implicit functions F and G of
  K and L, and ask how the critical points change in response to increases in each of the
exogenous variables r and w. In general both (Ko, Lo) are functions of both (r, w).

Thus with F = ∂π/∂K = 0, G = ∂π/∂L = 0, we take the total derivative of both F and G
wrt r, say, and in general get three terms  = 0 for each of a pair of equations:

 dF/dr =  ∂F/∂K ∂K/∂r + ∂F/∂L ∂L/∂r + ∂F/∂r = 0,
 dG/dr =  ∂G/∂K ∂K/∂r + ∂G/∂L ∂L/∂r + ∂G/∂r = 0,

We can write this pair of equations in matrix form  as J . X = B, where

killAB()$
J : matrix ( ["∂F/∂K", "∂F/∂L"],
                 ["∂G/∂K", "∂G/∂L"] );
X : cvec ( ["∂K/∂r", "∂L/∂r"] );
B : cvec ( ["- ∂F/∂r", "- ∂G/∂r"] );

Let QK stand for the partial derivative ∂Q/∂K and QL stand for ∂Q/∂L.

Before declaring dependencies of QK and QL, find vector B, in which we are only looking
for the partial derivatives of F and G wrt r (ie., explicit appearance of r).

F : P*QK - r;
G : P*QL - w;
B : - jacobian ( [F, G], [r] );

The 2x2 matrix J has content found by declaring an implicit dependence of QK and QL 
on (K,L) and then using the Maxima function jacobian. 
The content of J has been described in our above definition of J (in terms of strings).

depends ([ QK, QL], [K, L] );
jacobian ( [F, G], [K, L] );

Above, we used Maxima strings (stuff in double quotes) to show the general structure
of J, and using the jacobian with the actual model shows us how to introduce appropriate
symbols to represent J.

Let QKK stand for ∂²Q/∂K² and QLL stand for ∂²Q/∂L², each < 0 here.

Let QKL stand for ∂(QK)/∂L = ∂²Q/∂L∂K  and likewise 
  let  QLK stand for ∂(QL)/∂K = ∂²Q/∂K∂L.

[See Dowling, Sec. 5.3, p. 85 for Dowling's conventions for cross (mixed) partial derivative 
 notation. Note that some texts on mathematical economics use a different notational 
 convention. Dowling's conventions, which we use, agree with the Wikipedia page
  https://en.wikipedia.org/wiki/Partial_derivative#Higher_order_partial_derivatives]

We can now more specifically define our symbolic J as:

J : matrix ([P*QKK, P*QKL],
               [P*QLK, P*QLL] );

Let Kr stand for ∂K/∂r and  Lr stands for ∂L/∂r in our definition of the unknown vector X.
We want to know the signs of these first derivatives of the critical point solutions.

X : cvec ( [Kr, Lr]);

The symbolic matrix form of our pair of equations to solve for Kr and Lr are now:

J . X = B;

The determinant of J, |J|,  must be non-zero for a solution of this matrix equation to exist.
Let D stand for |J|.

D : determinant (J), factor;

The matrix J is the same as the Hessian matrix of either π or P*Q wrt (K,L), bearing in 
mind that  Maxima's symbolic diff function does not preserve (in the return value), the
difference between diff( diff(Q,K),L) and diff ( diff (Q,L), K).

depends ( Q, [K, L] );
H : hessian (P*Q, [K, L] );

[diff ( diff (Q,K), L),  diff ( diff (Q,L), K)];

so, of course |H| = |J|. The critical point (Kc,Lc) is a relative maximum of the profit function
π (in an unconstrained optimization) if (with H evaluated at the critical point (Kc, Lc) ) 
the first leading principal minor of H, LPM(H,1) < 0,  (true, since P*QKK < 0, due to our 
concavity assumptions and P > 0) , and if LPM (H, 2) = |H| > 0, which is equivalent to
|J| > 0. Then π is a "concave function of K and L" near the critical point and we assume
D = determinant(J) > 0.

For a symbolic solution in Maxima, we can use:  Xs : invert (J) . B

Xs : invert (J) . B, factor;

Kr : Xs[1,1];

The denominator of Kr is (D/P) > 0 by assumption. In the numerator, QLL < 0 so
we conclude Kr < 0 and the optimum level of capital K decreases with an increase in 
the interest rate r.

Lr : Xs[2,1];

To sign Lr, we need to know the sign of the cross partial QLK, the response to an increase 
in capital K of the marginal productivity of labor QL. If we assume QLK is positive, which is 
likely, then an increase in the interest rate r will cause a decrease in the optimum level of
units of labor.

### Problem 13.19 Continuation of Example 5

Using the same model as Example 5, calculate the response of (Kc, Lc) to an increase 
in wage w.

We use the same 2x2 matrix J. We just need to redefine the vector B.

remove ([QK, QL], dependency);
B : - jacobian ([ F, G], [w] );

Xs : invert (J) . B, factor;

Let Kw stand for ∂K/∂w and Lw stand for ∂L/∂w.

Kw : Xs[1,1];

Note that QKL = QLK if both cross derivatives are continuous (Young's Theorem).
As in Example 5, we assume the denominator (D/P) is positive, and QLK > 0 (which is
likely), so QKL > 0. 
Then Kw < 0 and the optimal level of capital will likely fall in response to an increase
in wage w.

Lw : Xs[2,1];

With the denominator (D/P)  positive, and QKK < 0 by assumption, Lw < 0 and the
optimal level of labor will decrease in response to an increase in wage w.

### Problem 13.20 Continuation of Prob. 13.19

Using the same model as in Example 5, what can be said about the response of the
optimum levels of capital K and labor L to an increase in commodity price P?

We continue to use the same matrix J. We need to redefine the vector B. We don't need
to remove the dependencies of QK and QL, since that removal remains in force from
Prob. 13.19 above.

B : - jacobian ([ F, G], [P] );

Xs : invert (J) . B, factor;

Let Kp stand for ∂K/∂P and Lp stand for ∂L/∂P.

Kp : Xs[1,1];

The denominator (D/P) is assumed positive, as in Example 5. 

Working on the numerator, the marginal productivity of capital MPK = QK = ∂Q/∂K > 0 and 
the marginal productivity of labor MPL = QL = ∂Q/∂L > 0 (both typical Economics 
assumptions). We also assume QLL < 0 to be considering a maximum point (Kc, Lc) of 
the profit function.

We continue to assume QLK > 0 (which is likely) and we assume QKL = QLK 
(assuming each is a continuous function of K and L), so we in effect assume the marginal 
productivity of capital QK will increase for an increase in labor.

With these assumptions, Kp > 0 and the optimum level of capital will increase with an
increase in commodity price P.

If QKL < 0, then the sign of Kp is indeterminate.

Lp : Xs[2,1];

With our previous assumptions, QKK < 0, QK > 0, QL > 0, QLK > 0 (likely), we get Lp > 0
and the optimum level of labor increases with price P. If QLK < 0, the sign of Lp is
indeterminate.

### Problem 13.21 Discounted Revenue Profit Function

A firm seeks to optimize a profit function in which the production revenue is discounted
with time t (using a constant interest rate r) x1 and x2 are production inputs, P1 and P2 are
the corresponding input prices. P0 is price obtained by the firm per unit produced, Q is the
number of units produced. Then the assumed profit function, given the time t and rate r, is:

  π(x1, x2)  = P0 Q (x1, x2) e^(- r t) - P1 x1 - P2 x2

where the unconstrained optimal production inputs (x1c,x2c) are solutions of the equations
  F =  ∂π/∂x1 = P0 ∂Q/∂x1 e^(- r t) - P1 = 0
  G =  ∂π/∂x2 = P0 ∂Q/∂x2 e^(- r t) - P2 = 0

Consider the response of the optimal production inputs (x1c,x2c) to an increase in P0.
The total derivatives of F and G wrt P0 must also be equal to zero, and have the form
     dF/dP0 =  ∂F/∂x1 ∂x1/∂P0 + ∂F/∂x2 ∂x2/∂P0 + ∂F/∂P0 = 0,
     dG/dP0 =  ∂G/∂x1 ∂x1/∂P0 + ∂G/∂x2 ∂x2/∂P0 + ∂G/∂P0 = 0,

We can write this pair of equations in the matrix form J . X = B. 

Let Q1 stand for ∂Q/∂x1 and Q2 stand for ∂Q/∂x2.

In Maxima, %e is the base of  the natural logarithm, and log(A) refers to the natural
logarithm, so in Maxima, we have log (%e^A) = A and %e^log(A) = A.

Before declaring dependencies of Q1 and Q2, find the vector B, in which we are only looking
for the partial derivatives of F and G wrt P0 (ie., explicit appearance of P0).

[log (%e^A), %e^log(A)];

killAB()$
F : P0*Q1*%e^(- r*t) - P1;
G : P0*Q2*%e^(- r*t) - P2;
B : - jacobian ( [F, G], [P0] );

The 2x2 matrix J has content found by declaring an implicit dependence of Q1 and Q2 
on (x1, x2) and then using the Maxima function jacobian. 
The interest rate r and the time t are treated as constants here.

depends ([ Q1, Q2], [x1, x2] );
jacobian ( [F, G], [x1, x2] );

Using the jacobian with the model shows us how to introduce appropriate symbols to 
represent the elements of the matrix J.

Let Q11 stand for ∂²Q/∂x1² and Q22 stand for ∂²Q/∂x2², each < 0 here.

Let Q12 stand for ∂(Q1)/∂x2 =  ∂²Q/∂(x2)∂(x1)  and likewise 
    let Q21 stand for ∂(Q2)/∂x1  =   ∂²Q/∂(x1)∂(x2). 

 [See Dowling Sec. 5.3, p. 85 for Dowling's conventions for cross (mixed) partial derivative 
   notation. Note that some texts on mathematical economics use a different notational 
   convention. Dowling's conventions, which we use, agree with the Wikipedia page
  https://en.wikipedia.org/wiki/Partial_derivative#Higher_order_partial_derivatives]

J : matrix ([P0*Q11*%e^(-r*t), P0*Q12*%e^(-r*t)],
               [P0*Q21*%e^(-r*t), P0*Q22*%e^(-r*t)] );

Let x1p stand for ∂x1/∂P0 and  x2p stands for ∂x2/∂P0 in our definition of the unknown 
vector X. We want to know the signs of these first derivatives of the critical point solutions.

X : cvec ( [x1p, x2p]);

The symbolic matrix form of our pair of equations to solve for x1p and x2p are now (as you
can check):

J . X = B;

The determinant of J, |J|,  must be non-zero for a solution of this matrix equation to exist.
Let D stand for |J|.

D : determinant (J), ratsimp, factor;

%e^(-2*r*t) > 0, P0^2 > 0, Q11 < 0, Q22 < 0, Q11*Q22 > 0, and we assume Q12 = Q21.
Finally, we assume Q11*Q22 > (Q12)^2, so D > 0.

The matrix J is the same as the Hessian matrix of either π or P0*Q wrt (x1,x2), bearing in 
mind that Maxima's symbolic diff function does not preserve (in the return value), the
difference between diff( diff(Q,x1),x2) and diff ( diff (Q,x2), x1).

depends (Q, [x1, x2]);
H : hessian (P0*Q*%e^(-r*t) - P1*x1 - P2*x2, [x1, x2] );

[diff( diff(Q,x1),x2), diff ( diff (Q,x2), x1)];

Compare H to the matrix J:

J;

so, of course |H| = |J|. The critical point (x1c,x2c) is a relative maximum of the profit function
π (in an unconstrained optimization) if (with H evaluated at the critical point (x1c, x2c) ) 
the first leading principal minor of H, LPM(H,1) < 0,  (true, since P0*Q11 < 0, due to our 
concavity assumptions and P0 > 0) , and if LPM (H, 2) = |H| > 0, which is equivalent to
|J| > 0. Then π is a "concave function of x1 and x2" near the critical point and we assume
D = determinant(J) > 0.

A reminder of what D is:

D;

Since P0^2 > 0 and e^(- 2*r*t) > 0, D > 0 implies the factor (in the denominator of our
solutions):    (Q11*Q22 - Q12*Q21)  is greater than zero.

To get a (symbolic) solution in Maxima, we can now use:  Xs : invert (J) . B

Xs : invert (J) . B, ratsimp, factor;

Both elements have a common denominator which is positive (P0 > 0).

x1p = ∂x1/∂P0 is the top element of the solution vector Xs.

x1p : Xs[1,1];

In the numerator of x1p,  Q22 < 0 to have a profit function maximum , and also Q1 > 0
since an increase of inputs x1 should imply an increase in production, likewise
Q2 > 0. It is likely that Q12 > 0, so x1p = ∂x1/∂P0 > 0, and an increase in sales price (P0) 
per unit of production will cause an increase in the optimum level of input x1.

x2p : Xs[2,1];

In the numerator, Q11 < 0 for a profit function maximum, we assume Q1 and Q2 are positive,
 and Q21 = Q12 > 0 is likely, so x2p = ∂x2/∂P0 > 0, and an increase in sales price P0
per unit of production will cause an increase in the optimum level of input x2.

### Problem 13.22:  Continuation of Prob. 13.21

Using the same model for discounted revenue profit function as Prob. 13.21, examine 
 the signs of  x1t = ∂x1/∂t and x2t = ∂x2/∂t.

We can use the same 2x2 matrix J as above, but need to redefine the vector B.

remove ( [F, G], dependency);
B : - jacobian ( [F, G], [t] );

Xs : invert (J) . B, ratsimp, factor;

The denominator of each element of Xs is positive, according to the discussion in Prob. 13.21.

Reminder:  x1t = ∂x1/∂t and x2t = ∂x2/∂t.

x1t : Xs[1,1];

In the numerator of x1t, r > 0, Q1 > 0, Q2 > 0, Q22 < 0 and Q12 is very likely > 0. So the
numerator < 0 and ∂x1/∂t < 0, and the optimum value of input x1 decreases as time increases.
However, in the unlikely event that Q12 < 0, the sign of x1t is indeterminate.

x2t : Xs[2,1];

Based on the same reasoning, it is likely that ∂x2/∂t < 0 also.

### Problem 13.23: π(K, L) with Cobb-Douglas Production Function

Assume the production function Q(K, L) in Example 5 is a Cobb-Douglas function with
"decreasing returns to scale", so that
     Q (K, L) = A K^α L^β,
K (>0) is the number of units of capital used,
L (>0) is the number of units of labor used,
A (>0) is a (constant) efficiency parameter indicating the level of technology, 
α ( 0 < α < 1) is the constant output elasticity of capital, 
β (0 < β < 1) is the constant output elasticity of labor, and 
"decreasing returns to scale" implies (α + β) < 1. Then the profit = revenue - cost is:

     π (K, L) = P Q (K, L) - r K - w L

in which P (>0) is the price received by the firm per unit of production.

Let pr stand for the profit function π.

killAB()$
Q : A*K^α * L^β;
pr : P*Q - r*K - w*L;
F : diff (pr, K);
G : diff (pr, L);

There are no implicit functions in the definition of the profit function, and the optimizing
values of (K,L) are the solutions of the pair of equations F = 0, G  = 0. To find the response
of (K,L) optimizing values to an increase in wage  w, we solve the pair of equations
dF/dw = 0, dG/dw = 0 (both are total derivatives, bearing in mind that the optimizing
values of K and L depend on the wage rate w.

The pair of total derivatives of F and G result in the equations

dF/dw =  ∂F/∂K ∂K/∂w + ∂F/∂L ∂L/∂w + ∂F/∂w = 0,
 dG/dw =  ∂G/∂K ∂K/∂w + ∂G/∂L ∂L/∂w + ∂G/∂w = 0,

which can be solved for Kw = ∂K/∂w, and for Lw = ∂L/∂w.

In matrix form the pair of equations is equivalent to J . X = B, if we define B, J and X
as follows.

B : - jacobian ([F, G], [w] );

J : jacobian ([F, G], [K, L]);

X : cvec ([Kw, Lw]);

J . X = B;

D : determinant (J), ratsimp, factor;

Since 0 < α, β < 1, and (α + β) < 1, (β + α - 1) < 0, and D = |J| > 0.

We can then solve for the unknown vector Xs.

Xs : invert (J) . B, ratsimp, factor;

Kw : Xs[1,1];

With A*P > 0, (α + β) < 1, the denominator < 0, and the numerator > 0, so Kw < 0 and an
increasing wage rate w causes a decrease in the optimum value of capital K.

Lw : Xs[2,1];

With A*K^α * P*β > 0 and (α + β) < 1, the denominator < 0. With 0 < α < 1, (α-1) < 0, so
sign(Lw) = (-) [ (-)/(-)] = (-) (+) = (-), so Lw < 0 and an increasing wage rate w causes a 
decrease in the optimum value of labor L.

### Problem 13.24: Continuation of Prob. 13.23

Assume the same profit function model of Prob. 13.24, and find the response of the
optimum values of (K,L) to an increase in the output price P.

We use the same matrix J, and only have to redefine the vector B.

B : - jacobian ([F, G], [P] );

Xs : invert (J) . B, ratsimp, factor;

Let Kp = ∂K/∂P and Lp = ∂L/∂P.

Kp : Xs[1,1];

With K > 0, P > 0, (α + β) < 1, the denominator < 0 and Kp = ∂K/∂P > 0, and the optimum
number of units of capital will increase as the output price P increases.

Lp : Xs[2,1];

With L > 0, and a negative denominator, Lp = ∂L/∂P > 0, and the optimum number of units 
of labor will increase as the output price P increases. 

### Comparative Statics in Constrained Optimization

Our approach to these problems differs in some details from Dowling's approach.
You should compare our approach with his - we will attempt to point out the main differences
and the rationale for our approach.

Because some constraints involve a budget constraint B, we will use Z as a
column vector in the matrix equation J . X = Z.

### Example 6: Maximization of Output with Budget Constraint

Our discussion of Example 6 will be rather long-winded, with perhaps more information
than really necessary. But clarity at this stage of methods discussion is essential. We are
also showing you more than one way to solve these problems.

Given an implicit output function q (K, L),  with the budget constraint 

        g (K,L) = B,  with g(K,L) = r K + w L,

the Lagrangian function to be maximized is (this choice differs from Dowling):

        Q =  q(K,L) + λ (g - B) =  q (K, L) + λ (r K + w L - B).

The three first order conditions (FOC) which must be satisfied are (the order differs from 
Dowling):

     F1 = ∂Q/∂λ =  r K + w L - B = 0.
     F2 = ∂Q/∂K = ∂q/∂K + λ r = 0,
     F3 = ∂Q/∂L = ∂q/∂L + λ w = 0,
     
Any proposed set of optimal values found (λo, Ko, Lo) which satisfy these three first order
conditions (FOC) will in general depend on (r, w, B), and we want to calculate the signs of
the derivatives (∂λo/∂B, ∂Ko/∂B, ∂Lo/∂B), for example.  Let lam stand for λ.

Define g (K,L) and the Lagrangian function Q (we don't use L for the Lagrangian because 
L here stands for the number of units of labor in the production process).

Calculate the first derivatives of the Lagrangian Q wrt (lam, K, L), using Maxima to repeat
what we have just written down as three equations above.

We want to define symbolic expressions for F1 = ∂Q/∂λ, F2 = ∂Q/∂K, F3 = ∂Q/∂L.

We show two different (equivalent) ways to calculate the first derivatives F1, F2, and F3.
The second method produces a list we call derivs.

killAB()$
g : r*K + w*L; 
Q : q + lam*(g - B);
depends (q, [K, L]);
[ diff (Q, lam),  diff(Q, K),  diff (Q, L)];
derivs : jacobian ([Q],[lam, K, L])[1];

grind (derivs)$

Let symbol qK stand for ∂q(K,L)/∂K = 'diff (q,K), and qL stand for ∂q(K,L)/∂L = 'diff (q,L).
The expression 'diff (q, K) is called a "noun form" in Maxima. The single quote mark in
front of diff prevents Maxima from trying to carry out differentiation "as a verb".

[F1, F2, F3] :  subst ( ['diff(q,K) = qK, 'diff(q,L) = qL],  derivs);

Instead of using subst, you could just define the three expressions directly as a list
using the symbols you want (by comparison), as in:

  [F1, F2, F3] : [r*K + w*L - B, r*lam + qK, w*lam + qL];

A matrix we call J will play a central role in finding the desired first derivatives of (lam, K, L)
with respect to any one of the exogenous variables, r, w, and B. 

We begin by calculating the Jacobian matrix of (F1, F2, F3) with respect to (lam, K, L) in a 
preliminary form we call Jstart. We need to tell Maxima the symbols qK and qL should be 
considered as functions of (K,L). Note lower case jacobian is the Maxima function.

depends ([qK,qL], [K,L]);
Jstart : jacobian ([F1, F2, F3], [lam, K, L]);

grind(Jstart)$

We next use subst to replace the noun forms like 'diff(qK,K) = 'diff(qK,K,1) with qKK, etc.
in order to define our final symbolic matrix J.

Let QKK stand for ∂²Q/∂K² and QLL stand for ∂²Q/∂L².

Let QKL stand for ∂(QK)/∂L = ∂²Q/∂L∂K  and likewise 
  let  QLK stand for ∂(QL)/∂K = ∂²Q/∂K∂L.

See Dowling, Sec. 5.3, p. 85 for Dowling's conventions for cross (mixed) partial derivative 
 notation. Note that some texts on mathematical economics use a different convention. 
Dowling's conventions, which we use, agree with the Wikipedia page:
  https://en.wikipedia.org/wiki/Partial_derivative#Higher_order_partial_derivatives

J : subst ([ 'diff(qK,K) = qKK, 'diff(qK,L) = qKL, 'diff (qL,K) = qLK, 'diff (qL,L) = qLL],Jstart);

Or you could avoid all the typing with subst, and just define J directly as a matrix using
the desired symbols, such as:

  J : matrix ( [0, r, w], [r, qKK, qKL], [w, qLK, qLL]);

The total derivative of F1, F2, and F3 with respect to any one of the exogenous (independent)
variables r, w, and B must be zero (since F1 = 0, etc) and we can determine the effects of 
an increase in any of these exogenous (r, w, B) variables on the optimum values of the three 
(endogenous) dependent variables (λ, K, L ). Here we look at the result of an increase in
 budget target B.

The total derivative of F1 = 0, F2 = 0, and F3 = 0 with respect to budget B, taking into 
account the dependence of the optimum values (λ, K, L ) on the value of B, implies three 
equations which can be solved for the three first derivatives: lamB = ∂lam/∂B,  KB = ∂K/∂B,
  and  LB =  ∂L/∂B.

dF1/dB =  ∂F1/∂λ ∂λ/∂B  +   ∂F1/∂K ∂K/∂B +  ∂F1/∂L ∂L/∂B    +  ∂F1/∂B = 0,
dF2/dB =  ∂F2/∂λ ∂λ/∂B  +   ∂F2/∂K ∂K/∂B +  ∂F2/∂L ∂L/∂B     + ∂F2/∂B = 0,
dF3/dB =  ∂F3/∂λ ∂λ/∂B  +   ∂F3/∂K ∂K/∂B +  ∂F3/∂L ∂L/∂B    +  ∂F3/∂B = 0,

which can be written in matrix form as J . X = Z, in which X is a matrix column vector
with unknown values, the sought-after first derivatives of (lam, K, L) wrt B.

X : cvec ([ lamB, KB, LB] );

Instead of using our cvec function (defined in Econ1.mac), you could do it the long way
using matrix and transpose, as in:

transpose( matrix ([lamB, KB, LB]));

grind(%)$

The partial derivatives ∂Fj/∂B in each equation will be transferred to the right hand
side, and form the elements of the matrix vector Z. We will show you two different but
equivalent ways to calcualte Z.

 Here are the partial derivatives of (F1,F2,F3) wrt budget B for our model:

[diff (F1,B), diff (F2, B), diff (F3, B)];

We transfer these to the right hand side of the equation, changing the sign of each.

- %;

Then turn the list into a matrix column vector:

Z : cvec (%);

grind(%)$

A quicker way to get this matrix column vector is to use the Maxima function jacobian
in the following form (note the crucial minus sign in front):

Z :   - jacobian ([F1, F2, F3], [B]);

The three equations to be solved for (lamB, KB, LB) are then equivalent to the matrix
equation:

J . X = Z;

We will show you two (equivalent) methods  to solve for the unknowns (lamB, KB, LB).

1. The first method is using Msolve (J, X, Z), defined in Econ1.mac. Msolve calls solve.

solns : Msolve (J, X, Z);

soln : solns[1];

We expect the denominators of these solutions to be simply related to |J|, the determinant of
the matrix J.

Let D = |J| = the determinant of the matrix J.

D : determinant (J), expand;

which agrees with Dowling's expression for |J| in Sec. 13.5, p. 290. The solutions returned
by Msolve above each have - |J| in the denominator. Later, we will used the symbol Dval 
to stand for D = |J|.

As Dowling reminds us "Since profit-maximizing firms in perfect competition operate only
in the area of decreasing marginal productivity of inputs", we can assume that both qKK 
and qLL are negative. So the first and fourth terms of D are positive. Also r and w are
positive by definition. So the sign of D depends on the sign of qKL, which should also
be the sign of qLK, since in almost all economics problems, we can assume we have
enough continuity to assume fxy = fyx, etc. (equality of second mixed partial derivatives).

We will see that the second order condition for a maximum is that |J| > 0, see below.

Then "the second order condition for a maximum will be fulfilled whenever K and L are 
complements ( qKL and qLK both positive ) and will depend on the relative strength of the 
direct and cross partials when K and L are substitutes ( qKL and qLK both negative)."

We have m = 1 constraints and n = 2 variables (K, L). 

Hence the number of leading principal minors to check for a sufficient condition for a relative
maximum is (n - m) = 1. 

Given the bordered Hessian matrix BH, we are to check LPM (BH, k) for k starting with
(2*m + 1) = 3, and ending with k = (m + n) = 3. 

For this problem, then, the only leading principal minor of BH we need to check is 
LPM (BH, 3) = determinant (BH). We want to show that, with our approach, the matrix J 
above is the same as the bordered Hessian matrix BH.

With our Lagrangian function Q(K,L) and one equality contraint g = B, we can use our
Bhessian function to return the bordered hessian matrix appropriate to this case.

Bhessian (Q, g, [K, L] );

The borders of this matrix contain ∂g/∂K = r, and ∂g/∂L = w, where g = r*K + w*L.
Define now a matrix BH in terms of the symbols qKK, qKL, qLK, qLL, in accordance with the
result from Bhessian. Note that Bhessian, like hessian, does not return results which
reveal any difference  between (a): diff ( diff (q, K), L) and (b): diff ( diff (q,L), K). 

Here is the problem (inherent in Maxima's design) in a nutshell:

[ diff (diff (q,K), L), diff (diff (q,L), K)];

However, because the Hessian part of Bhessian is properly defined as
jacobian ([qK, qL], [K, L] ), we know the correct interpretation:

BH : matrix ( [0, r, w], [r, qKK, qKL], [w, qLK, qLL] );

BH - J;

We see that J = BH, which is why we are setting up this problem the way we are.
Hence determinant (J) = determinant (BH), which we assume has the same sign
as (-1)^n = (-1)^2 = +1, so |J| > 0 to have sufficient conditions for a local maximum.

We can thus skip the extra step of calculating the bordered Hessian matrix, and just use
the matrix J to check the second order conditions (SOC).

2. The second method to get a solution is to use explicit matrix methods, using invert(J),
which is equivalent to the mathematical inverse matrix.

invert (J) . J, ratsimp, factor;

Let Xs be the solution matrix column vector.

Xs : invert (J) . Z, ratsimp, factor;

denom (Xs[1,1]);

D;

The denominator of each of the three elements of Xs above  is -|J| = -Dval, say.

for j thru 3 do Xs[j,1] : num(Xs[j,1]) / (-Dval)$

Xs;

lamB : Xs[1,1];

With Dval = |J|  > 0, qLL < 0, qKK < 0, and qKL and qLK having the same sign,
sign(lamB) = -[(+) - (+)]/(+) =  indeterminate without more information about the relative
strength of the second partials.

KB : Xs[2,1];

Assuming Dval = |J|  > 0, qLL < 0, qKL > 0 (K and L are complements), KB > 0,
but if K and L are substitutes (qKL < 0), the derivative KB is indeterminate. 

LB : Xs[3,1];

Assuming Dval = |J|  > 0 and qKK < 0,  and assuming K and L are complements and 
hence qLK > 0, we have LB > 0, but if K and L are substitutes (qLK < 0), LB is indeterminate .

The matrix solutions agree with our solutions above using solve.

Our solutions agree with Dowling, p 291, except that Dowling's λ is the negative of ours,
by definition, so 
    ∂λ/∂B = ∂(-λDowling) /∂B = - ∂(λDowling)/∂B, 
and that explains his minus sign compared with our result for the derivative λB.

### Problem 13.25: Maximize Utility u(a,b) with Linear Constraint 

A consumer wants to maximize utility u (a, b) subject to the constraint
   Pa*a + Pb*b = Y, a constant. Find the response of the optimum values of (a,b) 
     to an increase in the price Pa. Pa, Pb, and Y are positive parameters.

Let g = Pa*a + Pb*b. 

Given a Lagrangian function L(λ, a, b) , let
          F1 = ∂L/∂λ,   F2 = ∂L/∂a,    F3 = ∂L/∂b.
The three FOC for a maximum are the three equations
              F1 = 0, F2 = 0, F3 = 0

If we arbitrarily choose the Lagrangian function in the form:

      L    =  u(a,b) + λ( g(a,b) - Y) 
            =  u(a,b) + λ (Pa*a + Pb*b - Y)

we will obtain agreement between the Jacobian matrix

     J = ∂(F1, F2, F3)/∂(λ, a, b) = jacobian ([F1, F2, F3], [λ, a, b])

and the bordered Hessian matrix BH

      BH = Bhessian (L, g, [a,b])

Let lam stand for λ. 

The optimal values (λo, ao, bo) are the solutions of the three FOC, in which the three first 
derivatives of the Lagrangian function are set equal to zero.

killAB()$
g : Pa*a + Pb*b;
L : u + lam*(g - Y);
depends (u, [a,b]);
derivs : jacobian ([L],[lam, a, b])[1];

Let ua = ∂u/∂a,  ub = ∂u/∂b, and define F1, F2 and F3 symbolically to agree with these
first derivatives of L.

[F1, F2, F3] : [a*Pa + b*Pb - Y, lam*Pa + ua, lam*Pb + ub];

depends ([ua, ub], [a,b]);
Jstart : jacobian ([F1, F2, F3], [lam, a, b] );

Let uaa = ∂(ua)/∂a, uab = ∂(ua)/∂b, uba = ∂(ub)/∂a, ubb = ∂(ub)/∂b.

[See Dowling, Sec. 5.3, p. 85 for Dowling's conventions for cross (mixed) partial derivative 
 notation. Note that some texts on mathematical economics use a different notational 
 convention. Dowling's conventions, which we use, agree with the Wikipedia page
  https://en.wikipedia.org/wiki/Partial_derivative#Higher_order_partial_derivatives]

J : matrix ([0, Pa, Pb], [Pa, uaa, uab], [Pb, uba, ubb]);

B : - jacobian ([F1, F2, F3], [Pa]);

X : cvec ([lamPa, aPa, bPa]);

Solution using Msolve.

soln : Msolve (J, X, B)[1], ratsimp, factor;

D : determinant (J), expand;

We call this expression Dval and assume D = Dval = |J| > 0 for sufficient second order
conditions for maximization in this m = 1 constraint and n = 2  variable problem, as in 
Example 6.

We can express our solutions in terms of Dval > 0, with some typical Maxima tricks.

Xnames : map ('lhs,soln);

solns : map ('rhs, soln);

solns : map (lambda ([x], num(x)/(-Dval)), solns);

map ("=", Xnames, solns);

The "theory of the consumer" does not include information about the signs and
relative strengths of the second order partials of the utility function u(a,b) to be able to
assign definite signs to these three first derivatives lamPa = ∂lam/∂Pa,  aPa = ∂a/∂Pa,  
and  bPa =  ∂b/∂Pa.

If you compare our result for aPa = ∂a/∂(Pa) with Dowling's result on his page 312, 
the term in our numerator proportional to lam has the opposite sign compared with
his sign. This is because Dowling chooses the form L = u + λ ( Y - g), so the sign of  
Dowling's λ is the opposite of the sign of our λ.

### Problem 13.27: Continuation of Prob. 13.25, ∂a/∂Y sign

A consumer wants to maximize utility u (a, b) subject to the constraint
   Pa*a + Pb*b = Y. Find the response of the optimum values of (a,b) 
     to an increase in the parameter Y.

This is the same model and notation as Prob. 13.25, so we can continue to use the same
matrix J, but must define a different matrix column vector B to correspond to taking the
partial derivative of (F1, F2, F3) with respect to Y, and we need to define a new matrix 
column vector X containing different names.

B : - jacobian ([F1, F2, F3], [Y]);

Let lamY stand for ∂λ/∂Y, aY stand for ∂a/∂Y, bY stand for ∂b/∂Y

X : cvec ([lamY, aY, bY]);

soln : Msolve (J, X, B)[1], ratsimp, factor;

D;

Xnames : map ('lhs,soln);

solns : map ('rhs, soln);

solns : map (lambda ([x], num(x)/(-Dval)), solns);

map ("=", Xnames, solns);

Dowling remarks (p. 313): aY and bY cannot be signed from mathematics, but can be 
signed from the economics. "If a in a 'normal good', aY = ∂a/∂Y > 0; if a is a 'weakly inferior
good' then ∂a/∂Y = 0; and if a is a 'strictly inferior good' then ∂a/∂Y < 0.

Likewise for the sign of bY =  ∂b/∂Y.

### Problem 13.28:  Slutsky Eqn. for ∂a/∂Pa

DIGRESSION ON MARGINAL UTILITY

Marginal utility is the extra benefit derived from consuming one more unit of a specific good 
or service. The main types of marginal utility include positive marginal utility, 
zero marginal utility, and negative marginal utility. Consumers often experience higher 
marginal utility when marginal cost is lower.

In the context of cardinal utility, economists sometimes speak of a law of diminishing 
marginal utility, meaning that the first unit of consumption of a good or service yields 
more utility than the second and subsequent units, with a continuing reduction for greater 
amounts. Therefore, the fall in marginal utility as consumption increases is known as 
diminishing marginal utility. This concept is used by economists to determine how much of 
a good a consumer is willing to purchase

Negative marginal utility is where you have too much of an item, so consuming more is 
actually harmful. For instance, the fourth slice of cake might even make you sick after 
eating three pieces of cake.

Zero Marginal Utility

When you put your money into a machine to purchase a newspaper, the door opens, and 
you could presumably take more than one newspaper. However, there is typically little to no 
satisfaction in having more than one edition of the same newspaper.

For some reason you receive two of the same magazine in the mail instead of the usual one.
Though you greatly enjoy reading the first copy of the magazine, there is no satisfaction 
found in reading a second copy.

If you have several coupons for the same item but only plan to purchase one of that item,
 there is zero marginal utility in having those extra coupons.

A family of five purchases tickets to an amusement park, and is told there is a "buy five, 
get the sixth one free" sale. However, there is no additional happiness from that sixth ticket 
because they only need five tickets. If, however, they had a friend or relative they wanted 
to take with them, the sixth ticket would have positive marginal utility.

A person may win two airline tickets, but if he or she does not have someone to travel with 
to that particular destination on those particular dates, there is no additional satisfaction to 
having that second ticket.

In Prob. 13.25 we have the result:
  aPa = (ubb*a*Pa - uab*a*Pb + λ*Pb^2)/Dval
         = (λ*Pb^2)/Dval + a*(ubb*Pa - uab*Pb)/Dval.

In Prob 13.27 we have:
  aY = - (ubb*Pa - uab*Pb)/Dval.

Hence aPa = (λ*Pb^2)/Dval - a*aY, or 

∂a/∂Pa = (λ*Pb^2)/Dval - a*∂a/∂Y.

This is the Slutsky equation for the effect of a change in Pa on the optimal quantity of the
good a demanded. The first term on the right is the "substitution effect" and the second
term is the "income effect".

From the equation F2 = 0 in Prob. 13.25, we have the result:
   ua + lam*Pa = 0, or λ = - ua/Pa.
Assuming ua = ∂u/∂a = (marginal utility of good a) > 0, then λ < 0. 

Assuming Dval = |J| > 0 for constrained maximization and λ < 0, the substitution effect in the
first term is negative. The income effect in the second term will depend on the nature
of the good. For a normal good, ∂a/∂Y > 0, the income effect term is negative and
∂a/∂Pa < 0. 

For a weakly inferior good, ∂a/∂Y = 0, and again ∂a/∂Pa < 0. 

For a strictly inferior good ∂a/∂Y < 0, and the sign of ∂a/∂Pa will depend of the relative
magnitude of the two terms. If the income effect overwhelms the substitution effect, as in
the case of a "Giffen good", ∂a/∂Pa > 0 and the demand curve will be positively sloped.