Bayesian Zig Zag Developing probabilistic models using grid methods - - PowerPoint PPT Presentation

bayesian zig zag
SMART_READER_LITE
LIVE PREVIEW

Bayesian Zig Zag Developing probabilistic models using grid methods - - PowerPoint PPT Presentation

Bayesian Zig Zag Developing probabilistic models using grid methods and MCMC Allen Downey ACM Learning Center Olin College February 2019 These slides tinyurl.com/zigzagacm Bayesian methods Increasingly important, but Bayesian methods


slide-1
SLIDE 1

Bayesian Zig Zag

Developing probabilistic models using grid methods and MCMC Allen Downey Olin College ACM Learning Center February 2019

slide-2
SLIDE 2

These slides tinyurl.com/zigzagacm

slide-3
SLIDE 3

Bayesian methods

Increasingly important, but…

slide-4
SLIDE 4

Bayesian methods

Increasingly important, but… hard to get started.

slide-5
SLIDE 5

Bayesian Zig Zag

An approach I think is good for

  • 1. Learning.
  • 2. Developing models iteratively.
  • 3. Validating models incrementally.

Forward and inverse probability.

slide-6
SLIDE 6

Forward probability

You have a model of the system. You know the parameters. You can generate data.

slide-7
SLIDE 7

Inverse probability

You have a model of the system. You have data. You can estimate the parameters.

slide-8
SLIDE 8

Start forward

Simulate the model.

slide-9
SLIDE 9

Go backward

Run grid approximations.

slide-10
SLIDE 10

Go forward

Generate predictive distributions. And here is a key...

slide-11
SLIDE 11

Go forward

Generate predictive distributions. Generating predictions looks a lot like a PyMC model.

slide-12
SLIDE 12

Go backward

Run the PyMC model. Validate against the grid approximations.

slide-13
SLIDE 13

Go forward

Use PyMC to generate predictions.

slide-14
SLIDE 14

Let's look at an example.

slide-15
SLIDE 15
slide-16
SLIDE 16

Hockey?

Well, yes. But also any system well-modeled by a Poisson process.

slide-17
SLIDE 17

Poisson process

Events are equally likely to occur at any time.

  • 1. How long until the next event?
  • 2. How many events in a given interval?
slide-18
SLIDE 18

Let's get to it

These slides tinyurl.com/zigzagacm Read the notebook:

  • Static view on GitHub.
  • Live on Binder.
slide-19
SLIDE 19

I'll use Python code to show:

  • Most steps are a few lines of code,
  • Based on standard libraries (NumPy, SciPy, PyMC).

Don't panic.

slide-20
SLIDE 20

STEP 1: FORWARD

slide-21
SLIDE 21

Simulating hockey

Probability of scoring a goal in any minute is p. Pretend we know p. Simulate 60 minutes and add up the goals.

slide-22
SLIDE 22

def simulate_game(p, n=60): goals = np.random.choice([0, 1], n, p=[1-p, p]) return np.sum(goals)

slide-23
SLIDE 23
slide-24
SLIDE 24

Analytic distributions

Result of the simulation is binomial. Well approximated by Poisson.

slide-25
SLIDE 25

mu = n * p sample_poisson = np.random.poisson(mu, 1000)

slide-26
SLIDE 26
slide-27
SLIDE 27

To compare distributions, cumulative distribution function (CDF) is better than probability mass function (PMF).

slide-28
SLIDE 28
slide-29
SLIDE 29

Forward

So far, forward probability. Given mu, we can compute p(goals | mu). For inference we want p(mu | goals).

slide-30
SLIDE 30

Bayes's theorem tells us how they are related.

By mattbuck (category) - Own work by mattbuck., CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=14658489

slide-31
SLIDE 31

STEP 2: INVERSE

slide-32
SLIDE 32

Bayesian update

Start with prior beliefs, p(mu), for a range of mu. Compute the likelihood function, p(goals | mu) Use Bayes's theorem to get posterior beliefs, p(mu | goals).

slide-33
SLIDE 33

def bayes_update(suite, data, like_func): for hypo in suite: suite[hypo] *= like_func(data, hypo) normalize(suite) suite: dictionary with possible values of mu and probabilities data: observed number of goals like_func: likelihood function that computes p(goals | mu)

slide-34
SLIDE 34

from scipy.stats import poisson def poisson_likelihood(goals, mu): """Computes p(goals | mu)""" return poisson.pmf(goals, mu)

slide-35
SLIDE 35

Gamma prior

Gamma distribution has a reasonable shape for this context. And we can estimate parameters from past games.

slide-36
SLIDE 36

alpha = 9 beta = 3 hypo_mu = np.linspace(0, 15, num=101) gamma_prior = make_gamma_suite(hypo_mu, alpha, beta)

slide-37
SLIDE 37

Grid approximation

mu is actually continuous. We're approximating it with a grid of discrete values.

slide-38
SLIDE 38
slide-39
SLIDE 39

posterior = gamma_prior.copy() posterior.bayes_update(data=6, poisson_likelihood)

slide-40
SLIDE 40
slide-41
SLIDE 41

From posterior to predictive

Posterior distribution represents what we know about mu. Posterior predictive distribution represents a prediction about the number of goals.

slide-42
SLIDE 42

STEP 3: FORWARD

slide-43
SLIDE 43

