1 of 98

Multinomial

Chris Gregg

CS109, Stanford University

Summer 2026

2 of 98

Exciting Day!

2

Chris Piech, CS109

3 of 98

Who wrote Federalist Paper 53?

3

madison.txt

hamilton.txt

unknown.txt

Chris Piech, CS109

4 of 98

4

First, some review

Chris Piech, CS109

5 of 98

Recall the good times

5

 

 

Chris Piech, CS109

6 of 98

How Many Ways Are There to Order…

6

(H, H, H, H, T, T, T, T, T, T)

(H, H, H, T, H, T, T, T, T, T)

(H, H, H, T, T, H, T, T, T, T)

(H, H, H, T, T, T, H, T, T, T)

(H, H, H, T, T, T, T, H, T, T)

(H, H, H, T, T, T, T, T, H, T)

(H, H, H, T, T, T, T, T, T, H)

(H, H, T, H, H, T, T, T, T, T)

(H, H, T, H, T, H, T, T, T, T)

(H, H, T, H, T, T, H, T, T, T)

(H, H, T, H, T, T, T, H, T, T)

(H, H, T, H, T, T, T, T, H, T)

(H, H, T, H, T, T, T, T, T, H)

(H, H, T, T, H, H, T, T, T, T)

(H, H, T, T, H, T, H, T, T, T)

(H, H, T, T, H, T, T, H, T, T)

(H, H, T, T, H, T, T, T, H, T)

(H, H, T, T, H, T, T, T, T, H)

(H, H, T, T, T, H, H, T, T, T)

(H, H, T, T, T, H, T, H, T, T)

(H, H, T, T, T, H, T, T, H, T)

(H, H, T, T, T, H, T, T, T, H)

(H, H, T, T, T, T, H, H, T, T)

(H, H, T, T, T, T, H, T, H, T)

4 heads and 6 tails?

Chris Piech, CS109

7 of 98

How Many Ways Are There to Order…

7

(H, H, H, H, T, T, T, T, T, T)

(H, H, H, T, H, T, T, T, T, T)

(H, H, H, T, T, H, T, T, T, T)

(H, H, H, T, T, T, H, T, T, T)

(H, H, H, T, T, T, T, H, T, T)

(H, H, H, T, T, T, T, T, H, T)

(H, H, H, T, T, T, T, T, T, H)

(H, H, T, H, H, T, T, T, T, T)

(H, H, T, H, T, H, T, T, T, T)

(H, H, T, H, T, T, H, T, T, T)

(H, H, T, H, T, T, T, H, T, T)

(H, H, T, H, T, T, T, T, H, T)

(H, H, T, H, T, T, T, T, T, H)

(H, H, T, T, H, H, T, T, T, T)

(H, H, T, T, H, T, H, T, T, T)

(H, H, T, T, H, T, T, H, T, T)

(H, H, T, T, H, T, T, T, H, T)

(H, H, T, T, H, T, T, T, T, H)

(H, H, T, T, T, H, H, T, T, T)

(H, H, T, T, T, H, T, H, T, T)

(H, H, T, T, T, H, T, T, H, T)

(H, H, T, T, T, H, T, T, T, H)

(H, H, T, T, T, T, H, H, T, T)

(H, H, T, T, T, T, H, T, H, T)

4 heads and 6 tails?

Chris Piech, CS109

8 of 98

How Many Ways Are There to Order…

8

3 As, 5 Bs and 2Cs

Chris Piech, CS109

9 of 98

How Many Ways Are There to Order…

9

3 As, 5 Bs and 2Cs

Chris Piech, CS109

10 of 98

How Many Ways Are There to Order…

10

3 As, 5 Bs and 2Cs

Chris Piech, CS109

11 of 98

Counting unordered objects

11

Multinomials generalize Binomials for counting.

How many ways are there�to order n heads

and (n-k) tails

Called the binomial coefficient�because of something from Algebra

How many ways are there�to order n1 outcomes of type 1

