Skip to content

Use np.interp instead of interpolation.interp #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 0 additions & 53 deletions lectures/_static/lecture_specific/optgrowth/bellman_operator.py

This file was deleted.

17 changes: 3 additions & 14 deletions lectures/cake_eating_numerical.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cake_eating_problem>`.
Expand All @@ -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
```

Expand Down Expand Up @@ -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)
```
Expand Down Expand Up @@ -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)**α)
```
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 1 addition & 4 deletions lectures/coleman_policy_iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions lectures/egm_policy_iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions lectures/ifp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions lectures/ifp_advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 3 additions & 14 deletions lectures/jv.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions lectures/mccall_correlated.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 - β)
Expand Down Expand Up @@ -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):
Expand Down
16 changes: 3 additions & 13 deletions lectures/mccall_fitted_vfi.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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$.
Expand All @@ -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()

Expand Down Expand Up @@ -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))
Expand Down
Loading