Sampling

To sample the posterior predictive distribution:

  • 1. Draw random mu from the posterior.
  • 2. Draw random goals from Poisson(mu).
  • 3. Repeat.
slide-44
SLIDE 44

def sample_suite(suite, n): mus, p = zip(*suite.items()) return np.random.choice(mus, n, replace=True, p=p)

suite: dictionary with possible values of mu and probabilities

slide-45
SLIDE 45

sample_post = sample_suite(posterior, n) sample_post_pred = np.random.poisson(sample_post)

slide-46
SLIDE 46
slide-47
SLIDE 47

Posterior predictive distribution

Represents two sources of uncertainty:

  • 1. We're unsure about mu.
  • 2. Even if we knew mu, we would be unsure about goals.
slide-48
SLIDE 48

Forward PyMC

I'll use PyMC to run the forward model. Overkill, but it helps:

  • Validate: does the model make sense?
  • Verify: did we implement the model we intended?
slide-49
SLIDE 49

model = pm.Model() with model: mu = pm.Gamma('mu', alpha, beta) goals = pm.Poisson('goals', mu) trace = pm.sample_prior_predictive(1000)

slide-50
SLIDE 50
slide-51
SLIDE 51
slide-52
SLIDE 52

This confirms that we specified the model right. And it helps with the next step.

slide-53
SLIDE 53

STEP 4: INVERSE

slide-54
SLIDE 54

model = pm.Model() with model: mu = pm.Gamma('mu', alpha, beta) goals = pm.Poisson('goals', mu) trace = pm.sample_prior_predictive(1000)

slide-55
SLIDE 55

model = pm.Model() with model: mu = pm.Gamma('mu', alpha, beta) goals = pm.Poisson('goals', mu, observed=3) trace = pm.sample(1000)

slide-56
SLIDE 56
slide-57
SLIDE 57

STEP 5: FORWARD

slide-58
SLIDE 58

post_pred = pm.sample_posterior_predictive(trace, samples=1000)

slide-59
SLIDE 59
slide-60
SLIDE 60

With a working PyMC model, we can take on problems too big for grid algorithms.

slide-61
SLIDE 61
slide-62
SLIDE 62

Two teams

Starting with the same prior:

  • Update BOS with observed=3.
  • Update ANA with observed=1.
slide-63
SLIDE 63

model = pm.Model() with model: mu_BOS = pm.Gamma('mu_BOS', alpha, beta) mu_ANA = pm.Gamma('mu_ANA', alpha, beta) goals_BOS = pm.Poisson('goals_BOS', mu_BOS, observed=3) goals_ANA = pm.Poisson('goals_ANA', mu_ANA, observed=1) trace = pm.sample(1000)

slide-64
SLIDE 64
slide-65
SLIDE 65

Probability of superiority

mu_BOS = trace['mu_BOS'] mu_ANA = trace['mu_ANA'] np.mean(mu_BOS > mu_ANA) 0.67175

slide-66
SLIDE 66

post_pred = pm.sample_posterior_predictive(trace, samples=1000)

goals_BOS = post_pred['goals_BOS'] goals_ANA = post_pred['goals_ANA']

slide-67
SLIDE 67
slide-68
SLIDE 68

Probability of winning

win = np.mean(goals_BOS > goals_ANA) 0.488 lose = np.mean(goals_ANA > goals_BOS) 0.335 tie = np.mean(goals_BOS == goals_ANA) 0.177

slide-69
SLIDE 69

Overtime!

Time to first goal is exponential with 1/mu. Generate predictive samples.

slide-70
SLIDE 70

tts_BOS = np.random.exponential(1/mu_BOS) tts_ANA = np.random.exponential(1/mu_ANA) win_ot = np.mean(tts_BOS < tts_ANA) 0.55025

slide-71
SLIDE 71

total_win = win + tie * win_ot 0.58539425

slide-72
SLIDE 72

Summary

slide-73
SLIDE 73

Go Bruins!

slide-74
SLIDE 74

Think Bayes

Chapter 7: The Boston Bruins problem Available under a free license at thinkbayes.com. And published by O'Reilly Media.

slide-75
SLIDE 75

Please don't use this to gamble

First of all, it's only based on data from one previous game. Also...

slide-76
SLIDE 76

Please don't use this to gamble

Gambling a zero-sum game (or less). If you make money, you're just taking it from someone else. As opposed to creating value.

slide-77
SLIDE 77

If you made it this far, you probably have some skills. Use them for better things than gambling.

slide-78
SLIDE 78

https://opendatascience.com/data-science-for-good-part-1/

slide-79
SLIDE 79

And finally...

slide-80
SLIDE 80

Thanks

Chris Fonnesbeck for help getting these examples running. Colin Carroll for adding sample_prior_predictive. Eric Ma for moderating today, and for contributions to PyMC.

slide-81
SLIDE 81

downey@allendowney.com

github website twitter email

These slides: tinyurl.com/zigzagacm

slide-82
SLIDE 82
slide-83
SLIDE 83
slide-84
SLIDE 84
slide-85
SLIDE 85
slide-86
SLIDE 86
slide-87
SLIDE 87
slide-88
SLIDE 88
slide-89
SLIDE 89
slide-90
SLIDE 90
slide-91
SLIDE 91
slide-92
SLIDE 92