Skip to content

Commit 1162107

Browse files
Unify code format
1 parent e27eacd commit 1162107

File tree

2 files changed

+86
-90
lines changed

2 files changed

+86
-90
lines changed

lectures/intro_supply_demand.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ In addition
145145
```{code-cell} ipython3
146146
class SingleGoodMarket:
147147
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
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
153153
154154
self.d_0, self.d_1 = d_0, d_1
155155
self.s_0, self.s_1 = s_0, s_1
@@ -170,7 +170,6 @@ class SingleGoodMarket:
170170
171171
```{code-cell} ipython3
172172
def plot_supply_demand(market):
173-
174173
# Unpack
175174
d_0, d_1 = market.d_0, market.d_1
176175
s_0, s_1 = market.s_0, market.s_1
@@ -187,16 +186,16 @@ def plot_supply_demand(market):
187186
ax.plot(x_grid, supply_curve, label='Supply', color='#020060')
188187
ax.plot(x_grid, demand_curve, label='Demand', color='#600001')
189188
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')
189+
ax.fill_between(x_grid[x_grid <= q],
190+
demand_curve[x_grid <= q],
191+
ps[x_grid <= q],
192+
label='Consumer surplus',
193+
color='#EED1CF')
194+
ax.fill_between(x_grid[x_grid <= q],
195+
supply_curve[x_grid <= q],
196+
ps[x_grid <= q],
197+
label='Producer surplus',
198+
color='#E6E6F5')
200199
201200
ax.vlines(q, 0, p, linestyle="dashed", color='black', alpha=0.7)
202201
ax.hlines(p, 0, q, linestyle="dashed", color='black', alpha=0.7)

lectures/supply_demand_heterogeneity.md

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ class ExchangeEconomy:
249249
if np.min(b / np.max(Pi @ e)) <= 1.5:
250250
raise Exception('set bliss points further away')
251251
252-
if Ws==None:
252+
if Ws == None:
253253
Ws = np.zeros(m)
254254
else:
255-
if sum(Ws)!=0:
255+
if sum(Ws) != 0:
256256
raise Exception('invalid wealth distribution')
257257
258258
self.Pi, self.bs, self.es, self.Ws, self.n, self.m = Pi, bs, es, Ws, n, m
@@ -272,16 +272,16 @@ class ExchangeEconomy:
272272
273273
# compute price vector with mu=1 and renormalize
274274
p = Pi.T @ b - Pi.T @ Pi @ e
275-
p = p/p[0]
275+
p = p / p[0]
276276
277277
# compute marg util of wealth
278278
mu_s = []
279279
c_s = []
280280
A = p.T @ slope_dc @ p
281281
282282
for i in range(m):
283-
mu_i = (-Ws[i] + p.T @ (Pi_inv @ bs[i] - es[i]))/A
284-
c_i = Pi_inv @ bs[i] - mu_i*slope_dc @ p
283+
mu_i = (-Ws[i] + p.T @ (Pi_inv @ bs[i] - es[i])) / A
284+
c_i = Pi_inv @ bs[i] - mu_i * slope_dc @ p
285285
mu_s.append(mu_i)
286286
c_s.append(c_i)
287287
@@ -305,11 +305,11 @@ class ExchangeEconomy:
305305
Pi = np.array([[1, 0],
306306
[0, 1]])
307307
308-
bs = [np.array([5, 5]), # first consumer's bliss points
309-
np.array([5, 5])] # second consumer's bliss points
308+
bs = [np.array([5, 5]), # first consumer's bliss points
309+
np.array([5, 5])] # second consumer's bliss points
310310
311-
es = [np.array([0, 2]), # first consumer's endowment
312-
np.array([2, 0])] # second consumer's endowment
311+
es = [np.array([0, 2]), # first consumer's endowment
312+
np.array([2, 0])] # second consumer's endowment
313313
314314
example = ExchangeEconomy(Pi, bs, es)
315315
p, c_s, mu_s = example.competitive_equilibrium()
@@ -321,12 +321,11 @@ print('Competitive equilibrium allocation:', c_s)
321321
What happens if the first consumer likes the first good more and the second consumer likes the second good more?
322322

