Discrete probability distributions Beginning Bayes in R Course - - PowerPoint PPT Presentation

discrete probability distributions
SMART_READER_LITE
LIVE PREVIEW

Discrete probability distributions Beginning Bayes in R Course - - PowerPoint PPT Presentation

BEGINNING BAYES IN R Discrete probability distributions Beginning Bayes in R Course overview Two schools of thought: frequentist and Bayesian Introduction to the Bayesian way of thinking Subjective probability:


slide-1
SLIDE 1

BEGINNING BAYES IN R

Discrete probability distributions

slide-2
SLIDE 2

Beginning Bayes in R

Course overview

  • Two schools of thought: frequentist and Bayesian
  • Introduction to the Bayesian way of thinking
  • Subjective probability:
  • Probability describes beliefs about unknown quantities
  • Done through probability distributions
slide-3
SLIDE 3

Beginning Bayes in R

A spinner

1 2 3 4

slide-4
SLIDE 4

Beginning Bayes in R

TeachBayes package

  • Package (available on CRAN) for helping teach Bayesian thinking
  • Special functions for:
  • Bayes’ rule (spinners)
  • Learning about a proportion and a mean
  • Comparing two proportions
slide-5
SLIDE 5

Beginning Bayes in R

Construct my spinner

> library(TeachBayes) > areas <- c(2, 1, 2, 1, 2) > spinner_plot(areas)

For example, region 1 is twice the size of region 2

slide-6
SLIDE 6

Beginning Bayes in R

Construct probability distribution

> (df <- data.frame(Region = 1:5, areas, Probability = areas / sum(areas))) Region areas Probability 1 1 2 0.250 2 2 1 0.125 3 3 2 0.250 4 4 1 0.125 5 5 2 0.250

slide-7
SLIDE 7

Beginning Bayes in R

Numerical scale for probabilities

1 Probability More likely Less likely

slide-8
SLIDE 8

Beginning Bayes in R

My spinner

> df Region areas Probability 1 1 2 0.250 2 2 1 0.125 3 3 2 0.250 4 4 1 0.125 5 5 2 0.250 > sum(df$Probability) [1] 1

slide-9
SLIDE 9

Beginning Bayes in R

Probabilities of compound events

  • Identify outcomes of interest
  • Sum the probabilities of individual outcomes
slide-10
SLIDE 10

Beginning Bayes in R

> library(dplyr) > filter(df, Region %in% c(1, 3, 5)) Region areas Probability 1 1 2 0.25 2 3 2 0.25 3 5 2 0.25 > filter(df, Region > 3) Region areas Probability 1 4 1 0.125 2 5 2 0.250

Probabilities of compound events

slide-11
SLIDE 11

Beginning Bayes in R

Find probabilities by simulation

  • Spin spinner many times
  • Summarize simulated outcomes
slide-12
SLIDE 12

Beginning Bayes in R

Ploing simulation data

> library(TeachBayes) > (ten_spins <- spinner_data(areas, 10)) [1] 4 3 2 3 1 3 3 2 1 1 > many_spins <- spinner_data(areas, 1000) > bar_plot(many_spins)

slide-13
SLIDE 13

Beginning Bayes in R

Simulation data displayed as a table

> (S <- summarize(group_by(data.frame(Region = many_spins), Region), N = n())) # A tibble: 5 × 2 Region N <int> <int> 1 1 262 2 2 115 3 3 258 4 4 122 5 5 243

slide-14
SLIDE 14

Beginning Bayes in R

Probability of Region = 1

> (Freq_1 <- sum(S$N[S$Region == 1])) [1] 255 > (Prob_1 <- Freq_1 / 1000) [1] 0.255

An approximation to the actual probability of 0.25

slide-15
SLIDE 15

BEGINNING BAYES IN R

Let’s practice!

slide-16
SLIDE 16

BEGINNING BAYES IN R

Bayes’ rule

slide-17
SLIDE 17

Beginning Bayes in R

Thomas Bayes

  • Presbyterian minister (1702 - 1761)
  • Mathematician in his spare time
  • Essay Towards Solving a Problem in the

Doctrine of Chances (1763)

slide-18
SLIDE 18

Beginning Bayes in R

Define four spinners

slide-19
SLIDE 19

Beginning Bayes in R

Choose one spinner from box

slide-20
SLIDE 20

Beginning Bayes in R

Choose one spinner from box

Which spinner is she holding?

slide-21
SLIDE 21

Beginning Bayes in R

Outline of Bayesian approach

  • Identify possible models and construct prior probabilities

which reflect your knowledge about the models

  • Collect data — think of likelihoods, the chance of geing

this data for each model

  • Use Bayes’ rule to find posterior probabilities, updated

knowledge about models

slide-22
SLIDE 22

Beginning Bayes in R

Identity of the spinner is a model

> (bayes_df <- data.frame(Model = paste("Spinner", c("A", "B", "C", "D")))) Model 1 Spinner A 2 Spinner B 3 Spinner C 4 Spinner D

slide-23
SLIDE 23

