This workbook contains some of the wxMaxima commands that appear in 
Chapter 3.

The cell below is optional. The purpose of each of the commands
is indicated in the final section of Chapter 1 in the text.

load(draw)$ 
set_draw_defaults(  fill_color=gray40,color=gray10, 
draw_realpart=false,font_size=10)$
fpprintprec:5$  ratprint:false$ kill(all)$

# Demand Theory: Preferences

This chapter  assumes the existence of an established utility 
function that expresses the consumer's preferences.

## A Utility Function

We explore the Constant Elasticity of Substitution or CES 
function. Using a function like this one embeds 
the assumptions that the consumer can rank any two pairs 
of  x,y values, and that the consumer's preferences are convex

u(x,y,a,b):=(a*x^-b+(1-a)*y^-b)^-(1/b);

### Graphical Representation

The graph below shows a three dimensional representation of
this function for a set of parameters, a0 = 1/4 and  b0 = 4/5).  

The implied elasticity of substitution is s = 5/9.

Suggestion: Copy the wxdraw2d( ) command and place it in
another cell. Remove the wx prefix and execute it. The 
result will be a graph in a separate window. Use the cursor
to rotate that graph to find the view that you consider 
most informative. Then reset view=[ ] in the cell below and
execute it again.

a0:1/4$   b0:4/5$ [xmax, ymax]:[50,50]$   
umax : u(xmax, ymax, a0, b0)$    
wxdraw3d(view = [50,330],xlabel="x",
 ylabel="y",zlabel = "u",
 ztics={0,umax},xtics={0,xmax},ytics={0,ymax},
  explicit(u(x,y,a0,b0),x,0.01,xmax,y,0.01,ymax ))$

Each of the lines that form the grid below shows the utility level 
associated with various levels of one of the goods, given that 
the level of the other good is held constant. 

The two graphs below slice the graph above along the two
axes. The cell below contains commands that create two 
scenarios, UX and UY, and graph them. Thus commands that
appear in three places in the text are gathered into a 
single cell.

UX: gr2d( user_preamble = "set key top left",
 xlabel="Good x", ylabel = "utility",xtics=20, ytics=10,
 key= "y=25",explicit(u(x,25,a0,b0),x,0.001,xmax),
 line_width=2,key="y = 15",
 explicit(u(x,15,a0,b0),x,0.001,xmax ),
 color=red, key="y =10", 
 explicit(u(x,10,a0,b0),x,0.001,xmax ),
 color=black,key = "y = 5",
 explicit(u(x, 5, a0, b0),x,0.001,xmax ) )$
UY: gr2d( user_preamble = "set key top left",
 xlabel="Good y",ylabel="utility",xtics=20,ytics=10,
 key="x = 25",explicit(u(25,y,a0,b0),y,0.001,ymax), 
 line_width =2, key="x = 15", 
 explicit(u(15,y,a0,b0),y,0.001,ymax), color=red,
 key="x =10",explicit(u(10,y,a0,b0),y,0.001,xmax ),
 color = black, key = "x = 5", 
explicit(u(5,y, a0, b0), y, 0.001, ymax))$
wxdraw(UX,UY, columns = 2,dimensions=[640,480])$

TeX:
The \verb!UY! is essentially identical to \verb!UX!, so only the first and last 
lines of the input appear below.

UY: gr2d( user_preamble = "set key top left",xlabel
    /* delete the material  between these to comments 
        in tex editor*/ 
="Good y",ylabel="utility",xtics=20,ytics=10,key=
"x = 25",explicit(u(25,y,a0,b0),y,0.001,ymax), line_width 
 =2, key="x = 15", explicit(u(15,y,a0,b0),y,0.001,ymax), 
 line_width=1,key="x =10",explicit(u(10,y,a0,b0),y,0.001,
 xmax ),line_width=2, color = gray40, key = "x = 5", 
    /*     */
explicit(u(5,y, a0, b0), y, 0.001, ymax))$

### Marginal Utility

The expressions for the marginal utility of x and of y 
are the first partial derivatives of the utility 
functions with respect to these variables.

matrix( ["mux","muy"],
   [diff(u(x,y,a,b),x),diff(u(x,y,a,b),y)]);

For analysis, we cast these expressions as functional
relationships. Each has two variables (x and y) and two
paramters (a and b) as arguments. The text shows that the
functions below are derived from the expressions above.

mux(x,y,a,b):=a*((u(x,y,a,b))/x)^(1+b);
muy(x,y,a,b):=(1-a)*(u(x,y,a,b)/y)^(1+b);

The next cell shows the marginal utility of x 
expression, given the parameters used above and
a specified value of y. It also shows the value
of mux for a specified value of x.

matrix( ["mux expression", "mux value"], 
  [mux(x,15,a0,b0), float(mux(10,15,a0,b0))]  );

The graph below shows two $mux$ curves. 6

wxdraw2d(yrange=[0,2],xlabel="x",ylabel="mux",
 key="mux when y = 5",  line_width=2, 
 explicit(mux(x,5,a0,b0),x,0.001,50),
 color=red,key="mux when y = 25",
 explicit(mux(x,25,a0,b0),x,0.001,50)  )$

