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

# General Equilibrium

This workbook models an economy that consists of two 
industries that consist of price-taking firms. Each firm in
each industry employs two resources, K and L, and the total 
supply of both K and L are fixed (independent of the price
paid per unit). K and L are owned by two persons (or groups
of identical persons), A and B, each of who consumes both 
X and Y, the products of the price-taking firms. 

Beginning with production, the model highlights the nature
of efficiency and shows that the competitive equilibrium 
coincides with the efficient outcome. The model generates
the supply curve of X (which implies, via the Cobb-Douglas 
production functions, the supply of Y). 

Next, a model of consumption is developed. Again, both efficiency
and equilibrium are considered. A demand curve for good X is 
developed (which, given fixed K and L, and input prices, implies
a demand for Y).

Finally, full equilibrium is developed. Among other results, the
model confirms that total income equals the total market value of
the goods, a result that is developed in introductory macroeconomics.

## Production

Two goods are produced using K and L. Production is governed by 
good-specific Cobb-Douglas production functions:
    XT = A*LX^a*KX^(1-a) and
    YT = B*LY^b*KY^(1-b).
Note that the X and Y name the goods and T indicates total production.
Later XT and YT will be distributed between two (groups of) consumers
so that:    
    XT = XA + XB and YT = YA and YB, where A and B are the consumers.
In like fashion, the resources are allocation between X and Y:
    LT = LX + LY and KT = KX + KY.

### The Edgeworth Production Box

When the number of goods and the number of resources are 2, the
Edgeworth Production Box provides a useful graphical representation
of production.

The graph below shows the ways that LT = 700 and KT = 50 can
be allocated between X and Y. The upper righthand corner of the
box indicates that LX = LT and KX = KT, so YT = 0. The origin
shows the opposite, where XT = 0. The dot shows allocation
LX = LT0 = 350, so LY (not shown explicitly) equals 350. KT
is allocated thusly: LX = LX0 = 40, so KY = KT0 - 40 = 10.

Note the label for the Y origin and its placement.

[LT0,KT0,LX0,KX0]:[700,50,350,40]$  /*Resource endowments & 
        initial allocations */
EBox : if LX < LT0 then KT0 else 0$ /*Edgeworh Box expression*/
wxdraw2d(xlabel="Labor, LX",ylabel="Capital, KX",
 user_preamble="set key top left",xrange=[0,1.2*LT0],
 yrange=[0,1.2*KT0],point_type=circle,points([LX0],[KX0]),
 key="Edgeworth Box",line_width=2,explicit(EBox,LX,0,LT0),
 color=black,label(["Good Y origin",LT0,1.05*KT0]))$

### Initial Production Levels and Isoquants

The next cell specifies the production function parameters.
The values of a and b are chosen so that one good is much
more labor intensive than the other. This contrast is useful
for drawing the graphs throughout the workbook.

The top of the matrix below shows the initial values of XT 
and YT, given the data above. The bottom of the matrix shows
expressions for the isoquants consistent with XT0 and YT0.
The dependent variable in both expression is KX. KY values 
are deduced from the identity KY = KT0 - KX. Likewise, rather
than LY the expression is stated in terms of 700 - LX (LT0 = 700).

[A,a]:[10,0.75]$ XT : A*LX^a*KX^(1-a)$
[B,b]:[5,0.25]$ YT:B*(LT0-LX)^b*(KT0-KX)^(1-b)$
[XT0, YT0] : [XT, YT], LX = LX0, KX = KX0$
 matrix(["XT0", "YT0"], [XT0, YT0],["- - - - -",
"- - - - -"],["IsoX0 expression","IsoY0 expression"],
[IsoX0 : (XT0/(A*LX^a))^(1/(1 - a)),
IsoY0 : KT0-(YT0/(B*(LT0-LX)^b))^(1/(1-b))] );

The graph shows the initial isoquants.

