Skip to content

Commit 407b5fd

Browse files
authored
Merge pull request #387 from QuantEcon/interp_fixes
Use np.interp instead of interpolation.interp
2 parents ab08923 + 1145b7a commit 407b5fd

13 files changed

+26
-160
lines changed

lectures/_static/lecture_specific/optgrowth/bellman_operator.py

-53
This file was deleted.

lectures/cake_eating_numerical.md

+3-14
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ kernelspec:
1515
:depth: 2
1616
```
1717

18-
In addition to what's in Anaconda, this lecture will require the following library:
19-
20-
```{code-cell} ipython
21-
---
22-
tags: [hide-output]
23-
---
24-
!pip install interpolation
25-
```
26-
2718
## Overview
2819

2920
In this lecture we continue the study of {doc}`the cake eating problem <cake_eating_problem>`.
@@ -47,9 +38,7 @@ We will use the following imports:
4738

4839
```{code-cell} ipython
4940
import matplotlib.pyplot as plt
50-
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
5141
import numpy as np
52-
from interpolation import interp
5342
from scipy.optimize import minimize_scalar, bisect
5443
```
5544

@@ -211,7 +200,7 @@ class CakeEating:
211200
"""
212201
213202
u, β = self.u, self.β
214-
v = lambda x: interp(self.x_grid, v_array, x)
203+
v = lambda x: np.interp(x, self.x_grid, v_array)
215204
216205
return u(c) + β * v(x - c)
217206
```
@@ -533,7 +522,7 @@ class OptimalGrowth(CakeEating):
533522
"""
534523
535524
u, β, α = self.u, self.β, self.α
536-
v = lambda x: interp(self.x_grid, v_array, x)
525+
v = lambda x: np.interp(x, self.x_grid, v_array)
537526
538527
return u(c) + β * v((x - c)**α)
539528
```
@@ -609,7 +598,7 @@ def K(σ_array, ce):
609598
u_prime, β, x_grid = ce.u_prime, ce.β, ce.x_grid
610599
σ_new = np.empty_like(σ_array)
611600
612-
σ = lambda x: interp(x_grid, σ_array, x)
601+
σ = lambda x: np.interp(x, x_grid, σ_array)
613602
614603
def euler_diff(c, x):
615604
return u_prime(c) - β * u_prime(σ(x - c))

lectures/coleman_policy_iter.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie
3030
tags: [hide-output]
3131
---
3232
!pip install quantecon
33-
!pip install interpolation
3433
```
3534

3635
## Overview
@@ -62,9 +61,7 @@ Let's start with some imports:
6261

6362
```{code-cell} ipython
6463
import matplotlib.pyplot as plt
65-
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
6664
import numpy as np
67-
from interpolation import interp
6865
from quantecon.optimize import brentq
6966
from numba import njit
7067
```
@@ -301,7 +298,7 @@ def euler_diff(c, σ, y, og):
301298
f, f_prime, u_prime = og.f, og.f_prime, og.u_prime
302299
303300
# First turn σ into a function via interpolation
304-
σ_func = lambda x: interp(grid, σ, x)
301+
σ_func = lambda x: np.interp(x, grid, σ)
305302
306303
# Now set up the function we need to find the root of.
307304
vals = u_prime(σ_func(f(y - c) * shocks)) * f_prime(y - c) * shocks

lectures/egm_policy_iter.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ kernelspec:
2323
:depth: 2
2424
```
2525

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

3527
## Overview
3628

@@ -51,9 +43,7 @@ Let's start with some standard imports:
5143

5244
```{code-cell} ipython
5345
import matplotlib.pyplot as plt
54-
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
5546
import numpy as np
56-
from interpolation import interp
5747
from numba import njit
5848
```
5949

@@ -188,7 +178,7 @@ def K(σ_array, og):
188178
y = grid + σ_array # y_i = k_i + c_i
189179
190180
# Linear interpolation of policy using endogenous grid
191-
σ = lambda x: interp(y, σ_array, x)
181+
σ = lambda x: np.interp(x, y, σ_array)
192182
193183
# Allocate memory for new consumption array
194184
c = np.empty_like(grid)

lectures/ifp.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie
3030
tags: [hide-output]
3131
---
3232
!pip install quantecon
33-
!pip install interpolation
3433
```
3534

3635
## Overview
@@ -60,10 +59,8 @@ We'll need the following imports:
6059

6160
```{code-cell} ipython
6261
import matplotlib.pyplot as plt
63-
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
6462
import numpy as np
6563
from quantecon.optimize import brentq
66-
from interpolation import interp
6764
from numba import njit, float64
6865
from numba.experimental import jitclass
6966
from quantecon import MarkovChain
@@ -437,7 +434,7 @@ def euler_diff(c, a, z, σ_vals, ifp):
437434
438435
# Convert policy into a function by linear interpolation
439436
def σ(a, z):
440-
return interp(asset_grid, σ_vals[:, z], a)
437+
return np.interp(a, asset_grid, σ_vals[:, z])
441438
442439
# Calculate the expectation conditional on current z
443440
expect = 0.0
@@ -663,7 +660,7 @@ def compute_asset_series(ifp, T=500_000, seed=1234):
663660
664661
# Solve for the optimal policy
665662
σ_star = solve_model_time_iter(ifp, σ_init, verbose=False)
666-
σ = lambda a, z: interp(ifp.asset_grid, σ_star[:, z], a)
663+
σ = lambda a, z: np.interp(a, ifp.asset_grid, σ_star[:, z])
667664
668665
# Simulate the exogeneous state process
669666
mc = MarkovChain(P)

lectures/ifp_advanced.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie
3030
tags: [hide-output]
3131
---
3232
!pip install quantecon
33-
!pip install interpolation
3433
```
3534

3635
## Overview
@@ -57,9 +56,7 @@ We require the following imports:
5756

5857
```{code-cell} ipython
5958
import matplotlib.pyplot as plt
60-
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
6159
import numpy as np
62-
from interpolation import interp
6360
from numba import njit, float64
6461
from numba.experimental import jitclass
6562
from quantecon import MarkovChain
@@ -436,7 +433,7 @@ def K(a_in, σ_in, ifp):
436433
n = len(P)
437434
438435
# Create consumption function by linear interpolation
439-
σ = lambda a, z: interp(a_in[:, z], σ_in[:, z], a)
436+
σ = lambda a, z: np.interp(a, a_in[:, z], σ_in[:, z])
440437
441438
# Allocate memory
442439
σ_out = np.empty_like(σ_in)
@@ -636,7 +633,7 @@ def compute_asset_series(ifp, a_star, σ_star, z_seq, T=500_000):
636633
"""
637634
638635
# Create consumption function by linear interpolation
639-
σ = lambda a, z: interp(a_star[:, z], σ_star[:, z], a)
636+
σ = lambda a, z: np.interp(a, a_star[:, z], σ_star[:, z])
640637
641638
# Simulate the asset path
642639
a = np.zeros(T+1)

