# Dowling15.wxmx: The Definite Integral

load(draw)$  set_draw_defaults(line_width=2, grid = [2,2], 
             background_color = light_gray,  draw_realpart=false)$ 
            fpprintprec:5$   ratprint:false$     kill(all)$

load ("Econ1.mac");

## Preface

This worksheet is one of a number of wxMaxima files available in the section
    Economic Analysis with Maxima
on my CSULB webpage.

We use Maxima to solve a few of the problems (and draw some of the plots) in Ch. 15 of the 
supplemental text: Introduction to  Mathematical Economics, 3rd ed, (Schaum's Outline 
Series), by Edward T. Dowling (1992, 2001), McGraw-Hill. This modestly priced text is a 
bargain with many worked out examples. You should compare the examples worked out 
"by hand" in this text with what we do using Maxima. Section numbers [x.y] refer to sections 
in Dowling's text.

We have slightly changed some of the symbols used in particular problems.

Ted Woollett
https://web.csulb.edu/~woollett/
Sept. 1, 2021

## Consumers' and Producers' Surplus [15.8]

### Supply and Demand Analysis Review

Equilibrium in supply and demand analysis occurs when supply equals demand or when
 Qs = Qd. By equating the supply and demand functions, the equilibrium price and quantity 
can be determined. By way of review, we consider again an example discussed in 
Dowling, Ch.2,Sec.2.

killAB()$
[Qs : -5 + 3*P, Qd : 10 - 2*P, soln : solve (Qs = Qd, P)];

Substituting P = 3 into Qs or Qd then gives the equilibrium quantity Qe (4) with a per unit
price (Pe) of 3.

[Pe : at (P, soln), Qe : at (Qs, soln)];

To make a plot of the demand curve using the economics habit of always placing prices (P)
on the vertical (y) axis and quantity (Q) on the horizontal (x) axis, we express P as a function
of Q, that is we find the function Pd(Q) which is the "inverse demand function".

solve (Q = Qd, P);

Pd : at (P, %);

To make a plot of Pd(Q) versus Q, look at the range of Pd >= 0.

at (Pd, Q = 0);

solve(Pd = 0, Q);

wxdraw2d (xlabel = "Q", ylabel = "P", title = "inverse demand curve",
    explicit (Pd, Q, 0, 10))$

Now add the supply curve Ps(Q)  vs. Q.

solve (Q = Qs, P);

Ps : at (P, %);

We already know these curves must cross at Q = 4, P = 3.

wxdraw2d (xlabel = "Q", ylabel = "P", title = "supply and demand",
  key_pos = bottom_left, key = "Pd", explicit (Pd, Q, 0, 10), color = red, key = "Ps",
    explicit (Ps, Q, 0, 10), color = black, key = "", line_width = 1,
    explicit (Pe, Q, 0, Qe), parametric (Qe, yy, yy, 0, Pe))$

### Consumers' Surplus Illustrated

The demand function Pd(Q) represents the different prices per unit consumers are willing 
to pay for different quantities of a good. Consumers who would be willing to pay more than 
Pe (as the price per unit) benefit.

A consumer surplus happens when the price that consumers pay for a product or 
service is less than the price they're willing to pay. It's a measure of the additional benefit 
that consumers receive because they're paying less for something than what they were 
willing to pay.

The amount consumers actually have to pay when Qe = 4 unit and price per unit is 
Pe = $3/unit  is the amount ($3/unit) x 4 unit = $12, represented by the rectangle 
0 <= Q <= 4, 0 <= P <= 3 given by Qe*Pe which we can think of as an "area".  Because 
there are some consumers who would have been willing to pay $4 per unit for 2 units, etc, 
the upper triangle below the demand curve is an "area" which represents a benefit to 
consumers as a whole. This is easy to calculate when the demand curve is a straight line, 
as it is here.

The "area" is just half of the product of the base time the height (half the area of a rectangle).
The height is (5-3) = $2/unit, and the base is (4-0) = 4units, so the consumers' surplus is 
$2/unit*4 units = $8.

When the demand curve is not a straight line but a given curve f(Q), we can first calculate
the "area" under the demand curve from Q = 0 to Q = Qe by finding the area of each
vertical strip with small width dQ and height f(Q), which is just the product f(Q)*dQ, and 
then adding up the area of these narrow vertical strips from Q = 0 to Q = Qe (by doing
an integral) and then subtracting the rectangular area Qe*Pe = 4*3 = 12.

This vertical strip sum (definite integral in the limit that the width dQ of the strips goes to
zero) has the value 16 here:

integrate (Pd, Q, 0, 4);

We then subtract 12 to get consumers' surplus = $4.
Here we use all defined symbols:

CS :  integrate (Pd, Q, 0, Qe) - Qe*Pe;