wxdraw2d ( xlabel="Labor, LX",ylabel="Capital, KX",
 xrange = [0, 1.25*LT0], yrange=[0, 1.2*KT0],
 point_type=circle,point_size=2,points([[LX0, KX0]]),
 line_width=2,explicit(EBox,LX,0,1.01*LT0),  
 key = "XT0 isoquant, refer to X origin", 
 explicit(IsoX0, LX, 0.01*LT0, LT0),color=red, 
 key = "YT0 isoquant, refer to Y origin", 
 explicit(IsoY0, LX, 0.01*LT0, .99*LT0),
 dimensions=[480,480] )$

### A More Efficient Allocation

The last two input lines define two new expressions. These are used
subsequently to graph the isoquants that correspond to these two output 
rates. A table is added to compare the initial and new values of LX and 
KX.

MPLX:diff(XT,LX)$  MPKX:diff(XT, KX)$
MPLY : -diff(YT, LX)$ MPKY : -diff(YT, KX)$
[LX1,KX1]:[500,30]$
matrix(["","LX","KX"],["Initial",LX0,KX0],["New",LX1,KX1]);   

matrix(["", "Good X", "Good Y"],
 ["- - -","- - -", "- - -"],
 ["SlopeFunctions:", 
    Xslope: MPLX/MPKX, Yslope:MPLY/MPKY],
 ["SlopeValues:",subst([LX=LX0,KX=KX0],Xslope),
    subst([LX=LX0,KX=KX0],Yslope)],
 ["New X and Y Values:",  XT1: subst([LX=LX1,KX=KX1],XT),
    YT1: subst([LX=LX1,KX=KX1],YT)],
 ["X and Y Output Change ",XT1-XT0,YT1-YT0] );
/*Define isoquant functions for later use*/
  IsoX1 : (XT1/(A*LX^a))^(1/(1 - a))$
  IsoY1 : KT0 - (YT1/(B*(LT0-LX)^b))^(1/(1-b))$

### The Production Contract Curve

Setting the slopes of the two isoquants equal to each other and
solving for KX provides an expression for the contract curve, which
appears before the graph. 

The wxdraw2d( ) commands differ from those in the text in minor 
respects. First, the colors are and dimensions are changed. Second,
EBox is moved to the end. The latter uses the fact that draw( ) 
imposes later graphs over earlier ones.

solve(Xslope = Yslope, KX); KXCC : rhs(%[1])$
wxdraw2d(dimensions=[480,480],
 xlabel="Labor, LX",ylabel="Capital, KX",
 xrange=[0, 1.2*LT0],yrange=[0,1.2*KT0],point_type=circle, 
 point_size=2, points([[LX0,KX0]]),points([[LX1,KX1]]),
 color=blue,key="XT isoquants",explicit(IsoX0,LX,0.01*LT0,LT0),
 key = "",explicit(IsoX1,LX,0.01*LT0,LT0),color=red,
 key="YT isoquants",explicit(IsoY0,LX,0.01*LT0,.99*LT0), 
 key = "", explicit(IsoY1, LX, 0.01*LT0, .99*LT0), 
 color=black,color=yellow,key="Contract Curve", 
 explicit(KXCC, LX, 0, LT0),key="",
 color=black,line_width=2,explicit(EBox,LX,0,1.01*LT0)      )$

The next cell identifies the endpoints of the segment
of the contract curve that lie inside the initial lens.
At any point on that line, both XT and YT increase 
due to a movement from the initial point to the 
contract curve. Elsewhere, either XT or YT decreases.

LowerLX: find_root(KXCC - IsoX0, LX, 400, 700)$
UpperLX: find_root(KXCC - IsoY0, LX, 500, 699)$
matrix(["Lower LX value", "Upper LX value"],
[LowerLX, UpperLX ]);   kill(LowerLX, UpperLX)$

### Contract Curve and Production Possibilities

Any point on the contract curve corresponds to an efficient resource 
allocation. Accordingly, we can use the contract curve to generate a 
production possibilities curve (PPC).

Specify an LX value, LXCC0,  on the contract curve. Then
create two new variables, XTS is the amount of X supplied, given
LX and the amount of KX implied by KXCC. YTS is the amount of Y 
determined likewise. 

