You can think of demand and supply (of a product) as quantities that are functions of price. However, it can be convenient to also think of them as prices as functions of quantity, that is $$p = D(x)$$ and $$p = S(x)$$. Consumer surplus and producer surplus are such quantities.

A consumer’s demand curve is the graph of $$p = D(X)$$, which shows the price per unit that the consumer is willing to pay for $$x$$ units of a product. It is usually a decreasing function since consumers expect to pay less per unit of large quantities of the product. The producer’s supply curve is the graph of $$p = S(x)$$, which shows the price per unit the producer is willing to accept for selling $$x$$ units. It is usually and increasing function since a higher price per unit is an incentive for the producer to make more units available for sale.

As you can imagine, these expectations are at odds with each other. The equilibrium point ($$x_E$$, $$p_E$$) is the intersection of these two curves. utility is a function defined in economics as a certain amount of pleasure, or utility, that a consumer receives per $$x$$ units of a product.

Given that $$p = D(x)$$ describes the demand function for a commodity, then the consumer surplus is defined for the point ($$Q$$, $$P$$) as

$\int_0^Q D(x)dx - QP$

This formula tells us that the consumer surplus is given by the integral of the demand function as the demand of the commodity goes from zero to a given quantity, minus the cost incurred for getting that quantity.

As an example, let’s find the consumer surplus for the demand function given by $$D(x) = (x - 5)^2$$ when $$x = 3$$.

from sympy.abc import x
import sympy as sp

D = (x - 5)**2
Q = D.evalf(subs={x:3})
P = Q * 3
integral = sp.integrate(D, (x, 0, 3))
consumer_surplus = integral - P
consumer_surplus
## 27.0000000000000

Given that $$p = S(x)$$ describes the supply function for a commodity, then the producer surplus is defined for the point ($$Q$$, $$P$$) as

$QP - \int_0^P S(x)dx$

This formula tells us that the producer surplus is given by the cost incurred for getting a given quantity of a commodity minus the integral of the supply function as the price for the commodity goes from zero to a given price.

Let’s find the producer surplus for $$S(x) = x^2 + x + 3$$ when $$x = 3$$.

S = x**2 + x + 3
Q = S.evalf(subs={x:3})
P = Q * 3
integral = sp.integrate(S, (x, 0, 3))
producer_surplus = P - integral
producer_surplus
## 22.5000000000000

The equilibrium point ($$x_E$$, $$p_E$$) is the point at which the supply and demand curves intersect. It is the point at which sellers and buyers come together and purchases and sales actually occur.

Let’s examine

• where is the equilibrium point for these two functions
• what is the consumer surplus at the equilibrium point
• what is the producer surplus at the equilibrium point
equilibrium_quantity = sp.solve(sp.Eq(D, S), x)
equilibrium_quantity
## 
equilibrium_consumer_surplus = sp.integrate(D, (x, 0, 2)) - D.evalf(subs={x:2}) * 2
equilibrium_consumer_surplus
## 14.6666666666667
equilibrium_producer_surplus = S.evalf(subs={x:2}) * 2 - sp.integrate(S, (x, 0, 2))
equilibrium_producer_surplus
## 7.33333333333333