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,
set_draw_defaults(  fill_color=gray40,color=gray10, 
draw_realpart=false,font_size=10)$
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. 

## Surpluses

### Surpluses

The demand/supply model illustrates the gain from exchange that accrues 
to both buyers and sellers. These gains are given the names Consumer 
Surplus and Producer Surplus. 

We begin our development of Consumer Surplus 
by relating the demand curve to marginal value. First, we specify the
inverse demand curve in terms of quantity alone. That is, prices of 
related goods and income do not change during the analysis.

kill(all)$   DP(q,ED):= q^(1/ED);  
wxdraw2d( xlabel="Quantity", yrange=[0,10],ylabel=
"$ per unit",explicit(DP(q,-1.5),q,0,1.5))$

At each quantity, the height of the demand curve is the amount that some
consumer is willing and able to pay for that unit. Summing all of these
values, wouuld generate a measure of value for a specified number of units of 
this good. The text states a number of provisos that must hold for this
measure to be precisely the value of the units to the consumer(s).

Value of Additional Units

The graph below shows the gain in value from changing the quantity from
0.8 units to 1 unit. Three different demand curves appear. The value
to the consumer(s) of the additional 0.2 units is the area under the
demand curve, so three different values emerge.

ED0:-0.75$ [ED1,ED2]:[0.75*ED0,1.5*ED0]$ 
q1: 0.8$ p1:DP(q1,ED0)$ phigh: max(DP(q1,ED1),DP(q1,ED2))$
  pmax: max(1, phigh)$
wxdraw2d(yrange=[0,1.2*pmax],xlabel="Quantity",
 ylabel="$ per unit",user_preamble="set key bottom left",
 line_type=dots,points_joined=true, point_type=3,
 points([[1,0],[1,1]]),points([[q1,0],[q1,phigh]]) ,
 key="Median E",line_type=solid,
 explicit(DP(q,ED0),q,0,1.5*max(1,q1)),color=blue ,
 key="Less Elastic",line_width=2,
 explicit(DP(q,ED1),q,0,1.5*max(1,q1)),color=red,
 key="More Elastic",line_width=3,
 explicit(DP(q,ED2),q,0,1.5*max(1,q1))  )$

Suppose that empirical evidence suggests that E = -0.75 when q = 1 and
we wish to estimate the value of units 0.8 - 1. It would be useful to know
how sensitive the estimate of the value might be to the actual value of E. 

We can allow for a range of values from 3/4 of the estimated value to 
1.5 times that value. (Actually, we could select whatever range we like.)
The result below shows that, for this purpose, the actual value of the 
0.2 units is not very different for the three elasticity values.

fpprintprec:5$ratprint:false$

[[change1,change2,change3]:float([
 integrate(DP(q,ED0),q,1,q1),integrate(DP(q, ED1),q,1,q1),
 integrate(DP(q,ED2),q,1,q1)]), change2/change3];

Consumer surplus is a measure of the gain from exchange. Specifically,
it is the difference between willingness to pay and the amount paid. In
the present illustration, the amount paid is price*quantity. Thus consumer
surplus of one unit appears below as the area under the demand curve, 
from q=0 to q=1, less the amount paid, which happens to be $1 in this
illustration.

wxdraw2d(yrange=[0, 5*pmax],xlabel="Quantity",ylabel=
"$ per unit",user_preamble="set key bottom left",
 line_type=dots,points_joined=true,color=gray10,
 points([[0,1],[1,1]]),points([[1,0],[1,1]]),
 label(["Consumer Surplus", 0.25, 2.5]),label(
  ["Amount paid", 0.2, 0.5]),line_type=solid,
 explicit(DP(q,ED0), q, 0, 1.5*max(1,q1))  )$

Prices and Consumer Surplus
In a market setting, the question that faces the analyst is often this: 
What is the change in consumer surplus due to a change in the 
good's price? The answer to that question can be much more 
responsive to the elasticity value than the answer to the question
raised above.

We change slightly the demand curve, so that the elasticity parameter
is included as an argument. We keep the scale selection so that the
quantity demanded equals 1 unit when the price equals $1. We show
the effect of raising the price to $1.3465 for each elasticity value.

The value of $1.3465 is selected because it generates q = 0.8 for 
the inital elasticity value.

p1;  DQ(p, ED) := p^ED;
[q10,q11,q12]:[DQ(p1,ED0), DQ(p1,ED1),DQ(p1,ED2)];

The graph below shows the reduction in Consumer Surplus for
each of the three elasticity values, when the price rises from $1
to p1. It shows that the reduction is greater when the elasticity
is greater.

wxdraw2d( yrange = [0,1.2*pmax],xlabel="Quantity",ylabel=
"$ per unit",user_preamble="set key bottom left",
 line_type=dots,points_joined=true,points([[0,1],[1,1]]),
 points([[0, p1 ],[max(q10,q11,q12),p1]]), line_type=solid,
 key="Median E",explicit(DP(q,ED0),q,0,1.5*max(1,q10)),
 color=dark_grey,key="Less Elastic",line_width=2,
 explicit(DP(q,ED1),q,0,1.5*max(1,q11)),key="More Elastic",
 color=gray40,explicit(DP(q,ED2),q,0,1.5*max(1,q12)))$

To determine the value of the change in consumer surplus for 
each of the three demand curves, we integrate the demand curve (not
the inverse demand curve as above) over the range of prices. The
three resulting values appear below. Again, the difference between
the high-elasticity and the low elasticity cases is small.