n2 outcomes of type 2

n3 outcomes of type 3…

nr outcomes of type r

 

 

Binomial coefficient

Multinomial coefficient

Chris Piech, CS109

12 of 98

Where are we in CS109?

12

Chris Piech, CS109

13 of 98

Today: Compare and Contrast Many Examples

13

Age from C14

Updated Delivery Prob

Age from Name

Hidden Chambers

Stanford Eye Test

Updating Lidar Belief

Chris Piech, CS109

14 of 98

Joint Table for 3 Random Variables?

14

D is disease, S is can smell, F is fever status

Chris Piech, CS109

15 of 98

Joint Table for 3 Random Variables?

15

D is disease, S is can smell, F is fever status

Chris Piech, CS109

16 of 98

Joint Table for 10 Random Variables?

16

Imagine you have 10 discrete RVs which can each take on 5 values

# unique assignments

Chris Piech, CS109

17 of 98

Joint Table for 10 Random Variables?

17

Imagine you have 10 discrete RVs which can each take on 5 values

# unique assignments

Chris Piech, CS109

18 of 98

Joint Table for 10 Random Variables?

  • 10 million entries in your joint table.

  • So, we are going to need models …

  • probabilistic models

18

Imagine you have 10 discrete RVs which can each take on 5 values

# unique assignments

Chris Piech, CS109

19 of 98

Joint Table for 10 Random Variables?

  • 10 million entries in your joint table.

  • So, we are going to need models …

  • probabilistic models

19

Imagine you have 10 discrete RVs which can each take on 5 values

# unique assignments

Chris Piech, CS109

20 of 98

Joint Table for 10 Random Variables?

  • 10 million entries in your joint table.

  • So, we are going to need models …

  • probabilistic models

20

Imagine you have 10 discrete RVs which can each take on 5 values

# unique assignments

Chris Piech, CS109

21 of 98

Multiple Random Variables. Start of Digital Revolution

21

Chris Piech, CS109

22 of 98

22

Cousin 1

Cousin 2

?

Parent 1

Parent 2

?

?

Grand Parent 1

?

Grand Parent 2

?

Spouse 1

?

Spouse 2

?

You observe that someone has a recessive gene.

What is the probability that their cousin has the same recessive gene?

23 of 98

Four Prototypical Trajectories

Roll 100 dice.

X1 = How many 1s?

X2 = How many 2s?

X3 = How many 3s?

X4 = How many 4s?

X5 = How many 5s?

X6 = How many 6s?

How big is the joint table?

Chris Piech, CS109

24 of 98

Four Prototypical Trajectories

Sometimes the structure of the variables suggests a more efficient representation

Chris Piech, CS109

25 of 98

Multinomial RV

25

Chris Piech, CS109

26 of 98

Probability

26

Binomial RV

Multinomial RV

Chris Piech, CS109

27 of 98

Probability

27

Binomial RV

 

Multinomial RV

Chris Piech, CS109

28 of 98

Probability

28

Binomial RV

 

 

Multinomial RV

Chris Piech, CS109

29 of 98

Probability

29

Binomial RV

 

Binomial # of ways of ordering the successes

 

Multinomial RV

Chris Piech, CS109

30 of 98

Probability

30

Binomial RV

 

Binomial # of ways of ordering the successes

 

 

Multinomial RV

 

Chris Piech, CS109

31 of 98

Probability

31

Multinomial RVs generalize Binomial RVs

Binomial RV

 

Binomial # of ways of ordering the successes

 

 

Multinomial RV

 

Chris Piech, CS109

32 of 98

Recall the Binomial Derivation: What if more than two outcomes?

32

Chris Piech, CS109

33 of 98

Three Outcomes A, B, C

33

What is the probability of exactly 4 As, 1 B and 5 Cs?

Chris Piech, CS109

34 of 98

Multinomial Random Variable?

  •  

34

Joint PMF

 

where

and

Probability of each ordering is equal + mutually exclusive

