diff --git a/lectures/_static/lecture_specific/optgrowth/bellman_operator.py b/lectures/_static/lecture_specific/optgrowth/bellman_operator.py deleted file mode 100644 index b7c905484..000000000 --- a/lectures/_static/lecture_specific/optgrowth/bellman_operator.py +++ /dev/null @@ -1,53 +0,0 @@ -import numpy as np -from interpolation import interp -from numba import njit, prange -from quantecon.optimize.scalar_maximization import brent_max - - -def operator_factory(og, parallel_flag=True): - """ - A function factory for building the Bellman operator, as well as - a function that computes greedy policies. - - Here og is an instance of OptimalGrowthModel. - """ - - f, u, β = og.f, og.u, og.β - grid, shocks = og.grid, og.shocks - - @njit - def objective(c, v, y): - """ - The right-hand side of the Bellman equation - """ - # First turn v into a function via interpolation - v_func = lambda x: interp(grid, v, x) - return u(c) + β * np.mean(v_func(f(y - c) * shocks)) - - @njit(parallel=parallel_flag) - def T(v): - """ - The Bellman operator - """ - v_new = np.empty_like(v) - for i in prange(len(grid)): - y = grid[i] - # Solve for optimal v at y - v_max = brent_max(objective, 1e-10, y, args=(v, y))[1] - v_new[i] = v_max - return v_new - - @njit - def get_greedy(v): - """ - Computes the v-greedy policy of a given function v - """ - σ = np.empty_like(v) - for i in range(len(grid)): - y = grid[i] - # Solve for optimal c at y - c_max = brent_max(objective, 1e-10, y, args=(v, y))[0] - σ[i] = c_max - return σ - - return T, get_greedy diff --git a/lectures/cake_eating_numerical.md b/lectures/cake_eating_numerical.md index 839c9f8fb..3a0c2c3aa 100644 --- a/lectures/cake_eating_numerical.md +++ b/lectures/cake_eating_numerical.md @@ -15,15 +15,6 @@ kernelspec: :depth: 2 ``` -In addition to what's in Anaconda, this lecture will require the following library: - -```{code-cell} ipython ---- -tags: [hide-output] ---- -!pip install interpolation -``` - ## Overview In this lecture we continue the study of {doc}`the cake eating problem `. @@ -47,9 +38,7 @@ We will use the following imports: ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np -from interpolation import interp from scipy.optimize import minimize_scalar, bisect ``` @@ -211,7 +200,7 @@ class CakeEating: """ u, β = self.u, self.β - v = lambda x: interp(self.x_grid, v_array, x) + v = lambda x: np.interp(x, self.x_grid, v_array) return u(c) + β * v(x - c) ``` @@ -533,7 +522,7 @@ class OptimalGrowth(CakeEating): """ u, β, α = self.u, self.β, self.α - v = lambda x: interp(self.x_grid, v_array, x) + v = lambda x: np.interp(x, self.x_grid, v_array) return u(c) + β * v((x - c)**α) ``` @@ -609,7 +598,7 @@ def K(σ_array, ce): u_prime, β, x_grid = ce.u_prime, ce.β, ce.x_grid σ_new = np.empty_like(σ_array) - σ = lambda x: interp(x_grid, σ_array, x) + σ = lambda x: np.interp(x, x_grid, σ_array) def euler_diff(c, x): return u_prime(c) - β * u_prime(σ(x - c)) diff --git a/lectures/coleman_policy_iter.md b/lectures/coleman_policy_iter.md index 1c0a9c114..1eade63c6 100644 --- a/lectures/coleman_policy_iter.md +++ b/lectures/coleman_policy_iter.md @@ -30,7 +30,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie tags: [hide-output] --- !pip install quantecon -!pip install interpolation ``` ## Overview @@ -62,9 +61,7 @@ Let's start with some imports: ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np -from interpolation import interp from quantecon.optimize import brentq from numba import njit ``` @@ -301,7 +298,7 @@ def euler_diff(c, σ, y, og): f, f_prime, u_prime = og.f, og.f_prime, og.u_prime # First turn σ into a function via interpolation - σ_func = lambda x: interp(grid, σ, x) + σ_func = lambda x: np.interp(x, grid, σ) # Now set up the function we need to find the root of. vals = u_prime(σ_func(f(y - c) * shocks)) * f_prime(y - c) * shocks diff --git a/lectures/egm_policy_iter.md b/lectures/egm_policy_iter.md index 8e578b3c5..6350499a1 100644 --- a/lectures/egm_policy_iter.md +++ b/lectures/egm_policy_iter.md @@ -23,14 +23,6 @@ kernelspec: :depth: 2 ``` -In addition to what's in Anaconda, this lecture will need the following libraries: - -```{code-cell} ipython ---- -tags: [hide-output] ---- -!pip install interpolation -``` ## Overview @@ -51,9 +43,7 @@ Let's start with some standard imports: ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np -from interpolation import interp from numba import njit ``` @@ -188,7 +178,7 @@ def K(σ_array, og): y = grid + σ_array # y_i = k_i + c_i # Linear interpolation of policy using endogenous grid - σ = lambda x: interp(y, σ_array, x) + σ = lambda x: np.interp(x, y, σ_array) # Allocate memory for new consumption array c = np.empty_like(grid) diff --git a/lectures/ifp.md b/lectures/ifp.md index 07e421fe3..06eaf1984 100644 --- a/lectures/ifp.md +++ b/lectures/ifp.md @@ -30,7 +30,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie tags: [hide-output] --- !pip install quantecon -!pip install interpolation ``` ## Overview @@ -60,10 +59,8 @@ We'll need the following imports: ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np from quantecon.optimize import brentq -from interpolation import interp from numba import njit, float64 from numba.experimental import jitclass from quantecon import MarkovChain @@ -437,7 +434,7 @@ def euler_diff(c, a, z, σ_vals, ifp): # Convert policy into a function by linear interpolation def σ(a, z): - return interp(asset_grid, σ_vals[:, z], a) + return np.interp(a, asset_grid, σ_vals[:, z]) # Calculate the expectation conditional on current z expect = 0.0 @@ -663,7 +660,7 @@ def compute_asset_series(ifp, T=500_000, seed=1234): # Solve for the optimal policy σ_star = solve_model_time_iter(ifp, σ_init, verbose=False) - σ = lambda a, z: interp(ifp.asset_grid, σ_star[:, z], a) + σ = lambda a, z: np.interp(a, ifp.asset_grid, σ_star[:, z]) # Simulate the exogeneous state process mc = MarkovChain(P) diff --git a/lectures/ifp_advanced.md b/lectures/ifp_advanced.md index df0da9301..259b6ffdf 100644 --- a/lectures/ifp_advanced.md +++ b/lectures/ifp_advanced.md @@ -30,7 +30,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie tags: [hide-output] --- !pip install quantecon -!pip install interpolation ``` ## Overview @@ -57,9 +56,7 @@ We require the following imports: ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np -from interpolation import interp from numba import njit, float64 from numba.experimental import jitclass from quantecon import MarkovChain @@ -436,7 +433,7 @@ def K(a_in, σ_in, ifp): n = len(P) # Create consumption function by linear interpolation - σ = lambda a, z: interp(a_in[:, z], σ_in[:, z], a) + σ = lambda a, z: np.interp(a, a_in[:, z], σ_in[:, z]) # Allocate memory σ_out = np.empty_like(σ_in) @@ -636,7 +633,7 @@ def compute_asset_series(ifp, a_star, σ_star, z_seq, T=500_000): """ # Create consumption function by linear interpolation - σ = lambda a, z: interp(a_star[:, z], σ_star[:, z], a) + σ = lambda a, z: np.interp(a, a_star[:, z], σ_star[:, z]) # Simulate the asset path a = np.zeros(T+1) diff --git a/lectures/jv.md b/lectures/jv.md index db750fae8..7ebc8cfd1 100644 --- a/lectures/jv.md +++ b/lectures/jv.md @@ -27,15 +27,6 @@ kernelspec: :depth: 2 ``` -In addition to what's in Anaconda, this lecture will need the following libraries: - -```{code-cell} ipython ---- -tags: [hide-output] ---- -!pip install interpolation -``` - ## Overview In this section, we solve a simple on-the-job search model @@ -46,10 +37,8 @@ Let's start with some imports: ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np import scipy.stats as stats -from interpolation import interp from numba import njit, prange ``` @@ -269,7 +258,7 @@ def operator_factory(jv, parallel_flag=True): @njit def state_action_values(z, x, v): s, ϕ = z - v_func = lambda x: interp(x_grid, v, x) + v_func = lambda x: np.interp(x, x_grid, v) integral = 0 for m in range(mc_size): @@ -460,8 +449,8 @@ v_star = solve_model(jv, verbose=False) s_policy, ϕ_policy = get_greedy(v_star) # Turn the policy function arrays into actual functions -s = lambda y: interp(x_grid, s_policy, y) -ϕ = lambda y: interp(x_grid, ϕ_policy, y) +s = lambda y: np.interp(y, x_grid, s_policy) +ϕ = lambda y: np.interp(y, x_grid, ϕ_policy) def h(x, b, u): return (1 - b) * g(x, ϕ(x)) + b * max(g(x, ϕ(x)), u) diff --git a/lectures/mccall_correlated.md b/lectures/mccall_correlated.md index 18711c418..8ab7c23fa 100644 --- a/lectures/mccall_correlated.md +++ b/lectures/mccall_correlated.md @@ -30,7 +30,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie tags: [hide-output] --- !pip install quantecon -!pip install interpolation ``` ## Overview @@ -48,10 +47,8 @@ We will use the following imports: ```{code-cell} ipython3 import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np import quantecon as qe -from interpolation import interp from numpy.random import randn from numba import njit, prange, float64 from numba.experimental import jitclass @@ -245,7 +242,7 @@ def Q(js, f_in, f_out): for m in range(M): e1, e2 = js.e_draws[:, m] z_next = d + ρ * z + σ * e1 - go_val = interp(js.z_grid, f_in, z_next) # f(z') + go_val = np.interp(z_next, js.z_grid, f_in) # f(z') y_next = np.exp(μ + s * e2) # y' draw w_next = np.exp(z_next) + y_next # w' draw stop_val = np.log(w_next) / (1 - β) @@ -353,7 +350,7 @@ def compute_unemployment_duration(js, seed=1234): @njit def f_star_function(z): - return interp(z_grid, f_star, z) + return np.interp(z, z_grid, f_star) @njit def draw_tau(t_max=10_000): diff --git a/lectures/mccall_fitted_vfi.md b/lectures/mccall_fitted_vfi.md index ba54549a6..bfb95d486 100644 --- a/lectures/mccall_fitted_vfi.md +++ b/lectures/mccall_fitted_vfi.md @@ -23,14 +23,6 @@ kernelspec: :depth: 2 ``` -In addition to what's in Anaconda, this lecture will need the following libraries: - -```{code-cell} ipython ---- -tags: [hide-output] ---- -!pip install interpolation -``` ## Overview @@ -58,9 +50,7 @@ We will use the following imports: ```{code-cell} ipython3 import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np -from interpolation import interp from numba import njit, float64 from numba.experimental import jitclass ``` @@ -155,7 +145,7 @@ This method {cite}`gordon1995stable` or {cite}`stachurski2008continuous`) and 1. preserves useful shape properties such as monotonicity and concavity/convexity. -Linear interpolation will be implemented using a JIT-aware Python interpolation library called [interpolation.py](https://github.com/EconForge/interpolation.py). +Linear interpolation will be implemented using [numpy.interp](https://numpy.org/doc/stable/reference/generated/numpy.interp.html). The next figure illustrates piecewise linear interpolation of an arbitrary 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) f_grid = np.linspace(0, 1, 150) def Af(x): - return interp(c_grid, f(c_grid), x) + return np.interp(x, c_grid, f(c_grid)) fig, ax = plt.subplots() @@ -238,7 +228,7 @@ class McCallModelContinuous: u = lambda x: np.log(x) # Interpolate array represented value function - vf = lambda x: interp(w, v, x) + vf = lambda x: np.interp(x, w, v) # Update d using Monte Carlo to evaluate integral d_new = np.mean(np.maximum(vf(self.w_draws), u(c) + β * d)) diff --git a/lectures/navy_captain.md b/lectures/navy_captain.md index 6a2da8795..f9a97a1c9 100644 --- a/lectures/navy_captain.md +++ b/lectures/navy_captain.md @@ -24,22 +24,12 @@ kernelspec: :depth: 2 ``` -In addition to what's in Anaconda, this lecture will need the following libraries: - -```{code-cell} ipython ---- -tags: [hide-output] ---- -!pip install interpolation -``` ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np from numba import njit, prange, float64, int64 from numba.experimental import jitclass -from interpolation import interp from math import gamma from scipy.optimize import minimize ``` @@ -496,7 +486,7 @@ def Q(h, wf): κ = wf.κ h_new = np.empty_like(π_grid) - h_func = lambda p: interp(π_grid, h, p) + h_func = lambda p: np.interp(p, π_grid, h) for i in prange(len(π_grid)): π = π_grid[i] @@ -679,7 +669,7 @@ def V_q(wf, flag): for j in prange(len(z_arr)): π_next = wf.κ(z_arr[j], π) - V[i] += wf.c + interp(wf.π_grid, V_old, π_next) + V[i] += wf.c + np.interp(π_next, wf.π_grid, V_old) V[i] /= wf.mc_size diff --git a/lectures/odu.md b/lectures/odu.md index 0bf08adbe..0cfe26943 100644 --- a/lectures/odu.md +++ b/lectures/odu.md @@ -58,9 +58,8 @@ Let’s start with some imports ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size from numba import njit, prange, vectorize -from interpolation import mlinterp, interp +from interpolation import mlinterp from math import gamma import numpy as np from matplotlib import cm @@ -633,7 +632,7 @@ def Q_factory(sp, parallel_flag=True): @njit def ω_func(p, ω): - return interp(π_grid, ω, p) + return np.interp(p, π_grid, ω) @njit def κ(w, π): @@ -783,7 +782,7 @@ w_bar = solve_wbar(sp, verbose=False) # Interpolate reservation wage function π_grid = sp.π_grid -w_func = njit(lambda x: interp(π_grid, w_bar, x)) +w_func = njit(lambda x: np.interp(x, π_grid, w_bar)) @njit def update(a, b, e, π): @@ -907,7 +906,7 @@ def empirical_dist(F_a, F_b, G_a, G_b, w_bar, π_grid, π = π * lw / (π * lw + 1 - π) # move to next agent if accepts - if w >= interp(π_grid, w_bar, π): + if w >= np.interp(π, π_grid, w_bar): break # record the unemployment duration diff --git a/lectures/optgrowth_fast.md b/lectures/optgrowth_fast.md index 0feedfba9..dd07a1ffc 100644 --- a/lectures/optgrowth_fast.md +++ b/lectures/optgrowth_fast.md @@ -31,7 +31,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie tags: [hide-output] --- !pip install quantecon -!pip install interpolation ``` ## Overview @@ -61,17 +60,11 @@ Let's start with some imports: ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np -from interpolation import interp from numba import jit, njit from quantecon.optimize.scalar_maximization import brent_max ``` -We are using an interpolation function from -[interpolation.py](https://github.com/EconForge/interpolation.py) because it -helps us JIT-compile our code. - The function `brent_max` is also designed for embedding in JIT-compiled code. These are alternatives to similar functions in SciPy (which, unfortunately, are not JIT-aware). @@ -152,7 +145,7 @@ def state_action_value(c, y, v_array, og): u, f, β, shocks = og.u, og.f, og.β, og.shocks - v = lambda x: interp(og.grid, v_array, x) + v = lambda x: np.interp(x, og.grid, v_array) return u(c) + β * np.mean(v(f(y - c) * shocks)) ``` @@ -398,7 +391,7 @@ for β in (0.8, 0.9, 0.98): v_greedy, v_solution = solve_model(og, verbose=False) # Define an optimal policy function - σ_func = lambda x: interp(og.grid, v_greedy, x) + σ_func = lambda x: np.interp(x, og.grid, v_greedy) y = simulate_og(σ_func, og) ax.plot(y, lw=2, alpha=0.6, label=rf'$\beta = {β}$') diff --git a/lectures/wald_friedman.md b/lectures/wald_friedman.md index ffcc047ea..b64437851 100644 --- a/lectures/wald_friedman.md +++ b/lectures/wald_friedman.md @@ -31,14 +31,6 @@ kernelspec: :depth: 2 ``` -In addition to what's in Anaconda, this lecture will need the following libraries: - -```{code-cell} ipython3 -:tags: [hide-output] - -!pip install interpolation -``` - ## Overview This lecture describes a statistical decision problem presented to Milton @@ -69,7 +61,6 @@ import numpy as np import matplotlib.pyplot as plt from numba import jit, prange, float64, int64 from numba.experimental import jitclass -from interpolation import interp from math import gamma ``` @@ -487,7 +478,7 @@ def Q(h, wf): κ = wf.κ h_new = np.empty_like(π_grid) - h_func = lambda p: interp(π_grid, h, p) + h_func = lambda p: np.interp(p, π_grid, h) for i in prange(len(π_grid)): π = π_grid[i]