The HMC Revolution is Open Source¶

Who am I?¶

[email protected] • [email protected]¶

• Founded 2008, web optimization and personalization SaaS
• Observed 5B impressions and \$4.1B in revenue during Cyber Week 2017

Source

Probabilistic Programming¶

The Monty Hall Problem¶

Initially, we have no information about which door the prize is behind.

In [8]:
import pymc3 as pm

with pm.Model() as monty_model:
prize = pm.DiscreteUniform('prize', 0, 2)


If we choose door one:

 Monty can open Prize behind Door 1 Door 2 Door 3 Door 1 No Yes Yes Door 2 No No Yes Door 2 No Yes No
In [9]:
from theano import tensor as tt

with monty_model:
p_open = pm.Deterministic(
'p_open',
tt.switch(tt.eq(prize, 0),
np.array([0., 0.5, 0.5]), # it is behind the first door
tt.switch(tt.eq(prize, 1),
np.array([0., 0., 1.]),   # it is behind the second door
np.array([0., 1., 0.])))  # it is behind the third door
)


Monty opened the third door, revealing a goat.

In [10]:
with monty_model:
opened = pm.Categorical('opened', p_open, observed=2)


Should we switch our choice of door?

In [13]:
with monty_model:
monty_trace = pm.sample(1000, **MONTY_SAMPLE_KWARGS)

monty_df = pm.trace_to_dataframe(monty_trace)

Multiprocess sampling (3 chains in 2 jobs)
Metropolis: [prize]
Sampling 3 chains: 100%|██████████| 4500/4500 [00:02<00:00, 1683.19draws/s]

In [14]:
monty_df['prize'].head()

Out[14]:
0    0
1    0
2    0
3    0
4    1
Name: prize, dtype: int64
In [15]:
ax = (monty_df['prize']
.value_counts(normalize=True, ascending=True)
.plot(kind='bar', color='C0'))

ax.set_xlabel("Door");
ax.yaxis.set_major_formatter(pct_formatter);
ax.set_ylabel("Probability of prize");


Probabilistic programming is not new¶

 BUGS (1989) JAGS (2007)

From the PyMC3 documentation:

PyMC3 is a Python package for Bayesian statistical modeling and Probabilistic Machine Learning which focuses on advanced Markov chain Monte Carlo and variational fitting algorithms. Its flexibility and extensibility make it applicable to a large suite of problems.

Monte Carlo Methods¶

In [16]:
N = 5000

x, y = np.random.uniform(0, 1, size=(2, N))

In [18]:
fig

Out[18]:
In [19]:
in_circle = x**2 + y**2 <= 1

In [21]:
fig

Out[21]:
In [22]:
4 * in_circle.mean()

Out[22]:
3.1335999999999999

Case Study: NBA Foul Calls¶

Question: Is (not) committing and/or drawing fouls a measurable player skill?

In [26]:
orig_df.head(n=2).T

Out[26]:
play_id 20150301CLEHOU-0 20150301CLEHOU-1
period Q4 Q4
seconds_left 112 103
call_type Foul: Shooting Foul: Shooting
committing_player Josh Smith J.R. Smith
review_decision CNC CC
away CLE CLE
home HOU HOU
date 2015-03-01 00:00:00 2015-03-01 00:00:00
score_away 103 103
score_home 105 105
committing_team HOU CLE

Model outline¶

$$\operatorname{log-odds}(\textrm{Foul}) \ \sim \textrm{Season factor} + \left(\textrm{Disadvantaged skill} - \textrm{Committing skill}\right)$$
In [36]:
with pm.Model() as irt_model:
β_season = pm.Normal('β_season', 0., 2.5, shape=n_season)

θ = hierarchical_normal('θ', n_player) # disadvantaged skill
b = hierarchical_normal('b', n_player) # committing skill

p = pm.math.sigmoid(
)

obs = pm.Bernoulli(
'obs', p,
observed=df['foul_called'].values
)


Metropolis-Hastings Inference¶

In [37]:
with irt_model:
step = pm.Metropolis()
met_trace = pm.sample(5000, step=step, **SAMPLE_KWARGS)

Multiprocess sampling (3 chains in 2 jobs)
CompoundStep
>Metropolis: [σ_b]
>Metropolis: [Δ_b]
>Metropolis: [σ_θ]
>Metropolis: [Δ_θ]
>Metropolis: [β_season]
Sampling 3 chains: 100%|██████████| 16500/16500 [02:34<00:00, 106.97draws/s]
The gelman-rubin statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.

In [40]:
trace_fig

Out[40]:
In [41]:
max(np.max(var_stats) for var_stats in pm.gelman_rubin(met_trace).values())

Out[41]:
3.1038666861280597

The Curse of Dimensionality¶

This model has

In [42]:
n_param = n_season + 2 * n_player
n_param

Out[42]:
948

parameters

In [45]:
fig

Out[45]:

Hamiltonian Monte Carlo Inference¶

Automating calculus¶

$$\frac{d}{dx} \left(x^3\right) = 3 x^2$$
In [46]:
x = tt.dscalar('x')
x.tag.test_value = 0.

y = x**3

In [47]:
pprint(tt.grad(y, x))

Out[47]:
'((fill((x ** TensorConstant{3}), TensorConstant{1.0}) * TensorConstant{3}) * (x ** (TensorConstant{3} - TensorConstant{1})))'

Case Study Continued: NBA Foul Calls¶

In [48]:
with irt_model:
nuts_trace = pm.sample(500, **SAMPLE_KWARGS)

Auto-assigning NUTS sampler...
Multiprocess sampling (3 chains in 2 jobs)
NUTS: [σ_b, Δ_b, σ_θ, Δ_θ, β_season]
Sampling 3 chains: 100%|██████████| 3000/3000 [03:06<00:00, 16.11draws/s]

In [49]:
nuts_gr_stats = pm.gelman_rubin(nuts_trace)
max(np.max(var_stats) for var_stats in nuts_gr_stats.values())

Out[49]:
1.0069298341016355
In [52]:
fig

Out[52]:

In [57]:
fig

Out[57]:

Next Steps¶

PyMC4?¶

Prototype repositoryProposed timeline

Hamiltonian Monte Carlo¶

A Conceptual Introduction to Hamiltonian Monte Carlo