Darts, Dice, and Coins Sampling from a Discrete Distribution The - - PowerPoint PPT Presentation

darts dice and coins
SMART_READER_LITE
LIVE PREVIEW

Darts, Dice, and Coins Sampling from a Discrete Distribution The - - PowerPoint PPT Presentation

Darts, Dice, and Coins Sampling from a Discrete Distribution The Problem Statement You are given an n-sided loaded die with probabilities p 1 , p 2 , , p n of sides 1, 2, , n coming up. What is the most efficient algorithm for


slide-1
SLIDE 1

Darts, Dice, and Coins

Sampling from a Discrete Distribution

slide-2
SLIDE 2
slide-3
SLIDE 3

The Problem Statement

  • You are given an n-sided loaded die with

probabilities p1, p2, …, pn of sides 1, 2, …, n coming up.

  • What is the most efficient algorithm for

simulating rolls of the die?

slide-4
SLIDE 4

The Computational Model

  • Assume that the following can be done in

O(1) time:

  • Generation of a uniformly-random real

number in the interval [0, 1)

  • Addition, subtraction, multiplication,

division, and comparisons of real numbers.

  • Floor and ceiling of real numbers.
slide-5
SLIDE 5

Simulating a Fair Die

slide-6
SLIDE 6

Simulating a Fair Six-Sided Die

1

slide-7
SLIDE 7

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

slide-8
SLIDE 8

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

slide-9
SLIDE 9

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

slide-10
SLIDE 10

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

A uniformly- distributed value x in the range [0, 1)

slide-11
SLIDE 11

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

A uniformly- distributed value x in the range [0, 1)

What is the largest integer k such that k/6 < x?

slide-12
SLIDE 12

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

A uniformly- distributed value x in the range [0, 1)

What is the largest integer k such that k < 6x?

slide-13
SLIDE 13

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

A uniformly- distributed value x in the range [0, 1)

What is the largest integer k such that k < 6x? k = ⌊6x⌋

slide-14
SLIDE 14

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

A uniformly- distributed value x in the range [0, 1)

  • 1. Generate a random value x ∈ [0, 1)
  • 2. Output k = ⌊6x⌋
slide-15
SLIDE 15

Simulating a Fair Six-Sided Die

1/6 2/6 3/6 4/6 5/6 6/6

A uniformly- distributed value x in the range [0, 1)

  • 1. Generate a random value x ∈ [0, 1)
  • 2. Output k = ⌊6x⌋

Time required: O(1)

slide-16
SLIDE 16

Simulating a Fair n-Sided Die

slide-17
SLIDE 17

Simulating a Fair n-Sided Die

1/n 2/n 3/n ... n/n

slide-18
SLIDE 18

Simulating a Fair n-Sided Die

1/n 2/n 3/n ... n/n

  • 1. Generate a random value x ∈ [0, 1)
  • 2. Output k = ⌊nx⌋

Time required: O(1)

slide-19
SLIDE 19

Can we simulate a loaded die with a fair die?

slide-20
SLIDE 20

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12
slide-21
SLIDE 21

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1

1/2 5/6 11/12

slide-22
SLIDE 22

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1

1/2 5/6 11/12

slide-23
SLIDE 23

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1

1/2 5/6 11/12

How do we know which bucket this point is in?

slide-24
SLIDE 24

A Sample Loaded Die

  • A four-sided die:
  • p1 = 6/12
  • p2 = 4/12
  • p3 = 1/12
  • p4 = 1/12

1

1/2 5/6 11/12

slide-25
SLIDE 25

A Sample Loaded Die

  • A four-sided die:
  • p1 = 6/12
  • p2 = 4/12
  • p3 = 1/12
  • p4 = 1/12
  • A 12-sided fair die:
  • Six sides labeled 1
  • Four sides labeled

2

  • One side labeled 3
  • One side labeled 4

1

1/2 5/6 11/12

slide-26
SLIDE 26

A Sample Loaded Die

  • A four-sided die:
  • p1 = 6/12
  • p2 = 4/12
  • p3 = 1/12
  • p4 = 1/12
  • A 12-sided fair die:
  • Six sides labeled 1
  • Four sides labeled

