Skip to content

Commit 7e13bc2

Browse files
jstacmmcky
andauthored
Edits to intro supply demand lecture (#143)
* misc * misc * fix header for execution --------- Co-authored-by: mmcky <[email protected]>
1 parent 9f3dbd0 commit 7e13bc2

File tree

1 file changed

+116
-12
lines changed

1 file changed

+116
-12
lines changed

lectures/intro_supply_demand.md

Lines changed: 116 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
extension: .md
5+
format_name: myst
6+
kernelspec:
7+
display_name: Python 3 (ipykernel)
8+
language: python
9+
name: python3
10+
---
11+
112
# Introduction to Supply and Demand
213

3-
This lecture is about some linear models of equilibrium prices and
4-
quantities, one of the main topics of elementary microeconomics.
14+
## Outline
15+
16+
This lecture is about some models of equilibrium prices and quantities, one of
17+
the main topics of elementary microeconomics.
518

6-
Our approach is first to offer a scalar version with one good and one price.
19+
Throughout the lecture, we focus on models with one good and one price.
720

8-
## Outline
21+
({doc}`Later <supply_demand_multiple_goods>` we will investigate settings with
22+
many goods.)
923

1024
We shall describe two classic welfare theorems:
1125

@@ -23,6 +37,13 @@ Key infrastructure concepts that we'll encounter in this lecture are
2337
* social welfare as a sum of consumer and producer surpluses
2438
* competitive equilibrium
2539

40+
We will use the following imports.
41+
42+
```{code-cell} ipython3
43+
import numpy as np
44+
import matplotlib.pyplot as plt
45+
```
46+
2647
## Supply and Demand
2748

2849
We study a market for a single good in which buyers and sellers exchange a quantity $q$ for a price $p$.
@@ -41,7 +62,7 @@ $$
4162

4263
We call them inverse demand and supply curves because price is on the left side of the equation rather than on the right side as it would be in a direct demand or supply function.
4364

44-
65+
### Surpluses and Welfare
4566

4667
We define **consumer surplus** as the area under an inverse demand curve minus $p q$:
4768

@@ -83,15 +104,18 @@ $$ (eq:old1)
83104
84105
Let's remember the quantity $q$ given by equation {eq}`eq:old1` that a social planner would choose to maximize consumer plus producer surplus.
85106
86-
We'll compare it to the quantity that emerges in a competitive equilibrium equilibrium that equates
87-
supply to demand.
107+
We'll compare it to the quantity that emerges in a competitive equilibrium
108+
equilibrium that equates supply to demand.
109+
110+
### Competitive Equilibrium
88111
89112
Instead of equating quantities supplied and demanded, we'll can accomplish the same thing by equating demand price to supply price:
90113
91114
$$
92115
p = d_0 - d_1 q = s_0 + s_1 q ,
93116
$$
94117
118+
+++
95119
96120
It we solve the equation defined by the second equality in the above line for $q$, we obtain the
97121
competitive equilibrium quantity; it equals the same $q$ given by equation {eq}`eq:old1`.
@@ -105,15 +129,95 @@ It also brings a useful **competitive equilibrium computation strategy:**
105129
106130
* after solving the welfare problem for an optimal quantity, we can read a competitive equilibrium price from either supply price or demand price at the competitive equilibrium quantity
107131
108-
Soon we'll derive generalizations of the above demand and supply
109-
curves from other objects.
132+
### Generalizations
133+
134+
In later lectures, we'll derive generalizations of the above demand and
135+
supply curves from other objects.
110136
111-
Our generalizations will extend the preceding analysis of a market for a single good to the analysis
112-
of $n$ simultaneous markets in $n$ goods.
137+
Our generalizations will extend the preceding analysis of a market for a
138+
single good to the analysis of $n$ simultaneous markets in $n$ goods.
113139
114140
In addition
115141
116142
* we'll derive **demand curves** from a consumer problem that maximizes a **utility function** subject to a **budget constraint**.
117143
118144
* we'll derive **supply curves** from the problem of a producer who is price taker and maximizes his profits minus total costs that are described by a **cost function**.
119-
<!-- #endregion -->
145+
146+
## Code
147+
148+
```{code-cell} ipython3
149+
class SingleGoodMarket:
150+
151+
def __init__(self,
152+
d_0=1.0, # demand intercept
153+
d_1=0.5, # demand slope
154+
s_0=0.1, # supply intercept
155+
s_1=0.4): # supply slope
156+
157+
self.d_0, self.d_1 = d_0, d_1
158+
self.s_0, self.s_1 = s_0, s_1
159+
160+
def inverse_demand(self, q):
161+
return self.d_0 - self.d_1 * q
162+
163+
def inverse_supply(self, q):
164+
return self.s_0 + self.s_1 * q
165+
166+
def equilibrium_quantity(self):
167+
return (self.d_0 - self.s_0) / (self.d_1 + self.s_1)
168+
169+
def equilibrium_price(self):
170+
q = self.equilibrium_quantity()
171+
return self.s_0 + self.s_1 * q
172+
```
173+
174+
```{code-cell} ipython3
175+
def plot_supply_demand(market):
176+
177+
# Unpack
178+
d_0, d_1 = market.d_0, market.d_1
179+
s_0, s_1 = market.s_0, market.s_1
180+
q = market.equilibrium_quantity()
181+
p = market.equilibrium_price()
182+
grid_size = 200
183+
x_grid = np.linspace(0, 2 * q, grid_size)
184+
ps = np.ones_like(x_grid) * p
185+
supply_curve = market.inverse_supply(x_grid)
186+
demand_curve = market.inverse_demand(x_grid)
187+
188+
fig, ax = plt.subplots()
189+
190+
ax.plot(x_grid, supply_curve, label='Supply', color='#020060')
191+
ax.plot(x_grid, demand_curve, label='Demand', color='#600001')
192+
193+
ax.fill_between(x_grid[x_grid <= q],
194+
demand_curve[x_grid<=q],
195+
ps[x_grid <= q],
196+
label='Consumer surplus',
197+
color='#EED1CF')
198+
ax.fill_between(x_grid[x_grid <= q],
199+
supply_curve[x_grid <= q],
200+
ps[x_grid <= q],
201+
label='Producer surplus',
202+
color='#E6E6F5')
203+
204+
ax.vlines(q, 0, p, linestyle="dashed", color='black', alpha=0.7)
205+
ax.hlines(p, 0, q, linestyle="dashed", color='black', alpha=0.7)
206+
207+
ax.legend(loc='upper center', frameon=False)
208+
ax.margins(x=0, y=0)
209+
ax.set_ylim(0)
210+
ax.set_xlabel('Quantity')
211+
ax.set_ylabel('Price')
212+
213+
plt.show()
214+
```
215+
216+
```{code-cell} ipython3
217+
market = SingleGoodMarket()
218+
```
219+
220+
```{code-cell} ipython3
221+
plot_supply_demand(market)
222+
```
223+

0 commit comments

Comments
 (0)