This workbook contains some of the wxMaxima commands that appear in 
Chapter 2.
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)$ fpprintprec:5$ ratprint:false$
load(draw)$ set_draw_defaults( draw_realpart=false)$
kill(all)$

# Chapter 2. Simple Economic Models

This chapter illustrates two models using Maxima, the simple
production possibilities model and the market model based on
demand and supply. 

## Competitive Markets

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

Competitive markets can be analyzed with the familiar demand and 
supply model. This section looks at equilibrium, disequilibrium,
efficiency, a number of elasticity measures, and the relationship
between the price elasticity of demand and total spending by the
buyers of a good or service.

### Demand Curves and Supply Curves

The market demand curve DQ = A*p^E states the quantity demanded
at each of a range of prices. The inverse demand curve 
DP = (q/a)^{1/E}, states the height of the supply
curve, or the price that buyers are willing and able to pay, at each of a range
of quantities. The supply curve can be represented in either of the two 
ways listed below, as well.

[DQ(p,a,E):=a*p^E,    DP(q,a,E):=(q/a)^(1/E)];
[SQ(p,b,c):=b+c*p,    SP(q,b,c):=(q-b)/c];

Once the parameters are specified a particular demand and 
supply pair are identified.

[DP(q, 1200, -2.5), DP(40, 1200, -2.5)];

### Equilibrium

Solving the inverse curves yields a quantity.  To confirm that this
is the equilibrium quantity place the quantity in both the inverse
demand curve expression and the expression for the inverse 
supply curve.

[a0, E0, b0, c0] : [1200, -2.5, -5, 10]$  
[qE0:find_root(DP(q,a0,E0)-SP(q,b0,c0),q,.001,500),
["Prices:",DP(qE0, 1200, - 2.5),  SP(qE0, -5, 10)]];

Try applying solve( ) to this pair of expressions.

solve(DP(q,a0,E0)-SP(q,b0,c0),q);

Solving the system for quantity is arbitrary. We can determine the price 
at which the demand price equals the supply price.  

[pE0:find_root(DQ(q,a0,E0)-SQ(q,b0,c0),q,0.001,10),
["Quantities", DQ(pE0,1200,-2.5), SQ(pE0,-5,10)]  ];

The next cell creates a table of eight pairs of quantities,
one for each price. The prices are defined in increments of
1/5 of the equilibrium price; the fifth line contains 
equilibrium values.
All $ have been replaced with ;, so the steps toward the table
are apparent.
On the first input line, notice the use application of the 
quote-quote operator on the expressions inside parentheses.

[DQ0(p) := ''(DQ(p,a0,E0)), SQ0(p):=''(SQ(p,b0,c0))];
/*Create lists*/ 
  pList0:makelist(pE0*i/5,i,1,8);  
  qdList0:maplist(DQ0,pList0);
  qsList0 : maplist(SQ0, pList0);
/*Append names */  
  pList0:append(["Price"],pList0);
  qdList0:append( ["DQ"], qdList0);   
  qsList0:append(["SQ"],qsList0);
  edList0:qdList0-qsList0$ /*No append( ) needed here */
/*Create table */
  transpose(matrix(pList0,qdList0,qsList0, edList0));

Finally we draw the supply and demand curve and add the line that 
is defined above. 
One way to draw a line that shows the equilibrium price,quantity
pair is to use if...then...else, as below. Another is to use
points( ). We use the latter at various points in the book.
The choice is often a matter of preference.

eqline : if q < qE0 then pE0 else 0$  
wxdraw2d(yrange=[0, 2.0*pE0],ylabel="$ per unit", 
 xlabel="Quantity: units per time period",
 line_type=dots, explicit(eqline,q,0,1.05*qE0),
 line_width=2,line_type=solid, key="Demand",
 explicit(DP(q,a0,E0),q,0,1.5*qE0),color=blue,
 key ="Supply", explicit(SP(q,b0,c0),q,0,1.5*qE0) )$

### Disequilibrium

For q > qE0 the supply curve lies above the 
demand curve. That is, confirm that the price that producers would have to
be paid in order to produce this quantity exceeds the price that buyers are
willing and able to pay for that quantity. 
\index{competitive markets!disequilibrium}

1.1*qE0; 
[DP(1.1*qE0, 1200, -3), SP(1.1*qE0, 20, 5)];

TeX: Confirm that for $q < qE0$, the demand curve lies above the 
supply curve.

0.9*qE0; 
[DP(0.9*qE0, 1200, -3), SP(0.9*qE0, 20, 5)];

### Price Ceilings

Markets tend to drive the price toward the equilibrium price, but
ceilings and floors can override market forces, at least for a 
while.
The next cell uses multiple to define a nonequilibrium price. 
If multiple=1, then the price is the equilibrium value. Here
mulltiple < 1, so the established price is below the equilibrium
value. 
the maxq variable is used for graphing below.

multiple : 0.8$                  [pNE : multiple*pE0,
[qNED, qNES] :[ DQ(pNE, a0, E0), SQ(pNE, b0, c0) ] ];
maxq : max(qNED, qNES)$

The graph shows the result of this price ceiling. The xlabel
is verbose. It is intended to emphasize that the shortage is
a quantity per period. If stockpiles exist, they will be  
depleted at this rate.
The maxq value defined above is used to specify the range
over which the demand and supply curves are drawn.

wxdraw2d(yrange=[0,2.5*pE0],xrange=[0,1.25*maxq],
 xlabel ="Quantity in units per time period", 
 ylabel="$ per unit",line_width=2,key="Demand",
 explicit (DP(q,a0,E0), q,.01,1.25*maxq),
 key="Supply",color=blue,
 explicit(SP(q, b0, c0), q, 0, 1.25*maxq),
 point_size = 2, points_joined= true,
 color=gray30,line_type=dots,key="Excess Demand",  
 points([qNED, qNES], [pNE, pNE])  )$