323323
```{code-cell} ipython3
324-
bs = [np.array([6, 5]), # first consumer's bliss points
325-
np.array([5, 6])] # second consumer's bliss points
326-
327-
es = [np.array([0, 2]), # first consumer's endowment
328-
np.array([2, 0])] # second consumer's endowment
324+
bs = [np.array([6, 5]), # first consumer's bliss points
325+
np.array([5, 6])] # second consumer's bliss points
329326
327+
es = [np.array([0, 2]), # first consumer's endowment
328+
np.array([2, 0])] # second consumer's endowment
330329
331330
example = ExchangeEconomy(Pi, bs, es)
332331
p, c_s, mu_s = example.competitive_equilibrium()
@@ -338,12 +337,11 @@ print('Competitive equilibrium allocation:', c_s)
338337
Let the first consumer be poorer.
339338

340339
```{code-cell} ipython3
341-
bs = [np.array([5, 5]), # first consumer's bliss points
342-
np.array([5, 5])] # second consumer's bliss points
343-
344-
es = [np.array([0.5, 0.5]), # first consumer's endowment
345-
np.array([1, 1])] # second consumer's endowment
340+
bs = [np.array([5, 5]), # first consumer's bliss points
341+
np.array([5, 5])] # second consumer's bliss points
346342
343+
es = [np.array([0.5, 0.5]), # first consumer's endowment
344+
np.array([1, 1])] # second consumer's endowment
347345
348346
example = ExchangeEconomy(Pi, bs, es)
349347
p, c_s, mu_s = example.competitive_equilibrium()
@@ -355,12 +353,11 @@ print('Competitive equilibrium allocation:', c_s)
355353
Now let's construct an autarky (i.e, no-trade) equilibrium.
356354