For specific values, XTS0$ and $YTS0, evaluate these expressions for LXCC0. 

The last command defines maxXT0, the amount X that would result if 
all resources were allocated to X,and its Y counterpart, maxYT0.

LXCC0:580$  KXCC0:float(subst(LX=LXCC0,KXCC))$
XTS: A*LX^a*KXCC^(1-a)$
YTS:B*(LT0-LX)^b*(KT0-KXCC)^(1-b)$
[XTS0,YTS0]: subst(LX=LXCC0,
  [XTS,YTS:B*(LT0-LX)^b*(KT0-KXCC)^(1-b)])$
matrix(["LXCC0:", LXCC0], ["KXCC0:",KXCC0],
 ["XTS Expression:",XTS],["YTS Expression:",YTS],
 ["XTS value:",XTS0],["YTS value:",YTS0]); 
IsoXCC0 : (XTS0/(A*LX^a))^(1/(1 - a))$
IsoYCC0:KT0-(YTS0/(B*(LT0-LX)^b))^(1/(1-b))$

matrix(["Maximum X", "Maximum Y"],
[maxXT0:subst([LX=LT0, KX = KT0],XT),
maxYT0:subst([LX=0,KX =0],YT)]);

The next two graphs show the relationships above. The first
graph is expressed in terms of inputs, with the output levels
being the values on isoquants. All curves are based on 
explicit functions of LX.

wxdraw2d(xtics={0,LT0/2,LT0},xlabel="Labor, LX", 
 ylabel="Capital, KX",xrange=[0,1.2*LT0], 
 yrange = [0, 1.2*KT0],key="XT isoquants",
 line_width=2, explicit(IsoX0, LX, 0.01*LT0,LT0),
 key="",explicit(IsoXCC0,LX,0.01*LT0,LT0),
 key="YT isoquants",color=red,
 explicit(IsoY0,LX,0.01*LT0,.99*LT0),key="",
 explicit(IsoYCC0, LX, 0.01*LT0, .99*LT0), 
 color=black,color=yellow,key="Contract Curve",
 explicit(KXCC, LX, 0, LT0),key="Edgeworth Box",
 color=black,explicit(EBox,LX,0,1.01*LT0))$

The second graph shows the same relationships but they
appear in terms of XT and YT. The production possibilities
curve relates YT to XT but does so via the parametric 
relationship of each variable to LX.

wxdraw2d(xlabel="Good X",ylabel="Good Y",
 xtics={0,maxXT0/2,maxXT0},key="Initial Output",
 point_type=circle,point_size = 2, 
 points([[XT0, YT0]]), key = "Efficient Output",
 point_type=diamant, points([[XTS0, YTS0 ]]) ,
 line_width=2,color=black,key="Production Possibilities",
 parametric(XTS,YTS,LX,0, LT0), dimensions=[480,480]  )$

### Efficiency and Resource Prices

