Comparative Statics Using MathematicaComparative Statics Using Mathematica

by 
Hal R. Varian
August, 1991

This Notebook describes how to use Mathematica to do comparative statics calculations.  See  the chapter on "Optimization" in Microeconomic Analysis, W. W. Norton and Company, 1992.  This should be thought of as a
"pre-beta" version.  Suggestions for the final version are welcome and should be sent to Hal.Varian@umich.edu.

A Simple example

Let's start with a simple example.  We have an equation of the form
                            g[x,a]=0.
Here we think of x as being some endogenous variable determined by the relationship g[x,a]=0, and a as some exogenous parameter in the system. We want to determine how x varies as the parameter a varies.  To do this, we first take a total derivative of the system.

Dt[g[x,a]]

In standard mathematical notation, this would be written as
                      dg(x,a)                dg(x,a)
                       --------- da   +      ---------dx
                            da                       dx
The differential elements dx and da are denoted by Dt[x] and Dt[a] in Mathematica.
                                
Next we set this  total differential equal to zero and solve  the  resulting  equation for  Dt[x]:

Solve[Dt[g[x,a]]==0,Dt[x]]

This  gives us an expression for dx. If we want to solve for  the derivative dx/da, we simply divide both sides by  da (= Dt[a]).   However, it is usually more helpful to leave Dt[a] on the right hand side.  

Note that this the solution is a list  that has one element in it.  We can strip off one layer of listing to get a cleaner form for the final solution.

%[[1]]

Optimization

It is common in economic applications to apply comparative statics methods to optimization problems.   In this case, the first-order condition is the first derivative of the objective function.  For example, suppose that we have a problem
               max  f[x,a]
                    x
This gives us a first-order condition of the form
                                  df[x,a]   
                                   -------  = 0
                                      dx
 We want to differentiate this expression with respect to a to determine how  x varies with a.  This is easily done:

Solve[Dt[D[f[x,a],x]]==0,Dt[x]][[1]]

This gives us the standard formula for Dt[x]: the ratio of the
mixed partial and the second derivative with respect to the choice variable.

Two equations

Let's try the same calculation with two equations.  We have  now have two variable optimization problem

      max       f[x1,x2,a]
             x1,x2

which leads to FOC
          
           df[x1,x2,a]
           -----------   = 0
              dx1
              
           df[x1,x2,a]
           -----------   = 0
              dx2

We totally differentiate this system and solve for {dx1,dx2}:

Solve[{Dt[D[f[x1,x2,a],x1]]==0,
       Dt[D[f[x1,x2,a],x2]]==0},
         {Dt[x1],Dt[x2]}][[1]]

This is kind of messy.  In the standard treatment  of comparative statics problems we use  Cramer's rule to solve for (dx1, dx2).  In this case we know that the denominator of the expression  for dx1, say, is the determinant of the Hessian.  Since the Hessian is a positive definite matrix due to the second-order conditions, we know its sign automatically.

However,  if we look more closely at the expression immediately above, we see that Mathematica automatically puts this expression in the "right" format: the determinant of the mixed partials over the determinant of the Hessian.  All we need to do is to substitute out for the Hessian.

Simplifying the expression

As we just saw, the above expression is messier than it has to be.  We know that the denominator is negative because of the second-order conditions.   We will calculate the determinant of the Hessian and substitute it out.

We compute the Hessian matrix using the "Table" command:

Hessian = Table[D[f[x[1],x[2],a],x[i],x[j]],{i,2},{j,2}]

Now compute the determinant of this matrix:

Det[%]

Finally, change back to the (x1,x2) notation

%/.{x[1]->x1,x[2]->x2}

This is the determinant of the bordered Hession; call it DH.   We recalculate the comparative statics expression given above, but now substitue the result of the last calculation.

Solve[{Dt[D[f[x1,x2,a],x1]]==0,
       Dt[D[f[x1,x2,a],x2]]==0},
         {Dt[x1],Dt[x2]}][[1]]/.%->DH

This gives the final form of the result

An Economic Example

Let's try an economic example.  Let h[x1,x2] be a production function 
and consider the profit maximization problem, 
        
         max  h[x1,x2] - w1 x1 - w2 x2

Solve[{Dt[D[h[x1,x2]-w1 x1 - w2 x2,x1]]==0,
       Dt[D[h[x1,x2]-w1 x1 - w2 x2,x2]]==0},
       {Dt[x1],Dt[x2]}][[1]]

Again, it makes sense to compute the Hessian and substitute it out.  However, we leave this an an exercise for the reader.

Constrained Maximization

Now let's try a constrained maximization problem.  This is a cost minimization problem:
          max w1 x1 + w2 x2
                 f[x1,x2] = y

Define the Lagrangian

L[x1,x2,t] = w1 x1 + w2 x2 - t(f[x1,x2] - y)

The  Hessian is

Hessian={{D[L[x1,x2,t],t,t], D[L[x1,x2,t],t,x1], D[L[x1,x2,t],t,x2]},
         {D[L[x1,x2,t],x1,t],D[L[x1,x2,t],x1,x1],D[L[x1,x2,t],x1,x2]},
          {D[L[x1,x2,t],x2,t],D[L[x1,x2,t],x2,x1],D[L[x1,x2,t],x2,x2]}}

Det[%]

Solve[{Dt[D[L[x1,x2,t],x1]]==0,
       Dt[D[L[x1,x2,t],x2]]==0,
       Dt[D[L[x1,x2,t],t]] ==0},
       {Dt[x1],Dt[x2],Dt[t]}]/.(%)->DHH

Note that the denominators in the expressions for dx1 and dx2 are negative due to the second-order conditions.  The numerators in these expressions are positive.  It follows that dx1/dw1 and dx2/dw2 are negative.

Note that there is more information in these expressions than in the standard comparative statics results.  Mathematica has made comparative statics calculations (almost) painless!