Skip to content

Commit 533fee9

Browse files
committed
Merge branch 'main' into anaconda=2024.02
2 parents 1cea394 + 03196de commit 533fee9

File tree

6 files changed

+60
-79
lines changed

6 files changed

+60
-79
lines changed

lectures/BCG_complete_mkts.md

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie
3131
tags: [hide-output]
3232
---
3333
!pip install --upgrade quantecon
34-
!pip install interpolation
3534
!conda install -y -c plotly plotly plotly-orca
3635
```
3736

lectures/BCG_incomplete_mkts.md

+24-25
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ jupytext:
33
text_representation:
44
extension: .md
55
format_name: myst
6+
format_version: 0.13
7+
jupytext_version: 1.16.1
68
kernelspec:
7-
display_name: Python 3
9+
display_name: Python 3 (ipykernel)
810
language: python
911
name: python3
1012
---
@@ -26,12 +28,10 @@ kernelspec:
2628

2729
In addition to what's in Anaconda, this lecture will need the following libraries:
2830

29-
```{code-cell} ipython
30-
---
31-
tags: [hide-output]
32-
---
31+
```{code-cell} ipython3
32+
:tags: [hide-output]
33+
3334
!pip install --upgrade quantecon
34-
!pip install interpolation
3535
!conda install -y -c plotly plotly plotly-orca
3636
```
3737

@@ -703,15 +703,14 @@ Parameters include:
703703
- $\beta$: Discount factor. Default value is 0.96.
704704
- bound: Bound for truncated normal distribution. Default value is 3.
705705

706-
```{code-cell} ipython
706+
```{code-cell} ipython3
707707
import numpy as np
708708
from scipy.stats import truncnorm
709709
from scipy.integrate import quad
710710
from numba import njit
711-
from interpolation import interp
712711
```
713712

714-
```{code-cell} python3
713+
```{code-cell} ipython3
715714
class BCG_incomplete_markets:
716715
717716
# init method or constructor
@@ -784,7 +783,7 @@ class BCG_incomplete_markets:
784783
rv = truncnorm(ta, tb, loc=𝜇, scale=𝜎)
785784
𝜖_range = np.linspace(ta, tb, 1000000)
786785
pdf_range = rv.pdf(𝜖_range)
787-
self.g = njit(lambda 𝜖: interp(𝜖_range, pdf_range, 𝜖))
786+
self.g = njit(lambda 𝜖: np.interp(𝜖, 𝜖_range, pdf_range))
788787
789788
790789
#*************************************************************
@@ -1206,14 +1205,14 @@ Below we show some examples computed with the class `BCG_incomplete markets`.
12061205
In the first example, we set up an instance of the BCG incomplete
12071206
markets model with default parameter values.
12081207

1209-
```{code-cell} python3
1210-
:tags: ["hide-output"]
1208+
```{code-cell} ipython3
1209+
:tags: [hide-output]
12111210
12121211
mdl = BCG_incomplete_markets()
12131212
kss,bss,Vss,qss,pss,c10ss,c11ss,c20ss,c21ss,𝜃1ss = mdl.solve_eq(print_crit=False)
12141213
```
12151214

1216-
```{code-cell} python3
1215+
```{code-cell} ipython3
12171216
print(-kss+qss+pss*bss)
12181217
print(Vss)
12191218
print(𝜃1ss)
@@ -1229,7 +1228,7 @@ Thus, let’s see if the firm is actually maximizing its firm value given
12291228
the equilibrium pricing function $q(k,b)$ for equity and
12301229
$p(k,b)$ for bonds.
12311230

1232-
```{code-cell} python3
1231+
```{code-cell} ipython3
12331232
kgrid, bgrid, Vgrid, Qgrid, Pgrid = mdl.eq_valuation(c10ss, c11ss, c20ss, c21ss,N=30)
12341233
12351234
print('Maximum valuation of the firm value in the (k,B) grid: {:.5f}'.format(Vgrid.max()))
@@ -1246,7 +1245,7 @@ Below we will plot the firm’s value as a function of $k,b$.
12461245
We’ll also plot the equilibrium price functions $q(k,b)$ and
12471246
$p(k,b)$.
12481247

