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

# Competitive Markets: 
  Extensions and Application

## Demand and Supply Shifts

## Allocative Efficiency

The preceding chapter shows that in an industry of profit-maximizing
price-taking firms, X is produced at the lowest possible cost. We 
now address allocative efficiency by comparing the X that yields 
maximum value to consumers and producers with the equilibrium X.

We use constant-elasticity demand and supply curves, the equations
for which appear below.

kill(all)$ [Xs(p,A,Esp):=A*p^Esp,Xd(p,B,Edp):=B*p^Edp];

Because prices are measures of value, the inverse supply and 
demand curves are more directly useful, so they are derived here.

assume(A > 0, B > 0, X > 0)$
declare([Esp,Edp],noninteger)$ 
[ps(X, A, Esp) := ''(rhs(solve(Xs(p,A,Esp)-X,p)[1])),
pd(X, B, Edp):=''(rhs(solve(Xd(p,B,Edp)-X,p)[1]))];

First, specify the equilibrium X, given the previously specified 
parameters. W must e determine the values of A and B that are 
consistent with these equilibrium values.

[X0,p0,Esp0,Edp0]:[2000,50,2.5,-1.5]$   
A0:rhs(float(solve(Xs(p0,A,Esp0)=X0,A))[1])$   B0:
  rhs(float(solve(Xd(p0,B,Edp0)=X0,B))[1])$  matrix(["X0",
"p0","A0","Esp0","B0","Edp0"],[X0,p0,A0,Esp0,B0,Edp0]);

The graph is useful in addressing allocative efficiency. The two 
curves are labeled "Marginal Value" and "Marginal Opportunity Cost." 
The text discusses conditions under which these correspond to the 
demand and supply curves.

wxdraw2d(yrange=[0,2*p0],xlabel="X",ylabel="$ per unit",
 line_width=2,key="Marginal Value",explicit(pd(X,B0,Edp0),
   X,0,1.5*X0), key="Marginal Opportunity Cost",color=
  red,explicit(ps(X,A0,Esp0),X,0,1.5*X0),key = "",line_width=1,
 line_type = dots, points_joined=true,
 points([[0,p0],[X0,p0],[X0,0]]) )$

For each unit up to X = 2000, the added value exceeds the added cost,
so increasing X adds to total value. For each unit above X = 2000, added
cost exceeds added value, so increasing X diminishes total value.

### Consumer Surplus and Producer Surplus

Earlier chapters have shown that the area under the demand curve
and above a horizontal line whose height is the price that consumers 
pay provides a measure of Consumer Surplus--how much consumers 
gain by virtue of being able to buy whatever quantity they choose at 
the selected price.  The text provides some caveats regarding the 
preceding statement.

The height of the supply curve at each quantity corresponds to the 
marginal cost of producing that quantity. As a result, the area between
the marginal cost curve and a horizontal line whose heigh is the price
that producers are paid provides a measure of Producer Surplus--how
much producers gain by virtue of being able to sell whatever quantity
they choose at the selected price. The text elaborates on the nature
of Producer Surplus, including conditions that determine how closely
the supply curve corresponds to the Marginal Opportunity Cost curve.

The analysis below is based on the assumptions required for exact
correspondence between the Marginal Value curve and the demand
curve, and between the Marginal Opportunity Cost curve and the
supply curve.  

Given these correspondences, the integral defined below expresses
the sum of Consumer Surplus and Producer Surplus as a function 
of X and the parameters.

assume(Edp<0, Esp>0, notequal(Edp+1,0))$
integrate(pd(X,B,Edp)-ps(X,A,Esp),X);
surpluses(X, A, B, Esp, Edp) := ''%$

The three pieces of output below do the following:
*Express the derivative of surpluses with respect to X as a function of X;
*Determine the efficient quanity, 2000 units; and
*Determine the level of combined surpluses, $228,570.

diff(surpluses(X,A0,B0,Esp0,Edp0), X);
find_root(%,X,.1,2*X0); S0:surpluses(%,A0,B0,Esp0,Edp0);

The next cell confirms that the equilibrium quantity is 2000, the 
same as the value above. It also shows that the heights of the
demand and supply curves are equal, as required for equilibrium.

Xeq0:find_root(ps(X,A0,Esp0)-pd(X,B0,Edp0),X,.1,2*X0)$
pseq0:ps(X0,A0,Esp0)$   pdeq0:pd(X0,B0,Edp0)$
matrix(["Equilibrium Quantity", "Supply Price", 
"Demand Price"],  [Xeq0, pseq0, pdeq0]);

### An Excise Tax

Impose a per-unit tax, T = 10 on the sale  of X. The inverse
supply curve shifts upward by T and is parallel to the initial 
inverse supply curve.

The find_root( ) command finds the new equilibrium quantity. Then
the supply price (the net price received by the sellers) and the 
demand price are computed. The tax revenue is T*X. 

The supply curve is more elastic than the demand curve (2.5 vs. -1.5),
so $6.476 per unit of the $10 tax falls on consumers.

T0:10$  
X1: find_root(pd(X, B0, Edp0) - (ps(X, A0, Esp0)+T0), X,.1,2*X0)$
ps1:ps(X1,A0,Esp0)$  pd1:pd(X1,B0,Edp0)$
Revenue1: (pd1-ps1)*X1$   
matrix(["Quantity","Net Price","Price w/Tax","Tax Revenue"], 
            [X1,ps1,pd1,Revenue1]);

The combined surpluses without the tax, from above, is $228,570.
The tax reduces these to $210,310, so consumers and producers 
of this product jointly lose this much. 

S1: surpluses(X1,A0,B0,Esp0,Edp0)-Revenue1$  change:
 surpluses(X1,A0,B0,Esp0,Edp0)-surpluses(X0,A0,B0,
 Esp0,Edp0)-Revenue1$  matrix( ["Surplus w/o Tax",
"Surplus w/Tax","Change","Net Change"],[S0,S1,change,
change+Revenue1]);

The government receives $16,660, so per $1 raised the combined 
surpluses in this industry fall by about $1.10. The effect on overall
economic efficiency, therefore, depends on whether the tax revenue
is used in a way that generates a benefit/cost ratio above this value
(or, total benefits above $18,265).

float(18265/16600);

The graphs below show the effects of the tax on equilibrium values and
on the total surpluses (labeled ``Net Gains'').

/*market scenario*/
market: gr2d(user_preamble="set key bottom",
 yrange=[0,2*p0],xlabel="X",ylabel="$ per unit", 
 line_width=2,key="Supply w/o Tax",
 explicit(ps(X,A0,Esp0),X,0,1.5*max(X0,X1)),
 color=red,key="Supply w/Tax",
 explicit(ps(X,A0,Esp0)+T0,X,0,1.5*max(X0,X1)),
 color=black,key ="Demand",
 explicit(pd(X, B0, Edp0),X, 0,1.5*max(X0,X1)), 
 color=black,line_type=dots,points_joined=true, 
 key="",points([[X1,0],[X1,pd1]])  )$
/*gains scenario*/
gains: gr2d( xlabel="X",ylabel = "$",
 user_preamble="set key top",
 yrange = [0.8*S1, 1.1*S0],  key="Net Gains",
 explicit(surpluses(X,A0,B0,Esp0,Edp0),X,.1,1.5*max(X0,X1)),
 key="",line_type=dots,points_joined=true,points(
  [[0,S1],[X1,S1],[X1,0]]),points([[0,S0],[X0,S0],[X0,0]]) )$
/*draw the market and gains scenarios*/
wxdraw(market, gains,dimensions=[480,540])$