Beginning Bayes in R

  • Don’t know what spinner she’s holding
  • Assign probabilities that reflect belief about likelihoods of

her holding each of these spinners (i.e. assign priors)

The prior

> bayes_df$Prior <- rep(0.25, 4) > bayes_df Model Prior 1 Spinner A 0.25 2 Spinner B 0.25 3 Spinner C 0.25 4 Spinner D 0.25

This is an example of a uniform prior since prior probabilities are spread out uniformly

slide-24
SLIDE 24

Beginning Bayes in R

Finding the likelihood of observing red

  • She spins her spinner once
  • Lands on RED
  • For each spinner, what is the probability of
  • bserving RED?
slide-25
SLIDE 25

Beginning Bayes in R

Spinner A

Likelihood of Spinner A?

1/3

slide-26
SLIDE 26

Beginning Bayes in R

Likelihood of Spinner B?

Spinner B 1/2

slide-27
SLIDE 27

Beginning Bayes in R

Add likelihoods to table

> bayes_df$Likelihood <- round(c(1/3, 1/2, 1/4, 1/6), 2) > bayes_df Model Prior Likelihood 1 Spinner A 0.25 0.33 2 Spinner B 0.25 0.50 3 Spinner C 0.25 0.25 4 Spinner D 0.25 0.17

Likelihoods for Spinner C and Spinner D

slide-28
SLIDE 28

Beginning Bayes in R

Bayes’ rule

  • Posterior probability is proportional to 


Prior Probability x Likelihood

  • “Turn the Bayesian Crank” means to compute posterior

probabilities using Bayes’ rule

slide-29
SLIDE 29

Beginning Bayes in R

Add products and posterior to table

> library(TeachBayes) > bayesian_crank(bayes_df) Model Prior Likelihood Product Posterior 1 Spinner A 0.25 0.33 0.0825 0.264 2 Spinner B 0.25 0.50 0.1250 0.400 3 Spinner C 0.25 0.25 0.0625 0.200 4 Spinner D 0.25 0.17 0.0425 0.136

bayes_df contains the prior and likelihood probabilities

Prior Likelihood x = Product Product / sum(Product) = Posterior

slide-30
SLIDE 30

Beginning Bayes in R

Review: the prior

slide-31
SLIDE 31

Beginning Bayes in R

Review: the posterior

slide-32
SLIDE 32

Beginning Bayes in R

Interpreting the posterior probabilities

We can learn more about the identity

  • f spinners by simulating more spins
slide-33
SLIDE 33

BEGINNING BAYES IN R

Let’s practice!

slide-34
SLIDE 34

BEGINNING BAYES IN R

Sequential Bayes and looking ahead

slide-35
SLIDE 35

Beginning Bayes in R

Four spinners example

Spun spinner once Observed RED

slide-36
SLIDE 36

Beginning Bayes in R

The posterior

0.26 0.40 0.2o 0.14

slide-37
SLIDE 37

Beginning Bayes in R

Suppose you observe a second spin

? ? ? ?

Spin spinner again Observe BLUE Prior? Likelihood?

slide-38
SLIDE 38

Beginning Bayes in R

Prior?

  • Prior represents our current beliefs about the spinners
  • Aer first spin, the posterior becomes my new prior: 


(.264, .400, .200, .136)

> # Old posterior becomes new prior > bayes_df Model Prior 1 Spinner A 0.264 2 Spinner B 0.400 3 Spinner C 0.200 4 Spinner D 0.136

slide-39
SLIDE 39

Beginning Bayes in R

> bayes_df$Likelihood <- c(1/3, 1/4, 1/2, 2/3) > bayes_df Model Prior Likelihood 1 Spinner A 0.264 0.3333333 2 Spinner B 0.400 0.2500000 3 Spinner C 0.200 0.5000000 4 Spinner D 0.136 0.6666667

Likelihoods of all four spinners

Likelihood?

For each spinner, find chance of BLUE

slide-40
SLIDE 40

Beginning Bayes in R

Likelihood?

For each spinner, find chance of BLUE

1/3

slide-41
SLIDE 41

Beginning Bayes in R

Update probabilities

Aer observing two spins (RED, BLUE), Spinners B and C are each slightly more likely than Spinners A and D

> library(TeachBayes) > bayesian_crank(bayes_df) Model Prior Likelihood Product Posterior 1 Spinner A 0.264 0.3333333 0.08800000 0.2323944 2 Spinner B 0.400 0.2500000 0.10000000 0.2640845 3 Spinner C 0.200 0.5000000 0.10000000 0.2640845 4 Spinner D 0.136 0.6666667 0.09066667 0.2394366

slide-42
SLIDE 42

Beginning Bayes in R

Looking ahead

  • Bayesian methods for one proportion and one normal

mean inference

  • Introduce continuous priors
  • Input prior information?
  • Obtain posterior?
  • Compare with frequentist inferences?
  • Use simulation to summarize posterior distributions
  • Simulation for prediction and Bayesian regression models
slide-43
SLIDE 43

BEGINNING BAYES IN R

Let’s practice!