2

  • One side labeled 3
  • One side labeled 4

1/12 2/12 3/12

1

4/12 5/12 6/12 7/12 8/12 9/12 10/12 11/12

slide-27
SLIDE 27

A Sample Loaded Die

  • A four-sided die:
  • p1 = 6/12
  • p2 = 4/12
  • p3 = 1/12
  • p4 = 1/12
  • A 12-sided fair die:
  • Six sides labeled 1
  • Four sides labeled

2

  • One side labeled 3
  • One side labeled 4

1/12 2/12 3/12

1

4/12 5/12 6/12 7/12 8/12 9/12 10/12 11/12

1 1 1 1 1 1 2 2 2 2 3 4

slide-28
SLIDE 28

Loaded Dice from Fair Dice

  • Normalize the probabilities so that they

have the same denominator d.

  • Create a fair d-sided die, and label the

sides with a frequency that guarantees the original probabilities.

  • Roll the fair die, then report the result.
slide-29
SLIDE 29

The Catch

  • The common denominator can be huge!
  • Example: 1/1,000,000 and

999,999/1,000,000 requires a one-million- sided die to simulate a two-sided die.

  • Can require arbitrarily many sides with just

two outcomes.

  • Storing the table necessary to map back to

the original distribution is completely infeasible here.

  • Algorithm impractical unless we know

something about the inputs in advance.

slide-30
SLIDE 30

Simulating a Biased Coin

slide-31
SLIDE 31

Simulating a Biased Coin

1

slide-32
SLIDE 32

Simulating a Biased Coin

p 1

slide-33
SLIDE 33

Simulating a Biased Coin

p 1

slide-34
SLIDE 34

Simulating a Biased Coin

p 1

slide-35
SLIDE 35

Simulating a Biased Coin

p

A uniformly- distributed value x in the range [0, 1)

1

slide-36
SLIDE 36

Simulating a Biased Coin

p

A uniformly- distributed value x in the range [0, 1)

What bucket does x belong to?

1

slide-37
SLIDE 37

Simulating a Biased Coin

p

A uniformly- distributed value x in the range [0, 1)

If x < p, output “heads” Otherwise output “tails.”

1

slide-38
SLIDE 38

Generalizing This Idea

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10
slide-39
SLIDE 39

Generalizing This Idea

1

1/4 23/40 9/20 7/10 4/5 9/10

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10
slide-40
SLIDE 40

Generalizing This Idea

1

1/4 23/40 9/20 7/10 4/5 9/10

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10
slide-41
SLIDE 41

Generalizing This Idea

1

1/4 23/40 9/20 7/10 4/5 9/10

What bucket does x belong to?

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10
slide-42
SLIDE 42

Generalizing This Idea

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10

1

1/4 23/40 9/20 7/10 4/5 9/10

What bucket does x belong to?

1/4 1/4 9/20 9/20 23/40 23/40 7/10 7/10 4/5 4/5 9/10 9/10 1

slide-43
SLIDE 43

Generalizing This Idea

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10

1

1/4 23/40 9/20 7/10 4/5 9/10

What bucket does x belong to?

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 1/4 9/20 9/20 23/40 23/40 7/10 7/10 4/5 4/5 9/10 9/10 1

slide-44
SLIDE 44

Generalizing This Idea

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10

1

1/4 23/40 9/20 7/10 4/5 9/10

What bucket does x belong to?

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 1/4 9/20 9/20 23/40 23/40 7/10 7/10 4/5 4/5 9/10 9/10 1

slide-45
SLIDE 45

Generalizing This Idea

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10

1

1/4 23/40 9/20 7/10 4/5 9/10

What bucket does x belong to?

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 1/4 9/20 9/20 23/40 23/40 7/10 7/10 4/5 4/5 9/10 9/10 1

slide-46
SLIDE 46

Generalizing This Idea

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10

1

1/4 23/40 9/20 7/10 4/5 9/10

What bucket does x belong to?

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 1/4 9/20 9/20 23/40 23/40 7/10 7/10 4/5 4/5 9/10 9/10 1

slide-47
SLIDE 47

