Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran...

61
Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Transcript of Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran...

Page 1: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Probability

Will MonroeSummer 2017

with materials byMehran Sahamiand Chris Piech

June 30, 2017

Page 2: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Today we will make history

Page 3: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Logistics: Office hours

Page 4: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Review: Permutations

The number of ways of orderingn distinguishable objects.

n ! =1⋅2⋅3⋅...⋅n=∏i=1

n

i

Page 5: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Review: Combinations

The number of unique subsets of size k from a larger set of size n. (objects are distinguishable, unordered)

(nk ) =n !

k !(n−k)!

choose k

n

Page 6: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Review: Bucketing

The number of ways of assigning n distinguishable objects to a fixedset of k buckets or labels.

kn

k buckets

n objects

Page 7: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Divider method

The number of ways of assigning n indistinguishable objects to a fixedset of k buckets or labels.

(n+(k−1)

n )

k buckets

n objects

(k - 1 dividers)

Page 8: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

A grid of ways of counting

kn(nk )n !

n !k1! k2!…km !

(n+(k−1)

n )1 1

Creativity!– Split into cases– Use inclusion/exclusion– Reframe the problem

Ordering Subsets Bucketing

Alldistinct

Someindistinct

Allindistinct

Page 9: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Sample space

S = the set of all possible outcomes

Coin flip

Two coin flips

Roll of 6-sided die

# emails in day

Netflix hours in day

S = {Heads, Tails}

S = {(H, H), (H, T), (T, H), (T, T)}

S = {1, 2, 3, 4, 5, 6}

S = (non-neg. integers)ℕ

S = [0, 24] (interval, inclusive)

Page 10: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Events

E = some subset of S (E ⊆ S)

Coin flip is heads

≥ 1 head on 2 flips

Roll of die ≤ 3

# emails in day ≤ 20

“Wasted day”

E = {Heads}

E = {(H, H), (H, T), (T, H)}

E = {1, 2, 3}

E = {x | x ∈ , x ≤ 20}ℕ

E = [5, 24]

(Ross uses to mean )⊂ ⊆

Page 11: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Set operations

E ∪ F = {1, 2, 3}

E = {1, 2}

F ={2, 3}

S = {1, 2, 3, 4, 5, 6}

Union: outcomes that are in E or F

Page 12: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Set operations

EF = E ∩ F = {2}

E = {1, 2}

F ={2, 3}

S = {1, 2, 3, 4, 5, 6}

Intersection: outcomes that are in E and F

Page 13: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Set operations

Ec = {3, 4, 5, 6}

E = {1, 2}

F ={2, 3}

S = {1, 2, 3, 4, 5, 6}

Complement: outcomes that are not in E

Page 14: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

A profound truth

https://medium.com/@timanglade/how-hbos-silicon-valley-built-not-hotdog-with-mobile-tensorflow-keras-react-native-ef03260747f3

Page 15: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

A profound truth

Everything in the world is either

a hot dog

or

not a hot dog.

(E ∪ Ec = S)

https://medium.com/@timanglade/how-hbos-silicon-valley-built-not-hotdog-with-mobile-tensorflow-keras-react-native-ef03260747f3

Page 16: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Quiz question

https://b.socrative.com/login/student/

Room: CS109SUMMER17

Page 17: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Quiz questionWhich is the correct picture for EcFc (= Ec ∩ Fc)?

E F

S

E F

S

E F

S

E F

S

E F

S

A

B

C

D

E

Page 18: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Quiz question

E F

S

C

Which is the correct picture for EcFc (= Ec ∩ Fc)?

Page 19: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Quiz questionWhich is the correct picture for Ec ∪ Fc?

E F

S

E F

S

E F

S

E F

S

E F

S

A

B

C

D

E

Page 21: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

De Morgan's Laws“distributive laws” for set complement

E F

S

(E ∪ F)c = Ec ∩ Fc (E ∩ F)c = Ec ∪ Fc

E F

S

(⋃i Ei)c=⋂

i(Ei

c ) (⋂i Ei )c=⋃

i(Ei

c )

Page 23: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Sources of probability

Experiment

Expert opinion Analytical solution

Datasets

Page 24: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Meaning of probability

A quantification of ignorance

image: Frank Derks

Page 25: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

P(E)=limn→∞

# (E)

n

What is a probability?

Page 26: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Axioms of probability

0≤P(E)≤1

P(S)=1