357355
```{code-cell} ipython3
358-
bs = [np.array([4, 6]), # first consumer's bliss points
359-
np.array([6, 4])] # second consumer's bliss points
360-
361-
es = [np.array([0, 2]), # first consumer's endowment
362-
np.array([2, 0])] # second consumer's endowment
356+
bs = [np.array([4, 6]), # first consumer's bliss points
357+
np.array([6, 4])] # second consumer's bliss points
363358
359+
es = [np.array([0, 2]), # first consumer's endowment
360+
np.array([2, 0])] # second consumer's endowment
364361
365362
example = ExchangeEconomy(Pi, bs, es)
366363
p, c_s, mu_s = example.competitive_equilibrium()
@@ -372,11 +369,11 @@ print('Competitive equilibrium allocation:', c_s)
372369
Now let's redistribute endowments before trade.
373370

374371
```{code-cell} ipython3
375-
bs = [np.array([5, 5]), # first consumer's bliss points
376-
np.array([5, 5])] # second consumer's bliss points
372+
bs = [np.array([5, 5]), # first consumer's bliss points
373+
np.array([5, 5])] # second consumer's bliss points
377374
378-
es = [np.array([1, 1]), # first consumer's endowment
379-
np.array([1, 1])] # second consumer's endowment
375+
es = [np.array([1, 1]), # first consumer's endowment
376+
np.array([1, 1])] # second consumer's endowment
380377
381378
Ws = [0.5, -0.5]
382379
example = ExchangeEconomy(Pi, bs, es, Ws)
@@ -397,9 +394,9 @@ beta = 0.95
397394
Pi = np.array([[1, 0],
398395
[0, np.sqrt(beta)]])
399396
400-
bs = [np.array([5, np.sqrt(beta)*5])]
397+
bs = [np.array([5, np.sqrt(beta) * 5])]
401398
402-
es = [np.array([1,1])]
399+
es = [np.array([1, 1])]
403400
404401
example = ExchangeEconomy(Pi, bs, es)
405402
p, c_s, mu_s = example.competitive_equilibrium()
@@ -419,10 +416,10 @@ We use the tricks described above to interpret $c_1, c_2$ as "Arrow securities"
419416
prob = 0.7
420417
421418
Pi = np.array([[np.sqrt(prob), 0],
422-
[0, np.sqrt(1-prob)]])
419+
[0, np.sqrt(1 - prob)]])
423420
424-
bs = [np.array([np.sqrt(prob)*5, np.sqrt(1-prob)*5]),
425-
np.array([np.sqrt(prob)*5, np.sqrt(1-prob)*5])]
421+
bs = [np.array([np.sqrt(prob) * 5, np.sqrt(1 - prob) * 5]),
422+
np.array([np.sqrt(prob) * 5, np.sqrt(1 - prob) * 5])]
426423
427424
es = [np.array([1, 0]),
428425
np.array([0, 1])]
@@ -464,13 +461,13 @@ class ProductionEconomy:
464461
Compute a competitive equilibrium of the production economy
465462
"""
466463
Pi, b, h, mu, J = self.Pi, self.b, self.h, self.mu, self.J
467-
H = .5*(J+J.T)
464+
H = .5 * (J + J.T)
468465
469466
# allocation
470-
c = inv(Pi.T@Pi + mu*H) @ (Pi.T@b - mu*h)
467+
c = inv(Pi.T @ Pi + mu * H) @ (Pi.T @ b - mu * h)
471468
472469
# price
473-
p = 1/mu * (Pi.T@b - Pi.T@Pi@c)
470+
p = 1 / mu * (Pi.T @ b - Pi.T @ Pi @ c)
474471
475472
# check non-satiation
476473
if any(Pi @ c - b >= 0):
@@ -483,13 +480,13 @@ class ProductionEconomy:
483480
Compute the equilibrium price and allocation when there is a monopolist supplier
484481
"""
485482
Pi, b, h, mu, J = self.Pi, self.b, self.h, self.mu, self.J
486-
H = .5*(J+J.T)
483+
H = .5 * (J + J.T)
487484
488485
# allocation
489-
q = inv(mu*H + 2*Pi.T@Pi)@(Pi.T@b - mu*h)
486+
q = inv(mu * H + 2 * Pi.T @ Pi) @ (Pi.T @ b - mu * h)
490487
491488
# price
492-
p = 1/mu * (Pi.T@b - Pi.T@Pi@q)
489+
p = 1 / mu * (Pi.T @ b - Pi.T @ Pi @ q)
493490
494491
if any(Pi @ q - b >= 0):
495492
raise Exception('invalid result: set bliss points further away')
@@ -500,21 +497,21 @@ class ProductionEconomy:
500497
"""
501498
Compute consumer and producer surplus for single good case
502499
"""
503-
if self.n!=1:
500+
if self.n != 1:
504501
raise Exception('not single good')
505502
h, J, Pi, b, mu = self.h.item(), self.J.item(), self.Pi.item(), self.b.item(), self.mu
506503
H = J
507504
508505
# supply/demand curve coefficients
509506
s0, s1 = h, H
510-
d0, d1 = 1/mu * Pi * b, 1/mu * Pi**2
507+
d0, d1 = 1 / mu * Pi * b, 1 / mu * Pi**2
511508
512509
# competitive equilibrium
513510
c, p = self.competitive_equilibrium()
514511
515512
# calculate surplus
516-
c_surplus = d0*c - .5*d1*c**2 - p*c
517-
p_surplus = p*c - s0*c - .5*s1*c**2
513+
c_surplus = d0 * c - .5 * d1 * c**2 - p * c
514+
p_surplus = p * c - s0 * c - .5 * s1 * c**2
518515
519516
return c_surplus, p_surplus
520517
@@ -536,21 +533,21 @@ def plot_competitive_equilibrium(PE):
536533
c, p = c.item(), p.item()
537534
538535
# inverse supply/demand curve
539-
supply_inv = lambda x: h + H*x
540-
demand_inv = lambda x: 1/mu*(Pi*b - Pi*Pi*x)
536+
supply_inv = lambda x: h + H * x
537+
demand_inv = lambda x: 1 / mu * (Pi * b - Pi * Pi * x)
541538
542-
xs = np.linspace(0, 2*c, 100)
539+
xs = np.linspace(0, 2 * c, 100)
543540
ps = np.ones(100) * p
544541
supply_curve = supply_inv(xs)
545-
demand_curve = demand_inv(xs)
542+
demand_curve = demand_inv(xs)
546543
547544
# plot
548-
plt.figure(figsize=[7,5])
545+
plt.figure(figsize=[7, 5])
549546
plt.plot(xs, supply_curve, label='Supply', color='#020060')
550547
plt.plot(xs, demand_curve, label='Demand', color='#600001')
551548
552-
plt.fill_between(xs[xs<=c], demand_curve[xs<=c], ps[xs<=c], label='Consumer surplus', color='#EED1CF')
553-
plt.fill_between(xs[xs<=c], supply_curve[xs<=c], ps[xs<=c], label='Producer surplus', color='#E6E6F5')
549+
plt.fill_between(xs[xs <= c], demand_curve[xs <= c], ps[xs <= c], label='Consumer surplus', color='#EED1CF')
550+
plt.fill_between(xs[xs <= c], supply_curve[xs <= c], ps[xs <= c], label='Producer surplus', color='#E6E6F5')
554551
555552
plt.vlines(c, 0, p, linestyle="dashed", color='black', alpha=0.7)
556553
plt.hlines(p, 0, c, linestyle="dashed", color='black', alpha=0.7)
@@ -580,10 +577,10 @@ To do this we
580577
* do experiments in which we shift $b$ and watch what happens to $p, c$.
581578

