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

# Long-run Supply

The short run the supply curve for an industry of price-taking 
firms is the horizontal sum of a fixed number of firms' supply 
curves. 

In contrast, the number of firms can vary with the price in the long 
run, and the number of firms must be determined. We begin analysis
of the long run by assuming that all firms are identical and the firms' 
common cost curves do not depend on the number of firms in the 
industry. Both assumptions are relaxed as we proceed. 

## A Constant-Cost Industry

This section develops the supply curve for a "constant-cost"
industry that consists of identical firms. The term "constant-cost" 
means that no firm's cost curve is affected by the number
or size of other firms in the industry. 

The first cell establishes the parameters of the long-run
cost curve for each firm in this industry, ltc = a + b*x^2. We specify 
a minimum long-run average cost, lac0, and a quantity, x0, at 
which that average cost is realized.

[ltc:a+b*x^2, lmc:diff(ltc,x)]; [lac0,x0]:[30,50];

For this simple function, the marginal cost, 2*b*x, equals the lac,
at the minimum lac value, lac0. Thus, b0 = lac0/(2*x0). Once b0 is 
determined, a0 can be calculated from the fact that, at x = x0, 
ltc = x0*lac0. Equivalently, as below, Maxima can solve for the 
values of a and b simultaneously.

solve([(a+b*x0^2)/x0=lac0,2*b*x0=lac0],[a,b])$
[a0,b0]:[rhs(%[1][1]), rhs(%[1][2]) ]$
matrix(["a0 =",a0],["b0 =",b0]);

Tracing the move between long-run equilibrium requires looking at 
the short-run adjustment, which requires a short-run cost function.
We a cubic function to illustrate the effects of a change. 

[stc:d+e*x^3, smc:diff(stc,x)];

In full equilibrium, each firm operates at the minimum point on
its ltc, so it has chosen the optimal short-run size. This implies
that the lac = sac, given the right size. Also, the ltc and stc 
 curves have the same slope, which equals lac0. First solve
for e using 3*e*x^2 =lac0. Then solve for d0. (Or solve for 
the two simultaneously).  

[e0 :lac0/(3*x0^2), d0:lac0*x0-e0*x0^3]$
matrix(["d0","e0"],[d0,e0]);

We now have expressions for the four relevant per-unit cost curves.

[lac(x,a0,b0):=''(a0/x+b0*x),lmc(x,b0):=''(2*b0*x)];
[sac(x,d0,e0):=''(d0/x+e0*x^2), smc(x,e0):=''(3*e0*x^2)];

### Firm Supply

The next cell creates long-run and short-run supply curves for
the firm represented above. The "if then else" statement accounts
for the fact that for p < lac0, the firm would exit in the long run.
In the short-run a firm would have to cover variable costs, but for
the function used here, average variable cost (e0*x^2) is always
less than marginal cost (3*e0*x^2). This firm would never shut 
down in the short run (this, of course, is not generally true).

xl(p):=''(if p < lac0 then 0 else p/(2*b0))$
xs(p):=''(sqrt(p/(3*e0)))$  plist:makelist(10*i,i,0,8)$ 
xslist:float(xs(plist))$    xllist:float(map(xl,plist))$
names:matrix(["p, dollars/unit)","x,short run", 
"x, long run"])$   numbers:transpose(
matrix(plist,xslist,xllist))$    addrow(names,numbers);

The figure below shows these two supply curves. 

wxdraw2d(user_preamble="set key bottom",
yrange=[0, 2*lac0],xlabel="x",ylabel="$ per unit", 
key="sac",explicit(sac(x,d0,e0),x,0,2*x0),line_width=2,
key="lac",explicit(lac(x,a0,b0),x,0,2*x0),color=gray40,
key="smc",explicit(smc(x, e0),x,0,2*x0),color=black, 
key="lmc",explicit(lmc(x,b0),x,x0,2*x0),line_type=dots, 
points_joined=true,points([[0,lac0],[x0,lac0]]) )$