Chris Piech, CS109

35 of 98

Multinomial Random Variable?

  •  

35

Joint PMF

 

where

and

Multinomial # of ways of ordering the outcomes

Probability of each ordering is equal + mutually exclusive

Chris Piech, CS109

36 of 98

Four Prototypical Trajectories

Sometimes the structure of the variables suggests a more efficient representation

Chris Piech, CS109

37 of 98

Four Prototypical Trajectories

I roll 6 dice. What is more probable:

  1. I roll 6 “sixes”

B) I roll exactly one of each number

Chris Piech, CS109

38 of 98

Hello dice rolls, my old friends

  • A 6-sided die is rolled 7 times.
  • What is the probability of getting:

38

  • 1 one
  • 1 two
  • 0 threes
  • 2 fours
  • 0 fives
  • 3 sixes

🤔

Chris Piech, CS109

39 of 98

Hello dice rolls, my old friends

  • A 6-sided die is rolled 7 times.
  • What is the probability of getting:

39

  • 1 one
  • 1 two
  • 0 threes
  • 2 fours
  • 0 fives
  • 3 sixes

Chris Piech, CS109

40 of 98

Hello dice rolls, my old friends

  • A 6-sided die is rolled 7 times.
  • What is the probability of getting:

40

  • 1 one
  • 1 two
  • 0 threes
  • 2 fours
  • 0 fives
  • 3 sixes

 

Chris Piech, CS109

41 of 98

Hello dice rolls, my old friends

  • A 6-sided die is rolled 7 times.
  • What is the probability of getting:

41

  • 1 one
  • 1 two
  • 0 threes
  • 2 fours
  • 0 fives
  • 3 sixes

 

 

Chris Piech, CS109

42 of 98

Hello dice rolls, my old friends

  • A 6-sided die is rolled 7 times.
  • What is the probability of getting:

42

  • 1 one
  • 1 two
  • 0 threes
  • 2 fours
  • 0 fives
  • 3 sixes

 

 

# of times�a six appears

Chris Piech, CS109

43 of 98

Hello dice rolls, my old friends

  • A 6-sided die is rolled 7 times.
  • What is the probability of getting:

43

  • 1 one
  • 1 two
  • 0 threes
  • 2 fours
  • 0 fives
  • 3 sixes

 

 

# of times�a six appears

choose where�the sixes appear

Chris Piech, CS109

44 of 98

Hello dice rolls, my old friends

  • A 6-sided die is rolled 7 times.
  • What is the probability of getting:

44

  • 1 one
  • 1 two
  • 0 threes
  • 2 fours
  • 0 fives
  • 3 sixes

 

 

# of times�a six appears

probability�of rolling a six

choose where�the sixes appear

Chris Piech, CS109

45 of 98

Hello dice rolls, my old friends

  • A 6-sided die is rolled 7 times.
  • What is the probability of getting:

45

  • 1 one
  • 1 two
  • 0 threes
  • 2 fours
  • 0 fives
  • 3 sixes

 

 

# of times�a six appears

probability�of rolling a six

choose where�the sixes appear

this many times

Chris Piech, CS109

46 of 98

Parameters of a Multinomial RV?

  •  

46

 

 

 

 

 

Chris Piech, CS109

47 of 98

Most useful when probabilities are not equal

47

Chris Piech, CS109

48 of 98

Most useful when probabilities are not equal

48

You have a funny shaped six-sided die with the following probabilities:

  • P(1) = 0.2, P(2) = 0.3
  • P(3) = 0.1, P(4) = 0.1
  • P(5) = 0.1, P(6) = 0.2

You roll the die 6 times.

What is the probability of getting exactly: two 2s, two 4s, and two 6s?

Chris Piech, CS109

49 of 98

The Federalist Papers

49

Chris Piech, CS109

50 of 98

Four Prototypical Trajectories

Intro to Natural Language Processing

Chris Piech, CS109

51 of 98

51

