This notebook uses a pure Python implementation of a simplified version of the model described 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
In [3]:
learner = optlearner.ProbabilityLearner()
In [4]:
p_grid = learner.p_grid
I_grid = learner.I_grid
In [5]:
with mpl.rc_context({"font.size": 16}):
learner.show_model();
In [6]:
%timeit optlearner.ProbabilityLearner()
In [7]:
%timeit learner.fit([0])
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._I_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]:
learner = optlearner.ProbabilityLearner()
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()
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()
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()
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()