Skip to content

Commit 38bf614

Browse files
committed
misc
1 parent 0a84696 commit 38bf614

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,12 +1,17 @@
11
# Introduction to Supply and Demand
22

3-
This lecture is about some linear models of equilibrium prices and
4-
quantities, one of the main topics of elementary microeconomics.
5-
6-
Our approach is first to offer a scalar version with one good and one price.
3+
+++
74

85
## Outline
96

7+
This lecture is about some models of equilibrium prices and quantities, one of
8+
the main topics of elementary microeconomics.
9+
10+
Throughout the lecture, we focus on models with one good and one price.
11+
12+
({doc}`Later <supply_demand_multiple_goods>` we will investigate settings with
13+
many goods.)
14+
1015
We shall describe two classic welfare theorems:
1116

1217
* **first welfare theorem:** for a given a distribution of wealth among consumers, a competitive equilibrium allocation of goods solves a social planning problem.
@@ -23,6 +28,13 @@ Key infrastructure concepts that we'll encounter in this lecture are
2328
* social welfare as a sum of consumer and producer surpluses
2429
* competitive equilibrium
2530

31+
We will use the following imports.
32+
33+
```python
34+
import numpy as np
35+
import matplotlib.pyplot as plt
36+
```
37+
2638
## Supply and Demand
2739

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

4254
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.
4355

44-
56+
### Surpluses and Welfare
4557

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

@@ -83,15 +95,20 @@ $$ (eq:old1)
8395
8496
Let's remember the quantity $q$ given by equation {eq}`eq:old1` that a social planner would choose to maximize consumer plus producer surplus.
8597
86-
We'll compare it to the quantity that emerges in a competitive equilibrium equilibrium that equates
87-
supply to demand.
98+
We'll compare it to the quantity that emerges in a competitive equilibrium
99+
equilibrium that equates supply to demand.
100+
101+
+++
102+
103+
### Competitive Equilibrium
88104
89105
Instead of equating quantities supplied and demanded, we'll can accomplish the same thing by equating demand price to supply price:
90106
91107
$$
92108
p = d_0 - d_1 q = s_0 + s_1 q ,
93109
$$
94110
111+
+++
95112
96113
It we solve the equation defined by the second equality in the above line for $q$, we obtain the
97114
competitive equilibrium quantity; it equals the same $q$ given by equation {eq}`eq:old1`.
@@ -105,15 +122,102 @@ It also brings a useful **competitive equilibrium computation strategy:**
105122
106123
* 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
107124
108-
Soon we'll derive generalizations of the above demand and supply
109-
curves from other objects.
125+
### Generalizations
126+
127+
In later lectures, we'll derive generalizations of the above demand and
128+
supply curves from other objects.
110129
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.
130+
Our generalizations will extend the preceding analysis of a market for a
131+
single good to the analysis of $n$ simultaneous markets in $n$ goods.
113132
114133
In addition
115134
116135
* we'll derive **demand curves** from a consumer problem that maximizes a **utility function** subject to a **budget constraint**.
117136
118137
* 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 -->
138+
139+
+++
140+
141+
## Code
142+
143+
+++
144+
145+
```python
146+
class SingleGoodMarket:
147+
148+
def __init__(self,
149+
d_0=1.0, # demand intercept
150+
d_1=0.5, # demand slope
151+
s_0=0.1, # supply intercept
152+
s_1=0.4): # supply slope
153+
154+
self.d_0, self.d_1 = d_0, d_1
155+
self.s_0, self.s_1 = s_0, s_1
156+
157+
def inverse_demand(self, q):
158+
return self.d_0 - self.d_1 * q
159+
160+
def inverse_supply(self, q):
161+
return self.s_0 + self.s_1 * q
162+
163+
def equilibrium_quantity(self):
164+
return (self.d_0 - self.s_0) / (self.d_1 + self.s_1)
165+
166+
def equilibrium_price(self):
167+
q = self.equilibrium_quantity()
168+
return self.s_0 + self.s_1 * q
169+
```
170+
171+
```python
172+
def plot_supply_demand(market):
173+
174+
# Unpack
175+
d_0, d_1 = market.d_0, market.d_1
176+
s_0, s_1 = market.s_0, market.s_1
177+
q = market.equilibrium_quantity()
178+
p = market.equilibrium_price()
179+
grid_size = 200
180+
x_grid = np.linspace(0, 2 * q, grid_size)
181+
ps = np.ones_like(x_grid) * p
182+
supply_curve = market.inverse_supply(x_grid)
183+
demand_curve = market.inverse_demand(x_grid)
184+
185+
fig, ax = plt.subplots()
186+
187+
ax.plot(x_grid, supply_curve, label='Supply', color='#020060')
188+
ax.plot(x_grid, demand_curve, label='Demand', color='#600001')
189+
190+
ax.fill_between(x_grid[x_grid <= q],
191+
demand_curve[x_grid<=q],
192+
ps[x_grid <= q],
193+
label='Consumer surplus',
194+
color='#EED1CF')
195+
ax.fill_between(x_grid[x_grid <= q],
196+
supply_curve[x_grid <= q],
197+
ps[x_grid <= q],
198+
label='Producer surplus',
199+
color='#E6E6F5')
200+
201+
ax.vlines(q, 0, p, linestyle="dashed", color='black', alpha=0.7)
202+
ax.hlines(p, 0, q, linestyle="dashed", color='black', alpha=0.7)
203+
204+
ax.legend(loc='upper center', frameon=False)
205+
ax.margins(x=0, y=0)
206+
ax.set_ylim(0)
207+
ax.set_xlabel('Quantity')
208+
ax.set_ylabel('Price')
209+
210+
plt.show()
211+
```
212+
213+
```python
214+
market = SingleGoodMarket()
215+
```
216+
217+
```python
218+
plot_supply_demand(market)
219+
```
220+
221+
```python
222+
223+
```

0 commit comments

Comments
 (0)