Dear Professor Piech. Can you help me? I have a large amount of money in my dorm room, but I can’t get it out. If you sent me a $500 coupa card I could distract my TA and pay you back double.

Sam Smith

60%

60%

40%

1

2

Let E be the entire email text. Let F be the event the email is Spam.

3

string_pr(f"This email is spam: {email}")

string_pr(f"This email is spam: ")

email

Chris Piech, CS109

52 of 98

52

string_pr(f"This email is spam: {email}")

string_pr(f"This email is spam: ")

email

spam

"This email is spam: Pay for drugs"

The token at index 1 is “this”

Chris Piech, CS109

53 of 98

53

string_pr(f"This email is spam: {email}")

string_pr(f"This email is spam: ")

email

spam

"This email is spam: Pay for drugs"

The token at index 1 is “this”

Chris Piech, CS109

54 of 98

54

string_pr(f"This email is spam: {email}")

string_pr(f"This email is spam: ")

email

spam

"This email is spam: Pay for drugs"

The token at index 1 is “this”

Chris Piech, CS109

55 of 98

55

string_pr(f"This email is spam: {email}")

string_pr(f"This email is spam: ")

email

spam

"This email is spam: Pay for drugs"

The token at index 1 is “this”

drugs

Chris Piech, CS109

56 of 98

Four Prototypical Trajectories

We are going to contrast that to an older (but cheaper) method…

Chris Piech, CS109

57 of 98

Four Prototypical Trajectories

Before 2021…

Chris Piech, CS109

58 of 98

Probabilistic text analysis

58

Chris Piech, CS109

59 of 98

Probabilistic text analysis

  •  

59

Chris Piech, CS109

60 of 98

Probabilistic text analysis

  •  

60

👈

Chris Piech, CS109

61 of 98

Probabilistic text analysis

  •  

61

👈

A document is a large multinomial.

(according to the Global Language Monitor,�there are 988,968 words in the English language used on the internet.)

Chris Piech, CS109

62 of 98

Model text as a multinomial

62

Chris Piech, CS109

63 of 98

Model text as a multinomial

Example document:

“Pay for drugs with a credit-card. drugs is great. So are credit-cards. Risk free drugs. Click for free.”

n = 18

63

Chris Piech, CS109

64 of 98

Model text as a multinomial

Example document:

“Pay for drugs with a credit-card. drugs is great. So are credit-cards. Risk free drugs. Click for free.”

n = 18

64

drugs = 3

Free = 2

Risk = 1

Credit-card: 2

For = 2

Chris Piech, CS109

65 of 98

Model text as a multinomial

Example document:

“Pay for drugs with a credit-card. drugs is great. So are credit-cards. Risk free drugs. Click for free.”

n = 18

65

drugs = 3

Free = 2

Risk = 1

Credit-card: 2

For = 2

Probability of seeing this document | spam

Chris Piech, CS109

66 of 98

Model text as a multinomial

Example document:

“Pay for drugs with a credit-card. drugs is great. So are credit-cards. Risk free drugs. Click for free.”

n = 18

66

drugs = 3

Free = 2

Risk = 1

Credit-card: 2

For = 2

Probability of seeing this document | spam

It’s a Multinomial!

Chris Piech, CS109

67 of 98

Model text as a multinomial

Example document:

“Pay for drugs with a credit-card. drugs is great. So are credit-cards. Risk free drugs. Click for free.”

n = 18

67

drugs = 3

Free = 2

Risk = 1

Credit-card: 2

For = 2

Probability of seeing this document | spam

It’s a Multinomial!

The probability of a word in spam email being drugs

Chris Piech, CS109

68 of 98

Four Prototypical Trajectories

Who wrote the federalist papers?

69 of 98

70 of 98

Old and New Analysis

70

Chris Piech, CS109

71 of 98

Old and New Analysis

  • Authorship of the Federalist Papers
    • 85 essays advocating ratification�of the US constitution
    • Written under the pseudonym “Publius”�(really, Alexander Hamilton, James Madison, John Jay)

