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

# Competitive Markets: 
  Extensions and Application

This chapter extends the analysis in the preceding chapters
by analyzing the effects of shifts in demand and supply and the
implications of competitive markets for economic efficiency.

## Demand and Supply Shifts

A useful way to address the effects of shifts is to express the quantity 
demanded as a function of p and  a shift parameter: Xd = f(p,beta). 
Likewise, the supply curve can be phrased as Xs = g(p, alpha).  
Equibrium requires that Xd = Xs.

The text shows that implications of changes in alpha or beta can be
expressed in terms of the price elasticities of the demand and supply
expressions:
    Epb = Edb/(Esp - Edp) is the elasticity of price with respect to a 
        change in beta, where Edb is the percentage shift in the demand
        curve per one percent change in beta, and Esp and Edp are the
        price elasticities of supply and demand, and
    Epa =  Esa/(Esp - Edp), where Epa is the elasticity of price with respect
        to a change in alpha, and Esa is the percentage shift in the supply 
        curve per one-percent change in alpha.

### Examples

Consider a system of two curves, the general nature of which 
is this: Xd = f(p, I), and Xs = g(p, w). That is, I (income) is the beta
shift factor and w (a wage rate) is the alpha shift factor.

Base Values. The next cell specifies a set of elasticities for the 
demand and supply curves. The four elasticity values are applied 
directly to the constant-elasticity equations; they are used to
determine the slope coefficients in the linear equations. Also, initial
equilibrium values for X, p, I and w are specified. The values of 
B and A consistent with all of the above are determined 
momentarily.

[Esp0,Edp0,Esa0,Edb0]:[5.0, -1.25, -0.5,2.0]$
[X0,p0,I0,w0]:[5000.0, 7.0, 40.0, 25.0]$