lectures/jv.md

+3-14
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ kernelspec:
2727
:depth: 2
2828
```
2929

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

4132
In this section, we solve a simple on-the-job search model
@@ -46,10 +37,8 @@ Let's start with some imports:
4637

4738
```{code-cell} ipython
4839
import matplotlib.pyplot as plt
49-
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
5040
import numpy as np
5141
import scipy.stats as stats
52-
from interpolation import interp
5342
from numba import njit, prange
5443
```
5544

@@ -269,7 +258,7 @@ def operator_factory(jv, parallel_flag=True):
269258
@njit
270259
def state_action_values(z, x, v):
271260
s, ϕ = z
272-
v_func = lambda x: interp(x_grid, v, x)
261+
v_func = lambda x: np.interp(x, x_grid, v)
273262
274263
integral = 0
275264
for m in range(mc_size):
@@ -460,8 +449,8 @@ v_star = solve_model(jv, verbose=False)
460449
s_policy, ϕ_policy = get_greedy(v_star)
461450
462451
# Turn the policy function arrays into actual functions
463-
s = lambda y: interp(x_grid, s_policy, y)
464-
ϕ = lambda y: interp(x_grid, ϕ_policy, y)
452+
s = lambda y: np.interp(y, x_grid, s_policy)
453+
ϕ = lambda y: np.interp(y, x_grid, ϕ_policy)
465454
466455
def h(x, b, u):
467456
return (1 - b) * g(x, ϕ(x)) + b * max(g(x, ϕ(x)), u)

lectures/mccall_correlated.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie
3030
tags: [hide-output]
3131
---
3232
!pip install quantecon
33-
!pip install interpolation
3433
```
3534