Generalizing This Idea

  • A 7-sided die:
  • p1 = 1/4
  • p2 = 1/5
  • p3 = 1/8
  • p4 = 1/8
  • p5 = 1/10
  • p6 = 1/10
  • p7 = 1/10

1

1/4 23/40 9/20 7/10 4/5 9/10

What bucket does x belong to?

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 9/20 23/40 7/10 4/5 9/10

1

1/4 1/4 9/20 9/20 23/40 23/40 7/10 7/10 4/5 4/5 9/10 9/10 1

slide-48
SLIDE 48

Roulette Wheel Selection

  • Construct a cumulative probability

table containing a running total of the probabilities so far.

  • To simulate the loaded die:
  • Generate a random value in [0, 1).
  • Do a binary search on the table for the

bucket.

  • Initialization time: Θ(n)
  • Generation time: O(log n)
slide-49
SLIDE 49

Throwing Darts

slide-50
SLIDE 50

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12
slide-51
SLIDE 51

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

slide-52
SLIDE 52

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

slide-53
SLIDE 53

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

slide-54
SLIDE 54

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

slide-55
SLIDE 55

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

slide-56
SLIDE 56

Throw Again!

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

slide-57
SLIDE 57

Throw Again!

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

slide-58
SLIDE 58

Throw Again!

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

How do we know what we hit?

slide-59
SLIDE 59

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

How do we know what we hit?

slide-60
SLIDE 60

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

How do we know what we hit?

Each column has the same width. Each column has only two

  • ptions.
slide-61
SLIDE 61

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

How do we know what we hit?

Choose the column by rolling a fair die. Each column has only two

  • ptions.
slide-62
SLIDE 62

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

How do we know what we hit?

Choose the column by rolling a fair die. Each column has only two

  • ptions.
slide-63
SLIDE 63

A Sample Loaded Die

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1 2 3 4

How do we know what we hit?

Choose the column by rolling a fair die. Choose which part of the column by flipping a biased coin.

slide-64
SLIDE 64

1 2 3 4

Throwing a dart at this board is equivalent to rolling a fair die, then flipping one of many biased coins.

slide-65
SLIDE 65

1 2/3 1/6 1/6

Throwing a dart at this board is equivalent to rolling a fair die, then flipping one of many biased coins.

slide-66
SLIDE 66

The Dartboard Algorithm

  • Build the target by dividing

all probabilities by the largest probability.

  • Until you choose a side:
  • Roll a fair die to choose a

column.

  • Flip the coin in that column.
  • If it's heads, output the

column.

1 2/3 1/6 1/6

slide-67
SLIDE 67

Not All Dartboards are Equal

slide-68
SLIDE 68

Not All Dartboards are Equal

slide-69
SLIDE 69

Not All Dartboards are Equal

  • If the die is close to being

fair, the probability of missing is very low.

  • As the die becomes more

unfair, the probability of missing keeps increasing.

  • In the limit, the dartboard

might have n – 1 out of n columns always fail.

slide-70
SLIDE 70

Not All Dartboards are Equal

  • If the die is close to being

fair, the probability of missing is very low.

  • As the die becomes more

unfair, the probability of missing keeps increasing.

  • In the limit, the dartboard

might have n – 1 out of n columns always fail.

slide-71
SLIDE 71

Not All Dartboards are Equal

  • If the die is close to being

fair, the probability of missing is very low.

  • As the die becomes more

unfair, the probability of missing keeps increasing.

  • In the limit, the dartboard

might have n – 1 out of n columns always fail.

slide-72
SLIDE 72

Not All Dartboards are Equal

  • If the die is close to being

fair, the probability of missing is very low.

  • As the die becomes more

unfair, the probability of missing keeps increasing.

  • In the limit, the dartboard

might have n – 1 out of n columns always fail.

slide-73
SLIDE 73

Not All Dartboards are Equal

  • If the die is close to being

fair, the probability of missing is very low.

  • As the die becomes more

unfair, the probability of missing keeps increasing.

  • In the limit, the dartboard

might have n – 1 out of n columns always fail.

slide-74
SLIDE 74

Not All Dartboards are Equal

  • If the die is close to being

fair, the probability of missing is very low.

  • As the die becomes more