[change4,change5,change6]:float([integrate(
  DQ(p,ED0),p,1,p1),integrate(DQ(p, ED1),p,1,p1),
 integrate(DQ(p,ED2),p,1, p1)]);  change5/change6;

This assertion appears in the text: "This result should not be taken as 
general. Suppose the demand curves were shifted horizontally to the 
left .... Then the change in consumer surplus would be smaller than 
appears above, and the percentage differences among
the three cases would be larger." Amend the demand function
as in the cell below and repeat the exercise above.

Now the percentage difference between is approximately twice
as large as before.

DQ2(p, ED) := p^ED-0.4;
[q10,q11,q12]:[DQ2(p1,ED0), DQ2(p1,ED1),DQ2(p1,ED2)];
[change7,change8,change9]:float([integrate(
  DQ2(p,ED0),p,1,p1),integrate(DQ2(p, ED1),p,1,p1),
 integrate(DQ2(p,ED2),p,1, p1)]);  change8/change9;

Here's what the new demand curves look like. The implicit( ) command
is used because the inverse demand curve has not been defined. Note
that, inside the first command is DQ2(p,ED0)-q, the implicit expression
for the demand curve (likewise for ED1 and ED2).

wxdraw2d( xrange=[0,1.6],yrange = [0,1.2*pmax],xlabel="Quantity",
 ylabel="$ per unit",  line_type=dots,points_joined=true,
 points([  [ 0,1], [.6,1 ] ]), points( [ [ 0,p1],[DQ2(p1,ED1), p1 ] ]),
 line_type=solid,key="Median E",
 implicit( DQ2(p,ED0)-q,q,0,1.6,p,0,1.6),
 color=black,key="Less Elastic",line_width=2,
implicit( DQ2(p,ED1)-q,q,0,1.6,p,0,1.6),key="More Elastic",
 color=red,implicit( DQ2(p,ED2)-q,q,0,1.6,p,0,1.6)   )$

Producer Surplus

Producer Surplus is analogous to Consumer Surplus. It is the difference 
between the revenue received by sellers and the revenue required to 
induce them to produce a given quantity. The supply curve is expressed
in much the same way as the demand curve.

[SQ(p, ES, b) := b*p^ES,SP(q, ES, b) := (1/b)*q^(1/ES)];

The graph below shows Producer Surplus. The supply
curve passes through the quantity $q = 1$ at a price determined
by the income elasticity ($ES = 1$) and the coefficient ($b = 1.5$).
These values are arbitrary. 

ptemp : SP(1, 0.5,1.5);    
wxdraw2d( yrange=[0,1.5*ptemp],  xlabel="Quantity", 
 ylabel="$ per unit", color=black,
 user_preamble="set key center right",
 label(["Producer Surplus",.4,.8*ptemp]),
 label(["Producer Cost", 0.65, 0.1*ptemp]),
 label_alignment=left,
 label(["Revenue = Producer Surplus+Producer Cost",0.05,1.4*ptemp]),
 points_joined=true,
 points([[0,ptemp ],[1, ptemp]]),points([[1,0],[1,ptemp]]),
 line_width = 2, color = blue, key = "Supply", 
explicit(SP(q, 0.5,1.5),q,0,1.5*max(1,q1)))$

The next graph contains both demand and supply. It shows that 
the equilibrium quantity is just above 1.0 and that the equilibrium 
height of either the demand curve or the supply at the equilibrium 
quantity is just under 1.0.

wxdraw2d(yrange=[0,1.25*pmax],xlabel="Quantity",
 user_preamble="set key center right", ylabel=
"$ per unit",color=gray10,key="Demand",
 explicit(SP(q,0.5,1.5),q,0,2), line_width=2, 
 key="Supply",explicit(DP(q, ED0), q, 0, 2) )$

The next cell confirms that the equilibrium quantity
is 1.1293 units and the equilibrium price is $0.85.

[q_e: find_root(SP(q,0.5,1.5)-DP(q,ED0),q,0.001,200),
[pd_e, ps_e] : [DP(q_e, ED0) ,SP(q_e, 0.5, 1.5)]];

Combined Surpluses

The sum of the two surpluses is a measure of the value created
by exchange in a competitive market. We show that this sum is
maximized at the equilibrium outcome, so that this outcome has
positive interest (it is what tends to happen) and normative 
interest (it is attractive in that it maximizes a measure
of the benefits from trade). 

The next cell presents an expression for the gain from trade, without
specifying a quantity.

assume(ES > 0, ED < -1)$     surpluses: 
    integrate(DP(q, ED)-p+(p-SP(q,ES,b)),q);

The next cell shows the derivate of the expression above.
Setting this derivative equal to zero yields the quantity that
maximizes the expression above.

diff(surpluses, q);

The necessary condition identified above is that the inverse 
demand curve's value equal that of the inverse supply curve,
which corresponds to the equilibrium condition.

The next cell shows that the sufficient condition for a maximum 
is satisfied, given that ED < 0.

diff2 : diff(surpluses, q, 2);

Finally, confirm that the price required for maximizing combined
surpluses equals the equilibrium price, and that the second 
derivative is negative, for the parameters used above.

[[q_e^(1/ED0), q_e^(1/0.5)/1.5], 
subst([ED=ED0, ES = 0.5,b=1.5,q=q_e],diff2)];