\subsection{
\nopagebreak 
So far, the discussion of efficiency has been technical,
specifying the condition that must be met if efficiency is to be achieved.
The technical condition has implications for an important part of an 
economy, the relative cost of resources (or, from another perspective, 
the relative income of resource owners). Competitive firms achieve
minimum cost production when $MPL/MPK = w/r$, where $MPL$ and 
$MPK$ are the marginal products of labor and capital in the production
of the firm's output and w/r is the ratio of the wage rate to the per-unit
cost of employing capital. 

In a competitive economy all firms face the same values for $w$ and 
$r$, so all firms have the same $MPL/MPK$. They will not generally 
have the same value of either $MPL$ or $MPK$; it
is the ratio that will be common to all firms. 

Consider the case above.
$LXCC0$ is specified and the amounts of $KX$, $LY$, and $KY$ are
determined, consistent with the ratio of marginal products being the same
in both sectors. The cell below shows the value of $w/r$ consistent
with the values above. First, compute $KXCC0$, the amount of 
capital used to produce $X$. Once that is determined, compute the 
slopes of the two isoquants. Both slopes equal the same value, 0.0904. Finally,
assign one of these two values the name \texttt{wrRatio0}, which will be used
subsequently. 

[Xslope0,Yslope0]:subst([LX=LXCC0,KX=KXCC0],
  [Xslope,Yslope])$  wrRatio0: Xslope0$   
matrix(["X Isoquant Slope", 
"Y Isoquant Slope"], [Xslope0, Yslope0]);

The graph below incorporates w/r into the analysis. For firms that produce 
good w/r is the slope of an isocost line, viewed from the X-origin. 
For firms that produce good Y, w/r is the slope of an isocost line, viewed 
from the Y-origin.

wxdraw2d( xtics={0,LT0/2,LT0},xlabel="Labor, LX", 
 ylabel="Capital, KX", xrange=[0,1.2*LT0],
 yrange=[0,1.55*KT0], color=black,line_width=2,
 explicit(EBox,LX,0,1.01*LT0),key="XTS0 isoquant",
 color=blue,explicit(IsoXCC0,LX,.01*LT0,LT0),color=red,
 key="YTS0 isoquant",explicit(IsoYCC0,LX,0.01*LT0,.99*LT0),
 key="IsoCost Line",color=orange, 
 explicit(wrRatio0*LXCC0+KXCC0-wrRatio0*LX,LX,0,1.5*LT0),
 color=brown, key = "Contract Curve",
 explicit(KXCC,LX,0,LT0),dimensions=[480,480])$

## Relative Product Prices

This section develops the relationship between relative input
prices and relative product prices.

### Marginal Products and Marginal Costs

delta(XT) = MPLX*delta(LX)    + MPKX*delta(KX), and 
delta(YT) = MPLY*(-delta(LX)) + MPKY*(-delta(KX)).

Let delta(KX) = 0. The marginal cost of X is MCX = w/MPLX, and MCY = w/MPLY,
so the relative marginal cost is MCX/MCY = MPLY/MPLX. In competitive markets
p = MC, so PX/PY = MPLY/MPLX. 

We set PY = 1 throughout the analysis, so that PX = PX/PY = MCX/MCY. 
Next, determine r, the rental price of K: MCY = PY = 1 =r/MPKY, so r = MPKY.
Likewise w = MPLY. (Y is a numeraire, so all prices are Y-unit equivalents.) 

These values, given LXCC0, appear below.

KXCC0 : subst([LX = LXCC0], KXCC)$
[MPLXCC0, MPKXCC0, MPLYCC0,MPKYCC0 ]: 
 subst( [LX=LXCC0,KX=KXCC0],[MPLX,MPKX,MPLY,MPKY])$
PX0: MPLYCC0/MPLXCC0$   PY:1.0$    w0: MPLYCC0$     
r0:MPKYCC0$  matrix(["MPLX","MPLY","MPKX","MPKY"],
  [MPLXCC0,MPLYCC0,MPKXCC0,MPKYCC0] );  matrix(
  ["PX0","PY","w0","r0"],[PX0,PY,PX0*MPLXCC0,PX0*MPKXCC0]);

## Product Value and Resource Cost

We now determine the economy's GDP or, equivalently, its GDI, given
the values used so far. GDP = YT + PX*XT and GDI = w*LT + r*KT.

The first table below shows the output levels and the implied 
GDP and GDI values, which are equal. The second table shows that 
PX*XT = the cost of producing XT, and YT = the cost (stated in Y units)
of producing the output. In these perfectly competitive industries 
that consist of identical firms, no profits or losses exist at equilibrium.

[XTS0,YTS0] : subst([LX = LXCC0, KX = KXCC0],[XT,YT])$
GDP0: PX0*XTS0 + YTS0$        GDI0: w0*LT0 + r0*KT0$
matrix(["X Level", "Y Level", "GDP0", "GDI0"], 
    [ XTS0, YTS0, GDP0, GDI0 ]);
matrix(["Xvalue", "Xcost", "Yvalue", "Ycost" ], 
[PX0*XTS0, w0*LXCC0+r0*KXCC0, YTS0, 
    r0*(KT0 - KXCC0) + w0*(LT0 - LXCC0) ] );

The graph shows that the IsoValue line with slope = -PX0
is tangent to the production possibilities curve at the
values derived above.

wxdraw2d( xrange=[0, 1.1*maxXT0],yrange=[0,1.1*GDP0],
 xlabel="Good X",ylabel="Good Y",
 xtics={0,maxXT0/2,maxXT0}, key="Production Possibilities",
 line_width=2,parametric(XTS,YTS,LX,0,LT0),
 key="IsoValue",color=black,
 explicit(GDP0-PX0*X,X,0,1.1*maxXT0),
 color=black,line_type=dots,key="",points_joined=true,
 line_width=1,points([[XTS0,0],[XTS0,YTS0]]),
 points([[0,YTS0],[XTS0,YTS0]]),dimensions=[480,480])$

### The Supply Curve for Good X

We generalize the results above by expressing the relationship
between the contract curve and the inverse supply curve for X.

[MPLXCC,MPLYCC]: subst(KX=KXCC,[MPLX,MPLY])$
PXCC : MPLYCC/MPLXCC;

The graph below shows the supply curve. A logical function 
is used to show the result of LX = LX0. We could have used
a points( ) command instead.

PX_QX:if XTsup < XTS0 then PX0 else 0$
wxdraw2d(xlabel="Good X (XT)",ylabel="$ per unit",
 user_preamble="set key bottom",
 line_width=2, key="Supply Curve",
 parametric(XTS,PXCC,LX,.01*LT0,.99*LT0),
 key="Point on Supply Curve",line_type=dots,
 color=black,line_width=1,
 explicit(PX_QX, XTsup, 0, XTS0) )$

### Finding LX, given PX

Because LX serves as the parameter in the functions above,
we restate the inverse supply curve as it relates to LX.
Beware: The graph below is not a supply curve.

The graph below differs from that in the text in that 
it is in terms of PX, not PX - PXset. Nothing of substance
depends on which way the information is presented.

/* Select a price */ PXset : 0.125$      
wxdraw2d(xlabel="LX", ylabel = "PX",
   xaxis=true,line_width=2, key=
"Not a Supply Curve--relates PX to LX",
 explicit(PXCC, LX, 0, LT0),line_width=1,
 color=black,key=concat("P = ",string(PXset)),
 explicit(PXset,LX,0,LT0),dimensions=[540,480])$

Once this value of LX, named LXset here, is determined, it can be 
inserted into LXCC0 to calculate the approximate values of XT, YT, 
w, r, GDP, GDI. That is, if PX is determined outside of the economy,
then the model is complete, except for the distribution of the 
output among the owners of resources L and T.

LXset : find_root(PXCC - PXset, LX, 400, 600)$
matrix(["PXset", "LXset"], [PXset, LXset]);

## Distributing Output: Consumption

The point that we have reached on the PPC defines an XT,YT pair.
These values can be used to define an Edgeworth Consumption Box,
which guides the examination of product distribution beween two
consumers. This examination reveals an added criterion for 
overall economic efficiency.

Like its production counterpart, the Consumption Edgeworth Box
is graphed by applying a logical (if...then...else) expression.
A points( ) command could be used. 

wxdraw2d( xlabel="Good X",ylabel="Good Y",
 xtics={0, maxXT0/2,maxXT0}, line_width=2,
 key="Production Possibilities",
 parametric(XTS,YTS,LX,0,LT0), color=black,
 key="Consumption Edgeworth Box",
 explicit(if XTsup<XTS0 then YTS0 else 0, XTsup,0,XTS0))$

### Distributing Output

As with production, begin with an arbitrary distribution
of X and Y between consumers A and B. XA indicates consumer
A's quantity of X; XB, YA, and YB are interpreted in a 
similar fashion.

[XA0,YA0]:[1500,75]$  [XB0,YB0]:[(XTS0-XA0),(YTS0-YA0)]$       
matrix( ["XT","YT","XA","YA","XB","YB"],
    [XTS0,YTS0,XA0,YA0,XB0,YB0]);

The next cell depicts this arbitrary distribution 
using the Consumption Edgeworth Box.

wxdraw2d(xlabel="Good X",ylabel="Good Y",
 xrange=[0, 1.25*XTS0],yrange =[0,1.25*YTS0],
 key = "arbitrary distribution",points([XA0],[YA0]),
 color= black, label(["Brooks origin",XTS0,1.05*YTS0]),
 key="Consumption Edgeworth Box",line_width=2,
 explicit(
  if XTsup < XTS0 then YTS0 else 0,XTsup,0,XTS0),
 dimensions=[480,440])$

### Indifference Curves

In order to construct indifference curves, we determine the utility
levels of A and B, based on the following Cobb-Douglas specifications.

[c,d]:[0.3,0.7]$   
[UA,UB]:[XA^c*YA^(1-c),(XB)^d*(YB)^(1-d)]$
matrix( ["UA","UB","UA0","UB0"],
  [UA,UB,UA0:XA0^c*YA0^(1-c), UB0:XB0^d*YB0^(1-d)]);

The two indifference curve expressions appear below. As with
production, the expressions appear in terms of XA, with XB
being determined by XB = XT - XA. 

/*Indifference curves */ [ICA0 : (UA0/XA^c)^(1/(1-c)),
ICB0 : YTS0-(UB0/(XTS0-XA)^d)^(1/(1-d))];

As with the arbitrary allocation of LT and KT, this 
arbitrary distribution of XT and YT creates a lens-
shaped area that defines possible advantageous redistribution
of the two goods between A and B.

A point is added to emphasize the location of B's origin.

wxdraw2d( xlabel="Good X",ylabel="Good Y",
 xrange=[0,1.25*XTS0],yrange=[0,1.4*YTS0 ],
 point_type=filled_square, points([XA0],[YA0]), 
 color=black,line_width = 2, key="B origin",
 points([[XTS0,YTS0]]),
 key="Consumption Edgeworth Box", 
 explicit(if XTsup<XTS0 then YTS0 else 0,XTsup,0,XTS0), 
 color=blue,   key ="ICA0, refer to SW origin",
 explicit(ICA0, XA, .01*XTS0, .99*XTS0),
 line_width=2,color= red,key="ICB0, refer to NE origin",
 explicit(ICB0,XA, .01*XTS0, 0.99*XTS0),
 dimensions=[480,480]     )$

### The Consumption Contract Curve

Efficiency requires that the slopes of A's indifferences
curve equals that of B. The first two lines of output
report expressions for marginal utility: MUXA, MUYA, MUXB,
and MUYB. The third output line shows the results of 
imposing the efficiency condition; it yields the amount
of YA that the contract curve dictates given XA.

[MUXA:diff(UA,XA),  MUYA:diff(UA,YA)];
UB:(XTS0-XA)^d * (YTS0-YA)^(1-d)$
[MUXB: -diff(UB,XA),  MUYB: -diff(UB,YA)]; 
YACC:rhs((float(solve(MUXA/MUYA=MUXB/MUYB,YA)))[1]);

The figure below graphs YACC, along the initial indifference 
curves.

wxdraw2d (xlabel="Good X",ylabel="Good Y",
 xrange = [0, 1.25*XTS0], yrange =[ 0, 1.4*YTS0 ],
 points([XA0],[YA0]),key="",line_width=2, color=black,
 explicit(if XTsup < XTS0 then YTS0 else 0,XTsup,0,XTS0),
 key="ICA0, refer to SW origin", color=blue,
 explicit(ICA0, XA, .01*XTS0, .99*XTS0),color=gray, 
 key = "ICB0, refer to NE origin", color=red,
 explicit(ICB0, XA,.01*XTS0,.99*XTS0), 
 key = "Consumption Contract Curve",color=orange,
 explicit(YACC, XA, 0, .99*XTS0))$

The output below show the values of XA at which YACC intersects 
A's and B's initial indifference curves. 

[find_root(ICA0 - YACC, XA, 200, 600),
find_root(ICB0 - YACC, XA, 500, 1500)];

We arbitrarily select the value XA = 600, determine A's and 
B's new utility levels given that YA = YACC, and 
add the associated indifference curves to the previous graph.

XA1:600$  YACC1: subst(XA = XA1,YACC)$
[UA1,UB1]: subst([XA=XA1,YA=YACC1],[UA,UB])$
matrix(["UA0","UA1","UB0","UB1"],[UA0,UA1,UB0,UB1]);
matrix(["ICA1:",ICA1 :(UA1/XA^c)^(1/(1-c)),"   ICB1:",
ICB1 : YTS0 - (UB1/(XTS0 - XA)^d)^(1/(1-d))]);

wxdraw2d (xlabel="Good X",ylabel="Good Y",
 xrange=[0,1.25*XTS0],yrange=[0, 1.8*YTS0],
 point_type=filled_square,points([XA0],[YA0]),key = "", 
 color=black,line_width=2, 
 explicit(if XTsup<XTS0 then YTS0 else 0,XTsup,0,XTS0),
 color=blue,key ="ICA0, refer to SW origin",
 explicit(ICA0,XA,.01*XTS0,.99*XTS0),color=red,
 key="ICB0, refer to NE origin",
 explicit(ICB0, XA, .01*XTS0, .99*XTS0), color=orange, 
 key = "Consumption Contract Curve",
 explicit(YACC, XA, 0, .99*XTS0),color=black,
 key ="ICA1, refer to SW origin",
 explicit(ICA1,XA,.01*XTS0,.99*XTS0),
 color=brown,key="ICB1, refer to NE origin",
 explicit(ICB1, XA, .01*XTS0, .99*XTS0),
 dimensions=[480,480]   )$

## Demand and Equilibrium

To complete this model, making PX endogenous, this section
develops a model of demand. The next cell assigns ownership
of resources L and K to consumers A and B. These ownership
shares generate money income levels that are functions of 
w and r. (A unit of "money" is a unit of Y.)

Then, based on the fact that consumers with Cobb-Douglas 
utility functions spend a constant share of money income on 
each good, it derives individual and market demand curves.

The market demand curve, expressed in terms of LX,
which was not printed in the text, appears below.

[alpha0,beta0]:[0.7,0.5]$
[LA, KA] : [alpha0*LT0,beta0*KT0]$
[w,r] : subst([KX=KXCC,alpha=alpha0,%beta=beta],[MPLY,MPKY])$
[MA,MB]:[w*LA+r*KA,w*(LT0-LA)+r*(KT0-KA)]$
[XAD, XBD] : subst([alpha=alpha0,%beta=beta0],
  [c*MA/PXCC, d*MB/PXCC])$    XD : XAD + XBD;

The next cell graphically summarizes the values of some variables
as LX (and hence XT) increases. Be aware that the relationships 
in the last panel are not demand curves.

maxMT : subst(LX = 0.9999*LT0, MA + MB)$

inputPrices:gr2d(title="Input Prices",
 user_preamble="set key center",xrange=[0,LT0],
 xtics=200,xlabel="LX",ylabel="$ per input unit",
 line_width=2,key="w",explicit(w,LX,.01*LT0,.99*LT0),
 key="r",color=red,explicit(r, LX,.01*LT0,.99*LT0))$

xPrices:gr2d(xlabel="LX",ylabel="$ per unit of X",
 xtics=200,title="Product Price, PXCC",xrange=[0,LT0],
 line_width=2,explicit(PXCC,LX,.01*LT0,.99*LT0) )$

IncomeLevels:gr2d(xrange=[0, LT0],yrange=[0,maxMT],
 user_preamble="set key top left",
 title="Money Income Level",xtics=200,xlabel="LX", 
 ylabel="$",line_width=2,key="A",
 explicit(MA,LX,.01*LT0,.99*LT0),key="B",color=red,
 explicit(MB,LX,.01*LT0,.99*LT0),key="Total",
 color=black,explicit(MA+MB,LX,.01*LT0,.99*LT0) )$

Xvalues: gr2d(xrange=[0,LT0],xlabel="LX",
 user_preamble="set key top right",xtics=200,
 ylabel="X values",title="Quantity Demanded, X",
 line_width=2,key="A",explicit(XAD,LX,.01*LT0,.99*LT0), 
 color=red,key="B",explicit(XBD,LX,.01*LT0,.99*LT0),
 color=black,key="Total",explicit(XD,LX,.01*LT0,.99*LT0))$

wxdraw(inputPrices, xPrices, IncomeLevels, Xvalues,
 columns = 2, dimensions=[720,640])$

Drawing the demand and curves requires a parametric approach 

maxPX:subst(LX= .99*LT0,PXCC)$    
    
wxdraw2d(user_preamble="set key bottom", 
 xlabel="Good X",ylabel="$ per unit",
 yrange=[0,maxPX],key="Supply Curve",line_width=2,
 parametric(XTS,PXCC,LX,.01*LT0,.99*LT0),color=orange,
 key="Demand Curve A",parametric(XAD,PXCC,LX,.01*LT0,.99*LT0),
 color=red,key="Demand Curve B",
 parametric(XBD,PXCC,LX,.01*LT0,.99*LT0),color=black,
 key="Market Demand",parametric(XD,PXCC,LX,.01*LT0,.99*LT0),
 dimensions=[480,480])$

### subsection{Equilibrium Values

Once the quantity demanded of good X is determined, all other values 
can be deduced. We require a calculation of the value of XT at which
the demand and supply curves intersect. The find_root( ) command 
achieves this result.

LXE:find_root( XD-XTS,LX,LT0/100,LT0*.99);

Determine the equilibrium price and the quantities of the two goods 
by evaluating the relevant function given that LX = LXE. The 
equilibrium values are $PE = 0.129$, XTE = 1942.1 units, and 
YTE = 291.34 units.

[PXE,XTE,YTE]:subst(LX=LXE,[PXCC, XTS, YTS]);

The input price levels are determined the same way. These values 
are wE = $0.372 and rE = $5.6187.

[wE,rE]:subst(LX=LXE,[w, r]);

Determine GDP and GDI. GDP = GDI = $541.06 at equilibrium.

[GDPE:XTE*PXE+YTE, GDIE:wE*LT0+rE*KT0];

Determine the income levels of Anderson and Brooks,and 
confirm that these sum to GDP.

MAE: subst([alpha=alpha0,beta=beta0,LX=LXE],MA)$
MBE: subst([alpha=alpha0,beta=beta0,LX=LXE],MB)$
MtotalE : MAE + MBE$  matrix(["MAE","MBE","
  Total Income","GDPE"],[MAE,MBE,MtotalE,GDPE]);

Next, determine the equilibrium quantity demanded of each
good by each consumer and confirm that these sum to the 
equilibrium quantities.

[XAE,YAE]:[c*MAE/PXE,(1-c)*MAE]$ 
[XBE, YBE]:[d*MBE/PXE, (1-d)*MBE]$
matrix(["","X","Y"],["Anderson",XAE,YAE],
  ["Brooks",XBE,YBE], ["Total",XAE+XBE,YAE+YBE]);

Finally, determine Anderson's and Brooks's utility levels.

[UAE:XAE^c*YAE^(1-c), UBE:XBE^d*YAE^(1-d)];

### Summary of Specifications and Results

The first cell reports the input values and parameters used in 
this workbook

matrix(  ["ResourceEndowment", LT0, KT0, "", ""],
["ProductionParameters", A, a, B, b],
["Utility Parameters", c, d, "",""],
["OwnershipParameters", alpha0, beta0,"",""]);

The next cell shows the equilibrium values.

matrix(["wE","rE","MAE","MBE","GDIE"], 
  [wE,rE,MAE,MBE,GDIE] ,["--","--","--","--","--"],
["PXE","XTE","YTE","GDPE"," "], 
  [PXE, XTE,YTE,GDPE," "],["--","--","--","--",""],
["XAE","YAE","XBE","YBE" ," "],  
  [XAE, YAE, XBE, YBE," "],["--","--","","",""],
  ["UAE", "UBE"," "," "," "], [UAE, UBE," "," "," "]);