unfair, the probability of missing keeps increasing.

  • In the limit, the dartboard

might have n – 1 out of n columns always fail.

slide-75
SLIDE 75

Not All Dartboards are Equal

  • If the die is close to being

fair, the probability of missing is very low.

  • As the die becomes more

unfair, the probability of missing keeps increasing.

  • In the limit, the dartboard

might have n – 1 out of n columns always fail.

slide-76
SLIDE 76

Not All Dartboards are Equal

  • Perfectly fair die: O(1)

dart tosses required.

  • Perfectly biased die:

O(n) dart tosses required on expectation.

  • General expected

number of dart tosses: O(n pmax)

slide-77
SLIDE 77

Can we get rid of the empty space?

slide-78
SLIDE 78

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

slide-79
SLIDE 79

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

slide-80
SLIDE 80

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

Height is ½, the maximum

  • f these

values.

slide-81
SLIDE 81

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

slide-82
SLIDE 82

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

Height is ¼, the average of these values.

slide-83
SLIDE 83

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

1/3

slide-84
SLIDE 84

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2

1/12 1/12 1/12

1/4

slide-85
SLIDE 85

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/4

1/12 1/12 1/12

1/4 1/4

slide-86
SLIDE 86

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/4

1/12 1/12 1/12

1/4 1/4

We have gotten rid of the blank spaces. Every dart tossed will hit something!

slide-87
SLIDE 87

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/4

1/12 1/12 1/12

1/4 1/4

We have gotten rid of the blank spaces. Every dart tossed will hit something! But now, each column isn't a biased coin.

slide-88
SLIDE 88

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/4

1/12 1/12 1/12

1/4 1/4

We have gotten rid of the blank spaces. Every dart tossed will hit something! But now, each column isn't a biased coin.

slide-89
SLIDE 89

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

slide-90
SLIDE 90

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/3

1/12 1/12

Goal: Reshape this setup so that each column has at most two different blocks.

slide-91
SLIDE 91

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/2 1/6

1/12 1/12

Goal: Reshape this setup so that each column has at most two different blocks.

1/6

slide-92
SLIDE 92

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

5/12

1/6

1/12 1/12

Goal: Reshape this setup so that each column has at most two different blocks.

1/6 1/6

slide-93
SLIDE 93

Getting Rid of Spaces

  • A four-sided die:
  • p1 = 1/2
  • p2 = 1/3
  • p3 = 1/12
  • p4 = 1/12

1/4

1/6

1/12 1/12

Goal: Reshape this setup so that each column has at most two different blocks.

1/6 1/6

1/12

slide-94
SLIDE 94

1/4

1/6

1/12 1/12

1/6 1/6

1/12

We can choose the dart's x coordinate by rolling a fair die. We can choose the dart's y coordinate by flipping a biased coin. If the coin yields heads,

  • utput the lower half as the

answer. If the coin yields tails,

  • utput the upper half as the

answer.

slide-95
SLIDE 95

1

2/3

1/3 1/3

2/3 2/3

1/3

We can choose the dart's x coordinate by rolling a fair die. We can choose the dart's y coordinate by flipping a biased coin. If the coin yields heads,

  • utput the lower half as the

answer. If the coin yields tails,

  • utput the upper half as the

answer.

slide-96
SLIDE 96

1/4

1/6

1/12 1/12

1/6 1/6

1/12

We can choose the dart's x coordinate by rolling a fair die. We can choose the dart's y coordinate by flipping a biased coin. If the coin yields heads,

  • utput the lower half as the

answer. If the coin yields tails,

  • utput the upper half as the

answer.

1

2/3

1/3 1/3

2/3 2/3

1/3

slide-97
SLIDE 97

1/4

1/6

1/12 1/12

1/6 1/6

1/12

We can choose the dart's x coordinate by rolling a fair die. We can choose the dart's y coordinate by flipping a biased coin. If the coin yields heads,

  • utput the lower half as the

answer. If the coin yields tails,

  • utput the upper half as the

answer.

1

2/3

1/3 1/3

2/3 2/3

1/3

slide-98
SLIDE 98

1/4

1/6

1/12 1/12

1/6 1/6

1/12

