# Equilibrium, Shifts, and Elasticities

Write equations for an inverse demand curve and an inverse supply curve. 
Use these commands:
kill(all)$ fpprintprec:5$ ratprint:false$
pd(x, i, A):= A*x^-2.5*i^0.8;
ps(x, w, B):= B*sqrt(x*w);
The first line of commands is not required, but can help subsequently.

/*Input. Execute (ctrl-enter) for results */
kill(all)$ fpprintprec:5$ ratprint:false$
pd(x, i, A):= A*x^-2.5*i^0.8;
ps(x, w, B):= B*sqrt(x*w);

Tell Maxima to assume that the following are positive: i, p, A, B, and x.
Solve the expression p - pd(x,i,A) for x to derive the demand curve.

Only the last of the list of solutions is relevant. What is the price
elasticity of demand for this product? The income elasticity?

/*Input. Execute (ctrl-enter) for results */
assume(i>0,p>0,A>0,B>0,x>0)$
solve(p - pd(x,i,A),x);

## Equilibrium

In this case, though not in general, we can use Maxima to generate an expression 
for xeq, the equilibrium value of x. Begin with solnx: solve(pd(x,i,A)=ps(x,w,B),x);.
First, however, assume(x>0); is useful in finding this conclusion.

/*Input. Execute (ctrl-enter) for results */
solnx: solve(pd(x,i,A)=ps(x,w,B),x);

The third solution is the only real one, so retrieve the value of x from
the list and bind it to the name xeq. We use this below to check the
numerical values.

/*Input. Execute (ctrl-enter) for results */
xeq: rhs(solnx[3]);

At equilibrium a one percent income increase causes the quantity
to increase by 4/15 of one percent, and a one percent wage 
increase causes quantity to decrease by 1/6 of one percent.

Determine the equibrium price using the inverse demand curve and again
using the inverse supply curve. 

/*Input. Execute (ctrl-enter) for results */
peq:pd(xeq,i,A); 
/*
Assigning a name to this expression is not 
required. It is not used below.
*/
ps(xeq,w,B);

Confirm that the two expressions are equivalent.

/*Input. Execute (ctrl-enter) for results */
float([2/15,5/12,1/6,5/6]);

Draw the demand and supply curves. The default color is blue,
so make the second curve that you enter black. Enter the
appropriate key entries. Label the x axis "x" and the y axis
"$ per unit". Set the y range to [0, 5] (why?). 

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(yrange=[0,5], xlabel="x",ylabel=
"$ per unit",key="demand",
 explicit(pd(x,10,120),x,0,15),color=black,
 key="supply", explicit(ps(x,2,.4),x,0,15))$

Use find_root to determine the equilibrium value
of x and bind that value to the name x1. Use both the 
demand equation and the supply equation to determine the
price, and label the resulting price p1. For this exercise,
let i = 10, A = 120, w = 0.1, and B = 200.

/*Input. Execute (ctrl-enter) for results */
x1:find_root(pd(x,10,120) -ps(x,2,.4),x,0.1,200);
[p1: pd(x1,10,120), ps(x1,2,.4)];
load(newton1)$
/*
find_root might be a better root finding option here.
Without float( ... ), newton produces a long exact 
representation of the value, even though the value itself
cannot be exact.
*/
float(newton(pd(x,10,120) -ps(x,2,.4),x,11.5,0.001));

Substitute the parameter values above into xeq and confirm
that the find_root solution and the analytical solution are
the same, to the available number of decimal places.

/*Input. Execute (ctrl-enter) for results */
float(subst([i=10,A=120,w=2,B=.4],xeq));

Use points([[0,p1],[x1,p1],[x1,0]  ] ) to mark the equiibrium quantity 
and price. Set points_joined = true and set line_type=dots to draw the 
reference lines to the axes.

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(yrange=[0,5],xlabel="Quantity",
 ylabel="$ per unit",key="demand",
 explicit(pd(x,10,120),x,0,15),color=black,
 key="supply", explicit(ps(x,2,.4),x,0,15), 
 key="equilibrium", points_joined=true, 
 line_type=dots, points([[0,p1],[x1,p1],[x1,0] ]))$