If we work at it we can make a plot with the consumers' surplus area highlighted in
blue, by making used of filled_func = true or false appropriately.

wxdraw2d (xlabel = "Q", ylabel = "P", title = "supply (blue) and demand (red)",
  color = blue, fill_color = blue, filled_func = true,  explicit (Pd, Q, 0, 4),
    filled_func = false, explicit (Pd,Q,4,10), filled_func = true,
    fill_color = light_gray, explicit (3, Q, 0, 4), filled_func = false,
    color = red, explicit (Ps, Q, 0, 10))$

### Producers' Surplus Illustrated

at (Ps, Q = 0);

float(%);

A supply function such as Ps(Q) represents the prices (per unit) which must be offered for
(some) producers to be willing to produce and sell the corresponding quantity Q of units.
If market equilibrium occurs at (Qe, Pe), those producers who would be willing to supply at
a lower price benefit. This gain to producers as a group is called producers' surplus, and
is pictorially the area above the supply curve, but below P = Pe. This area can be calculated
subtracting from the rectangular area Pe*Qe the sum (definite integral) of vertical strips of
width dQ and height Ps(Q) from Q = 0 to Q = Qe. When Ps(Q) is a straight line, the 
calculation is easy by taking half the area of a rectangle:  (1/2) * (4/3)*4 = 8/3 = 2.67.

For a symbolic formula, we can always use

Qe*Pe - integrate (Ps, Q, 0, Qe);

float(%);

wxdraw2d (xlabel = "Q", ylabel = "P", title = "supply (blue) and demand (red)",
    xrange = [0,10], yrange = [0, 5],
    color = red, fill_color = red, filled_func = true,explicit (Pe, Q, 0, Qe),
    fill_color = light_gray, explicit (Ps, Q, 0, Qe), filled_func = false,
    explicit (Ps, Q, Qe, 10), color = blue, explicit (Pd, Q, 0, 10))$
    

The filled red triangle is the producers' surplus for this example.

The reader can continue experimenting in this way to show both the consumers' surplus
(in blue) and the producers' surplus (in red) on the same plot.

### Example 7

Assume the inverse demand function P(Q) is given by the non-linear expression.
     P = 42 - 5 Q - Q^2.
Also assume that the equilibrium price is Pe = 6.
Determine the corresponding equilibrium quantity Qe, and calculate the consumers'
surplus.

P : 42 - 5*Q - Q^2;
Pe : 6;
solns : solve ( P = Pe, Q);

In economics, Q < 0 is meaningless, so

soln : rest(solns,1);

Qe : at (Q, soln);

solns : solve (P = 0,Q), numer;

soln : rest (solns,1);

Qmax : at (Q,soln);

wxdraw2d (xlabel = "Q", ylabel = "P", explicit (P, Q, 0, Qmax),
    color = black, line_width = 1, explicit (Pe, Q, 0, Qe),
    parametric (Qe, yy, yy, 0, Pe))$

We can now use the symbolic integral method to calculate the consumers' surplus for
this non-linear demand curve example, the area above the line P = Pe and below the
curve P(Q) = demand curve.

CS : integrate (P, Q, 0, Qe) - Qe*Pe;

float(%);

### Dowling Prob 15.27

With demand prices Pd = 45 - Q/2, find the consumers' surplus if Pe = 32.5, Qe = 25

killAB()$
[Pd : 45 - Q/2, Pe : 32.5, Qe : 25];
CS : integrate (Pd, Q, 0, Qe) - Qe*Pe;

### Dowling Prob 15.28

Given the supply function Ps = (Q + 3)^2, find the producers' surplus PS at Pe = 81 and
Qe = 6.

killAB()$
[Ps : (Q+ 3)^2, Pe : 81, Qe : 6];
PS : Qe*Pe - integrate (Ps, Q, 0, Qe);

### Dowling Prob 15.29

Given the demand function Pd  = 25 - Q^2 and the supply function Ps = 2 Q + 1, assume
pure competition and find (a) the consumers' surplus CS and (b) the producers' surplus PS.

killAB()$
[Pd : 25 - Q^2, Ps : 2*Q + 1];
solns : solve (Pd = Ps, Q);

soln : rest (solns,1);

[Qe : at (Q, soln), Pe : at (Pd, Q = Qe)];

CS : integrate (Pd, Q, 0, Qe) - Qe*Pe, numer;

PS : Qe*Pe - integrate (Ps, Q, 0, Qe), numer;

### Dowling Prob 15.31

Under a monopoly, the quantity sold and the market price are determined by the demand
function. If the demand function for a profit-maximizing monopolist is
  P = 274 - Q^2 and MC = 4 + 3 Q, find the consumers' surplus.

