Lecture 13 – Simulation

DSC 10, Fall 2022

Announcements

Midterm Exam details

The Midterm Exam is in one week, on Friday, 10/28 during your assigned lecture.

Agenda

Simulation.

Simulation

Simulation

Making a random choice

Making multiple random choices

np.random.choice(options, n) will return an array of n randomly selected elements from options.

With replacement vs. without replacement

Example: What's the probability of getting 60 or more heads if we flip 100 coins?

Flipping coins

What is the probability of getting 60 or more heads if we flip 100 coins?

Strategy:

  1. Figure out how to do one experiment (i.e., flip 100 coins).
  2. Run the experiment a bunch of times.
  3. Find the proportion of experiments in which the number of heads was 60 or more.

Step 1: Figure out how to do one experiment

Aside: Putting the experiment in a function

It's a good idea to do this, as it makes it easier to run the experiment repeatedly.

Step 2: Repeat the experiment

Step 2: Repeat the experiment

Step 3: Find the proportion of experiments in which the number of heads was 60 or more

This is quite close to the true theoretical answer!

Visualizing the distribution

Example: The "Monty Hall" Problem

The "Monty Hall" Problem

Suppose you’re on a game show, and you’re given the choice of three doors: behind one door is a car 🚗; behind the others, goats 🐐🐐.

(The question was originally posed in Parade magazine’s "Ask Marilyn" column. It is called the "Monty Hall problem" because Monty Hall was the host of the game show in question, "Let's Make a Deal.")

Concept Check ✅ – Answer at cc.dsc10.com

You originally selected door #2. The host reveals door #3 to have a goat behind it. What should you do?

A. Might as well stick with door number #2; it has just as high a chance of winning as door #1. It doesn't matter whether you switch or not.

B. Switch to door number #1; it has a higher chance of winning than door #2.

Let's see 🤔

Time to simulate!

Let's simulate the Monty Hall problem many times to estimate the probability of winning.

  1. Figure out how to simulate one game of Monty Hall.
  2. Play the game many times.
  3. Count the proportion of wins for each strategy (stay or switch).

Step 1: Simulate a single game

When a contestant picks their door, there are three equally-likely outcomes:

  1. Car.
  2. Goat #1.
  3. Goat #2.

Step 1: Simulate a single game

Suppose we can see what is behind their door (but the contestant can't).

Step 1: Simulate a single game

Step 1: Simulate a single game

Let's turn this into a function to make it easier to repeat:

Step 2: Play the game many times

We should save the winning strategies. To do so, let's use np.append:

Step 3: Count the proportion of wins for each strategy (stay or switch)

Alternate implementation

No arrays needed! This strategy won't always work; it depends on the goal of the simulation.

Marilyn vos Savant's column

  • vos Savant asked the question in Parade magazine.
  • She stated the correct answer: switch.
  • She received over 10,000 letters in disagreement, including over 1,000 letters from people with Ph.D.s.

Summary, next time

Simulation finds probabilities

The simulation "recipe"

To estimate the probability of an event through simulation:

  1. Make a function that runs the experiment once.
  2. Run that function many, many times (usually 10000) with a for-loop, and save the results in an array with np.append.
  3. Compute the proportion of times the event occurs using np.count_nonzero.

What's next?