Skip to content

Commit 03196de

Browse files
kp992mmcky
andauthored
Use np.interp instead of interpolation.interp (#156)
* Use np.interp instead of interpolation.interp * apply suggestions * re-enable build failures on warnings * MAINT: update ci.yml workflow versions and report upload * Fix interp issues --------- Co-authored-by: mmcky <[email protected]>
1 parent 0ad4aeb commit 03196de

File tree

7 files changed

+72
-85
lines changed

7 files changed

+72
-85
lines changed

.github/workflows/ci.yml

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ jobs:
55
runs-on: ubuntu-latest
66
steps:
77
- name: Checkout
8-
uses: actions/checkout@v2
8+
uses: actions/checkout@v4
99
- name: Setup Anaconda
10-
uses: conda-incubator/setup-miniconda@v2
10+
uses: conda-incubator/setup-miniconda@v3
1111
with:
1212
auto-update-conda: true
1313
auto-activate-base: true
@@ -35,7 +35,7 @@ jobs:
3535
shell: bash -l {0}
3636
run: pip list
3737
- name: Download "build" folder (cache)
38-
uses: dawidd6/action-download-artifact@v2
38+
uses: dawidd6/action-download-artifact@v3
3939
with:
4040
workflow: cache.yml
4141
branch: main
@@ -61,14 +61,20 @@ jobs:
6161
shell: bash -l {0}
6262
run: |
6363
rm -r _build/.doctrees
64-
jb build lectures --path-output ./
64+
jb build lectures --path-output ./ -nW --keep-going
65+
- name: Upload Execution Reports (Download Notebooks)
66+
uses: actions/upload-artifact@v4
67+
if: failure()
68+
with:
69+
name: execution-reports
70+
path: _build/jupyter/reports
6571
- name: Save Build as Artifact
66-
uses: actions/upload-artifact@v1
72+
uses: actions/upload-artifact@v4
6773
with:
6874
name: _build
6975
path: _build
7076
- name: Preview Deploy to Netlify
71-
uses: nwtgck/actions-netlify@v1.1
77+
uses: nwtgck/actions-netlify@v2
7278
with:
7379
publish-dir: '_build/html/'
7480
production-branch: master

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

0 commit comments

Comments
 (0)