Introduction to the Course Peter Chi Assistant Professor of - - PowerPoint PPT Presentation

introduction to the course
SMART_READER_LITE
LIVE PREVIEW

Introduction to the Course Peter Chi Assistant Professor of - - PowerPoint PPT Presentation

DataCamp Probability Puzzles in R PROBABILITY PUZZLES IN R Introduction to the Course Peter Chi Assistant Professor of Statistics Villanova University DataCamp Probability Puzzles in R Course overview Chapter 1: The Classics Chapter 3:


slide-1
SLIDE 1

DataCamp Probability Puzzles in R

Introduction to the Course

PROBABILITY PUZZLES IN R

Peter Chi

Assistant Professor of Statistics Villanova University

slide-2
SLIDE 2

DataCamp Probability Puzzles in R

Course overview

Chapter 1: The Classics The Birthday Problem Monty Hall Chapter 2: Games with Dice Yahtzee Settlers of Catan Craps Chapter 3: Inspired by the Web iPhone Passcode Combinations Sign Error Cancellation Factoring a Quadratic Chapter 4: Poker Games Texas Hold'em Hole Cards Consecutive Cashes in the WSOP von Neumann Model of Poker

slide-3
SLIDE 3

DataCamp Probability Puzzles in R

Combinatorics

factorial(n) factorial(3) = 3! = 3 × 2 × 1 choose(n,k) choose(5,3) =

=

factorial(3) [1] 6

(3

5) 3!×(5−3)! 5!

choose(5,3) [1] 10

slide-4
SLIDE 4

DataCamp Probability Puzzles in R

Simulations

Select an object at random - sample() Simulate a coinflip - rbinom() Repeat a process

replicate()

Loops: for, while Set a seed - set.seed()

slide-5
SLIDE 5

DataCamp Probability Puzzles in R

More details on for loops

Storing the results:

for(i in 1:10){ sum(sample(x = c(1,2,3,4,5,6), size = 2, replace = TRUE)) } rolls <- rep(NA, 10) for(i in 1:10){ rolls[i] <- sum(sample(x = c(1,2,3,4,5,6), size = 2, replace = TRUE)) } rolls [1] 3 6 3 9 9 3 6 11 9 10

slide-6
SLIDE 6

DataCamp Probability Puzzles in R

Functions

my_function <- function(n){ answer <- n^3 return(answer) } my_function(10) [1] 1000

slide-7
SLIDE 7

DataCamp Probability Puzzles in R

Let's practice!

PROBABILITY PUZZLES IN R

slide-8
SLIDE 8

DataCamp Probability Puzzles in R

The Birthday Problem

PROBABILITY PUZZLES IN R

Peter Chi

Assistant Professor of Statistics Villanova University

slide-9
SLIDE 9

DataCamp Probability Puzzles in R

Problem overview

Setup Room with n people in it What is the probability that anyone shares the same birthday? Assumptions Ignore February 29th Birthdays are uniformly distributed across the remaining 365 days Each individual in the room is independent

slide-10
SLIDE 10

DataCamp Probability Puzzles in R

Defining a counter

Simulating the probability of rolling a 12

counter <- 0 roll <- roll_dice(2) roll [1] 12 if(roll == 12){ counter <- counter + 1 } counter [1] 1 roll_dice <- function(k){ all_rolls <- sample(c(1,2,3,4,5,6), k, replace = TRUE) final_answer <- sum(all_rolls) return(final_answer) }

slide-11
SLIDE 11

DataCamp Probability Puzzles in R

Incrementing a counter in a loop

Simulating the probability of rolling a 12

counter <- 0 for(i in 1:10000){ roll <- roll_dice(2) if(roll == 12){ counter <- counter + 1 } } p_twelve <- counter / 10000 print(p_twelve) [1] 0.0278 1/36 [1] 0.02777778

slide-12
SLIDE 12

DataCamp Probability Puzzles in R

The pbirthday function

pbirthday(10) [1] 0.1169482 room_sizes <- c(1:10) match_probs <- sapply(room_sizes, pbirthday) print(match_probs) [1] 0.000000000 0.002739726 0.008204166 0.016355912 0.027135574 [6] 0.040462484 0.056235703 0.074335292 0.094623834 0.116948178

slide-13
SLIDE 13

DataCamp Probability Puzzles in R

Plotting

plot(match_probs ~ room_size)

slide-14
SLIDE 14

DataCamp Probability Puzzles in R

Let's solve it!

PROBABILITY PUZZLES IN R

slide-15
SLIDE 15

DataCamp Probability Puzzles in R

Monty Hall

PROBABILITY PUZZLES IN R

Peter Chi

Assistant Professor of Statistics Villanova University

slide-16
SLIDE 16

DataCamp Probability Puzzles in R

Choose one of the doors

slide-17
SLIDE 17

DataCamp Probability Puzzles in R

One door is revealed

slide-18
SLIDE 18

DataCamp Probability Puzzles in R

Revealing a door with reverse indexing

The doors object: Suppose that Door #1 is chosen, and Door #2 contains the prize... Revealing the remaining door:

doors [1] 1 2 3 reveal <- doors[-c(1,2)] reveal [1] 3

slide-19
SLIDE 19

DataCamp Probability Puzzles in R

Revealing a door at random

The doors object: Suppose that Door #1 is chosen, and Door #1 in fact contains the prize... Revealing the remaining door:

doors [1] 1 2 3 reveal <- sample(x = doors[-1], size = 1)

slide-20
SLIDE 20

DataCamp Probability Puzzles in R

Let's try it!

PROBABILITY PUZZLES IN R