## Demand Shift

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(user_preamble="set key left",
yrange=[0,5],xlabel="Quantity",
ylabel="$ per unit",
key="demand",
explicit(pd(x,10,120),x,0,15),color=black,
key="supply",
explicit(ps(x,2,.4),x,0,15), line_width=2,
color=blue, key="new demand",
explicit(pd(x,15,120),x,0,15)
)$

/*Input. Execute (ctrl-enter) for results */
x2: find_root(pd(x,15,120)- ps(x,2,.4),x,0.1,20);
[p2:pd(x2,15,120), ps(x2,2,.4)];

/*Input. Execute (ctrl-enter) for results */
float(subst([i=15,A=120,w=2,B=.4],xeq));

Compute the arc price elasticity of supply over this range. What is the
exact value of the elasticicty of supply at every point on the supply 
curve?

/*Input. Execute (ctrl-enter) for results */
((x2-x1)/(x2+x1))/((p2-p1)/(p2+p1));

Assume that income is in hundreds of dollars per time period.
Calculate the fraction of income that consumers would spend 
on this good if p = 1.5 when i = 10 and when i = 15. Also
compute the arc income elasticity and compare it to the point
income elasticity. Explain how income elasticity relates to 
changes in share of income spent on the good when income 
changes. Let A = 120.

Assign the names xLow and xHigh to the two x values. Use
find_root, because the analytical solution provies a list 
of values and involves an additional step.

/*Input. Execute (ctrl-enter) for results */
xLow: find_root(1.5=pd(x,10,120),x,0.1,20);
xHigh: find_root(1.5-pd(x,15,120),x,0.1,20);
/*
Note that the two commands are entered in slightly
different, equivalent ways.

When income increase so does the quantity demaned at
this price. The share of income falls, however, from
1.8085% to 1.3727%.
*/
[xLow*1.5/1000, xHigh*1.5/1500];

/*
The change in the share of income spent on this good
when income increases reflects the fact that the 
income elasticity of demand is less that 1, so the
percentage change in x is less than the percentage
change in i.

The first command below computes the arc income
elasticity. The second command produces a floating-
point representation of the point elasticity, 8/25.
*/
((xHigh-xLow)/(xHigh+xLow))/(5/25);
float(8/25);

## Supply Shift

Add a second supply curve to show equilibrium when
w increases from 2 to 5. 

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(user_preamble="set key left",
yrange=[0,5],xlabel="Quantity",
ylabel="$ per unit",
key="demand",
explicit(pd(x,10,120),x,0,15),color=black,
key="supply",
explicit(ps(x,2,.4),x,0,15), line_width=2,
key="new supply",
explicit(ps(x,5,.4),x,0,15) )$

/*Input. Execute (ctrl-enter) for results */
x3: find_root(pd(x,10,120)- ps(x,5,.4),x,0.1,20);
[p3:pd(x3,10,120), ps(x3,5,.4)];

/*Input. Execute (ctrl-enter) for results */
float(subst([i=10,A=120,w=5,B=.4],xeq));

/*Input. Execute (ctrl-enter) for results */
e_arc0:((x3-x1)/(x3+x1))/((p3-p1)/(p3+p1));

/*Input. Execute (ctrl-enter) for results */
[x1*p1, x3*p3];

/*Input. Execute (ctrl-enter) for results */
mr_arc: (x3*p3-x1*p1)/(x3-x1);

/*Input. Execute (ctrl-enter) for results */
float(solve((1+1/e_arc)*(p3+p1)/2 - mr_arc,e_arc));
e_point0:float(-2/5);

/*Input. Execute (ctrl-enter) for results */
(1+ 1/e_point0)*(p1+p3)/2;
(1+ 1/e_arc0)*(p1+p3)/2;