+ - 0:00:00
Notes for current slide
Notes for next slide

STA 326 2.0 Programming and Data Analysis with R

Lesson 5.2: Simulations

Dr Thiyanga Talagala

2020-03-10

1 / 11

Monty Hall Problem

Image Credit: Ben Bennetts

2 / 11
3 / 11
4 / 11

Monty Hall Problem: Rules

  • The host must always open a door that was not picked by the contestant.
5 / 11

Monty Hall Problem: Rules

  • The host must always open a door that was not picked by the contestant.
  • The host must always open a door to reveal a goat and never the car.
5 / 11

Monty Hall Problem: Rules

  • The host must always open a door that was not picked by the contestant.
  • The host must always open a door to reveal a goat and never the car.
  • The host must always offer the chance to switch between the originally chosen door and the remaining closed door.

    Taken from https://en.wikipedia.org/wiki/Monty_Hall_problem
5 / 11

Let's play the game!

Click here to play: http://www.math.ucsd.edu/~crypto/Monty/monty.html

Does switching increases the chance of winning the car?

6 / 11

We use sample function to assign car, goat, goat, behind the doors.

sample(c("car", "goat", "goat"))
[1] "goat" "goat" "car"
sample(c("car", "goat", "goat"))
[1] "goat" "car" "goat"
7 / 11

We use sample function to assign car, goat, goat, behind the doors.

sample(c("car", "goat", "goat"))
[1] "goat" "goat" "car"
sample(c("car", "goat", "goat"))
[1] "goat" "car" "goat"

Here is a function that will take player's initial choice of door.

select_first_door <- function(door){
door.allocation <- sample(c("car", "goat", "goat"))
door.allocation[door]
}
select_first_door(2) # My first choice is the 2nd door
[1] "goat"
7 / 11

We use sample function to assign car, goat, goat, behind the doors.

sample(c("car", "goat", "goat"))
[1] "goat" "goat" "car"
sample(c("car", "goat", "goat"))
[1] "goat" "car" "goat"

Here is a function that will take player's initial choice of door.

select_first_door <- function(door){
door.allocation <- sample(c("car", "goat", "goat"))
door.allocation[door]
}
select_first_door(2) # My first choice is the 2nd door
[1] "goat"

Modify your function so that it shows the player the door that the host selects. Hint: sample function act as a host.

7 / 11

Modify your function so that it includes the player strategy (switch or stay) as an input, and returns the appropriate result.

  • Run your function 1000 times, for each strategy and report the proportion of times that car is selected. (You could put this in a loop and compute the proportion of times car shows.)
  • How many times would you expect car to be selected out of 1000 runs?
8 / 11

Approximating Pi = 3.14616

AcircleAsquare=πr24r2 Equation of a circle center around 0: x2+y2=r2

Write a function to approximate π using Monte Carlo simulations.

9 / 11

Random Variable Generation: Inverse-Transform Method

Let X be a random variable with CDF F. Since F is a nondecreasing function, the inverse function F1 may be defined as F1(y)=inf{x:F(x)y},0y1.

It is easy to show that if UU(0,1), then

X=F1(U)

has CDF F. Namely, since F is invertible and P(Uu)=u, we have

P(Xx)=P(F1(U)x)=P(UF(x))=F(x)

10 / 11

Slides available at: hellor.netlify.com

All rights reserved by Thiyanga S. Talagala

11 / 11

Monty Hall Problem

Image Credit: Ben Bennetts

2 / 11
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
s Start & Stop the presentation timer
t Reset the presentation timer
?, h Toggle this help
Esc Back to slideshow