Skip to content

Commit

Permalink
Use np.interp instead of interpolation.interp (#156)
Browse files Browse the repository at this point in the history
* Use np.interp instead of interpolation.interp

* apply suggestions

* re-enable build failures on warnings

* MAINT: update ci.yml workflow versions and report upload

* Fix interp issues

---------

Co-authored-by: mmcky <[email protected]>
  • Loading branch information
kp992 and mmcky authored Apr 29, 2024
1 parent 0ad4aeb commit 03196de
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 85 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup Anaconda
uses: conda-incubator/setup-miniconda@v2
uses: conda-incubator/setup-miniconda@v3
with:
auto-update-conda: true
auto-activate-base: true
Expand Down Expand Up @@ -35,7 +35,7 @@ jobs:
shell: bash -l {0}
run: pip list
- name: Download "build" folder (cache)
uses: dawidd6/action-download-artifact@v2
uses: dawidd6/action-download-artifact@v3
with:
workflow: cache.yml
branch: main
Expand All @@ -61,14 +61,20 @@ jobs:
shell: bash -l {0}
run: |
rm -r _build/.doctrees
jb build lectures --path-output ./
jb build lectures --path-output ./ -nW --keep-going
- name: Upload Execution Reports (Download Notebooks)
uses: actions/upload-artifact@v4
if: failure()
with:
name: execution-reports
path: _build/jupyter/reports
- name: Save Build as Artifact
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: _build
path: _build
- name: Preview Deploy to Netlify
uses: nwtgck/actions-netlify@v1.1
uses: nwtgck/actions-netlify@v2
with:
publish-dir: '_build/html/'
production-branch: master
Expand Down
1 change: 0 additions & 1 deletion lectures/BCG_complete_mkts.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ In addition to what's in Anaconda, this lecture will need the following librarie
tags: [hide-output]
---
!pip install --upgrade quantecon
!pip install interpolation
!conda install -y -c plotly plotly plotly-orca
```

Expand Down
49 changes: 24 additions & 25 deletions lectures/BCG_incomplete_mkts.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ jupytext:
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.16.1
kernelspec:
display_name: Python 3
display_name: Python 3 (ipykernel)
language: python
name: python3
---
Expand All @@ -26,12 +28,10 @@ kernelspec:

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

```{code-cell} ipython
---
tags: [hide-output]
---
```{code-cell} ipython3
:tags: [hide-output]
!pip install --upgrade quantecon
!pip install interpolation
!conda install -y -c plotly plotly plotly-orca
```

Expand Down Expand Up @@ -703,15 +703,14 @@ Parameters include:
- $\beta$: Discount factor. Default value is 0.96.
- bound: Bound for truncated normal distribution. Default value is 3.

```{code-cell} ipython
```{code-cell} ipython3
import numpy as np
from scipy.stats import truncnorm
from scipy.integrate import quad
from numba import njit
from interpolation import interp
```

```{code-cell} python3
```{code-cell} ipython3
class BCG_incomplete_markets:
# init method or constructor
Expand Down Expand Up @@ -784,7 +783,7 @@ class BCG_incomplete_markets:
rv = truncnorm(ta, tb, loc=𝜇, scale=𝜎)
𝜖_range = np.linspace(ta, tb, 1000000)
pdf_range = rv.pdf(𝜖_range)
self.g = njit(lambda 𝜖: interp(𝜖_range, pdf_range, 𝜖))
self.g = njit(lambda 𝜖: np.interp(𝜖, 𝜖_range, pdf_range))
#*************************************************************
Expand Down Expand Up @@ -1206,14 +1205,14 @@ Below we show some examples computed with the class `BCG_incomplete markets`.
In the first example, we set up an instance of the BCG incomplete
markets model with default parameter values.

```{code-cell} python3
:tags: ["hide-output"]
```{code-cell} ipython3
:tags: [hide-output]
mdl = BCG_incomplete_markets()
kss,bss,Vss,qss,pss,c10ss,c11ss,c20ss,c21ss,𝜃1ss = mdl.solve_eq(print_crit=False)
```

```{code-cell} python3
```{code-cell} ipython3
print(-kss+qss+pss*bss)
print(Vss)
print(𝜃1ss)
Expand All @@ -1229,7 +1228,7 @@ Thus, let’s see if the firm is actually maximizing its firm value given
the equilibrium pricing function $q(k,b)$ for equity and
$p(k,b)$ for bonds.

```{code-cell} python3
```{code-cell} ipython3
kgrid, bgrid, Vgrid, Qgrid, Pgrid = mdl.eq_valuation(c10ss, c11ss, c20ss, c21ss,N=30)
print('Maximum valuation of the firm value in the (k,B) grid: {:.5f}'.format(Vgrid.max()))
Expand All @@ -1246,7 +1245,7 @@ Below we will plot the firm’s value as a function of $k,b$.
We’ll also plot the equilibrium price functions $q(k,b)$ and
$p(k,b)$.

```{code-cell} python3
```{code-cell} ipython3
from IPython.display import Image
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
Expand Down Expand Up @@ -1367,7 +1366,7 @@ $$

The function also outputs agent 1’s bond holdings $\xi_1$.

```{code-cell} python3
```{code-cell} ipython3
def off_eq_check(mdl,kss,bss,e=0.1):
# Big K and big B
k = kss
Expand Down Expand Up @@ -1603,7 +1602,7 @@ structure for the firm.
We first check the case in which $b^{**} = b^* - e$ where
$e = 0.1$:

```{code-cell} python3
```{code-cell} ipython3
#====================== Experiment 1 ======================#
Ve1,ke1,be1,pe1,qe1,c10e1,c11e1,c20e1,c21e1,𝜉1e1 = off_eq_check(mdl,
kss,
Expand Down Expand Up @@ -1655,7 +1654,7 @@ Therefore, $(k^*,b^{*}-e)$ would not be an equilibrium.

Next, we check for $b^{**} = b^* + e$.

```{code-cell} python3
```{code-cell} ipython3
#====================== Experiment 2 ======================#
Ve2,ke2,be2,pe2,qe2,c10e2,c11e2,c20e2,c21e2,𝜉1e2 = off_eq_check(mdl,
kss,
Expand Down Expand Up @@ -1708,7 +1707,7 @@ want to hold corporate debt.

For example, $\xi^1 > 0$:

```{code-cell} python3
```{code-cell} ipython3
print('Bond holdings of agent 1: {:.3f}'.format(𝜉1e2))
```

Expand All @@ -1726,7 +1725,7 @@ It is also interesting to look at the equilibrium price functions
$q(k,b)$ and $p(k,b)$ faced by firms in our rational
expectations equilibrium.

```{code-cell} python3
```{code-cell} ipython3
# Equity Valuation
fig = go.Figure(data=[go.Scatter3d(x=[kss],
y=[bss],
Expand Down Expand Up @@ -1756,7 +1755,7 @@ Image(fig.to_image(format="png"))
# code locally
```

```{code-cell} python3
```{code-cell} ipython3
# Bond Valuation
fig = go.Figure(data=[go.Scatter3d(x=[kss],
y=[bss],
Expand Down Expand Up @@ -1815,8 +1814,8 @@ $$
The function `valuations_by_agent` is used in calculating these
valuations.

```{code-cell} python3
:tags: ["hide-output"]
```{code-cell} ipython3
:tags: [hide-output]
# Lists for storage
wlist = []
Expand Down Expand Up @@ -1864,7 +1863,7 @@ for i in range(10):
p2list.append(P2)
```

```{code-cell} python3
```{code-cell} ipython3
# Plot
fig, ax = plt.subplots(3,2,figsize=(12,12))
ax[0,0].plot(wlist,klist)
Expand Down Expand Up @@ -1909,7 +1908,7 @@ Now let’s see how the two types of agents value bonds and equities,
keeping in mind that the type that values the asset highest determines
the equilibrium price (and thus the pertinent set of Big $C$’s).

```{code-cell} python3
```{code-cell} ipython3
# Comparing the prices
fig, ax = plt.subplots(1,3,figsize=(16,6))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,17 @@ def simulate(self, b0, s0, T, sHist=None):
# Time 1 onward
for t in range(1, T):
s, x = sHist[t], xHist[t-1]
cHist[t] = interp(self.x_grid, self.c1[:, s], x)
nHist[t] = interp(self.x_grid, self.n1[:, s], x)
cHist[t] = np.interp(x, self.x_grid, self.c1[:, s])
nHist[t] = np.interp(x, self.x_grid, self.n1[:, s])

τHist[t] = self.τ(cHist[t], nHist[t])

Bhist[t] = x / Uc(cHist[t], 1-nHist[t])

c, n = np.empty((2, self.S))
for sprime in range(self.S):
c[sprime] = interp(x_grid, self.c1[:, sprime], x)
n[sprime] = interp(x_grid, self.n1[:, sprime], x)
c[sprime] = np.interp(x, x_grid, self.c1[:, sprime])
n[sprime] = np.interp(x, x_grid, self.n1[:, sprime])
Euc = π[sHist[t-1]] @ Uc(c, 1-n)
RHist[t-1] = Uc(cHist[t-1], 1-nHist[t-1]) / (self.pref.β * Euc)

Expand All @@ -150,7 +150,7 @@ def simulate(self, b0, s0, T, sHist=None):

if t < T-1:
sprime = sHist[t+1]
xHist[t] = interp(self.x_grid, self.xprime1[:, s, sprime], x)
xHist[t] = np.interp(x, self.x_grid, self.xprime1[:, s, sprime])

return [cHist, nHist, Bhist, τHist, gHist, yHist, xHist, RHist]

Expand Down Expand Up @@ -209,7 +209,7 @@ def obj_V(z_sub, x, s, V, pref, π, g, x_grid, b0=None):
# prepare Vprime vector
Vprime = np.empty(S)
for sprime in range(S):
Vprime[sprime] = interp(x_grid, V[:, sprime], xprime[sprime])
Vprime[sprime] = np.interp(xprime[sprime], x_grid, V[:, sprime])

# compute the objective value
obj = U(c, l) + β * π[s] @ Vprime
Expand Down
Loading

1 comment on commit 03196de

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.