Skip to content

Commit 9dd5f55

Browse files
Adjust to pep8
1 parent b3df796 commit 9dd5f55

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

lectures/intro_supply_demand.md

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ jupytext:
33
text_representation:
44
extension: .md
55
format_name: myst
6+
format_version: 0.13
7+
jupytext_version: 1.11.5
68
kernelspec:
79
display_name: Python 3 (ipykernel)
810
language: python
@@ -46,7 +48,6 @@ import numpy as np
4648
import matplotlib.pyplot as plt
4749
```
4850

49-
5051
## Supply and demand
5152

5253
We study a market for a single good in which buyers and sellers exchange a quantity $q$ for a price $p$.
@@ -73,11 +74,11 @@ implementing the inverse demand and supply curves.
7374
```{code-cell} ipython3
7475
class Market:
7576
76-
def __init__(self,
77-
d_0=1.0, # demand intercept
78-
d_1=0.6, # demand slope
79-
s_0=0.1, # supply intercept
80-
s_1=0.4): # supply slope
77+
def __init__(self,
78+
d_0=1.0, # demand intercept
79+
d_1=0.6, # demand slope
80+
s_0=0.1, # supply intercept
81+
s_1=0.4): # supply slope
8182
8283
self.d_0, self.d_1 = d_0, d_1
8384
self.s_0, self.s_1 = s_0, s_1
@@ -87,22 +88,19 @@ class Market:
8788
8889
def inverse_supply(self, q):
8990
return self.s_0 + self.s_1 * q
90-
9191
```
9292

93-
94-
9593
Let's create an instance.
9694

9795
```{code-cell} ipython3
9896
market = Market()
9997
```
10098

101-
10299
Here is a plot of these two functions using `market`.
103100

104101
```{code-cell} ipython3
105102
:tags: [hide-input]
103+
106104
market = Market()
107105
108106
grid_min, grid_max, grid_size = 0, 1.5, 200
@@ -149,11 +147,11 @@ ps = np.ones_like(q_grid) * p
149147
150148
fig, ax = plt.subplots()
151149
ax.plot(q_grid, demand_curve, label='demand')
152-
ax.fill_between(q_grid[q_grid <= q],
153-
demand_curve[q_grid<=q],
154-
ps[q_grid <= q],
155-
label='consumer surplus',
156-
color='#EED1CF')
150+
ax.fill_between(q_grid[q_grid <= q],
151+
demand_curve[q_grid <= q],
152+
ps[q_grid <= q],
153+
label='consumer surplus',
154+
color='#EED1CF')
157155
ax.vlines(q, 0, p, linestyle="dashed", color='black', alpha=0.7)
158156
ax.hlines(p, 0, q, linestyle="dashed", color='black', alpha=0.7)
159157
@@ -168,7 +166,6 @@ ax.set_ylabel('price')
168166
plt.show()
169167
```
170168
171-
172169
Consumer surplus provides a measure of total consumer welfare at quantity $q$.
173170
174171
The idea is that the inverse demand curve $d_0 - d_1 q$ shows a consumer's willingness to
@@ -213,11 +210,11 @@ ps = np.ones_like(q_grid) * p
213210
214211
fig, ax = plt.subplots()
215212
ax.plot(q_grid, supply_curve, label='supply')
216-
ax.fill_between(q_grid[q_grid <= q],
217-
supply_curve[q_grid<=q],
218-
ps[q_grid <= q],
219-
label='producer surplus',
220-
color='#E6E6F5')
213+
ax.fill_between(q_grid[q_grid <= q],
214+
supply_curve[q_grid <= q],
215+
ps[q_grid <= q],
216+
label='producer surplus',
217+
color='#E6E6F5')
221218
ax.vlines(q, 0, p, linestyle="dashed", color='black', alpha=0.7)
222219
ax.hlines(p, 0, q, linestyle="dashed", color='black', alpha=0.7)
223220
@@ -275,14 +272,15 @@ def W(q, market):
275272
# Unpack
276273
d_0, d_1, s_0, s_1 = market.d_0, market.d_1, market.s_0, market.s_1
277274
# Compute and return welfare
278-
return (d_0 - s_0) * q - 0.5 * (d_1 + s_1) * q**2
275+
return (d_0 - s_0) * q - 0.5 * (d_1 + s_1) * q**2
279276
```
280277
281278
The next figure plots welfare as a function of $q$.
282279
283280
284281
```{code-cell} ipython3
285282
:tags: [hide-input]
283+
286284
q_vals = np.linspace(0, 1.78, 200)
287285
fig, ax = plt.subplots()
288286
ax.plot(q_vals, W(q_vals, market), label='welfare')
@@ -394,11 +392,11 @@ Using the class, plot the inverse demand and supply curves $i_d$ and $i_s$
394392
```{code-cell} ipython3
395393
class Market:
396394
397-
def __init__(self,
398-
d_0=1.0, # demand intercept
399-
d_1=0.6, # demand slope
400-
s_0=0.1, # supply intercept
401-
s_1=0.4): # supply slope
395+
def __init__(self,
396+
d_0=1.0, # demand intercept
397+
d_1=0.6, # demand slope
398+
s_0=0.1, # supply intercept
399+
s_1=0.4): # supply slope
402400
403401
self.d_0, self.d_1 = d_0, d_1
404402
self.s_0, self.s_1 = s_0, s_1
@@ -408,7 +406,6 @@ class Market:
408406
409407
def inverse_supply(self, q):
410408
return self.s_0 + self.s_1 * q**1.8
411-
412409
```
413410
414411
Let's create an instance.
@@ -498,7 +495,8 @@ Here's a Python function that computes this value:
498495
```{code-cell} ipython3
499496
def W(q, market):
500497
# Unpack
501-
d_0, d_1, s_0, s_1 = market.d_0, market.d_1, market.s_0, market.s_1
498+
d_0, d_1 = market.d_0, market.d_1
499+
s_0, s_1 = market.s_0, market.s_1
502500
# Compute and return welfare
503501
S_c = d_0 * q - d_1 * q**1.6 / 1.6
504502
S_p = s_0 * q + s_1 * q**2.8 / 2.8

0 commit comments

Comments
 (0)