### Long-run Equilibrium

The graph below shows why the long-run equilibrium price
must be 30. Any other price would induce either entry or exit
of firms. Thus, the height of the supply curve is lac0 = 30 for
all values of industry output. 

firmsupply:gr2d(user_preamble="set key bottom",
 yrange = [0, 2*lac0], xlabel="Firm Quantity",ylabel = 
"$ per unit",key="sac",title="Firm",explicit(sac(x,d0,e0),x,0, 2*x0),
 line_width=2,key="lac", explicit(lac(x,a0,b0),x,0,2*x0),color=gray, 
 key="smc",explicit(smc(x, e0), x, 0, 2*x0), color=black,
 line_width=3, key=  "relevant lmc",explicit(lmc(x,b0),x,x0,2*x0),
 line_type=dots, points_joined=true,key="",points([[0,lac0 ], [x0,lac0] ]))$
industrysupply : gr2d(xlabel="Industry Quantity",ylabel=
"$ per unit",yrange=[0, 2*lac0], title = "Industry",xtics=2000,
 key="LR Supply",line_width=2, explicit(lac0, x, 0, 100*x0) )$
 wxdraw(firmsupply,industrysupply,columns = 2,
  dimensions=[540,400])$

### Long-run and Short-run Equilibria

Given the two conditions that unlie this analysis, the development of
the logic of the movement from one long-run equilibrium to another
requires that a market demand curve be added to the model. For the 
other cases, developed below, we can develop the supply relationships
without reference to demand.

The next cell begins at full equilibrium. In addition to specifying a 
number of firms and thereby a supply curve, it specifies a demand
curve consistent with p = lac0 at X = n0*x0, where X is the 
industry quantity.

The last output line, produced by the last three commands, 
calculates n0, given the equilibrium condition. The calculated
value equals the stipulated value, as a check of the reasoning
that generated the equilibrium condition.

n0:200$    Xeq0:n0*x0$  
Xs(p,n):=''(n*xs(p));  
Xl(p,n):=''(n*xl(p));   
Xd(p,Xeq,T):=Xeq+T-(5000/lac0)*p;

solve(Xd(lac0,Xeq0,5000)-Xs(lac0,n),n)$  
n0calc:float(rhs(%[1]))$  
matrix(["Check: n0calc = ",float(n0calc)]);

Changing T from 5000 to 15000 shifts the demand curve rightward.
To trace the effects of this demand increase, we first determine 
the new price, p1, in the short run. The price rises to 63.031,
and the quantity increases from 10000 to 14495 units. 
This is a short-run equilibrium because firms cannot enter. 

To find the new long-run equilibrium number of firms, 
set demand equal to the post-entry short-run supply with 
the price constrained to lac0, but with n unknown. 

In this special case, and only in this special case}, the long-run 
supply curve determines the price, and the demand curve 
determines the quantity.

p1:find_root(Xd(p,Xeq0,15000)-Xs(p,n0),p,lac0,10*lac0);
solve(Xd(lac0,Xeq0,15000)-Xs(lac0,n),n)$  n1:float(rhs(%[1]));

matrix(["No. Firms", "Price", "Xs", "Xd"],
[n0,lac0,float(Xs(lac0, n0)),Xd(lac0,Xeq0,5000)],   
[n0,p1,float(Xs(p1,n0)),Xd(p1,Xeq0,15000)],
[n1,lac0,float(Xs(lac0,n1)),Xd(lac0,Xeq0,15000)]);

To draw a graph of the demand and supply curves, we 
we express the demand price  in terms of X. 

solve(Xd(p,Xeq,T)-X,p)$ p(X,Xeq,T):=''(rhs(%[1]));

In the next graph, the initial demand curve intersects 
the long-run supply curve at X = 10000, as does the initial 
short-run supply curve. The demand shift raises the price and 
quantity.  At p = 63.031, each firm produces 14495/200 = 72.475
units.

Profit leads to the entry of 200 additional firms, so the 
short-run supply curve shifts rightward. Once again each firm produces 
x = 50. The values in the points( ) commands are from above.

wxdraw2d( yrange=[0,2*p(0,Xeq0,1500)],
 xlabel="Industry Quantity", ylabel="$ per unit",
 line_width = 2, key="Initial Short-run Supply",
 explicit(smc(X/n0,e0),X,0,25000), color=red,
 key="New Short-run Supply",
 explicit(smc(X/n1,e0),X,0,25000),
 line_width=2, color= black,key="Initial Demand",
 explicit(p(X, Xeq0,5000),X,0,25000),
 key="New Demand",color=green,
 explicit(p(X, Xeq0,15000),X,0,25000),line_width=3,
 color=brown,key="Long-run supply curve",
 explicit(lac0,x,0,25000), points_joined=true,key="",
 line_type=dots,color=black,line_width=1,
 points([Xeq0,Xeq0],[0,lac0]),
 points([0,Xs(p1,n0)],[p1,p1]),
 points([Xs(p1,n0),Xs(p1,n0)],[0,p1]),
 points([Xs(lac0,n1),Xs(lac0,n1)],[0,lac0]),
 dimensions=[480,480])$

values;

## A Variable Cost Industry

This section retains these parameter values from the previous section.

[lac0,x0,a0,b0,d0,e0]:[30,50,750,0.3,1000,1/250];

Next, let the number of firms in an industry affect the cost functions
of the (still identical)  firms that constitute the
industry. 

We derive illustrative long-run average and marginal cost functions 
for the firms in such an industry. Both are derived from this long-run 
total cost function: ltc2 =  n^alpha*(a + b*x^2),  
where n  firms constitute the industry and alpha is the elasticity 
of change in total cost per one percent change in n.

We retain a0 and b0 values from above and set alpha = 0.1. 
Setting lac = lmc and solving for x confirms that the minimum 
value of lac occurs at a value that depends only on a and b and not
on n or alpha.

[lmc2(x,n,%alpha,b):=2*b*n^%alpha*x,
lac2(x,n,%alpha,a,b):=a*n^%alpha/x + b*n^%alpha*x];
solve(lmc2(x,n,%alpha,b)=lac2(x,n,%alpha,a,b),x); matrix(
 [float(subst([a=a0,b=b0],%[2])),"lac =",lac2(x0,1,.1,a0,b0)]);

### The Value of alpha

The text argues for a low value of alpha. As an exercise,
try larger values of alpha in this section. 

### Industry Supply 

To examine the nature of supply in this type of industry,
first determine the relationship between p and n. Set lmc = p 
at x = x0, which is the quantity at which each firm achieves
least-cost production.

We solve this equation twice. The first solution defines
the number of firms as a function of price and the 
parameter $\alpha$. The second solution defines the
inverse of the first one, stating price as a function of 
the number of firms.
\index{long-run supply!variable-cost industry!identical firms}

assume(p>0)$  declare(%alpha,noninteger)$
solnn:solve(lmc2(x0,n,%alpha,b0)-p,n);
solnp:solve(lmc2(x0,n,%alpha,b0)-p,p);
matrix([n(p, %alpha) := ''(rhs(solnn[1]))," or",
p(n, %alpha) := ''(rhs(solnp[1]))]);

We examine the adjustment process by including the short-run 
effects. Doing so requires that we define the short-run marginal 
cost curves for firms in this industry. We specify the short-run 
marginal cost curve the same way as its long-run counterpart, using
n^alpha as the impact of the number of firms.

smc2(x,n,%alpha,e):=3*e*n^%alpha*x^2;

The next cell produces five points on the long-run supply
curve for this industry. It works backward, specifying the 
number of firms and than deriving the price that must pertain
for that number of firms to inhabit the industry. Finally, 
because x = 50 when p = lac at its minimum point, the 
industry supply is 50*n.

nlist:makelist(n,n,100,500,100)$
plist:lac2(x0,nlist,0.1,a0,b0)$   Xlist:x0*nlist$
names:matrix(["No. of Firms","Price","Quantity"])$
numbers:matrix(nlist,plist,Xlist)$addrow(names,numbers);

The graph below shows the long-run inverse supply curve. It also shows the 
short-run supply curves when the number of firms is set 
equal to the second item in nlist (above) and again for the fifth item in that list. 

In the first explicit( ) command, n is expressed in the function 
p(n, alpha) as X/x0, so that p(n, alpha) can be stated in terms of X.

Xmax : 1.25*Xlist[5]$ pmax:1.25*p(Xlist[5]/x0,0.1)$
wxdraw2d(user_preamble="set key bottom",
 yrange = [0,pmax],xlabel="X",ylabel="$ per unit",
 line_width=2, key="Long-run Supply",
 explicit(p(X/x0,0.1),X,0,Xmax),
 color=red,key=concat("Short-run Supply, n = ", 
  string(nlist[2])),explicit(smc2(X/nlist[2],
  nlist[2],.1,e0),X,0,Xmax),color=gray40,
 key=concat("Short-run Supply, n = ",string(nlist[5])),
 explicit(smc2(X/nlist[5],nlist[5],.1,e0),X,0,Xmax ),
 line_type=dots, points_joined=true,
 key="Initial Price",points([[0,plist[2]], 
  [Xmax,plist[2]]]), color=red,key="New Price",
 points([[0,plist[5]], [Xmax,plist[5]]]),
 dimensions=[480,480]  )$

The presentation above determines the supply price for each quantity. We can 
specify price and determine the number of firms that will inhabit the industry 
given long-run equilibirum, and the quantity that they will produce. 

p1 : 51$ n1 : n(p1, 0.1)$   X1 : n1*x0$
p2:  55$  n2 : n(p2, 0.1)$   X2 : n2*x0$
matrix(["Price of x","# Firms","Quantity"], 
[p1, n1, X1], [p2, n2, X2]);

## An Industry with Non-identical Firms}

This section allows each firm to have a unique cost function. The firms are
ranked from least-cost (i = 1) to highest cost (i = n), where i is a counter and
n is the number of firms in the industry. 

The number of firms, imax, is the number that results in zero profits for the 
imax-th firm (i = imax), given the product price. Strictly speaking, we restrict
imax to integer values, so the marginal firm (i = imax) can earn a slight profit, 
but not enough to attract the higher-cost (imax+1)-th firm. 

a0*i^%alpha/x0+b0*i^%alpha*x0;solve(%=p,i)$ 
imax(p,%alpha):=''(floor(rhs(%[1])));

The next cell shows that p = 100 results in an industry that consists 
of 123 firm.

p0:100$   imax0: imax(p0, 0.25);

To extract a firm's supply curve, set its marginal cost equal to p.
Each firm's output is a function of p, i, alpha. Each firm's output 
differs from those of the other firms. These are long-run supply
curves.

x_i(p,i,%alpha):= ''(rhs(solve(2*b0*i^%alpha*x-p,x)[1]));

If p = p0 = 100, the least cost firm produces over three times as much
as the marginal (i = 123) firm.

[x_1:x_i(p0,1,0.25), x_imax0:x_i(p0,imax0,0.25)];

The graph below shows the average cost and marginal 
cost firms for Firm 1 and Firm 123. Each firm's supply 
curve is the segment of the marginal cost curve that lies
above its average cost curve.

wxdraw2d(dimensions=[480,480],
yrange = [0,  2*b0*imax0^0.25*(3*x0)],
xlabel = "Output Levels", ylabel = "$ per unit",
key="p0", explicit(p0, x, 0, 3*x0),
line_width = 2, key = "Average Cost Curves",
explicit(a0/x + b0*x, x, 0, 3*x0),key="",
explicit(a0*imax0^0.25/x+b0*imax0^0.25*x,x,0,3*x0),
color="red", key = "Marginal Cost Curves",
explicit(2*b0*x, x, 0, 3*x0), key = "",
explicit(2*b0*imax0^0.25*x, x, 0, 3*x0) )$