The Cobb Douglas function u = x^a * y^(1 - a) is a 
limiting case of the CES function, as b approachs 0 (or
equivalently, a s approaches 1). 

matrix([limit(u(x,y,a,b),b,0), 
radcan(limit(u(x,y,a,b),b,0))]);

## Indifference Curves

In our two-good model, an indifference curve consists of all 
combinations of the two goods that result in the same level of 
utility for the consumer. 

### Graphical Representation

The graphs below show the same utility function 
representation as above, but focus on contour lines) that 
collect points of equal height on the utility surface.
The second graph shows the results of projecting these 
contour lines onto the base of the graph. Thus, the base in 
this graph contains a two-dimensional ($x$ and $y$)
representation of the three-dimensional utility surface.

G1:gr3d(color=green,view=[50,300], contour=surface,
 title="Contour Lines on Surface", 
 contour_levels={.4*umax,.6*umax,.8*umax}, xlabel="x", 
 ylabel="y",zlabel="u",ztics={0,umax},xtics={0,xmax},
 ytics={0,ymax},
 explicit(u(x,y,a0,b0),x,.01,xmax,y,.01,ymax))$
G2:gr3d(color=light-green,view=[50,300], contour=base,
 title="Contour Lines on Base",
 contour_levels={.4*umax,.6*umax,.8*umax}, xlabel="x", 
 ylabel="y",zlabel="u",
 ztics={0,umax},xtics={0,xmax},ytics={0,ymax},
 explicit(u(x,y,a0,b0),x,0.001,xmax,y,0.001,ymax))$
wxdraw(G1,G2,columns=2,dimensions=[640,400])$

An alternative, not shown in the text, is to map the 
contour lines onto the base and not show the surface.

We removed some of the options that relate to the z 
(utility) axis. Doing so is not required; draw ignores 
them.

wxdraw3d( contour=map,
 title="Contour Lines Mapped onto Base",
 contour_levels={.4*umax,.6*umax,.8*umax}, 
 xlabel="x", ylabel="y",ytics={0,ymax},
 explicit(u(x,y,a0,b0),x,0.001,xmax,y,0.001,ymax))$

### Defining Indifference Curves

A functional expression for indifference curves, generated 
using solve( ), is useful. The assume command reduces the
number of answers that Maxima requires to two. The answers 
to the remaining questions are no; and neg; (n; and n; will
work).

assume(u0>0,a>0,a<1)$  declare(b,noninteger)$
soln: ratsimp(solve(u(x,y,a,b)=u0,y));

The next cell simplifies the right-hand side of the expression
above and assigns the result to the function ic(x, a, b, u0).

ic(x, a, b, u0):= ''(rhs(soln[1]));

The ic( ) function yields an economically sensible 
value ($y > 0$) only when the denominator is positive. 
An xmin function determines the implied minimum x value.

xmin(a,b,u):=a^(1/b)*u; [u1,u2,u3]:[20,30,40]$
[xmin1,xmin2,xmin3]:float([xmin(a0,b0,u1),  
xmin(a0, b0, u2), xmin(a0, b0, u3)])$
matrix(["utility level",u1,u2,u3],
   ["minimum x value", xmin1,xmin2,xmin3]);

