Skip to content

Commit 0790469

Browse files
committed
Use np.interp instead of interpolation.interp
1 parent ab08923 commit 0790469

File tree

5 files changed

+9
-39
lines changed

5 files changed

+9
-39
lines changed

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)

0 commit comments

Comments
 (0)