GitHub

Original file line numberDiff line numberDiff line change

@@ -1,5 +1,4 @@

11

import numpy as np

2-

from interpolation import interp

32

from numba import njit, prange

43

from quantecon.optimize.scalar_maximization import brent_max

54

@@ -21,7 +20,7 @@ def objective(c, v, y):

2120

The right-hand side of the Bellman equation

2221

"""

2322

# First turn v into a function via interpolation

24-

v_func = lambda x: interp(grid, v, x)

23+

v_func = lambda x: np.interp(x, grid, v)

2524

return u(c) + β * np.mean(v_func(f(y - c) * shocks))

2625
2726

@njit(parallel=parallel_flag)

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)

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):

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))

Original file line numberDiff line numberDiff line change

@@ -24,22 +24,12 @@ kernelspec:

2424

:depth: 2

2525

```

2626
27-

In addition to what's in Anaconda, this lecture will need the following libraries:

28-
29-

```{code-cell} ipython

30-

---

31-

tags: [hide-output]

32-

---

33-

!pip install interpolation

34-

```

3527
3628

```{code-cell} ipython

3729

import matplotlib.pyplot as plt

38-

plt.rcParams["figure.figsize"] = (11, 5) #set default figure size

3930

import numpy as np

4031

from numba import njit, prange, float64, int64

4132

from numba.experimental import jitclass

42-

from interpolation import interp

4333

from math import gamma

4434

from scipy.optimize import minimize

4535

```

@@ -496,7 +486,7 @@ def Q(h, wf):

496486

κ = wf.κ

497487
498488

h_new = np.empty_like(π_grid)

499-

h_func = lambda p: interp(π_grid, h, p)

489+

h_func = lambda p: np.interp(p, π_grid, h)

500490
501491

for i in prange(len(π_grid)):

502492

π = π_grid[i]

@@ -679,7 +669,7 @@ def V_q(wf, flag):

679669
680670

for j in prange(len(z_arr)):

681671

π_next = wf.κ(z_arr[j], π)

682-

V[i] += wf.c + interp(wf.π_grid, V_old, π_next)

672+

V[i] += wf.c + np.interp(π_next, wf.π_grid, V_old)

683673
684674

V[i] /= wf.mc_size

685675
Original file line numberDiff line numberDiff line change

@@ -58,9 +58,8 @@ Let’s start with some imports

5858
5959

```{code-cell} ipython

6060

import matplotlib.pyplot as plt

61-

plt.rcParams["figure.figsize"] = (11, 5) #set default figure size

6261

from numba import njit, prange, vectorize

63-

from interpolation import mlinterp, interp

62+

from interpolation import mlinterp

6463

from math import gamma

6564

import numpy as np

6665

from matplotlib import cm

@@ -633,7 +632,7 @@ def Q_factory(sp, parallel_flag=True):

633632
634633

@njit

635634

def ω_func(p, ω):

636-

return interp(π_grid, ω, p)

635+

return np.interp(p, π_grid, ω)

637636
638637

@njit

639638

def κ(w, π):

@@ -783,7 +782,7 @@ w_bar = solve_wbar(sp, verbose=False)

783782
784783

# Interpolate reservation wage function

785784

π_grid = sp.π_grid

786-

w_func = njit(lambda x: interp(π_grid, w_bar, x))

785+

w_func = njit(lambda x: np.interp(x, π_grid, w_bar))

787786
788787

@njit

789788

def update(a, b, e, π):

@@ -907,7 +906,7 @@ def empirical_dist(F_a, F_b, G_a, G_b, w_bar, π_grid,

907906

π = π * lw / (π * lw + 1 - π)

908907
909908

# move to next agent if accepts

910-

if w >= interp(π_grid, w_bar, π):

909+

if w >= np.interp(π, π_grid, w_bar):

911910

break

912911
913912

# record the unemployment duration

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 quantecon

34-

!pip install interpolation

3534

```

3635
3736

## Overview

@@ -61,17 +60,11 @@ Let's start with some imports:

6160
6261

```{code-cell} ipython

6362

import matplotlib.pyplot as plt

64-

plt.rcParams["figure.figsize"] = (11, 5) #set default figure size

6563

import numpy as np

66-

from interpolation import interp

6764

from numba import jit, njit

6865

from quantecon.optimize.scalar_maximization import brent_max

6966

```

7067
71-

We are using an interpolation function from

72-

[interpolation.py](https://github.com/EconForge/interpolation.py) because it

73-

helps us JIT-compile our code.

74-
7568

The function `brent_max` is also designed for embedding in JIT-compiled code.

7669
7770

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):

152145
153146

u, f, β, shocks = og.u, og.f, og.β, og.shocks

154147
155-

v = lambda x: interp(og.grid, v_array, x)

148+

v = lambda x: np.interp(x, og.grid, v_array)

156149
157150

return u(c) + β * np.mean(v(f(y - c) * shocks))

158151

```

@@ -398,7 +391,7 @@ for β in (0.8, 0.9, 0.98):

398391

v_greedy, v_solution = solve_model(og, verbose=False)

399392
400393

# Define an optimal policy function

401-

σ_func = lambda x: interp(og.grid, v_greedy, x)

394+

σ_func = lambda x: np.interp(x, og.grid, v_greedy)

402395

y = simulate_og(σ_func, og)

403396

ax.plot(y, lw=2, alpha=0.6, label=rf'$\beta = {β}$')

404397
Original file line numberDiff line numberDiff line change

@@ -31,14 +31,6 @@ kernelspec:

3131

:depth: 2

3232

```

3333
34-

In addition to what's in Anaconda, this lecture will need the following libraries:

35-
36-

```{code-cell} ipython3

37-

:tags: [hide-output]

38-
39-

!pip install interpolation

40-

```

41-
4234

## Overview

4335
4436

This lecture describes a statistical decision problem presented to Milton

@@ -69,7 +61,6 @@ import numpy as np

6961

import matplotlib.pyplot as plt

7062

from numba import jit, prange, float64, int64

7163

from numba.experimental import jitclass

72-

from interpolation import interp

7364

from math import gamma

7465

```

7566

@@ -487,7 +478,7 @@ def Q(h, wf):

487478

κ = wf.κ

488479
489480

h_new = np.empty_like(π_grid)

490-

h_func = lambda p: interp(π_grid, h, p)

481+

h_func = lambda p: np.interp(p, π_grid, h)

491482
492483

for i in prange(len(π_grid)):

493484

π = π_grid[i]

Read the original on github.com ↗