Skip to content
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

FIX: Short term fix for ipywidgets #32

Closed
wants to merge 5 commits into from
Closed
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
33 changes: 33 additions & 0 deletions lectures/black_litterman.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,20 @@ If $\hat \mu$ is the maximum likelihood estimator
and $\tau$ is chosen heavily to weight this view, then the
customer's portfolio will involve big short-long positions.

```{note}
To Generate an Interactive Plot you can use the ipywidget
decorator `interact`. Interactive plotting requires the [ipywidgets](https://github.com/jupyter-widgets/ipywidgets) module to be installed and enabled.

The following `code-cell` doesn't currently display on the website. We are working
to support rich content such as `ipywidgets` as HTML javascript objects.

https://github.com/QuantEcon/lecture-python-advanced.myst/issues/28

However you can use the [binder link](https://mybinder.org/v2/gh/QuantEcon/lecture-python-advanced.notebooks/master?urlpath=tree/black_litterman.ipynb) to launch this notebook on mybinder
```

```{code-cell} python3
:tags: ["remove-output"]
def black_litterman(λ, μ1, μ2, Σ1, Σ2):
"""
This function calculates the Black-Litterman mixture
Expand Down Expand Up @@ -603,7 +616,17 @@ $\bar d_2$ on the RHS of the constraint, by varying
$\bar d_2$ (or $\lambda$ ), we can trace out the whole curve
as the figure below illustrates.

```{note}
The following `code-cell` doesn't currently display on the website. We are working
to support rich content such as `ipywidgets` as HTML javascript objects.

https://github.com/QuantEcon/lecture-python-advanced.myst/issues/28

However you can use the [binder link](https://mybinder.org/v2/gh/QuantEcon/lecture-python-advanced.notebooks/master?urlpath=tree/black_litterman.ipynb) to launch this notebook on mybinder
```

```{code-cell} python3
:tags: ["remove-output"]
np.random.seed(1987102)

N = 2 # Number of assets
Expand Down Expand Up @@ -688,7 +711,17 @@ This leads to the
following figure, on which the curve connecting $\hat \mu$
and $\mu_{BL}$ are bending

```{note}
The following `code-cell` doesn't currently display on the website. We are working
to support rich content such as `ipywidgets` as HTML javascript objects.

https://github.com/QuantEcon/lecture-python-advanced.myst/issues/28

However you can use the [binder link](https://mybinder.org/v2/gh/QuantEcon/lecture-python-advanced.notebooks/master?urlpath=tree/black_litterman.ipynb) to launch this notebook on mybinder
```

```{code-cell} python3
:tags: ["remove-output"]
λ_grid = np.linspace(.001, 20000, 1000)
curve = np.asarray([black_litterman(λ, μ_m, μ_est, Σ_est,
τ * np.eye(N)).flatten() for λ in λ_grid])
Expand Down
46 changes: 36 additions & 10 deletions lectures/matsuyama.md
Original file line number Diff line number Diff line change
Expand Up @@ -787,11 +787,45 @@ plt.show()

Additionally, instead of just seeing 4 plots at once, we might want to
manually be able to change $\rho$ and see how it affects the plot
in real-time. Below we use an interactive plot to do this.
in real-time. Below we will turn the following figure into an interactive plot.

Note, interactive plotting requires the [ipywidgets](https://github.com/jupyter-widgets/ipywidgets) module to be installed and enabled.
```{code-cell} python3
#Setup Parameters
ρ=0.2
maxiter=250
npts=250

# Create the figure and axis that we will plot on
fig, ax = plt.subplots(figsize=(12, 10))

# Create model and attraction basis
s1, θ, δ = 0.5, 2.5, 0.75
model = MSGSync(s1, θ, δ, ρ)
ab = model.create_attraction_basis(maxiter=maxiter, npts=npts)

# Color map with colormesh
unitrange = np.linspace(0, 1, npts)
cf = ax.pcolormesh(unitrange, unitrange, ab, cmap="viridis")
cbar_ax = fig.add_axes([0.95, 0.15, 0.05, 0.7])
plt.colorbar(cf, cax=cbar_ax)
plt.show()
```

```{note}
To Generate an Interactive Plot you can use the ipywidget
decorator `interact`. Interactive plotting requires the [ipywidgets](https://github.com/jupyter-widgets/ipywidgets) module to be installed and enabled.

This doesn't currently display on the website. We are working
to support rich content such as ipywidgets as HTML javascript objects.

https://github.com/QuantEcon/lecture-python-advanced.myst/issues/28

However you can use the [binder link](https://mybinder.org/v2/gh/QuantEcon/lecture-python-advanced.notebooks/master?urlpath=tree/matsuyama.ipynb) to launch this notebook on mybinder.
```

```{code-cell} python3
:tags: ["remove-output"]
@interact(ρ=(0.0, 1.0, 0.05), maxiter=(50, 5000, 50), npts=(25, 750, 25))
def interact_attraction_basis(ρ=0.2, maxiter=250, npts=250):
# Create the figure and axis that we will plot on
fig, ax = plt.subplots(figsize=(12, 10))
Expand All @@ -807,13 +841,5 @@ def interact_attraction_basis(ρ=0.2, maxiter=250, npts=250):
cbar_ax = fig.add_axes([0.95, 0.15, 0.05, 0.7])
plt.colorbar(cf, cax=cbar_ax)
plt.show()
return None
```

```{code-cell} python3
fig = interact(interact_attraction_basis,
ρ=(0.0, 1.0, 0.05),
maxiter=(50, 5000, 50),
npts=(25, 750, 25))
```