Vault #4: Simulation for Ten Heads in a Row

In this DISCOVERY Data Science Project, you will do real data science in less than an hour and you will earn this project's card to your collection when you fully complete this project! 🎉

Data Source: Your Simulation of Flipping a Coin

Simulation is a powerful tool that allows us to run an event with a probabilistic outcome millions of times in under a second. In this Project, you will use simple simulation to flip a coin a million times and discover how to find trends in the simulated data. After writing this simulation, you will do analysis that compiles data over multiple observations -- a simple form of "time-series analysis" -- to find if the statistical probability of events measures the simulated probability.

Create Your Simulation

Create a simulation of flipping a fair coin 1,000,000 times. Record an "H" for heads (50%) and a "T" for tails (50%), store the coin flip result in a column called flip, and store all 1,000,000 observations in a DataFrame df:

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)
Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

🔬 Checkpoint Tests 🔬

⚙️ Test Case: Part 1: Initial Simulation

Part 2: Finding the Two-Coin Sequence

The chance of getting a fair coin to land on "heads" is 50% -- but how common is it to get 2 heads in a row? Before we write the Python code to find the simulated probability, let's calculate the true probability.

Statistical Probability of Two Heads

In the cell below, calculate P_twoheads to be the probability of flipping two fair coins and they both land on heads:

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

Sequences of Two Heads

To find the simulated probability of two heads, we need to observe sequences of two coin flips. Instead of writing a new simulation, we will calculate each two-coin sequence by evaluating every row and calculating the sequence that includes:

  • The result of the current observation's coin flip
  • The result of the previous observation's coin flip

For each observation of the simulation, create a new column called seq2 that contains the two coin sequence as described above (ex: HH, HT, TH, or TT).

Unsure about how to get the previous observation's coin flip? Read the DISCOVERY guide "Using Previous Observations when Computation Values in a DataFrame" to find out more on how to use a previous observation's value with the shift function in your formula:

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

Simulated Probability of Two Heads

Create a new DataFrame called df_twoheads that contains all the rows that contain a sequence of two heads:

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

Finally, calculate the simulated probability of two heads:

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

Simulation Error

To find the error in a simulation result when we know the true probability, the following formula can be used:

$$error = \frac{|(\text{true probability}) - (\text{simulated probability})|}{(\text{true probability})}$$

Using the variables you created earlier, P_twoheads and P_sim_twoheads, calculate the simulation error in the result and store the error in the variable error.

  • Useful Function: The abs function in Python can be used to find the absolute value. For example, abs(-5) will return 5.
Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)
Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

🔬 Checkpoint Tests 🔬

⚙️ Test Case: Part 2: Finding the Two-Coin Sequence

Part 3: Flipping Ten Heads in a Row!

It would be awesome to flip a coin and get ten heads in a row!

Statistical Probability of Ten Heads

Before we look at our data, what is the statistical probability of flipping a fair coin ten times and getting ten heads?

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

Simulation of Ten Heads in a Row

Using the same technique as above, create a new column seq10 that contains the sequence of 10 coin flips that starts with your current observation's coin flip and adds the previous nine coin flips:

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

Simulated Probability of Ten Heads

Create a new DataFrame df_tenheads that contains all sequences of ten heads in a row:

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

Finding the Simulation Error

First, calculate the simulated probability of getting heads 10 times in a row and store it in P_sim_tenheads:

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

Using the variables you created earlier, P_tenheads and P_sim_tenheads, calculate the simulation error in the result and store the error in the variable error10.

Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)
Reset Code Run All to Here Python Output:
(Run your code to see your code result's here.)

🤔 Reflection: Observing the Simulation Error 🤔

Re-run this entire project from the top (again and again, about 5 times) and see how the analysis values above change. Note the trends you see in the percentage errors.

Nearly every time you run this project (but not always), the percentage of simulation error for flipping two coins will be MUCH SMALLER than the percentage error in flipping ten coins.

Two significant factors that influence lowering simulation error are:

  1. Number of Simulations -- The more times you run a simulation, the lower your expected simulation error.
  2. Frequency of Event -- The more common the event, the lower your expected simulation error.

Since we ran the simulation a large number of times (1,000,000), the error rate for common events ("HH" happens 25% of the time) will usually be very low. However, for very rare events (there's only a 1:1024 chance of getting "HHHHHHHHHH"), the simulation will often be quite high -- even with 1,000,000 simulations.

🔬 Checkpoint Tests 🔬

⚙️ Test Case: Part 3: Flipping Ten Heads in a Row

Validate and Complete This Project!

Congratulations on finishing this DISCOVERY Data Science Project! 🎉🎉

To validate your entire project, your entire code will run from top-to-bottom on this page and each test case will be validated one final time. If everything looks good, you'll earn the card for completing this project: