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

# Cost Theory

This workbook analyzes the nature of the cost of producing x. 
The analysis is divided into the analysis of cost in a stylized long 
run in which the firm can select any quantities of both inputs, and 
a stylized short-run in which only the employment of L can vary. 

The first cell recalls perinent data from the previous workbook,
ch06Production.

x(K, L, a, b, c, e) := a*K^e*L^(1-e) + 
      b*K^(2*e)*L^(2*(1-e)) + c*K^(3*e)*L^(3*(1-e))$
[a0,b0,c0,e0]:[2,30,-0.25,0.3]$   [w0,r0]:[50,80]$

The next cell recreates the functional expression for the
marginal rate of technical substitution. 

mrts(K,L,e):=''(
   ratsimp(-diff(x(K,L,a,b,c,e),L)/diff(x(K,L,a,b,c,e),K)));

## The Optimal Input Mix

As part of maximizing its profit, the firm must find the input mix 
that results in $mrt = w/r$. To see this, consider a general production
 function $y(K, L)$. 

Define a Lagrangian function as below (ltc represents long-run 
total costs).

The second output line shows the first two first-order conditions.

The third output line is a list that shows that the Lagrangian
multiplier equals MPK/r and MPL/w. From this we determine that
cost minimization requires that L and K be selected so that 
MPL/MPK = w/r.

W:y(K,L)-%mu*(ltc-r*K-w*L);  
[WK,WL] : [diff(W, K),diff(W, L)];   
[expK: solve(WK,%mu)[1], expL:solve(WL,%mu)[1],
w/r*expL/expK ];

The graph below illustrates this conclusion. If production
occurs at the point of intersection of the isoquant and the
isocost line for ltc=2400, then the MPL/MPK > w/r or,
equivalently, MPL/w > MPK/r, so at the margin labor's
productivity per dollar exceeds that of capital. The 
firm can either move to the right along its isocost line
and increase output, or it can more to the right along
the isoquant and decrease cost. Or it can move into the 
area defined by the two curves and simultaneously increase
x and decrease ltc.

wxdraw2d(xlabel="L",ylabel="K", key = concat(
"The isoquant for x = ",string(x(20,20,a0,b0,c0,e0)) ),
 implicit(x(K,L,a0,b0,c0,e0)=x(20,20,a0,b0,c0,e0),
  L,.1,40, K,.1,40),line_width=2, key=concat(
"Isocost line for cost = ",string(30*r0)),
 explicit(30 - (w0/r0)*L, L, 0, 30*r0/w0)  )$

Setting -mrts = w/r and solving reveals that for a w,r pair, 
the marginal rate of technical substitution does not depend on x.

ksoln: solve(-mrts(K, L, e) = w/r, K)[1];
m(e, w, r):= ''(rhs(ksoln/L));

## The Long-run Cost Curve

The long run is a sufficiently long period for the firm to be
able to implement the optimal input mix, as defined above. 
The production function used here does not generate a 
tractable explicit function in which cost is stated as a function
of quantity. Rather, we derive both the output level and 
cost as functions of L and examine the implicit relationship
between the two, each of which contains L as a parameter.

To define the relationship between x and L, use the relationship 
named ksoln above. Substitute the rhs( ) of that expression for K
in the production function.

The next command uses the same information, placing it into the
equation ltc=r*K + w*L and solving.

temp:r*K + w*L;
subst(ksoln,temp);

xaug(L, w, r, a, b, c, e) := ''(
      radcan(x(rhs(ksoln),L,a,b,c,e)));
subst(ksoln,r*K+w*L);
ltcaug(L,w,r,e):=w*L-(e*w*L)/(e-1);

To guide the drawing of  the long-run total cost curve, we define 
this set of values: m0, L2, K2, x2, and ltc2. The value of x2 is the 
largest amount the firm can produce given m0. The values of 
L2 and K2 are the employment levels consistent with x2. Finally, 
ltc2 is the cost of employing K2 and L2.

The first input finds the L value at which output is maximized, 
given m. The result in an expression, not a value The second line 
sets a value of m, based on the relevant input prices and the
value f parameter e. 

