L20 Can You Fake Random?

DATA SCIENCE · CYCLE 2 · L20 · TEXTBOOK CHAPTER

Can You Fake Random?

A coin has no memory. People forget that, and it costs them: they see a streak and bet that it must end. Today you work out what chance says before you flip, and see what really happens when you do.

After this chapter, you can

For your project: your ANALYSIS tab starts with your question, your number and your two groups. The checkpoint is at the end.

Why It Matters

Drawing of a crowded casino in 1913, gamblers in top hats leaning over a roulette wheel
Monte Carlo, 1913.

At the Monte Carlo Casino on 18 August 1913, the roulette ball landed on black 26 times in a row. As the streak grew, gamblers bet more and more on red, sure that red was due. They lost, spin after spin.

The wheel has 37 pockets: 18 red, 18 black and one green zero. On every spin the chance of red is 18 in 37. On spin 27, after 26 blacks, the chance of red was still 18 in 37, the same as on spin 1. The wheel did not remember the last spins. Red was never due.

The Idea

A coin has no memory

Each flip is a new chance, the same as the first. Results like that are independent: one result does not change the chance of the next.

What it isWhen you know it
theoretical chancethe ways it can happen ÷ all the equally likely waysbefore you flip
observed sharethe times it happened ÷ the triesafter you flip

In a few tries the observed share can be far off. In many tries it settles near the theoretical chance.

Two or three in a row? Each flip is independent, so multiply the chances. That gives the chance of the whole streak, worked out before it starts. Once part of the streak has happened, the next flip is a new chance again.

Worked Examples

Example 1 · Count the equally likely ways. Roll a fair six-sided die. What is the chance of a 5 or a 6?

Create a strategy

List every way the die can land. They are equally likely. Count the ways that give a 5 or a 6.

Apply the idea

The die can land 1, 2, 3, 4, 5 or 6: 6 equally likely ways. Two of them, 5 and 6, count. The chance is 26, which is 1 in 3, or about 33.3%.

Reflect and check

The ways that count can never be more than all the ways, so a chance is never more than 1 in 1, 100%. Here 2 of 6 is a third of the faces, and a third of 100% is about 33.3%.

Example 2 · Three heads in a row. Flip a fair coin three times. What is the chance of three heads?

Create a strategy

Each flip is heads with chance 12, and the flips are independent, so multiply. Check it by drawing every way three flips can go.

Apply the idea

12 × 12 × 12 = 18, which is 1 in 8, or 12.5%.

HTHTHTHTHTHTHTHHHHHTHTHHTTTHHTHTTTHTTTFlip 1Flip 2Flip 3
Every way three flips can go: 8 equally likely paths. One of them, HHH, is three heads.
Reflect and check

The tree agrees: 1 path of 8. Now say you have flipped HHH already. The chance that flip 4 is heads is 1 in 2, the same as flip 1. The 1 in 8 was the chance of the whole streak, worked out before the first flip. Four heads in a row, before any flip, is 1 in 16.

Example 3 · Observed against theoretical. Sam flipped a fair coin 40 times and got 26 heads. The class pooled its flips: 2,000 flips, 1,012 heads. Compare each with the theoretical chance.

Create a strategy

Observed share = the times it happened ÷ the tries. The theoretical chance of heads is 50% every time.

Apply the idea
FlipsHeadsObserved shareTheoretical chance
402626 ÷ 40 = 65%50%
2,0001,0121,012 ÷ 2,000 = 50.6%50%
Reflect and check

Sam’s 65% does not mean the coin is broken. Forty flips is few, and a few tries wobble. Two thousand flips settled within one point of 50%. The more tries, the closer the observed share comes to the theoretical chance.

In Sheets: Flip a Coin 1,000 Times

  1. Go to sheets.new.
  2. Click cell A1. Type: =RANDBETWEEN(0,1) Press Return.
  3. Click cell A1. Press Command-C.
  4. Click the Name box, left of the formula bar. Type A1:A1000. Press Return.
  5. Press Command-V. One coin, flipped 1,000 times. 1 is heads, 0 is tails.
  6. Click cell L1. Type: =AVERAGE(A1:A100) Press Return.
  7. Click cell L2. Type: =AVERAGE(A1:A1000) Press Return.
  8. New flips: click cell W10, type x, press Return. L1 and L2 change.