3635
## Overview
@@ -48,10 +47,8 @@ We will use the following imports:
4847

4948
```{code-cell} ipython3
5049
import matplotlib.pyplot as plt
51-
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
5250
import numpy as np
5351
import quantecon as qe
54-
from interpolation import interp
5552
from numpy.random import randn
5653
from numba import njit, prange, float64
5754
from numba.experimental import jitclass
@@ -245,7 +242,7 @@ def Q(js, f_in, f_out):
245242
for m in range(M):
246243
e1, e2 = js.e_draws[:, m]
247244
z_next = d + ρ * z + σ * e1
248-
go_val = interp(js.z_grid, f_in, z_next) # f(z')
245+
go_val = np.interp(z_next, js.z_grid, f_in) # f(z')
249246
y_next = np.exp(μ + s * e2) # y' draw
250247
w_next = np.exp(z_next) + y_next # w' draw
251248
stop_val = np.log(w_next) / (1 - β)
@@ -353,7 +350,7 @@ def compute_unemployment_duration(js, seed=1234):
353350
354351
@njit
355352
def f_star_function(z):
356-
return interp(z_grid, f_star, z)
353+
return np.interp(z, z_grid, f_star)
357354
358355
@njit
359356
def draw_tau(t_max=10_000):

lectures/mccall_fitted_vfi.md

+3-13
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ kernelspec:
2323
:depth: 2
2424
```
2525

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

3527
## Overview
3628

@@ -58,9 +50,7 @@ We will use the following imports:
5850

5951
```{code-cell} ipython3
6052
import matplotlib.pyplot as plt
61-
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
6253
import numpy as np
63-
from interpolation import interp
6454
from numba import njit, float64
6555
from numba.experimental import jitclass
6656
```
@@ -155,7 +145,7 @@ This method
155145
{cite}`gordon1995stable` or {cite}`stachurski2008continuous`) and
156146
1. preserves useful shape properties such as monotonicity and concavity/convexity.
157147

158-
Linear interpolation will be implemented using a JIT-aware Python interpolation library called [interpolation.py](https://github.com/EconForge/interpolation.py).
148+
Linear interpolation will be implemented using [numpy.interp](https://numpy.org/doc/stable/reference/generated/numpy.interp.html).
159149

160150
The next figure illustrates piecewise linear interpolation of an arbitrary
161151
function on grid points $0, 0.2, 0.4, 0.6, 0.8, 1$.
@@ -169,7 +159,7 @@ c_grid = np.linspace(0, 1, 6)
169159
f_grid = np.linspace(0, 1, 150)
170160
171161
def Af(x):
172-
return interp(c_grid, f(c_grid), x)
162+
return np.interp(x, c_grid, f(c_grid))
173163
174164
fig, ax = plt.subplots()
175165
@@ -238,7 +228,7 @@ class McCallModelContinuous:
238228
u = lambda x: np.log(x)
239229
240230
# Interpolate array represented value function
241-
vf = lambda x: interp(w, v, x)
231+
vf = lambda x: np.interp(x, w, v)
242232
243233
# Update d using Monte Carlo to evaluate integral
244234
d_new = np.mean(np.maximum(vf(self.w_draws), u(c) + β * d))

0 commit comments

Comments
 (0)