The total revenue is TR  = P*Q, and the marginal revenue MR = d(TR)/dQ.
The monopolist maximizes profit by maximizing the difference: (revenue - cost).
Taking the first derivative wrt Q, and setting that first derivative equal to zero to find
the critical point(s), we arrive at the condition MR  = MC.

killAB()$
MC : 4 + 3*Q;
P : 274 - Q^2;
R : expand (P*Q);
MR : diff (R, Q);
solns : solve (MR = MC, Q);

soln : rest (solns, -1);

Let (Qs, Ps) be the critical point of maximum profit.

[Qs : at (Q, soln), Ps : at(P, Q = Qs)];

CS : integrate (P, Q, 0, Qs) - Qs*Ps;

## The Definite Integral and Probability Density [15.9]

The probability P that an event will occur can be measured by the corresponding area
under a "probability density function". A "probability density" or "frequency" function is a
continuous function f(x) which satisfies the conditions:

  1. f(x) >= 0 (Probability cannot be negative.)

  2. f(x) must have the property that the fraction of cases observed for which x is found
      in the interval [a,b] is given by the ratio:

         integrate (f(x),x,a,b) / integrate (f(x), x, R1, R2),

     where all possible observed instances of x lie in the larger interval [R1, R2].

If f(x) is "normalized" such that the denominator equals unity:

           integrate (f(x), x, R1, R2) = 1,

  then the fraction of cases for which x is observed to have a value in the range [a,b]
  is just integrate (f(x), x, a, b), which is a number less than one, and we can call it
  the decimal probability for x to be in [a,b].

 With the assumption of normalization in place, we can then use the notation:

        P (a < x < b) = integrate (f, x, a, b). 

The probability of the value of x falling within the interval [a,b] is the value of the 
 definite integral of f(x) from a to b, pictorially the area under the probability density 
curve from a to b.

### Example 8

The time in minutes between cars passing on a highway is given by the frequency function
f(t) = 2 exp(- 2*t) for t >= 0.  The probability of a new car passing in 0.25 minutes is 
calculated as follows:

The time t between passing cars (in minutes) cannot be negative and when positive can be
arbitrarily large, so the frequency function must be normalized to 1 over [0, inf]. We must
always check the normalization of a probability density function.

killAB()$
f : 2*exp (- 2*t);
integrate (f, t, 0, inf);
P : integrate (f, t, 0, 0.25), numer;

Compare t = 1/10 min, 1/4 min, 1 min.

[integrate (f, t, 0, 0.1), P, integrate (f, t, 0, 1 )], numer;

The probability increases the longer the interval of time between successive cars.
The probability that the time between successive cars is 15 sec (1/4 min) is roughly twice the
probability that the time between successive cars is 6 sec (1/10 min).

wxdraw2d ( xlabel = "t min", ylabel = "f(t)", explicit (f, t, 0, 3))$

The larger the interval of time (starting at t = 0), the greater the area under the frequency
density function.

### Dowling Prob. 15.32

The probability (t in minutes) of being waited on in a large chain restaurant is "given by"
the frequency function f(t) = (4/81) t^3 for 0 <= t <= 3 min.
What is the probability of being waited on between 1 and 2 minutes?

killAB()$
f : (4/81)*t^3;
norm : integrate (f, t, 0, 3);

P : integrate (f, t, 1, 2);

float(%);

wxdraw2d (xlabel = "t min", ylabel = "f(t)", 
    explicit (f, t, 0, 3))$

The probability of being waited on within 1 - 2 minutes after being seated is about 0.185 or
18.5%.

P23 : integrate (f, t, 2, 3), numer;

About 80% probability of being waited on within 2 - 3 minutes after being seated.

### Dowling Prob. 15.33

The proportion of assignments completed within a given day is described by the
probability density function f(x) = 12 (x^2 - x^3) for 0 <= x <= 1. 

(a) What is the probability that 50% or less of the assignments will be completed 
    within the day?

The probability that x lies in the interval [0, 0.5] is the integral of f(x) dx over [0, 0.5],
assuming f(x) is normalized to unity over [0,1]. For x < 1, x^2 > x^3, so f(x) > 0 over the
domain of x.

killAB()$
f : 12* (x^2 - x^3);
norm : integrate (f, x, 0, 1);

Since f(x) is properly normed,

Pa : integrate (f, x, 0, 0.5), numer;

So about 30% probability that 50% or less of the assignments will be completed within
the day.

(b) What is the probability that 50% or more of the assignments will be completed within
the day?

Pb : integrate (f, x, 0.5, 1), numer;

Pa + Pb;

So about 69% probability that 50% or more of the assignments will be completed within
the day.

wxdraw2d (xlabel = "x ", ylabel = "f(x)", 
    explicit (f, x, 0, 1))$