From 9ec9e13edbb94df61701e54087fed2554e1ea7f5 Mon Sep 17 00:00:00 2001 From: Matt McKay Date: Mon, 18 Nov 2024 16:47:28 +1100 Subject: [PATCH] MAINT: update @jit(nopython=True) to @jit with numba>=0.59 (#395) * MAINT: update @jit(nopython=True) to @jit with numba>=0.59 * update @njit to @jit * update imports njit to jit and usage * minor fixes for import * remove njit from cass_koopmans_1 * remove njit from ak2 --- lectures/aiyagari.md | 6 ++--- lectures/ak2.md | 14 ++++++------ lectures/career.md | 10 ++++----- lectures/cass_koopmans_1.md | 20 ++++++++--------- lectures/cass_koopmans_2.md | 16 +++++++------- lectures/coleman_policy_iter.md | 6 ++--- lectures/egm_policy_iter.md | 4 ++-- lectures/eig_circulant.md | 6 ++--- lectures/exchangeable.md | 18 +++++++-------- lectures/ifp.md | 6 ++--- lectures/ifp_advanced.md | 6 ++--- lectures/imp_sample.md | 12 +++++----- lectures/inventory_dynamics.md | 6 ++--- lectures/jv.md | 10 ++++----- lectures/kesten_processes.md | 4 ++-- lectures/likelihood_bayes.md | 16 +++++++------- lectures/likelihood_ratio_process.md | 12 +++++----- lectures/mccall_correlated.md | 10 ++++----- lectures/mccall_fitted_vfi.md | 8 +++---- lectures/mccall_model.md | 10 ++++----- lectures/mccall_model_with_separation.md | 8 +++---- lectures/mccall_q.md | 2 +- lectures/mix_model.md | 14 ++++++------ lectures/multi_hyper.md | 4 ++-- lectures/multivariate_normal.md | 4 ++-- lectures/navy_captain.md | 16 +++++++------- lectures/odu.md | 28 ++++++++++++------------ lectures/optgrowth_fast.md | 6 ++--- lectures/perm_income.md | 6 ++--- lectures/wald_friedman.md | 6 ++--- lectures/wealth_dynamics.md | 6 ++--- 31 files changed, 150 insertions(+), 150 deletions(-) diff --git a/lectures/aiyagari.md b/lectures/aiyagari.md index 362e922c7..b465f1947 100644 --- a/lectures/aiyagari.md +++ b/lectures/aiyagari.md @@ -283,7 +283,7 @@ class Household: # Do the hard work using JIT-ed functions -@jit(nopython=True) +@jit def populate_R(R, a_size, z_size, a_vals, z_vals, r, w): n = a_size * z_size for s_i in range(n): @@ -297,7 +297,7 @@ def populate_R(R, a_size, z_size, a_vals, z_vals, r, w): if c > 0: R[s_i, new_a_i] = np.log(c) # Utility -@jit(nopython=True) +@jit def populate_Q(Q, a_size, z_size, Π): n = a_size * z_size for s_i in range(n): @@ -307,7 +307,7 @@ def populate_Q(Q, a_size, z_size, Π): Q[s_i, a_i, a_i*z_size + next_z_i] = Π[z_i, next_z_i] -@jit(nopython=True) +@jit def asset_marginal(s_probs, a_size, z_size): a_probs = np.zeros(a_size) for a_i in range(a_size): diff --git a/lectures/ak2.md b/lectures/ak2.md index 67726502c..91a737c1b 100644 --- a/lectures/ak2.md +++ b/lectures/ak2.md @@ -408,7 +408,7 @@ $$ ```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt -from numba import njit +from numba import jit from quantecon.optimize import brent_max ``` @@ -433,22 +433,22 @@ Knowing $\hat K$, we can calculate other equilibrium objects. Let's first define some Python helper functions. ```{code-cell} ipython3 -@njit +@jit def K_to_Y(K, α): return K ** α -@njit +@jit def K_to_r(K, α): return α * K ** (α - 1) -@njit +@jit def K_to_W(K, α): return (1 - α) * K ** α -@njit +@jit def K_to_C(K, D, τ, r, α, β): # optimal consumption for the old when δ=0 @@ -913,7 +913,7 @@ Let's implement this "guess and verify" approach We start by defining the Cobb-Douglas utility function ```{code-cell} ipython3 -@njit +@jit def U(Cy, Co, β): return (Cy ** β) * (Co ** (1-β)) @@ -924,7 +924,7 @@ We use `Cy_val` to compute the lifetime value of an arbitrary consumption plan, Note that it requires knowing future prices $r_{t+1}$ and tax rate $\tau_{t+1}$. ```{code-cell} ipython3 -@njit +@jit def Cy_val(Cy, W, r_next, τ, τ_next, δy, δo_next, β): # Co given by the budget constraint diff --git a/lectures/career.md b/lectures/career.md index 19047c780..ab2e43a90 100644 --- a/lectures/career.md +++ b/lectures/career.md @@ -51,7 +51,7 @@ import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np import quantecon as qe -from numba import njit, prange +from numba import jit, prange from quantecon.distributions import BetaBinomial from scipy.special import binom, beta from mpl_toolkits.mplot3d.axes3d import Axes3D @@ -234,7 +234,7 @@ def operator_factory(cw, parallel_flag=True): F_probs, G_probs = cw.F_probs, cw.G_probs F_mean, G_mean = cw.F_mean, cw.G_mean - @njit(parallel=parallel_flag) + @jit(parallel=parallel_flag) def T(v): "The Bellman operator" @@ -249,7 +249,7 @@ def operator_factory(cw, parallel_flag=True): return v_new - @njit + @jit def get_greedy(v): "Computes the v-greedy policy" @@ -472,7 +472,7 @@ T, get_greedy = operator_factory(cw) v_star = solve_model(cw, verbose=False) greedy_star = get_greedy(v_star) -@njit +@jit def passage_time(optimal_policy, F, G): t = 0 i = j = 0 @@ -485,7 +485,7 @@ def passage_time(optimal_policy, F, G): i, j = qe.random.draw(F), qe.random.draw(G) t += 1 -@njit(parallel=True) +@jit(parallel=True) def median_time(optimal_policy, F, G, M=25000): samples = np.empty(M) for i in prange(M): diff --git a/lectures/cass_koopmans_1.md b/lectures/cass_koopmans_1.md index 889dc8f8b..f29e25a7a 100644 --- a/lectures/cass_koopmans_1.md +++ b/lectures/cass_koopmans_1.md @@ -4,7 +4,7 @@ jupytext: extension: .md format_name: myst format_version: 0.13 - jupytext_version: 1.16.1 + jupytext_version: 1.16.4 kernelspec: display_name: Python 3 (ipykernel) language: python @@ -66,7 +66,7 @@ Let's start with some standard imports: ```{code-cell} ipython3 import matplotlib.pyplot as plt -from numba import njit, float64 +from numba import jit, float64 from numba.experimental import jitclass import numpy as np from quantecon.optimize import brentq @@ -525,7 +525,7 @@ planning problem. $c_0$ instead of $\mu_0$ in the following code.) ```{code-cell} ipython3 -@njit +@jit def shooting(pp, c0, k0, T=10): ''' Given the initial condition of capital k0 and an initial guess @@ -610,7 +610,7 @@ When $K_{T+1}$ gets close enough to $0$ (i.e., within an error tolerance bounds), we stop. ```{code-cell} ipython3 -@njit +@jit def bisection(pp, c0, k0, T=10, tol=1e-4, max_iter=500, k_ter=0, verbose=True): # initial boundaries for guess c0 @@ -804,7 +804,7 @@ over time. Let's calculate and plot the saving rate. ```{code-cell} ipython3 -@njit +@jit def saving_rate(pp, c_path, k_path): 'Given paths of c and k, computes the path of saving rate.' production = pp.f(k_path[:-1]) @@ -912,7 +912,7 @@ $$ (eq:tildeC) A positive fixed point $C = \tilde C(K)$ exists only if $f\left(K\right)+\left(1-\delta\right)K-f^{\prime-1}\left(\frac{1}{\beta}-\left(1-\delta\right)\right)>0$ ```{code-cell} ipython3 -@njit +@jit def C_tilde(K, pp): return pp.f(K) + (1 - pp.δ) * K - pp.f_prime_inv(1 / pp.β - 1 + pp.δ) @@ -931,11 +931,11 @@ K = \tilde K(C) $$ (eq:tildeK) ```{code-cell} ipython3 -@njit +@jit def K_diff(K, C, pp): return pp.f(K) - pp.δ * K - C -@njit +@jit def K_tilde(C, pp): res = brentq(K_diff, 1e-6, 100, args=(C, pp)) @@ -951,7 +951,7 @@ It is thus the intersection of the two curves $\tilde{C}$ and $\tilde{K}$ that w We can compute $K_s$ by solving the equation $K_s = \tilde{K}\left(\tilde{C}\left(K_s\right)\right)$ ```{code-cell} ipython3 -@njit +@jit def K_tilde_diff(K, pp): K_out = K_tilde(C_tilde(K, pp), pp) @@ -1003,7 +1003,7 @@ In addition to the three curves, Figure {numref}`stable_manifold` plots arrows --- mystnb: figure: - caption: "Stable Manifold and Phase Plane" + caption: Stable Manifold and Phase Plane name: stable_manifold tags: [hide-input] --- diff --git a/lectures/cass_koopmans_2.md b/lectures/cass_koopmans_2.md index 5c35f5ad5..2613cc312 100644 --- a/lectures/cass_koopmans_2.md +++ b/lectures/cass_koopmans_2.md @@ -68,7 +68,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 -from numba import njit, float64 +from numba import jit, float64 from numba.experimental import jitclass import numpy as np ``` @@ -751,7 +751,7 @@ class PlanningProblem(): ``` ```{code-cell} python3 -@njit +@jit def shooting(pp, c0, k0, T=10): ''' Given the initial condition of capital k0 and an initial guess @@ -779,7 +779,7 @@ def shooting(pp, c0, k0, T=10): ``` ```{code-cell} python3 -@njit +@jit def bisection(pp, c0, k0, T=10, tol=1e-4, max_iter=500, k_ter=0, verbose=True): # initial boundaries for guess c0 @@ -828,7 +828,7 @@ The above code from this lecture {doc}`Cass-Koopmans Planning Model ` that computes $\alpha$ and $\beta$. ```{code-cell} python3 -@njit(parallel=True) +@jit(parallel=True) def Q(h, wf): c, π_grid = wf.c, wf.π_grid @@ -508,7 +508,7 @@ def Q(h, wf): ``` ```{code-cell} python3 -@njit +@jit def solve_model(wf, tol=1e-4, max_iter=1000): """ Compute the continuation value function @@ -538,7 +538,7 @@ h_star = solve_model(wf) ``` ```{code-cell} python3 -@njit +@jit def find_cutoff_rule(wf, h): """ @@ -645,7 +645,7 @@ $V^{0}\left(\pi\right)$ and $V^{1}\left(\pi\right)$ numerically. ```{code-cell} python3 -@njit(parallel=True) +@jit(parallel=True) def V_q(wf, flag): V = np.zeros(wf.π_grid_size) if flag == 0: @@ -877,7 +877,7 @@ On average the Bayesian rule decides **earlier** than the frequentist rule when $q= f_0$ and **later** when $q = f_1$. ```{code-cell} python3 -@njit(parallel=True) +@jit(parallel=True) def check_results(L_arr, α, β, flag, π0): N, T = L_arr.shape diff --git a/lectures/odu.md b/lectures/odu.md index 979f00b74..b8284cdde 100644 --- a/lectures/odu.md +++ b/lectures/odu.md @@ -58,7 +58,7 @@ Let’s start with some imports ```{code-cell} ipython import matplotlib.pyplot as plt -from numba import njit, prange, vectorize +from numba import jit, prange, vectorize from interpolation import mlinterp from math import gamma import numpy as np @@ -299,8 +299,8 @@ class SearchProblem: self.β, self.c, self.w_max = β, c, w_max - self.f = njit(lambda x: p(x, F_a, F_b)) - self.g = njit(lambda x: p(x, G_a, G_b)) + self.f = jit(lambda x: p(x, F_a, F_b)) + self.g = jit(lambda x: p(x, G_a, G_b)) self.π_min, self.π_max = 1e-3, 1-1e-3 # Avoids instability self.w_grid = np.linspace(0, w_max, w_grid_size) @@ -326,11 +326,11 @@ def operator_factory(sp, parallel_flag=True): mc_size = sp.mc_size w_grid, π_grid = sp.w_grid, sp.π_grid - @njit + @jit def v_func(x, y, v): return mlinterp((w_grid, π_grid), v, (x, y)) - @njit + @jit def κ(w, π): """ Updates π using Bayes' rule and the current wage observation w. @@ -340,7 +340,7 @@ def operator_factory(sp, parallel_flag=True): return π_new - @njit(parallel=parallel_flag) + @jit(parallel=parallel_flag) def T(v): """ The Bellman operator. @@ -366,7 +366,7 @@ def operator_factory(sp, parallel_flag=True): return v_new - @njit(parallel=parallel_flag) + @jit(parallel=parallel_flag) def get_greedy(v): """" Compute optimal actions taking v as the value function. @@ -638,11 +638,11 @@ def Q_factory(sp, parallel_flag=True): mc_size = sp.mc_size w_grid, π_grid = sp.w_grid, sp.π_grid - @njit + @jit def ω_func(p, ω): return np.interp(p, π_grid, ω) - @njit + @jit def κ(w, π): """ Updates π using Bayes' rule and the current wage observation w. @@ -652,7 +652,7 @@ def Q_factory(sp, parallel_flag=True): return π_new - @njit(parallel=parallel_flag) + @jit(parallel=parallel_flag) def Q(ω): """ @@ -790,9 +790,9 @@ w_bar = solve_wbar(sp, verbose=False) # Interpolate reservation wage function π_grid = sp.π_grid -w_func = njit(lambda x: np.interp(x, π_grid, w_bar)) +w_func = jit(lambda x: np.interp(x, π_grid, w_bar)) -@njit +@jit def update(a, b, e, π): "Update e and π by drawing wage offer from beta distribution with parameters a and b" @@ -805,7 +805,7 @@ def update(a, b, e, π): return e, π -@njit +@jit def simulate_path(F_a=F_a, F_b=F_b, G_a=G_a, @@ -868,7 +868,7 @@ empirical distributions of unemployment duration and π at the time of employment. ```{code-cell} python3 -@njit +@jit def empirical_dist(F_a, F_b, G_a, G_b, w_bar, π_grid, N=10000, T=600): """ diff --git a/lectures/optgrowth_fast.md b/lectures/optgrowth_fast.md index dd07a1ffc..69c4776c4 100644 --- a/lectures/optgrowth_fast.md +++ b/lectures/optgrowth_fast.md @@ -61,7 +61,7 @@ Let's start with some imports: ```{code-cell} ipython import matplotlib.pyplot as plt import numpy as np -from numba import jit, njit +from numba import jit, jit from quantecon.optimize.scalar_maximization import brent_max ``` @@ -131,7 +131,7 @@ We will use JIT compilation to accelerate the Bellman operator. First, here's a function that returns the value of a particular consumption choice `c`, given state `y`, as per the Bellman equation {eq}`fpb30`. ```{code-cell} python3 -@njit +@jit def state_action_value(c, y, v_array, og): """ Right hand side of the Bellman equation. @@ -154,7 +154,7 @@ Now we can implement the Bellman operator, which maximizes the right hand side of the Bellman equation: ```{code-cell} python3 -@jit(nopython=True) +@jit def T(v, og): """ The Bellman operator. diff --git a/lectures/perm_income.md b/lectures/perm_income.md index 600df8444..af1973607 100644 --- a/lectures/perm_income.md +++ b/lectures/perm_income.md @@ -51,7 +51,7 @@ import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np import random -from numba import njit +from numba import jit ``` ## The Savings Problem @@ -479,7 +479,7 @@ r = 0.05 μ = 1 T = 60 -@njit +@jit def time_path(T): w = np.random.randn(T+1) # w_0, w_1, ..., w_T w[0] = 0 @@ -807,7 +807,7 @@ r = 0.05 S = 5 # Impulse date σ1 = σ2 = 0.15 -@njit +@jit def time_path(T, permanent=False): "Time path of consumption and debt given shock sequence" w1 = np.zeros(T+1) diff --git a/lectures/wald_friedman.md b/lectures/wald_friedman.md index b64437851..b284176e7 100644 --- a/lectures/wald_friedman.md +++ b/lectures/wald_friedman.md @@ -186,7 +186,7 @@ The next figure shows two beta distributions in the top panel. The bottom panel presents mixtures of these distributions, with various mixing probabilities $\pi_k$ ```{code-cell} ipython3 -@jit(nopython=True) +@jit def p(x, a, b): r = gamma(a + b) / (gamma(a) * gamma(b)) return r * x**(a-1) * (1 - x)**(b-1) @@ -502,7 +502,7 @@ def Q(h, wf): To solve the key functional equation, we will iterate using `Q` to find the fixed point ```{code-cell} ipython3 -@jit(nopython=True) +@jit def solve_model(wf, tol=1e-4, max_iter=1000): """ Compute the continuation cost function @@ -557,7 +557,7 @@ We will also set up a function to compute the cutoffs $\alpha$ and $\beta$ and plot these on our cost function plot ```{code-cell} ipython3 -@jit(nopython=True) +@jit def find_cutoff_rule(wf, h): """ diff --git a/lectures/wealth_dynamics.md b/lectures/wealth_dynamics.md index 9236d6eb2..e7d34ed5e 100644 --- a/lectures/wealth_dynamics.md +++ b/lectures/wealth_dynamics.md @@ -80,7 +80,7 @@ import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = (11, 5) #set default figure size import numpy as np import quantecon as qe -from numba import njit, float64, prange +from numba import jit, float64, prange from numba.experimental import jitclass ``` @@ -352,7 +352,7 @@ Here's function to simulate the time series of wealth for in individual househol ```{code-cell} ipython3 -@njit +@jit def wealth_time_series(wdy, w_0, n): """ Generate a single time series of length n for wealth given @@ -381,7 +381,7 @@ Note the use of parallelization to speed up computation. ```{code-cell} ipython3 -@njit(parallel=True) +@jit(parallel=True) def update_cross_section(wdy, w_distribution, shift_length=500): """ Shifts a cross-section of household forward in time