Lecture 12 – Simulations

DSC 10, Spring 2023

Announcements

Midterm Exam details

The Midterm Exam is on Friday 5/5 during your assigned lecture.

Agenda

Simulations.

Simulations

Simulations

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?

Plan:

  1. Figure out how to run the experiment (flipping 100 coins) once.
  2. Repeat the experiment many times.
  3. Find the proportion of experiments in which the number of heads was 60 or more.

Step 1: Figure out how to run the experiment once

Aside: Defining a function to run the experiment

This makes it easy to run the experiment repeatedly.

Step 2: Repeat the experiment many times

Step 2: Repeat the experiment many times

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. A car 🚗 is behind one of the doors, and goats 🐐🐐 are behind the other two.

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

Let's play!

Below, we've embedded the Monty Hall simulator from this site.

Concept Check ✅ – Answer at cc.dsc10.com

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

A. Stay with Door #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 #1; it has a higher chance of winning than Door #2.

Time to simulate!

Plan:

  1. Figure out how to simulate a single game.
  2. Play the game many times, switching each time.
  3. Compute the proportion of wins.

Step 1: Simulate a single game

When you pick a door, there are three equally-likely outcomes:

Step 1: Simulate a single game

When the host opens a different door, they always reveal a goat.

If you always switch, you'll end up winning the prize that is neither behind_picked_door nor revealed.

Step 1: Simulate a single game

Let's put all of our work into a single function to make it easier to repeat.

Now, every time we call simulate_switch_strategy, the result is your prize.

Step 2: Play the game many times

We should save your prize in each game; to do so, we'll use np.append.

Step 3: Count the proportion of wins for this strategy (switching)

This is quite close to the true probability of winning if you switch, $\frac{2}{3}$.

Alternate implementation

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

What if you always stay with your original door?

In this case, your prize is always the same as what was behind the picked door.

Marilyn vos Savant's column in Parade magazine

  • When asked this question by a reader, vos Savant stated the correct answer: switch.
  • She received over 10,000 letters in disagreement, including over 1,000 letters from people with Ph.D.s.
  • This became a nationwide controversy, even getting a front-page New York Times article in 1991.

Summary, next time

Simulations find 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 times (usually 10,000) 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?