We can choose the dart's x coordinate by rolling a fair die. We can choose the dart's y coordinate by flipping a biased coin. If the coin yields heads,

  • utput the lower half as the

answer. If the coin yields tails,

  • utput the upper half as the

answer.

1

2/3

1/3 1/3

2/3 2/3

1/3

slide-99
SLIDE 99

1/4

1/6

1/12 1/12

1/6 1/6

1/12

We can choose the dart's x coordinate by rolling a fair die. We can choose the dart's y coordinate by flipping a biased coin. If the coin yields heads,

  • utput the lower half as the

answer. If the coin yields tails,

  • utput the upper half as the

answer.

1

2/3

1/3 1/3

2/3 2/3

1/3

slide-100
SLIDE 100

The Alias Method

  • Distribute probabilities such that
  • Each column has height 1 / n
  • Each column has at most two blocks in

it.

  • To generate a roll of the die:
  • Roll a fair die to choose the column.
  • Flip a biased coin to choose the upper
  • r lower block.
  • Generation time is now O(1).

1/4

1/6

1/12 1/12

1/6 1/6

1/12

1

2/3

1/3 1/3

2/3 2/3

1/3

slide-101
SLIDE 101

The Alias Method

  • For each column, have to

store

  • The probability that of the

coin for that column.

  • What true and false

evaluate to.

  • One column per side of

the die.

  • Total space: Θ(n)

1/4

1/6

1/12 1/12

1/6 1/6

1/3

1

2/3

1/3 1/3

2/3 2/3

slide-102
SLIDE 102

Building the Dartboard

slide-103
SLIDE 103

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-104
SLIDE 104

Building the Dartboard

1/2 1/3

1/12 1/12

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-105
SLIDE 105

Building the Dartboard

1/2 1/3

1/12 1/12

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-106
SLIDE 106

Building the Dartboard

1/3 1/3

1/12 1/12

1/6

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-107
SLIDE 107

Building the Dartboard

1/3 1/3

1/12 1/12

1/6

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-108
SLIDE 108

Building the Dartboard

1/3 1/3

1/12 1/12

1/6

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-109
SLIDE 109

Building the Dartboard

1/6 1/3

1/12 1/12

1/6 1/6

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-110
SLIDE 110

Building the Dartboard

1/6 1/3

1/12 1/12

1/6 1/6

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-111
SLIDE 111

Building the Dartboard

1/6 1/3

1/12 1/12

1/6 1/6

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-112
SLIDE 112

Building the Dartboard

1/6 1/6

1/12 1/12

1/6 1/6

1/12

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-113
SLIDE 113

Building the Dartboard

1/6 1/6

1/12 1/12

1/6 1/6

1/12

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-114
SLIDE 114

1/4

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

1/5 1/8 1/8 1/101/101/10

slide-115
SLIDE 115

1/4

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

1/5 1/8 1/8 1/101/101/10

slide-116
SLIDE 116

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-117
SLIDE 117

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-118
SLIDE 118

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-119
SLIDE 119

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-120
SLIDE 120

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-121
SLIDE 121

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-122
SLIDE 122

Building the Dartboard

  • Until no holes remain:
  • Find a column that has a

hole.

  • Find a column that has

some excess.

  • Move enough from the

column with an excess into the column with a gap to fill it in.

slide-123
SLIDE 123

Correctness Proof Sketch

  • If not all columns are balanced:
  • Something must be too small.
  • Since the total probability just covers the

dartboard, something must be too big as well.

  • Always possible to move probability from one

column to another.

  • Process eventually terminates:
  • Each iteration takes a column that wasn't

balanced and balances it.

  • Eventually all columns become balanced.
slide-124
SLIDE 124

Summary

  • We can efficiently roll a fair die.
  • Gives an inefficient algorithm for loaded dice.
  • We can efficiently flip a biased coin.
  • Generalizes to an efficient algorithm for loaded dice.
  • We can reduce rolling a loaded die to rolling a

fair die and flipping a biased coin.

  • Generalizes to an extremely efficient algorithm for

loaded dice.

slide-125
SLIDE 125

For More on These Algorithms

http://www.keithschwarz.com/darts-dice-coins/

slide-126
SLIDE 126

Questions?