P(E∪F)=P(E)+P(F)If E∩F=∅ , then

(1)

(2)

(3)

(Sum rule, but with probabilities!)

Page 27: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Corollaries

P(Ec)=1−P(E)

P(E∪F)=P(E)+P(F)−P(EF)

If E⊆F , then P(E)≤P(F)

(Principle of inclusion/exclusion, but with probabilities!)

Page 28: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Principle of Inclusion/Exclusion

The total number of elements in two sets is the sum of the number of elements of each set,minus the number of elements in both sets.

|A∪B|=|A|+|B|−|A∩B|

3 4

3+4-1 = 6

Page 29: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|E∪F∪G|=

E

F G

Page 30: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|E∪F∪G|=|E|

E

F G

1

1

11

Page 31: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|E∪F∪G|=|E|+|F|

E

F G

1

2

12

11

Page 32: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|E∪F∪G|=|E|+|F|+|G|

E

F G

1

3

22

12 1

Page 33: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|E∪F∪G|=|E|+|F|+|G|

E

F G

1

2

21

12 1

−|EF|

Page 34: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|E∪F∪G|=|E|+|F|+|G|

E

F G

1

1

11

12 1

−|EF|−|EG|

Page 35: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|E∪F∪G|=|E|+|F|+|G|

E

F G

1

0

11

11 1

−|EF|−|EG|−|FG|

Page 36: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|E∪F∪G|=|E|+|F|+|G|

E

F G

1

1

11

11 1

−|EF|−|EG|−|FG|

+|EFG|

Page 37: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

|⋃i=1

n

Ei|=∑r=1

n

(−1)(r+1) ∑

i1<⋯< ir

|⋂j=1

r

Ei j|

size ofthe union

sum oversubsetsizes

add orsubtract

(based on size)

size ofintersections

sum overall subsetsof that size

Page 38: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

P(⋃i=1

n

Ei)=∑r=1

n

(−1)(r+1) ∑

i1<⋯< ir

P(⋂j=1

r

Ei j)

prob. ofOR

sum oversubsetsizes

add orsubtract

(based on size)

prob. ofAND

sum overall subsetsof that size

Page 39: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Inclusion/exclusion withmore than two sets

P(E∪F∪G)=P(E)+P(F)+P(G)

E

F G

1

1

11

11 1

−P(EF)−P(EG)−P(FG)

+P(EFG)

Page 40: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Break time!

Page 41: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Equally likely outcomes

Coin flip

Two coin flips

Roll of 6-sided die

S = {Heads, Tails}

S = {(H, H), (H, T), (T, H), (T, T)}

S = {1, 2, 3, 4, 5, 6}

P(Each outcome)=1|S|

P(E)=|E|

|S|(counting!)

Page 42: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Rolling two dice

P(D1+D2=7)=?D1 D2

Page 43: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

How do I get started?

For word problems involving probability,start by defining events!

Page 44: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Rolling two dice

P(D1+D2=7)=?D1 D2

E: {all outcomes such that the sum of the two dice is 7}

Page 45: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Rolling two dice

P(D1+D2=7)=6

36=

16D1 D2