The mean of 1s and 0s is the share of 1s. So L1 is the observed share of heads after 100 flips, and L2 is the share after 1,000. Your task sheet does the same for ten coins at once, in columns A to J. The flips change every time the Sheet changes. To keep a set, freeze it: select the flips, press Command-C, then Edit > Paste special > Values only.

The Same in Python

The same 1,000 flips, in four lines. Python came in L19b; one word here is new. .sample( ) draws rows at random, the way =RANDBETWEEN does in Sheets.

In Sheets this is: =RANDBETWEEN(0,1) pasted into A1:A1000, then =AVERAGE(A1:A1000).

In English:

  1. Get the pandas toolbox and call it pd.
  2. Make a column of 0 and 1, and call it coin. 1 is heads, 0 is tails.
  3. Draw from coin 1,000 times, putting each one back, and call the draws flips.
  4. Print the mean of flips: the share of heads.

Predict: near what number will it print? Run it three times. Then change 1000 to 10 and run it three more: which wobbles more?

Can’t run it here? Show what it prints
0.507

Yours will differ: the flips are new every run. With 1,000 flips the share stays close to 0.5. With 10 it can print 0.2 or 0.8.

Want to type your own? The Python practice page has tasks and a notebook.

Try It: Wobble, Then Settle

Press each button a few times. Each press is a new set of flips, and drops a dot at its share of heads. Watch which row spreads out and which row bunches up.

Can You Fake Random?

Ask people to write 100 coin flips from their heads and they switch from H to T too often, and avoid long streaks. Real flips have longer streaks than people expect. A computer flipped 100 coins 20,000 times: 81% of those sets had a streak of 6 or more in a row. Only about 3 sets in 100 had no streak longer than 4, so a list of 100 like that is probably made up.

In your project

Your Cycle 2 project ends on how sure you are that a difference between two groups is real. Chance makes every share and every mean wobble, the way 40 flips wobble. Later in this cycle you will shuffle your own data to see how big a gap chance alone can make.

Check Yourself

Answer each one before you look. The first two are from L19 and L19b.

Find the SD of 1, 2, 3, 3, 11 by hand. L19

What does line 2 print? L19b

1steps = pd.Series([1, 2, 6]) 2print(steps.median())

In Sheets this is: =MEDIAN on three numbers. In English: 1. Make a column of the three numbers, and call it steps. 2. Print the median of steps.

Roll a fair six-sided die. What is the chance of an even number, as a percent?

Flip a fair coin twice. What is the chance of two heads in a row, as a percent?

A fair coin has landed tails 4 times in a row. What is the chance the next flip is heads?

You flip a coin 50 times and get 31 heads. What is the observed share of heads, as a percent?

Same coin: 31 heads in 50 flips. Is the coin unfair?

Three flips, done 400 times. About how many times would you expect three heads?

Two lists of 30 flips. One is real; one was made up.
List 1: HHTTTHTHHHHHHTTHTTTHHTHTHHTTTH
List 2: HTHTTHTHHTHTHTTHTHTHHTHTTHTHTH

Project Checkpoint

Homework 6 · due Wed 2 Dec

Your claim compares two groups. Before any number, your file says what you are asking, which column you measure and which two groups you compare. Every later step reads these three cells.

Tick each step once it is in your Sheet. Your ticks go into the PDF you hand in. Where everything goes: your Sheet, tab by tab.

Read More

OpenStax, Introductory Statistics 2e, section 3.1: Terminology. A free college textbook. It uses a die where we used a coin: the theoretical chance, the observed share (it says relative frequency), and the law of large numbers.

Stuck on any of this? Ask Mr. Case in class or send him a message in Canvas. Name the example or the question number, and say which step lost you.