The third line solves the expression generated by the first line, 
m. The next two lines define the value of L that yields
the maximum x, the maximum value of x, and the associated
cost level. 

The matrix( ) commands summarizes the values.

diff(x(m*L,L,a0,b0,c0,e0),L)$  expr:solve(%, m);
m0:m(e0,w0,r0);
float(solve(subst(m=m0,rhs(expr[1])), L) );

L2:rhs(%[1]);  K2:m0*L2;  ltc2:r0*K2+w0*L2;
x2:x(m0*L2,L2,a0,b0,c0,e0)$      matrix(
   ["m0","K2","L2","x2","ltc2"],[m0,K2,L2,x2,ltc2]);

The next graph shows what happens when L exceeds L2.
The L > L2 values produce the backward-bending part of the
cost curve.

wxdraw2d(user_preamble="set key bottom",
 xlabel="x",ylabel="$",xrange=[0, 1.1*x2],
 key="Long-run total cost",
 parametric(xaug(L,w0,r0,a0,b0,c0,e0), 
   ltcaug(L,w0,r0,e0), L,0.1,1.25*L2),
 key="L2, x2",line_type=dots,
 points_joined=true,points( [[0,ltc2], [x2,ltc2], [x2,0] ] ))$

### Per-unit Curves in the Long Run

Given ltc, we can derive lac (average cost) and lmc (marginal cost)
values. The table below shows ten such values.

diff(ltcaug(L,w,r,e),L)/diff(xaug(L,w,r,a,b,c,e),L)$
lmc(L,w,r,a,b,c,e):=''%$
Llist: makelist(L2*i/10,i,1,10)$   Klist: m0*Llist$
xlist : x(Klist, Llist, a0, b0, c0, e0)$
ltclist:ltcaug(Llist,w0,r0,e0)$ laclist:ltclist/xlist$
lmclist:lmc(Llist,w0,r0,a0,b0,c0,e0)$
names : matrix(["x", "ltc", "lac", "lmc"])$
costs: transpose(matrix(xlist,ltclist,laclist,
   lmclist) )$     addrow(names, costs);

An aside: We can calculae arc values for average cost or
marginal cost, given discrete values of x and ltc. 

mc_arc: (ltclist[5]-ltclist[4])/(xlist[5]-xlist[4])$
matrix(["mc[4]", "mc_arc", "mc[5]"], 
   [lmclist[4], mc_arc, lmclist[5]]);

The next graph plots the values in the table and connects the dots.

ac2 : ltcaug(L2,w0,r0,e0)/x(m0*L2, L2, a0, b0, c0, e0)$
wxdraw2d( user_preamble = "set key bottom right",
 yrange=[0,1.5*ac2],xlabel="x",ylabel="$ per unit", 
 points_joined=true,key="Long-run average cost", 
 points(matrix(xlist, laclist)),  
 key = "Long-run marginal cost", point_type= 6,
 points(matrix(xlist, lmclist))      )$

Next we use the parametric relationship between x and total cost
to derive the per-unit cost curves. Notice how the per-unit cost 
curves are defined within the parametric( ) commands. 

One could define the expressions before entering wxdraw2d( ),
assign names to the expressions, and enter these names as
arguments in parametric( ).

wxdraw2d( user_preamble="set key bottom right",xlabel= 
"x",ylabel="$ per unit",yrange=[0,1.5*ac2], 
   key="Long-run average cost", line_width=2,color=red,
 parametric(xaug(L,w0,r0,a0,b0,c0,e0),ltcaug(L,w0,r0,e0)/
   xaug(L, w0, r0, a0, b0, c0,e0), L,0.1*L2,0.99*L2), 
 key = "Long-run marginal cost", color=yellow,
 parametric( xaug(L, w0,r0,a0,b0,c0,e0), 
  lmc(L,w0,r0,a0,b0,c0,e0), L, .1, .99*L2), key="",
 color=black,  points(matrix(xlist, laclist)), 
 points(matrix(xlist, lmclist)) )$

## Short-run Costs

In the short run, K cannot be varied. For illustration, we bind 
the name itemNo to select a line from the table above. We use the 
seventh row, so KS0 = 22.27. 