582579
```{code-cell} ipython3
583-
Pi = np.array([[1]]) # the matrix now is a singleton
584-
b = np.array([10])
585-
h = np.array([0.5])
586-
J = np.array([[1]])
580+
Pi = np.array([[1]]) # the matrix now is a singleton
581+
b = np.array([10])
582+
h = np.array([0.5])
583+
J = np.array([[1]])
587584
mu = 1
588585
589586
PE = ProductionEconomy(Pi, b, h, J, mu)
@@ -648,13 +645,13 @@ This raises both the equilibrium price and quantity.
648645

649646

650647
```{code-cell} ipython3
651-
Pi = np.array([[1, 0],
652-
[0, 1]])
653-
b = np.array([10, 10])
648+
Pi = np.array([[1, 0],
649+
[0, 1]])
650+
b = np.array([10, 10])
654651
655-
h = np.array([0.5, 0.5])
656-
J = np.array([[1, 0.5],
657-
[0.5, 1]])
652+
h = np.array([0.5, 0.5])
653+
J = np.array([[1, 0.5],
654+
[0.5, 1]])
658655
mu = 1
659656
660657
PE = ProductionEconomy(Pi, b, h, J, mu)
@@ -750,24 +747,24 @@ def plot_monopoly(PE):
750747
# compute
751748
752749
# inverse supply/demand curve
753-
marg_cost = lambda x: h + H*x
754-
marg_rev = lambda x: -2*1/mu*Pi*Pi*x + 1/mu*Pi*b
755-
demand_inv = lambda x: 1/mu*(Pi*b - Pi*Pi*x)
750+
marg_cost = lambda x: h + H * x
751+
marg_rev = lambda x: -2 * 1 / mu * Pi * Pi * x + 1 / mu * Pi * b
752+
demand_inv = lambda x: 1 / mu * (Pi * b - Pi * Pi * x)
756753
757-
xs = np.linspace(0, 2*c, 100)
754+
xs = np.linspace(0, 2 * c, 100)
758755
pms = np.ones(100) * pm
759756
marg_cost_curve = marg_cost(xs)
760757
marg_rev_curve = marg_rev(xs)
761758
demand_curve = demand_inv(xs)
762759
763760
# plot
764-
plt.figure(figsize=[7,5])
761+
plt.figure(figsize=[7, 5])
765762
plt.plot(xs, marg_cost_curve, label='Marginal cost', color='#020060')
766763
plt.plot(xs, marg_rev_curve, label='Marginal revenue', color='#E55B13')
767764
plt.plot(xs, demand_curve, label='Demand', color='#600001')
768765
769-
plt.fill_between(xs[xs<=q], demand_curve[xs<=q], pms[xs<=q], label='Consumer surplus', color='#EED1CF')
770-
plt.fill_between(xs[xs<=q], marg_cost_curve[xs<=q], pms[xs<=q], label='Producer surplus', color='#E6E6F5')
766+
plt.fill_between(xs[xs <= q], demand_curve[xs <= q], pms[xs <= q], label='Consumer surplus', color='#EED1CF')
767+
plt.fill_between(xs[xs <= q], marg_cost_curve[xs <= q], pms[xs <= q], label='Producer surplus', color='#E6E6F5')
771768
772769
plt.vlines(c, 0, p, linestyle="dashed", color='black', alpha=0.7)
773770
plt.hlines(p, 0, c, linestyle="dashed", color='black', alpha=0.7)
@@ -790,13 +787,13 @@ def plot_monopoly(PE):
790787
Let's study compare competitive equilibrium and monopoly outcomes in a multiple goods economy.
791788

792789
```{code-cell} ipython3
793-
Pi = np.array([[1, 0],
794-
[0, 1.2]])
795-
b = np.array([10, 10])
790+
Pi = np.array([[1, 0],
791+
[0, 1.2]])
792+
b = np.array([10, 10])
796793
797-
h = np.array([0.5, 0.5])
798-
J = np.array([[1, 0.5],
799-
[0.5, 1]])
794+
h = np.array([0.5, 0.5])
795+
J = np.array([[1, 0.5],
796+
[0.5, 1]])
800797
mu = 1
801798
802799
PE = ProductionEconomy(Pi, b, h, J, mu)
@@ -813,7 +810,7 @@ print('Equilibrium with monopolist supplier allocation:', q)
813810
#### A Single-Good Example
814811

815812
```{code-cell} ipython3
816-
Pi = np.array([[1]]) # the matrix now is a singleton
813+
Pi = np.array([[1]]) # the matrix now is a singleton
817814
b = np.array([10])
818815
h = np.array([0.5])
819816
J = np.array([[1]])

0 commit comments

Comments
 (0)