Volatility Learner

This notebook uses a pure Python implementation of the computational model in this paper.


In [1]:
from __future__ import division
import numpy as np
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns

In [2]:
import optlearner

Initialize the learner


In [3]:
learner = optlearner.VolatilityLearner()

In [4]:
p_grid = learner.p_grid
I_grid = learner.I_grid
k_grid = learner.k_grid

Show the Bayes net


In [5]:
with mpl.rc_context({"font.size": 16}):
    learner.show_model();


Basic timing data


In [6]:
%timeit optlearner.VolatilityLearner()


1 loops, best of 3: 4.69 s per loop

In [7]:
%timeit learner.fit([0])


10 loops, best of 3: 24.2 ms per loop

Transition matrices


In [8]:
def plot_slices(grid, joint, cmap, step=2, var=""):
    pal = sns.blend_palette(sns.mpl_palette(cmap, 6), as_cmap=True)
    f, axes = plt.subplots(4, 4, figsize=(7, 7), sharex=True, sharey=True)
    xx, yy = np.meshgrid(grid, grid)
    for k, ax in zip(np.arange(learner._k_size) * step, axes.flat):
        ax.contour(xx, yy, joint[:, :, k], cmap=pal, vmin=joint.min(), vmax=joint.max())
    if var:
        for ax in axes[-1, :]:
            ax.set_xlabel(r"$%s_{i+1}$" % var, size=14)
        for ax in axes[:, 0]:
            ax.set_ylabel(r"$%s_{i}$" % var, size=14)
    plt.tight_layout()

In [9]:
plot_slices(learner.p_grid, learner._p_trans, "PuBuGn_d", var="p")



In [10]:
plot_slices(learner.I_grid, learner._I_trans, "BuGn_d", var="\log\/\ I")


Example model fits

Static probability


In [11]:
static_p = np.ones(500) * .65
static_y = stats.binom.rvs(1, static_p)

In [12]:
learner.reset()
learner.fit(static_y)

In [13]:
learner.plot_history(ground_truth=static_p)



In [14]:
learner.plot_joint()


Nonstationary probabilities, static volatility


In [15]:
moving_p = np.repeat([.75, .25, .75, .25, .75, .25], 100)
moving_y= stats.binom.rvs(1, moving_p)

In [16]:
learner.reset()
learner.fit(moving_y)

In [17]:
learner.plot_history(ground_truth=moving_p)



In [18]:
learner.plot_joint()


Nonstationary probabilities and volatility


In [19]:
metavol_p = np.repeat([.75, .75, .75, .25, .75, .25], 100)
metavol_y= stats.binom.rvs(1, metavol_p)

In [20]:
learner.reset()
learner.fit(metavol_y)

In [21]:
learner.plot_history(ground_truth=metavol_p)



In [22]:
learner.plot_joint()


Sinusoidal probabilities


In [23]:
x = np.linspace(0, 60, 600)
sin_p = (np.sin(x / 3) + 1.5) / 3
sin_y = stats.binom.rvs(1, sin_p)

In [24]:
learner.reset()
learner.fit(sin_y)

In [25]:
learner.plot_history(ground_truth=sin_p)



In [26]:
learner.plot_joint()