Exercise: Select other values for itemNo and compare the results.

itemNo: 7$  /*Select line number from list*/
KS0:Klist[itemNo]$ LS0:Llist[itemNo]$ 
xS0:xlist[itemNo]$ tcS0:ltclist[itemNo]$
matrix(["K,SR","L,LR&SR", "x,LR&SR", 
"ltc,LR&SR"],[KS0, LS0, xS0, tcS0]);

The input group below defines the total cost curve, the average 
cost curve, and the marginal cost curve in the short run.

The define( ) command has replaced the quote-quote operator. 

define(stc(K,L,w,r),  r*K+w*L);
define(smc(K,L,a,b,c,e,w),
  diff(stc(K,L,w,r),L)/diff(x(K,L,a,b,c,e),L));
define(sac(K, L,a,b,c,e,w,r), stc(K,L,w,r)/x(K,L,a,b,c,e));

/* stc(K,L,w,r):=r*K+w*L;
smc(K,L,a,b,c,e,w):=''(
  diff(stc(K,L,w,r),L)/diff(x(K,L,a,b,c,e),L));
sac(K, L,a,b,c,e,w,r):=''(stc(K,L,w,r)/x(K,L,a,b,c,e)); 

The commands have been commented out. Remove
the brackets and execute the cell to ensure that the 
results are as above. The end; that follow is entered
because wxMaxima does not allow a cell to end with
a comment.*/
end;

### Long-run Costs and Short-run Costs

The next table provides short-run cost information. It is like the
earlier table for long-run costs.

KS0list:makelist(KS0,i,1,10)$   x0list:
 x(KS0,Llist,a0,b0,c0,e0)$  stc0list:stc(KS0,Llist,w0,r0)$
sac0list:sac(KS0, Llist, a0, b0, c0, e0, w0, r0)$
smc0list:smc(KS0, Llist, a0, b0, c0, e0, w0)$
names : matrix(["KS0","L","x","stc0","sac0","smc0"])$
matrix(KS0list,Llist,x0list,stc0list,sac0list,smc0list)$
addrow(names, transpose(%));

The graph generalizes the information in the table, showing 
that for x < 50288 and x > 50288, stc > ltc. The table differs
from its counterpart in the text only in that a drop line has
been added and the colors and line types changed. Note
a small change in the points(  ) command, with the points
embedded in a list.

wxdraw2d(user_preamble="set key top left",xlabel="x",
 ylabel="$",yrange=[0,r0*KS0+w0*1.25*L2],
 points_joined=true,line_type=dots,
 points([[xS0,0],[xS0,tcS0],[0,tcS0]]),

 line_type=solid, line_width=2, key="Short-run total cost",
 parametric(x(KS0,L,a0,b0,c0,e0),r0*KS0+w0*L,L,.1,1.2*L2),
 key="Long-run total cost",color=red, parametric(xaug(L,w0,
   r0,a0,b0,c0,e0),ltcaug(L,w0,r0,e0),L,.1,.99*L2))$

The graph below converts the total cost functions to their
per-unit counterpats, average cost and marginal cost.

At the tangency point of the total cost curves, sac = lac,
because the total values are the same. Also, smc = lmc,
because the slopes of the total curves are the same.

For lower values of x, smc < lmc because the cost of 
capital is a fixed cost.

wxdraw2d( user_preamble="set key bottom right",
 xlabel="x",ylabel="$ per unit",yrange=[0.5*ac2,1.1*ac2],
 points_joined=true, points([xS0,xS0],[0, tcS0/xS0]),
 key = "Long-run average cost",parametric(
 xaug(L,w0,r0,a0,b0,c0,e0),ltcaug(L,w0,r0,e0)/
   xaug(L, w0, r0, a0, b0, c0,e0), L, .1, .99*L2), 
key="Short-run average cost",color=red,
 parametric( x(KS0, L, a0, b0, c0, e0), 
  sac(KS0, L, a0, b0, c0, e0, w0, r0),L,.1, 1.2*L2), 
 color=black,key="Long-run marginal cost",line_width=2,
 parametric( xaug(L, w0, r0, a0, b0, c0, e0), 
   lmc(L, w0, r0, a0, b0, c0, e0), L, .1, .99*L2),
 color=green,key="Short-run marginal cost",line_width=2,
  parametric( x(KS0, L, a0, b0, c0, e0), 
   smc(KS0, L,a0,b0,c0,e0,w0), L, .1, L2),
 dimensions=[480,480]  )$