Some prefer that the graph be drawn without the top
and right axes. The next input group makes two modifications.
It removes the axes, and it moves the key to the bottom.

wxdraw2d(axis_top=false,axis_right=false,
 user_preamble="set key bottom",
 yrange=[0,2.5*pE0],xrange=[0,1.25*maxq],
 xlabel ="Quantity in units per time period", 
 ylabel="$ per unit",line_width=2,key="Demand",
 explicit (DP(q,a0,E0), q,.01,1.25*maxq),
 key="Supply",color=blue,
 explicit(SP(q, b0, c0), q, 0, 1.25*maxq),
 point_size = 2, points_joined= true,
 color=gray30,line_type=dots,key="Excess Demand",  
 points([qNED, qNES], [pNE, pNE])  )$

tics;

Suppose that black market sellers can buy the
27.612 units and auction them to the highest bidders. 
Then the solution is simple: the black market price is $4.521 
per unit.

pBM : DP(qNES, a0, E0);

### Shifts

The initial and new set of parameters appear below.

[a0, E0, b0, c0] : [1200, -2.5, -5, 10]$ 
[a1, E1, b1, c1] : [1200, -1.5, -5, 15]$

Demand Shift
Shift the demand curve. Either a or E could be changed. 
We leave a unchanged and change E from E = -2.5 to E = -1.5. 
That is, the demand curve becomes less responsive to price 
(elasticity moves toward zero). 

qE1:find_root(DP(q,a0,E1) - SP(q,b0,c0),q,.001,500 )$
pE1 : DP(qE1,a0,E1)$
matrix(["qE0","qE1","pE0","pE1"],[qE0,qE1,pE0,pE1]);

TeX: The graph shows the shift in the demand curve and 
its effects.

maxq : max(qE0, qE1)$ maxp : max(pE0, pE1)$
wxdraw2d( user_preamble="set key bottom center",yrange= 
 [0, 1.5*maxp],xlabel="Quantity: units per time period",
 ylabel="$ per unit", key="Initial Demand",explicit(
 DP(q,a0,E0),q,0, 1.5*maxq),line_width=2,key=
"New Demand",explicit(DP(q,a0,E1),q,0,1.5*maxq), 
   color=gray,key="Supply", 
 explicit(SP(q,b0,c0),q,0,1.5*maxq) )$

The initial and new set of parameters appear below.

In order to animate the demand, supply model, we return to the
earlier graph, the one that drew a drop. We use the if...then...else
formulation again but change the criterion. We embed the find_root( )
command within the wxdraw2d( ) command, twice.

Supply Shift
The input cell below shows the effect of an increased supply. 
The table compares the initial quantity and price with the quantity 
and price after the shift in supply. 

qE2:find_root(DP(q,a0,E0) - SP(q,b0,c1),q,.001,500 )$
pE2 : DP(qE2,a0,E0)$
matrix(["qE0","qE2","pE0","pE2"],[qE0,qE2,pE0,pE2]);

As an exercise, replicate the graph above, with a supply increase
replacing a demand increase. Also, graphically
analyze the effects of simultaneous demand and supply shifts. 

Animation
The graph below replicates the first graph above and animates
the shifting of the demand curve. At the same time that the 
curve shifts, the drop line shows the effect on the equilibrium
quantity and price.

Before drawing the graph, create some lists. The first, aList,
is a list of 8 value for 8, the 4th of which is 1200. The 
next line defines an expression for the equilbrium 
quantity and creates a function named rootQ( ), where the 
demand price equals the supply price. The third line creates
a list of equilibrium quantities, qListEQ. The fourth line
uses these quantities to create a price list, pListEQ. The 
fifth line checks to ensure that the prices in pListEQ would
be the same if the supply curve, rather than the demand curve,
had generated the list.

aList: makelist(.2*i*a0,i,2,9);
 rootQ(a):=find_root(DP(q,a,E0)-SP(q,b0,c0)  ,q,1,100);
qListEQ:map(rootQ,aList);
/* Prices from demand curve*/  pListEQ:DP(qListEQ,aList,E0);
/* Check, prices from supply curve */SP(qListEQ,b0,c0);

Now we use with_slider_draw( ). It begins, as always with
a shift parameter, named k here, and a rule for changing k's 
value. In this case k begins at 1 and ends at 8, with increments
of 1. This generates a counter that corresponds to the 8 values
of aList, qListEQ, and pListEQ. These counters occur in the 
second explicit( ) command and in the first points( ) command.
The input below is longer than necessary. Part of its purpose
is to show the versatility of draw( ).

with_slider_draw(k, makelist(k,k,1,8),  xrange=[0,50],
 yrange=[0,10],xlabel="Quantity",ylabel="$ per unit",
 key="Initial Demand, a = 1200: ",
 explicit(DP(q,aList[4],E0),q,0,50), color=blue, 
 line_width=2,
 key=concat("Demand Curve when a =", string(aList[k]) ),
 explicit(DP(q,aList[k],E0),q,0,50), 
 color=red, key="Supply", explicit(SP(q,b0,c0),q,0,50),
 line_type=dots, color=blue,
 points_joined=true,point_size=2,key="New equilibrium",
 points([ [0,pListEQ[k]],[qListEQ[k],pListEQ[k]],
  [qListEQ[k],0]]),color=gray10, key="Initial Equilibrium",
 points([ [0,pListEQ[4]],[qListEQ[4],pListEQ[4]],
  [qListEQ[4],0]]),dimensions=[640,480]   )$