71

Chris Piech, CS109

72 of 98

Old and New Analysis

  • Authorship of the Federalist Papers
    • 85 essays advocating ratification�of the US constitution
    • Written under the pseudonym “Publius”�(really, Alexander Hamilton, James Madison, John Jay)
  • Who wrote which essays?
    • Analyze probability of words in each essay and compare against word distributions from known writings of three authors

72

Chris Piech, CS109

73 of 98

Who wrote Federalist Paper 53?

73

madison.txt

hamilton.txt

unknown.txt

Chris Piech, CS109

74 of 98

Where to start?

  • We have words, we want to know probability of authorship. We also know probability of words given author…

74

Chris Piech, CS109

75 of 98

Where to start?

  • We have words, we want to know probability of authorship. We also know probability of words given author…

75

Well hello again…

Chris Piech, CS109

76 of 98

Who wrote Federalist Paper 53?

76

Prob Hamilton given

Document

Prob Document given Hamilton

Prior belief it was Hamilton

Prob of the document???

Chris Piech, CS109

77 of 98

Who wrote Federalist Paper 53?

77

Model document as a multinomial where we care about count of words

Chris Piech, CS109

78 of 98

Who wrote Federalist Paper 53?

78

Prob hamilton would write word i

Number of times word i is in the doc

Loop over unique words

Chris Piech, CS109

79 of 98

Who wrote Federalist Paper 53?

79

Prob Hamilton given

Document

Prob hamilton would write word i

Prior belief it was Hamilton

Prob of the document???

Number of times word i is in the doc

Loop over unique words

Chris Piech, CS109

80 of 98

Who wrote Federalist Paper 53?

80

Prob that Hamilton wrote it

Prob that Madison wrote it

Chris Piech, CS109

81 of 98

Who wrote Federalist Paper 53?

81

Prob that Hamilton wrote it

Prob that Madison wrote it

Chris Piech, CS109

82 of 98

To the code

82

Chris Piech, CS109

83 of 98

83

What happened?

Chris Piech, CS109

84 of 98

All our probabilities are zero…

84

Chris Piech, CS109

85 of 98

Log Review

85

Chris Piech, CS109

86 of 98

Log Identities

86

Chris Piech, CS109

87 of 98

Products become sums!

87

Chris Piech, CS109

88 of 98

Products become sums!

88

* Spoiler alert: This is important because the product of many small numbers gets hard for computers to represent.

Chris Piech, CS109

89 of 98

Use logs when probabilities become too small!

89

Number of times word i shows up in the doc

Hamilton wrote it

Maddison wrote it

The doc

hi = Prob Hamilton writes word i

mi = Prob Maddison writes word i

Chris Piech, CS109

90 of 98

Use logs when probabilities become too small!

90

Number of times word i shows up in the doc

Hamilton wrote it

Maddison wrote it

The doc

hi = Prob Hamilton writes word i

mi = Prob Maddison writes word i

Chris Piech, CS109

91 of 98

What does it mean if a log value is positive / negative

91

If a log is positive, its input was > 1

If a log is negative, its input was between 0 and 1

Chris Piech, CS109

92 of 98

Use logs when probabilities become too small!

92

Hamilton Term -12925

Madison Term -11581

Chris Piech, CS109

93 of 98

Use logs when probabilities become too small!

93

Hamilton Term -12925

Madison Term -11581

Chris Piech, CS109

94 of 98

Use logs when probabilities become too small!

94

Hamilton Term -12925

Madison Term -11581

Chris Piech, CS109

95 of 98

Four Prototypical Trajectories

Madison wrote it!

Chris Piech, CS109

96 of 98

Four Prototypical Trajectories

Practice: Midterm question from last year

Chris Piech, CS109

97 of 98

Midterm Question

97

Chris Piech, CS109

98 of 98

Midterm Question (Winter 2025)

98

Chris Piech, CS109