S = {(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),

S = {(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),

S = {(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6),

S = {(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6),

S = {(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6),

S = {(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)}

|S|=36

|E|=6

Page 46: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Rolling two (indistinguishable) dice?

P(D1+D2=7)=3

21=

17

?D1 D2

S = {(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),

S = {(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),

S = {(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6),

S = {(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6),

S = {(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6),

S = {(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)}

|S|=21

|E|=3

[check your assumptions]

Page 47: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Getting rid of ORs

Finding the probability of an OR of events can be nasty. Try using De Morgan's laws to turn it into an AND!

P(A∪B∪⋯∪Z )=1−P(Ac Bc⋯Zc

)

E F

S

Page 48: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Running into a friend

image: torbakhopper / scott richard

21,000 people at StanfordYou're friends with 250

Go to an event with 70 other(equally-likely) Stanforders

What's P(at least one friend among the 70)?

S: {all subsets of size 70 of 21,000 students}

E: {subsets of size 70 with at least one friend}

|S|=(21,00070 )

|E|=???

Page 49: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Running into a friend

image: torbakhopper / scott richard

21,000 people at StanfordYou're friends with 250

Go to an event with 70 other(equally-likely) Stanforders

What's P(at least one friend among the 70)?

S: {all subsets of size 70 of 21,000 students}

E: {subsets of size 70 with at least one friend}Ec: {subsets of size 70 with no friends}|S|=(21,000

70 )

|EC|=(21,000−250

70 ) P(E)=1−P(EC)=1−

(20,75070 )

(21,00070 )

≈0.5512

Page 50: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Running into a friend

https://cs109.stanford.edu/demos/serendipity.html

serendipity [ˌse.ɹənˈdɪ.pɨ.ɾi] n the faculty or phenomenon of finding valuable or agreeable things not sought for

Merriam-Webster

Page 51: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Getting rid of ORs

Finding the probability of an OR of events can be nasty. Try using De Morgan's laws to turn it into an AND!

P(A∪B∪⋯∪Z )=1−P(Ac Bc⋯Zc

)

E F

S

Page 52: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Defective chips

n

k

|S|=(nk)

|E|=1⋅(n−1k−1)

S: {all subsets of size k from n chips}

E: {all outcomes such that the defective chip is chosen}

Page 53: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Defective chips

n

k

|S|=(nk)

|E|=1⋅(n−1k−1)

P(E)=(n−1k−1)

(nk)=

(n−1)!(k−1)!(n−k )!

n !k !(n−k)!

=(n−1)! k !n!(k−1)!

=(n−1)!⋅k⋅(k−1)!n⋅(n−1)!(k−1)!

=kn

Page 54: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Poker straight

(Standard deck:)52 cards = 13 ranks × 4 suits

“Straight”: 5 consecutive ranks(suits can be different)

S: {all possible hands}

E: {all hands that are straights}

|S|=(525 )

|E|=10⋅45

startingrank

suit ofeach card

image: Guts Gaming (www.guts.com/en)

Page 55: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Poker straight

(Standard deck:)52 cards = 13 ranks × 4 suits

“Straight”: 5 consecutive ranks(suits can be different)

S: {all possible hands}

E: {all hands that are straights}

|S|=(525 )

|E|=10⋅45

P(E)=10⋅45

(525 )

≈0.00394

image: Guts Gaming (www.guts.com/en)

Page 56: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Birthdays

n people in a room.What is the probability none share the same birthday?

S: {all ways of giving all n people each a birthday}

E: {all ways of giving all n people different birthdays}

|S|=365n

|E|=365⋅364⋅…⋅(365−n+1)=365 !

(365−n)!=(365

n )⋅n !

P(E)=(365

n )⋅n !

365n

n = 23: P(E) < 1/2n = 75: P(E) < 1/3,000n = 100: P(E) < 1/3,000,000n = 150: P(E) < 1/3,000,000,000,000,000

Page 57: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Flipping cards● Shuffle deck.

● Reveal cards from the top until we get an Ace. Put Ace aside.

● What is P(next card is the Ace of Spades)?

● P(next card is the 2 of Clubs)?

https://bit.ly/1a2ki4G → https://b.socrative.com/login/student/Room: CS109SUMMER17

A) P(Ace of Spades) > P(2 of Clubs)

B) P(Ace of Spades) = P(2 of Clubs)

C) P(Ace of Spades) < P(2 of Clubs)

Page 58: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Flipping cards● Shuffle deck.

● Reveal cards from the top until we get an Ace. Put Ace aside.

● What is P(next card is the Ace of Spades)?

● P(next card is the 2 of Clubs)?

S: {all orderings of the deck}

E: {all orderings for which Ace of Spadescomes immediately after first Ace}

|S|=52!

|E|=51!⋅1P(E)=

51!52!

=152

Page 59: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Flipping cards● Shuffle deck.

● Reveal cards from the top until we get an Ace. Put Ace aside.

● What is P(next card is the Ace of Spades)?

● P(next card is the 2 of Clubs)?

S: {all orderings of the deck}

E: {all orderings for which 2 of Clubscomes immediately after first Ace}

|S|=52!

|E|=51!⋅1P(E)=

51!52!

=152

Page 60: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

Flipping cards● Shuffle deck.

● Reveal cards from the top until we get an Ace. Put Ace aside.

● What is P(next card is the Ace of Spades)?

● P(next card is the 2 of Clubs)?

B) P(Ace of Spades) = P(2 of Clubs)

Page 61: Probability - Stanford University · Probability Will Monroe Summer 2017 with materials by Mehran Sahami and Chris Piech June 30, 2017

(We just made history)