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 1 commit
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
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