Constant-elasticity Curves. Suppose that Xd = B*(p^Edp)*(I^(Edb)
and that Xs = A*(p^Esp)*(w^Esa). 

The commands below determine the value of B that is consistent
with the values specified above.

solve(X0 = B*p0^Edp0*I0^Edb0, B);
[Xd(p,I,Edp,Edb):=''(float(subst(%,B*p^Edp*I^Edb))),
Xd(p,I0,Edp0,Edb0)];

The supply function is solved in the same fashion.

solve(X0 = A*p0^Esp0*w0^Esa0, A);
[Xs(p,w,Esp,Esa):=''(float(subst(%,A*p^Esp*w^Esa))),
Xs(p,w0,Esp0,Esa0)];

We confirm that the p = 7, and that X = 5000.

[solnp: find_root(Xd(p,I0,Edp0,Edb0)-Xs(p,w0,Esp0,Esa0),
        p,0.1,500), Xs(solnp,w0,Esp0,Esa0)];

We also determine the elasticity of price with respect to a change
in B (0.32) and with respect to a change in A (-0.08). These values
are compared to the results of discrete changes below.

[Epb0:Edb0/(Esp0-Edp0), Epa0:Esa0/(Esp0-Edp0)];

Let I shift by 10%. Edb = 2, so the demand curve shifts rightward 
by 20%. The price rises by 0.2168. Using the arc elasticity formula,
we determine that this change in consistent with the prediction in 
the preceding cell.

[Epb0:Edb0/(Esp0-Edp0), I1:1.1*I0]$  
matrix(["New price = ",p1:find_root(Xd(p,I1,Edp0,Edb0)-
  Xs(p,w0,Esp0,Esa0),p,p0,2*p0),"and Arc Elasticity =",
((p1-p0)/(p1+p0))/((I1-I0)/(I1+I0))]);

The new equilibrium quantity X1 can be determined using
either the demand curve or the supply curve. the results of
both appear below.

[X1:Xd(p1,I1,Edp0,Edb0),Xs(p1,w0,Esp0,Esa0)];

The graph shows the two demand curves, the supply curve, 
and the two points that correspond to equilibria.

Compared to the graph in the text, we have made two changes
other than in color. We truncated the yrange to emphasize the 
shift. Also, we increased the value of ip_grid, because the 
truncated range shows jagged representations of the curves.

wxdraw2d(yrange = [.8*p0,1.2*p0],ip_grid=[150,150],
 user_preamble="set key bottom left",xlabel = "X", 
 ylabel="$ per unit",points_joined=true,line_type=dots,
 points([[0, p0],[X0, p0]]), points([[X0, 0],[X0, p0]]),
 points([[0, p1],[X1, p1]]), points([[X1, 0],[X1, p1]]),
 line_type=solid,line_width=2,key="Initial Demand", 
 implicit(Xd(p,I0,Edp0,Edb0)-X,X,X0/10,2*X0,p,p0/10,2*p0),
 color=red,key="New Demand",
 implicit(Xd(p,I1,Edp0,Edb0)-X,X,X0/10,2*X0,p,p0/10,2*p0),
 color="black",key="Supply", 
 implicit(Xs(p,w0,Esp0,Esa0)-X,X,X0/10,2*X0,p,p0/10,2*p0) )$

We leave the analysis of a 10% change in w, holding I constant, as 
an exercise.

Linear Curves. The mechanics change somewhat when the demand
and supply curve are represented by straight lines. As before, we
must calculate B and A values, which are no intercepts. Also, we
convert the elasticities to slopes. 

Next, we fit linear demand and supply through an initial point
and shift the demand curve as before. The cell below contains
the commands needed to fit the curves and to define the 
functions. The functional names are changed to XsL(...) and XdL(...) 
to indicate the linear nature of the functions.

The first and third commands convert the given values to B and A
value. The second and fourth commands create functional 
expression for demand and supply.

solve(X0=B+p0*Edp0*X0/p0 + I0*Edb0*X0/I0, B);
XdL(p,I,Edp,Edb):=''(
   float(rhs(%[1]))+p*Edp0*X0/p0+I*Edb0*X0/I0);
solve(X0 = A + p0*Esp0*X0/p0 + w0*Esa0*X0/w0, A);
XsL(p, w, Esp, Esa) := ''(
   float(rhs(%[1])) + p*Esp0*X0/p0 + w*Esa0*X0/w0);

The cell below contains the calculation of the new price and
the new quantity (again, quantity is calculated twice as a check), 
given increased income. 

These values differ slightly from those generated when the functions 
exhibit constant elasticities.

The output also shows the arc elasticity of price with respect
to the demand shift parameter using the midpoint formula. In this case, 
the calculated value differs slightly from the predicted value.

p2:find_root(XdL(p,I1,Edp0,Edb0)-XsL(p,w0,Esp0,Esa0),
  p,0,2*p0);       matrix(
["Xsupply =",X2:XsL(p2,w0,Esp0,Esa0),"Xdemand =",
  XdL(p2, I1, Edp0, Edb0 ),"and Arc Elasticity =",
 ((p2-p0)/(p2+p0))/((I1-I0)/(I1+I0))]);

As before, the graph displays the demand shift and shows
the implied price and quantity changes.

Note the increased values for ip_grid, again due to 

wxdraw2d(yrange=[.8*p0,1.2*p0],ip_grid=[150,150],
 xlabel="X",ylabel="$ per unit",points_joined=true,
 line_type=dots,points([[0, p0],[X0, p0]]),points([[X0, 0],[X0,p0]]),
 points([[0, p2],[X2, p2]]),points([[X2, 0],[X2,p2]]),
 line_type=solid,line_width=2,key="Initial Demand",
 implicit(XdL(p,I0,Edp0,Edb0)-X,X,X0/10, 2*X0, p, p0/10, 2*p0),
 color=red, key = "New Demand",
 implicit(XdL(p,I1,Edp0,Edb0)-X,X,X0/10,2*X0,p, p0/10,2*p0),
 color=black,key="Supply",
 implicit(XsL(p,w0,Esp0,Esa0)-X,X,X0/10,2*X0,p,p0/10,2*p0) )$