## Long-run and Short-run Costs Using Isoquants

Isoquants provide another view of the connection between short-run and 
long-run costs. The next cell defines the values required to draw 
three isoquants. It also provides the slope of each isoquant when K 
can be varied to minimize the cost of producing each quantity.

[L3,L4,L5]:[5,10,15]$  [K3,K4,K5]:float(m0*[L3,L4,L5])$
x3:x(m0*L3,L3,a0,b0,c0,e0)$ x4:x(m0*L4,L4,a0,b0,c0,e0)$
x5:x(m0*L5,L5,a0,b0,c0,e0)$ slope:mrts(m0*L3,L3,e0)$
matrix(["slope=",slope]);   matrix(["L","K","x"],
 [L3, K3, x3], [L4, K4, x4], [L5, K5, x5] );

The graph below uses these values. The middle isoquant is for 
$x = 1298.1$ units. The line segment tangent to it is the 
isocost line that shows the minimum cost of producing
1298.1 units. The left-most isoquant depicts ways to produce
$x = 337.44$ units. The least-cost way of doing this is 
to use the $K, L$ pair indicated by the point of tangency 
with the lowest isoquant. In the shujort run, the firm employs
too much capital. Cost minimization cannot occur on the isoquant
at the right because the firm cannot get enough capital in the 
short run.

wxdraw2d(xlabel="L", ylabel="K", yrange=[0,(r0*K5+w0*L5)/r0], 
 key = "x=x3 isoquant and isocost line",
 implicit(x(K,L,a0,b0,c0,e0) =x3, L,.1,1.2*L5,K,.1,1.5*K5),
 key="",
 explicit( (r0*K3+w0*L3)/r0+slope*L,L,0,(r0*K3+w0*L3)/w0),
 color=red, key="x = x4 isoquant and isocost line",
 implicit(x(K,L,a0,b0,c0,e0)=x4,L,.1,1.2*L5,K,.1,1.5*K5), 
 key = "",
 explicit((r0*K4+w0*L4)/r0+slope*L,L,0,(r0*K4 +w0*L4)/w0), 
 color=black, key="x = x5 isoquant and isocost line",
 implicit(x(K,L,a0,b0,c0,e0)=x5,L,.1,1.5*L5,   K,.1,2*K5),
 key = "",
 explicit((r0*K5+w0*L5)/r0+slope*L,L,0,(r0*K5 + w0*L5)/w0), 
 line_type=dots, key = "K = K4", explicit(K4,L,0,1.5*L5)   )$

To determine the cost when the isoquants are not tangent to the 
isocost lines, use find_root( ). Set the production function, with 
K properly constrained, equal to the value of x along each isoquant 
and evaluate that expression. The table shows the required L values 
and the implied short-run costs. The long-run values are repeated 
for comparison.

L3s:find_root(x(K4,L,a0,b0,c0,e0)=x3,L,0,(r0*K5+w0*L5)/w0)$
L5s:find_root(x(K4,L,a0,b0,c0,e0)=x5,L,0,(r0*K5+w0*L5)/w0)$
matrix(["x","K,LR","L,LR","K,SR","L,SR","Cost,LR",
"Cost,SR"],[x3,K3,L3,K4,L3s,r0*K3+w0*L3,r0*K4+w0*L3s],
[x4, K4, L4, K4, L4, r0*K4 + w0*L4, r0*K4 + w0*L4],
[x5, K5, L5, K4, L5s, r0*K5 + w0*L5, r0*K4+w0*L5s] );

## Fixed and Variable Short-run Costs

In the short run the firm must incur fixed per-period costs

The two curves below show the total cost and the total variable cost. 
The vertical distance between the two, a constant, is the 
firm's total fixed cost.

