@@ -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
68kernelspec :
79 display_name : Python 3 (ipykernel)
810 language : python
@@ -46,7 +48,6 @@ import numpy as np
4648import matplotlib.pyplot as plt
4749```
4850
49-
5051## Supply and demand
5152
5253We 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
7475class 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-
9593Let's create an instance.
9694
9795``` {code-cell} ipython3
9896market = Market()
9997```
10098
101-
10299Here is a plot of these two functions using ` market ` .
103100
104101``` {code-cell} ipython3
105102:tags: [hide-input]
103+
106104market = Market()
107105
108106grid_min, grid_max, grid_size = 0, 1.5, 200
@@ -149,11 +147,11 @@ ps = np.ones_like(q_grid) * p
149147
150148fig, ax = plt.subplots()
151149ax.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')
157155ax.vlines(q, 0, p, linestyle="dashed", color='black', alpha=0.7)
158156ax.hlines(p, 0, q, linestyle="dashed", color='black', alpha=0.7)
159157
@@ -168,7 +166,6 @@ ax.set_ylabel('price')
168166plt.show()
169167```
170168
171-
172169Consumer surplus provides a measure of total consumer welfare at quantity $q$.
173170
174171The 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
214211fig, ax = plt.subplots()
215212ax.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')
221218ax.vlines(q, 0, p, linestyle="dashed", color='black', alpha=0.7)
222219ax.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
281278The next figure plots welfare as a function of $q$.
282279
283280
284281```{code-cell} ipython3
285282:tags: [hide-input]
283+
286284q_vals = np.linspace(0, 1.78, 200)
287285fig, ax = plt.subplots()
288286ax.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
395393class 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
414411Let's create an instance.
@@ -498,7 +495,8 @@ Here's a Python function that computes this value:
498495```{code-cell} ipython3
499496def 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