Skip to content

Commit 262f180

Browse files
committed
Use np.interp instead of interpolation.interp
1 parent 0ad4aeb commit 262f180

File tree

5 files changed

+60
-71
lines changed

5 files changed

+60
-71
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

+28-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
@@ -1943,3 +1942,7 @@ Agents of type 2 value bonds more highly (they want more hedging).
19431942

19441943
Taken together with our earlier plot of equity holdings, these graphs confirm our earlier conjecture that while both type
19451944
of agents hold equities, only agents of type 2 holds bonds.
1945+
1946+
```{code-cell} ipython3
1947+
1948+
```

lectures/coase.md

+18-24
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,14 +26,7 @@ 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-
```
29+
+++
3530

3631
## Overview
3732

@@ -57,12 +52,10 @@ Couldn't the associated within-firm planning be done more efficiently by the mar
5752

5853
We'll use the following imports:
5954

60-
```{code-cell} ipython
55+
```{code-cell} ipython3
6156
import numpy as np
6257
import matplotlib.pyplot as plt
63-
%matplotlib inline
6458
from scipy.optimize import fminbound
65-
from interpolation import interp
6659
```
6760

6861
### Why Firms Exist
@@ -447,7 +440,7 @@ At each iterate, we will use continuous piecewise linear interpolation of functi
447440

448441
To begin, here's a class to store primitives and a grid:
449442

450-
```{code-cell} python3
443+
```{code-cell} ipython3
451444
class ProductionChain:
452445
453446
def __init__(self,
@@ -464,7 +457,7 @@ Now let's implement and iterate with $T$ until convergence.
464457
Recalling that our initial condition must lie in $\mathcal P$, we set
465458
$p_0 = c$
466459

467-
```{code-cell} python3
460+
```{code-cell} ipython3
468461
def compute_prices(pc, tol=1e-5, max_iter=5000):
469462
"""
470463
Compute prices by iterating with T
@@ -481,7 +474,7 @@ def compute_prices(pc, tol=1e-5, max_iter=5000):
481474
482475
while error > tol and i < max_iter:
483476
for j, s in enumerate(grid):
484-
Tp = lambda t: delta * interp(grid, p, t) + c(s - t)
477+
Tp = lambda t: delta * np.interp(t, grid, p) + c(s - t)
485478
new_p[j] = Tp(fminbound(Tp, 0, s))
486479
error = np.max(np.abs(p - new_p))
487480
p = new_p
@@ -492,14 +485,14 @@ def compute_prices(pc, tol=1e-5, max_iter=5000):
492485
else:
493486
print(f"Warning: iteration hit upper bound {max_iter}")
494487
495-
p_func = lambda x: interp(grid, p, x)
488+
p_func = lambda x: np.interp(x, grid, p)
496489
return p_func
497490
```
498491

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

502-
```{code-cell} python3
495+
```{code-cell} ipython3
503496
def optimal_choices(pc, p_function, s):
504497
"""
505498
Takes p_func as the true function, minimizes on [0,s]
@@ -524,7 +517,7 @@ their respective upstream boundary, treating the previous firm's upstream bounda
524517

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

527-
```{code-cell} python3
520+
```{code-cell} ipython3
528521
def compute_stages(pc, p_function):
529522
s = 1.0
530523
transaction_stages = [s]
@@ -539,7 +532,7 @@ Let's try this at the default parameters.
539532
The next figure shows the equilibrium price function, as well as the
540533
boundaries of firms as vertical lines
541534

542-
```{code-cell} python3
535+
```{code-cell} ipython3
543536
pc = ProductionChain()
544537
p_star = compute_prices(pc)
545538
@@ -558,7 +551,7 @@ plt.show()
558551
Here's the function $\ell^*$, which shows how large a firm with
559552
downstream boundary $s$ chooses to be
560553

561-
```{code-cell} python3
554+
```{code-cell} ipython3
562555
ell_star = np.empty(pc.n)
563556
for i, s in enumerate(pc.grid):
564557
t, e = optimal_choices(pc, p_star, s)
@@ -590,9 +583,9 @@ Check your intuition by computing the number of firms at delta in (1.01, 1.05, 1
590583
```{solution-start} coa_ex1
591584
:class: dropdown
592585
```
593-
Here is one solution
586+
Here is one solution
594587

595-
```{code-cell} python3
588+
```{code-cell} ipython3
596589
for delta in (1.01, 1.05, 1.1):
597590
598591
pc = ProductionChain(delta=delta)
@@ -601,6 +594,7 @@ for delta in (1.01, 1.05, 1.1):
601594
num_firms = len(transaction_stages)
602595
print(f"When delta={delta} there are {num_firms} firms")
603596
```
597+
604598
```{solution-end}
605599
```
606600

@@ -636,7 +630,7 @@ associated with diminishing returns to management --- which induce convexity ---
636630

637631
Here's one way to compute and graph value added across firms
638632

639-
```{code-cell} python3
633+
```{code-cell} ipython3
640634
pc = ProductionChain()
641635
p_star = compute_prices(pc)
642636
stages = compute_stages(pc, p_star)
@@ -654,4 +648,4 @@ plt.show()
654648
```
655649

656650
```{solution-end}
657-
```
651+
```

0 commit comments

Comments
 (0)