### Profits, Rents, and Shifting Cost Curves

All firms but Firm 123 in the industry depicted above earn positive profits. Whether
these profits accrue to the firms' owners or to owners of specialized resources 
used by the firm depends on the peculiarities of each firm's setting.

## Industry Supply

The cell below shows the number of firms in this industry at each of a 
set of prices. It also shows the industry output X. The average amount
produced by each firm is reported. The table could be amended to 
show the output for individual firms, say the lowest-cost firm and the
marginal firm.

pList: makelist(lac0*(1+(j-1)/10),j,1, 11)$
imaxList:imax(pList,0.25)$

X_list: makelist(
   sum(x_i(pList[j],i,0.25),i,1,imaxList[j]),
   j,1,11)$  

perfirmxList: X_list/imaxList$

numbers: transpose(matrix(pList,imaxList,X_list,
   perfirmxList))$  names:matrix(["Price","No. of Firms",
"Industry X","Avg. Firm x"])$  addrow(names,numbers);

For p = 42, p = 45 and p = 57, we compare the output of 
the median firm with the average x values above.

[x_i(42,2,0.25),x_i(45,3,0.25),x_i(57,7,0.25)];

The notes above say that the lowest-cost and highest-cost firms' outputs 
could be listed. These appear below. Except for the fact that we round the
number of firms, the marginal firm would always produce x_imax = 50. 

matrix(x_i(pList,1,0.25),
       x_i(pList,imaxList,0.25));

Each firm's supply curve exhibits unitary elasticity, so doubling the price should 
double the lowest-cost firm's output, from x = 50 to x = 100. The result below shows
that it does.  It also shows the output for Firm 8 and for Firm 16, the
highest-cost firm at the highest price.

matrix(["Firm1's x", "Firm8's x", "Firm16's x"],
[x_i(pList[11],1,0.25),x_i(pList[11],8,0.25),
x_i(pList[11],16,0.25)]);

The graph below shows the 11 points on the industry supply curve that 
appear in the table above.  Even though each firm's supply
curve has an elasticity of 1.0, the industry supply curve becomes quite
elastic as price increases. 

wxdraw2d(user_preamble="set key bottom",
 yrange=[0,1.5*pList[11]],
 ylabel="$ per unit",xlabel="Industry Quantity, X",
 line_width=2,key="Industry Supply Curve",
 points_joined=true,points(X_list,pList))$

### Least-cost Production in a Competitive Industry

The text argues that the condition for efficiency in the long-run is not that
each firm reach the minimum point on its lac. Rather the condition is two-fold:
Output is allocated among firms so that mc is the same in each firm, and
The average cost of the marginal firm is (approximately, due to integers) 
equal to the marginal cost of all other firms. 

In the special case reproduced below, in which all firms are identical, then
the two conditions above collapse into the single condition that every 
firm produces at the minimum point on its long-run total cost curve.

firmsupply: gr2d(user_preamble="set key bottom right",
yrange = [0, 2*lac0], xlabel="Firm Quantity",
ylabel = "$ per unit", key = "sac", 
title = "Firm",explicit(sac(x, d0, e0), x, 0, 2*x0),
line_width=2, key="lac",explicit(lac(x,a0,b0),x,0,2*x0),
color = gray, key="smc",explicit(smc(x,e0),x,0,2*x0),
color = black, key = "lmc", 
explicit(lmc(x,b0),x,x0,2*x0),line_type=dots, 
points_joined=true,points([ [0,lac0],[x0,lac0 ]]))$
industrysupply : gr2d(xlabel = "Industry Quantity", 
yrange=[0,2*lac0],ylabel="$ per unit",title="Industry",
key = "LR Supply", xtics={100*x0/2, 100*x0},
line_width = 2, explicit(lac0, x, 0, 100*x0) )$
wxdraw(firmsupply, industrysupply, columns = 2,
 dimensions=[540,400])$