TeX: The graph below shows three utility curves for a consumer. 
A variable ymax_ic truncates the indifference curves.
The sharp curvature reflects the selection of a value of b 
that implies small s (s = 3/4$.

Other than changes in color, this command set differs from
the one in  the text by the use of concat("text",string( )
to show the values of u for the three indifference curvs.

ymax_ic:float(ic(1.1*xmin3,a0,b0,u3))$
wxdraw2d(title = "Three Indifference Curves",xlabel="x",
 ylabel="y",xrange=[0,2*xmin3],yrange =[0, ymax_ic],
 key=concat("u = ",string(u1)),
explicit(ic(x,a0,b0,u1),x,xmin1+0.1,2*xmin3), 
 line_width=2,key=concat("u = ",string(u2)),
 explicit(ic(x,a0,b0,u2),x,xmin1+0.1,2*xmin3),
 color=red, key=concat("u = ",string(u3)),
 explicit(ic(x, a0, b0, u3), x, xmin1+0.1, 2*xmin3)  )$

### Marginal Rates of Substitution

The table below illustrates the diminishing marginal
rate of substitution that the changing slope of the 
curves above reflect.

The value of x is increased in increments of 1. The
resulting changes in y required to keep u constant 
appear in the third column.

Strictly, the y change values should appear between
the x values, reflecting the fact that we could have
begun at x = 11.5355 and worked up. Maxima's matrix
command does not, however, provide an easy way to 
build such a table.

xlist1:makelist(xmin1+i,i,1,7)$  
ylist1:float(ic(xlist1,a0,b0,u1))$
xlist2:makelist(xmin1+i, i, 2, 8)$
ylist2:float(ic(xlist2, a0, b0, u1) )$ 
ychangelist:ylist2-ylist1$   ychangelist:
  append(["y change", "---"],ychangelist)$
xlist:cons(xmin1+1,xlist2)$
ylist:cons(ylist1[1],ylist2)$  transpose(
 matrix(cons("x",xlist),cons("y",ylist),ychangelist));

The next cell shows the slopes at points on an indifference
curve. 

We chose to use cons( ) to construct new lists that include
titles. At other places, we add a vector of titles to the
matrix( ) command. 

icslope(x,a,b,u):=''(diff(ic(x,a,b,u),x))$
slopelist: -float(icslope(xlist,a0,b0,u1))$  
transpose(
  matrix(cons("x",xlist),cons("mrs",slopelist)));

We chose to use cons( ) to construct new lists that include
titles. At other places, we add a vector of titles to the
matrix( ) command. One way to add titles is with addrow( ).

transpose(matrix(xlist,slopelist))$
addrow(matrix(["x","mrs"]),%);

The table below shows the result of computing the mrs
values as the ratio, -dy/dx = mux/muy

muxlist: mux(xlist, ylist, a0, b0)$
muylist : muy(xlist, ylist, a0, b0)$
mrslist : muxlist/muylist$
names: matrix(["x", "y","mux","muy","mrs"])$
numbers: matrix(xlist,ylist,muxlist,muylist,mrslist)$
addrow(names, numbers);

The cell below confirms that that the elasticity of 
substitution is s = 1/(1 + b). 

kill(s)$ /*Just in case s has been defined earlier
           in the session. */
mrs:diff(ic(x,a,b,u),x)$ 
dyx:diff(ic(x,a,b,u)/x,x)$   
dmrs:diff(mrs,x)$  
/*s=(d(y/x)/d(mrs))*(mrs/(y/x))$ */
s: ratsimp((dyx/dmrs)*(mrs/(ic(x,a,b,u)/x)))$
matrix(["s =", s]);

### Limiting Cases

The graph below shows three isoquants. Each reflects 
a given value of s. The nearly L-shaped isoquant 
represents a consumer for which x and y are consumed
in nearly fixed proportions (extreme complements); s
 is nearly zero).
The nearly linear isoquant reflects the opposite case,
where x and y are almost perfect substitutes. In between
is an approximation to the Cobb-Douglas case.

b4: 99$  xmin4: float(xmin(a0, b4, u1))$
ymax_ic : float(ic(1, a0, -0.99, u1))$
wxdraw2d(xlabel="x",ylabel="y",   
title="Indifference Curves for Three Consumers",
 xrange=[0,60],yrange =[0,1.1*ymax_ic], 
key="s near 1",explicit(ic(x,a0,.01,u1),x,0.01,60),
line_width=2,key="Small s",
explicit(ic(x,a0,b4,u1),x,1.000000001*xmin4,60),
color=red, key="Large s",
explicit(ic(x,a0,-0.99,u1),x,0.01,60))$

load(draw)$ 
set_draw_defaults(  fill_color=gray40,color=gray10, 
draw_realpart=false,font_size=10)$
fpprintprec:5$  ratprint:false$

We can animate an indifference curve. Let b = -0.99 +i/4,
where i ranges from 0 through 10. Then b ranges from -0.99
to 9.01, and s ranges from 100 (nearly linear indifference
curve) to 0.398 ((considerable curvature). 

with_slider_draw(bvalues, makelist(-.99+i/4,i,0,10),
 yrange=[0,50], draw_realpart=false,
 xlabel="x",ylabel="y",color=red,line_width=2,
key=concat("s = ",string(1/(1+bvalues))),
explicit(ic(x,a0,bvalues,u1),x,0.01,60) )$

For this graph, draw_realpart=false is important. The
graph below shows the result of letting draw( ) show 
part of the imaginary solution. Values of x below the
critical values that we determined earlier are graphed,
unless precautions are taken to avoid this result.
The x range is shortened to highlight the anamoly.

with_slider_draw(bvalues, makelist(-.99+i/4,i,0,10),
 yrange=[-250,250], draw_realpart=true,
 xlabel="x",ylabel="y",color=red,line_width=2,
key=concat("s = ",string(1/(1+bvalues))),
explicit(ic(x,a0,bvalues,u1),x,0.01,10) )$

The preceding cells show that for small values of s,
the quantity required for a utility level to be defined
is a value that we previously defined as xmin. The next
cell states the xmin(a,b,u) expression and shows that
when b becomes very large (s approaches zero), xmin
approaches the specified number of utility units. 

xmin(a,b,u);
limit(xmin(a, b, u), b, inf);

The cell below lets s = 1/150 and shows that even a 
quite large change in the value of y does not compensate
for a very small change in x, and vice versa.

u1: u(20, 20, a0, 149)$
u2: float(u(19.5, 20000, a0, 249))$
u3: float(u(20000, 19.5, a0, 249))$
matrix(["u,corner", "u, much y", "u, much x"], 
  [u1, u2, u3]);