Skip to content

Commit bbb1577

Browse files
committed
migrate to namedtuple over class implementation for Market
1 parent 541a962 commit bbb1577

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

lectures/intro_supply_demand.md

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ In our exposition we will use the following Python imports.
5757
```{code-cell} ipython3
5858
import numpy as np
5959
import matplotlib.pyplot as plt
60+
from collections import namedtuple
6061
```
6162

6263
## Consumer surplus
@@ -356,45 +357,38 @@ $$
356357

357358
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.
358359

359-
Here is a class that stores parameters for our single good market, as well as
360-
implementing the inverse demand and supply curves.
360+
We can use a [namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple) to store the parameters for our single good market.
361361

362362
```{code-cell} ipython3
363-
class Market:
363+
Market = namedtuple('Market', ['d_0', # demand intercept
364+
'd_1', # demand slope
365+
's_0', # supply intercept
366+
's_1'] # supply slope
367+
)
364368
365-
def __init__(self,
366-
d_0=1.0, # demand intercept
367-
d_1=0.6, # demand slope
368-
s_0=0.1, # supply intercept
369-
s_1=0.4): # supply slope
370-
371-
self.d_0, self.d_1 = d_0, d_1
372-
self.s_0, self.s_1 = s_0, s_1
373-
374-
def inverse_demand(self, q):
375-
return self.d_0 - self.d_1 * q
376-
377-
def inverse_supply(self, q):
378-
return self.s_0 + self.s_1 * q
369+
def create_market(d_0=1.0, d_1=0.6, s_0=0.1, s_1=0.4):
370+
return Market(d_0=d_0, d_1=d_1, s_0=s_0, s_1=s_1)
379371
```
380372

381-
Let's create an instance.
373+
This `Market` namedtuple can then be used by our `inverse_demand` and `inverse_supply` functions.
382374

383375
```{code-cell} ipython3
384-
market = Market()
376+
def inverse_demand(q, model):
377+
return model.d_0 - model.d_1 * q
378+
379+
def inverse_supply(q, model):
380+
return model.s_0 + model.s_1 * q
385381
```
386382

387383
Here is a plot of these two functions using `market`.
388384

389385
```{code-cell} ipython3
390-
:tags: [hide-input]
391-
392-
market = Market()
386+
market = create_market()
393387
394388
grid_min, grid_max, grid_size = 0, 1.5, 200
395389
q_grid = np.linspace(grid_min, grid_max, grid_size)
396-
supply_curve = market.inverse_supply(q_grid)
397-
demand_curve = market.inverse_demand(q_grid)
390+
supply_curve = inverse_supply(q_grid, market)
391+
demand_curve = inverse_demand(q_grid, market)
398392
399393
fig, ax = plt.subplots()
400394
ax.plot(q_grid, supply_curve, label='supply', color='green')
@@ -429,7 +423,7 @@ The next figure illustrates
429423
:tags: [hide-input]
430424
431425
q = 1.25
432-
p = market.inverse_demand(q)
426+
p = inverse_demand(q, market)
433427
ps = np.ones_like(q_grid) * p
434428
435429
fig, ax = plt.subplots()
@@ -489,7 +483,7 @@ The next figure illustrates
489483
:tags: [hide-input]
490484
491485
q = 0.75
492-
p = market.inverse_supply(q)
486+
p = inverse_supply(q, market)
493487
ps = np.ones_like(q_grid) * p
494488
495489
fig, ax = plt.subplots()
@@ -670,40 +664,37 @@ Using the class, plot the inverse demand and supply curves $i_d$ and $i_s$
670664
:class: dropdown
671665
```
672666
673-
```{code-cell} ipython3
674-
class Market:
675-
676-
def __init__(self,
677-
d_0=1.0, # demand intercept
678-
d_1=0.6, # demand slope
679-
s_0=0.1, # supply intercept
680-
s_1=0.4): # supply slope
667+
Let us make use of a [namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple) container provided by Python to hold the parameters of the Market.
681668
682-
self.d_0, self.d_1 = d_0, d_1
683-
self.s_0, self.s_1 = s_0, s_1
684-
685-
def inverse_demand(self, q):
686-
return self.d_0 - self.d_1 * q**0.6
687-
688-
def inverse_supply(self, q):
689-
return self.s_0 + self.s_1 * q**1.8
669+
```{code-cell} ipython3
670+
Market = namedtuple('Market', ['d_0', # demand intercept
671+
'd_1', # demand slope
672+
's_0', # supply intercept
673+
's_1'] # supply slope
674+
)
690675
```
691676
692-
Let's create an instance.
677+
We can now define some functions that `create` and `operate` on these Market parameters.
693678
694679
```{code-cell} ipython3
695-
market = Market()
680+
def create_market(d_0=1.0, d_1=0.6, s_0=0.1, s_1=0.4):
681+
return Market(d_0=d_0, d_1=d_1, s_0=s_0, s_1=s_1)
682+
683+
def inverse_demand(q, model):
684+
return model.d_0 - model.d_1 * q**0.6
685+
686+
def inverse_supply(q, model):
687+
return model.s_0 + model.s_1 * q**1.8
696688
```
697689
698690
Here is a plot of inverse supply and demand.
699691
700692
```{code-cell} ipython3
701-
:tags: [hide-input]
702-
703693
grid_min, grid_max, grid_size = 0, 1.5, 200
704694
q_grid = np.linspace(grid_min, grid_max, grid_size)
705-
supply_curve = market.inverse_supply(q_grid)
706-
demand_curve = market.inverse_demand(q_grid)
695+
market = create_market()
696+
supply_curve = inverse_supply(q_grid, market)
697+
demand_curve = inverse_demand(q_grid, market)
707698
708699
fig, ax = plt.subplots()
709700
ax.plot(q_grid, supply_curve, label='supply', color='green')
@@ -864,7 +855,7 @@ price, in line with the first fundamental welfare theorem.
864855
from scipy.optimize import newton
865856
866857
def excess_demand(q):
867-
return market.inverse_demand(q) - market.inverse_supply(q)
858+
return inverse_demand(q, market) - inverse_supply(q, market)
868859
869860
equilibrium_q = newton(excess_demand, 0.99)
870861
print(f"{equilibrium_q: .5f}")

0 commit comments

Comments
 (0)