1249-
```{code-cell} python3
1248+
```{code-cell} ipython3
12501249
from IPython.display import Image
12511250
import matplotlib.pyplot as plt
12521251
from mpl_toolkits import mplot3d
@@ -1367,7 +1366,7 @@ $$
13671366

13681367
The function also outputs agent 1’s bond holdings $\xi_1$.
13691368

1370-
```{code-cell} python3
1369+
```{code-cell} ipython3
13711370
def off_eq_check(mdl,kss,bss,e=0.1):
13721371
# Big K and big B
13731372
k = kss
@@ -1603,7 +1602,7 @@ structure for the firm.
16031602
We first check the case in which $b^{**} = b^* - e$ where
16041603
$e = 0.1$:
16051604

1606-
```{code-cell} python3
1605+
```{code-cell} ipython3
16071606
#====================== Experiment 1 ======================#
16081607
Ve1,ke1,be1,pe1,qe1,c10e1,c11e1,c20e1,c21e1,𝜉1e1 = off_eq_check(mdl,
16091608
kss,
@@ -1655,7 +1654,7 @@ Therefore, $(k^*,b^{*}-e)$ would not be an equilibrium.
16551654

16561655
Next, we check for $b^{**} = b^* + e$.
16571656

1658-
```{code-cell} python3
1657+
```{code-cell} ipython3
16591658
#====================== Experiment 2 ======================#
16601659
Ve2,ke2,be2,pe2,qe2,c10e2,c11e2,c20e2,c21e2,𝜉1e2 = off_eq_check(mdl,
16611660
kss,
@@ -1708,7 +1707,7 @@ want to hold corporate debt.
17081707

17091708
For example, $\xi^1 > 0$:
17101709

1711-
```{code-cell} python3
1710+
```{code-cell} ipython3
17121711
print('Bond holdings of agent 1: {:.3f}'.format(𝜉1e2))
17131712
```
17141713

@@ -1726,7 +1725,7 @@ It is also interesting to look at the equilibrium price functions
17261725
$q(k,b)$ and $p(k,b)$ faced by firms in our rational
17271726
expectations equilibrium.
17281727

1729-
```{code-cell} python3
1728+
```{code-cell} ipython3
17301729
# Equity Valuation
17311730
fig = go.Figure(data=[go.Scatter3d(x=[kss],
17321731
y=[bss],
@@ -1756,7 +1755,7 @@ Image(fig.to_image(format="png"))
17561755
# code locally
17571756
```
17581757

1759-
```{code-cell} python3
1758+
```{code-cell} ipython3
17601759
# Bond Valuation
17611760
fig = go.Figure(data=[go.Scatter3d(x=[kss],
17621761
y=[bss],
@@ -1815,8 +1814,8 @@ $$
18151814
The function `valuations_by_agent` is used in calculating these
18161815
valuations.
18171816

1818-
```{code-cell} python3
1819-
:tags: ["hide-output"]
1817+
```{code-cell} ipython3
1818+
:tags: [hide-output]
18201819
18211820
# Lists for storage
18221821
wlist = []
@@ -1864,7 +1863,7 @@ for i in range(10):
18641863
p2list.append(P2)
18651864
```
18661865

1867-
```{code-cell} python3
1866+
```{code-cell} ipython3
18681867
# Plot
18691868
fig, ax = plt.subplots(3,2,figsize=(12,12))
18701869
ax[0,0].plot(wlist,klist)
@@ -1909,7 +1908,7 @@ Now let’s see how the two types of agents value bonds and equities,
19091908
keeping in mind that the type that values the asset highest determines
19101909
the equilibrium price (and thus the pertinent set of Big $C$’s).
19111910

1912-
```{code-cell} python3
1911+
```{code-cell} ipython3
19131912
# Comparing the prices
19141913
fig, ax = plt.subplots(1,3,figsize=(16,6))
19151914

lectures/_static/lecture_specific/opt_tax_recur/recursive_allocation.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,17 @@ def simulate(self, b0, s0, T, sHist=None):
131131
# Time 1 onward
132132
for t in range(1, T):
133133
s, x = sHist[t], xHist[t-1]
134-
cHist[t] = interp(self.x_grid, self.c1[:, s], x)
135-
nHist[t] = interp(self.x_grid, self.n1[:, s], x)
134+
cHist[t] = np.interp(x, self.x_grid, self.c1[:, s])
135+
nHist[t] = np.interp(x, self.x_grid, self.n1[:, s])
136136

137137
τHist[t] = self.τ(cHist[t], nHist[t])
138138

139139
Bhist[t] = x / Uc(cHist[t], 1-nHist[t])
140140

141141
c, n = np.empty((2, self.S))
142142
for sprime in range(self.S):
143-
c[sprime] = interp(x_grid, self.c1[:, sprime], x)
144-
n[sprime] = interp(x_grid, self.n1[:, sprime], x)
143+
c[sprime] = np.interp(x, x_grid, self.c1[:, sprime])
144+
n[sprime] = np.interp(x, x_grid, self.n1[:, sprime])
145145
Euc = π[sHist[t-1]] @ Uc(c, 1-n)
146146
RHist[t-1] = Uc(cHist[t-1], 1-nHist[t-1]) / (self.pref.β * Euc)
147147

@@ -150,7 +150,7 @@ def simulate(self, b0, s0, T, sHist=None):
150150

151151
if t < T-1:
152152
sprime = sHist[t+1]
153-
xHist[t] = interp(self.x_grid, self.xprime1[:, s, sprime], x)
153+
xHist[t] = np.interp(x, self.x_grid, self.xprime1[:, s, sprime])
154154

155155
return [cHist, nHist, Bhist, τHist, gHist, yHist, xHist, RHist]
156156

@@ -209,7 +209,7 @@ def obj_V(z_sub, x, s, V, pref, π, g, x_grid, b0=None):
209209
# prepare Vprime vector
210210
Vprime = np.empty(S)
211211
for sprime in range(S):
212-
Vprime[sprime] = interp(x_grid, V[:, sprime], xprime[sprime])
212+
Vprime[sprime] = np.interp(xprime[sprime], x_grid, V[:, sprime])
213213

214214
# compute the objective value
215215
obj = U(c, l) + β * π[s] @ Vprime

lectures/coase.md

+17-25
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ jupytext:
33
text_representation:
44
extension: .md
55
format_name: myst
6+
format_version: 0.13
7+
jupytext_version: 1.16.1
68
kernelspec:
7-
display_name: Python 3
9+
display_name: Python 3 (ipykernel)
810
language: python
911
name: python3
1012
---
@@ -24,15 +26,6 @@ kernelspec:
2426
:depth: 2
2527
```
2628

27-
In addition to what's in Anaconda, this lecture will need the following libraries:
28-
29-
```{code-cell} ipython
30-
---
31-
tags: [hide-output]
32-
---
33-
!pip install interpolation
34-
```
35-
3629
## Overview
3730

3831
In 1937, Ronald Coase wrote a brilliant essay on the nature of the firm {cite}`coase1937nature`.
@@ -57,12 +50,10 @@ Couldn't the associated within-firm planning be done more efficiently by the mar
5750

5851
We'll use the following imports:
5952

60-
```{code-cell} ipython
53+
```{code-cell} ipython3
6154
import numpy as np
6255
import matplotlib.pyplot as plt
63-
%matplotlib inline
6456
from scipy.optimize import fminbound
65-
from interpolation import interp
6657
```
6758

6859
### Why Firms Exist
@@ -447,7 +438,7 @@ At each iterate, we will use continuous piecewise linear interpolation of functi
447438

448439
To begin, here's a class to store primitives and a grid:
449440

450-
```{code-cell} python3
441+
```{code-cell} ipython3
451442
class ProductionChain:
452443
453444
def __init__(self,
@@ -464,7 +455,7 @@ Now let's implement and iterate with $T$ until convergence.
464455
Recalling that our initial condition must lie in $\mathcal P$, we set
465456
$p_0 = c$
466457

467-
```{code-cell} python3
458+
```{code-cell} ipython3
468459
def compute_prices(pc, tol=1e-5, max_iter=5000):
469460
"""
470461
Compute prices by iterating with T
@@ -481,7 +472,7 @@ def compute_prices(pc, tol=1e-5, max_iter=5000):
481472
482473
while error > tol and i < max_iter:
483474
for j, s in enumerate(grid):
484-
Tp = lambda t: delta * interp(grid, p, t) + c(s - t)
475+
Tp = lambda t: delta * np.interp(t, grid, p) + c(s - t)
485476
new_p[j] = Tp(fminbound(Tp, 0, s))
486477
error = np.max(np.abs(p - new_p))
487478
p = new_p
@@ -492,14 +483,14 @@ def compute_prices(pc, tol=1e-5, max_iter=5000):
492483
else:
493484
print(f"Warning: iteration hit upper bound {max_iter}")
494485
495-
p_func = lambda x: interp(grid, p, x)
486+
p_func = lambda x: np.interp(x, grid, p)
496487
return p_func
497488
```
498489

499490
The next function computes optimal choice of upstream boundary and range of
500491
task implemented for a firm face price function p_function and with downstream boundary $s$.
501492

502-
```{code-cell} python3
493+
```{code-cell} ipython3
503494
def optimal_choices(pc, p_function, s):
504495
"""
505496
Takes p_func as the true function, minimizes on [0,s]
@@ -524,7 +515,7 @@ their respective upstream boundary, treating the previous firm's upstream bounda
524515

525516
In doing so, we start with firm 1, who has downstream boundary $s=1$.
526517

527-
```{code-cell} python3
518+
```{code-cell} ipython3
528519
def compute_stages(pc, p_function):
529520
s = 1.0
530521
transaction_stages = [s]
@@ -539,7 +530,7 @@ Let's try this at the default parameters.
539530
The next figure shows the equilibrium price function, as well as the
540531
boundaries of firms as vertical lines
541532

542-
```{code-cell} python3
533+
```{code-cell} ipython3
543534
pc = ProductionChain()
544535
p_star = compute_prices(pc)
545536
@@ -558,7 +549,7 @@ plt.show()
558549
Here's the function $\ell^*$, which shows how large a firm with
559550
downstream boundary $s$ chooses to be
560551

561-
```{code-cell} python3
552+
```{code-cell} ipython3
562553
ell_star = np.empty(pc.n)
563554
for i, s in enumerate(pc.grid):
564555
t, e = optimal_choices(pc, p_star, s)
@@ -590,9 +581,9 @@ Check your intuition by computing the number of firms at delta in (1.01, 1.05, 1
590581
```{solution-start} coa_ex1
591582
:class: dropdown
592583
```
593-
Here is one solution
584+
Here is one solution
594585

595-
```{code-cell} python3
586+
```{code-cell} ipython3
596587
for delta in (1.01, 1.05, 1.1):
597588
598589
pc = ProductionChain(delta=delta)
@@ -601,6 +592,7 @@ for delta in (1.01, 1.05, 1.1):
601592
num_firms = len(transaction_stages)
602593
print(f"When delta={delta} there are {num_firms} firms")
603594
```
595+
604596
```{solution-end}
605597
```
606598

@@ -636,7 +628,7 @@ associated with diminishing returns to management --- which induce convexity ---
636628

637629
Here's one way to compute and graph value added across firms
638630

639-
```{code-cell} python3
631+
```{code-cell} ipython3
640632
pc = ProductionChain()
641633
p_star = compute_prices(pc)
642634
stages = compute_stages(pc, p_star)
@@ -654,4 +646,4 @@ plt.show()
654646
```
655647

656648
```{solution-end}
657-
```
649+
```

0 commit comments

Comments
 (0)