wxdraw2d( user_preamble="set key bottom right",xlabel="x",
 ylabel = "$", yrange = [0, r0*KS0 + w0*1.25*L2],
 points_joined=true, points([[xS0,0],[xS0, tcS0],[0,tcS0] ]),
 line_width=2, key="Short-run total cost",
 parametric(x(KS0,L,a0,b0,c0,e0),r0*KS0+w0*L,L,.1,1.2*L2),
 color=red,key="Total variable cost",
 parametric( x(KS0,L,a0,b0,c0,e0),w0*L,L, .1,1.2*L2)   )$

The data in the graph above can be stated in per-unit terms. The 
result is the four curves below. 

wxdraw2d(xlabel="x",ylabel="$ per unit",yrange=[0,2*ac2],
 line_type=dots,points_joined=true, xtics={xS0},
 points([[xS0,0],[xS0, tcS0/xS0],[0, tcS0/xS0]]),
 line_type=solid,line_width=2, key = "Average variable cost", 
 parametric(x(KS0,L,a0,b0,c0,e0),sac(KS0,L,a0,b0,c0,e0,w0,r0)-
   r0*KS0/x(KS0,L,a0,b0,c0,e0),L,.1,L2),
 color=red,key="Short-run average cost",
 parametric( x(KS0,L,a0,b0,c0,e0),sac(KS0,L,a0,b0,c0,
   e0,w0,r0),L,.1,1.2*L2),
 color=black,key="Short-run Marginal cost", 
 parametric(xaug(L,w0,r0,a0,b0,c0,e0),
    smc(KS0,L,a0,b0,c0,e0,w0),L,.1,L2),
 key="Average fixed cost",color=orange,   
 explicit(r0*KS0/x,x,1,x2), dimensions=[480,480]  )$

## Long-run Average Cost as a Planning Curve

The next graph shows two sac curves. If only these two options 
were available, the the firm's lac curve would consist of the 
scalloped curve that consists of part of "sac, K = KS1" and 
then part of "sac, K = KS1".

KS1: Klist[3];  wxdraw2d(xlabel = "x", 
 ylabel="$ per unit",yrange=[.8*ac2,1.5*ac2],
 xrange= [0,1.1*xlist[10]], line_width=2,
 xtics={20000,40000,60000},key="sac, K = KS0",
 parametric( x(KS0,L,a0,b0,c0,e0),sac(KS0,
    L,a0,b0,c0,e0,w0,r0),L,.1,1.2*L2), 
 key="sac, K = KS1",color=red,
 parametric(x(KS1,L,a0,b0,c0,e0),
    sac(KS1,L,a0,b0,c0,e0,w0,r0),L,.1,1.2*L2)    )$

TeX: As the number of options increases, the scalloping becomes less
pronounced, and the envelope of all of the relevant segments of
various short-run average cost curves begins to look like the long-run 
average cost curve below. In the extreme, if capital employment were
actually infinitesimally variable, then each point on the long-run curve 
would consist of a single point on the relevant short-run curve. 

wxdraw2d(xlabel="x",ylabel="$ per unit",
  yrange=[.8*ac2,2*laclist[3]],xrange=[0, 1.1*xlist[10]],
 color=black,line_type=dots, points_joined=true,
 points([[xlist[3],0],[xlist[3],laclist[3]],[0,laclist[3]]]),
 points([[xlist[7],0],[xlist[7],laclist[7]], [0,laclist[7]]]),
 line_type=solid, color=blue, line_width=2,key="sac, K = KS0",
 parametric( x(KS0,L,a0,b0,c0,e0),
     sac(KS0,L,a0,b0,c0,e0,w0,r0),L,.1,1.2*L2), 
 key="sac, K = KS1", color = orange, 
 parametric(x(KS1,L,a0,b0,c0,e0),
     sac(KS1,L,a0,b0,c0,e0,w0,r0),L,.1,1.2*L2),
 color=black,key="Long-run average cost",
 parametric( xaug(L,w0,r0,a0,b0,c0,e0),
   ltcaug(L,w0,r0,e0)/xaug(L,w0,r0,a0,b0,c0,e0),L,.1,.9*L2),
 dimensions=[480,480] )$