1 of 50

Beyond CS109

Chris Gregg

Summer, 2026

CS109, Stanford University

2 of 50

2

Final Exam Logistics

  • 8:30am on Saturday
  • CoDa B80 (normal classroom)
  • 6 pages of handwritten notes
  • Cumulative, but focused on Machine Learning
  • 2 hours, not 3 hours

3 of 50

What have you

learned?

4 of 50

What is a probability?

4

The event we care about

How many times does it occur?

Out of (close to) infinite trials

Lecture 1

Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026

5 of 50

Time to Start Flippin Coins

6 of 50

Bayes’ Theorem

6

 

 

 

Lecture 2

7 of 50

8 of 50

9 of 50

Program the General Version

10 of 50

11 of 50

Learning Goals of Today

11

Mutually Exclusive

Independent

Makes AND easy:

Makes OR easy:

Lecture 3

12 of 50

Sort semi-distinct objects

12

Also:

Lecture 4

Sahami, Piech, Yang, Cain, Woodrow, Gregg

13 of 50

Counting Cards

14 of 50

Declaring a Random Variable to be Binomial

14

Our random variable

Is distributed as a

Binomial

With these parameters

Num trials

Probability of success on each trial

Piech & Cain, CS109, Stanford University

Lecture 5

15 of 50

16 of 50

Expected Value

16

Loop over all values x that X can take on

The value

The probability of that value

Lecture 6

17 of 50

Poisson Random Variable

  • X is a Poisson Random Variable: the number of occurrences in a fixed interval of time.

    • λ is the “rate”
    • X takes on values 0, 1, 2…
    • has distribution (PMF):

17

Lecture 7

18 of 50

19 of 50

19

Truth 2:

Truth 3:

Truths of Probability For Continuous Random Variables

Truth 1:

Truth 4:

Know why!

That’s all possible values (Axiom 2)

Since the integral is a probability (Axiom 1)

Area under the curve!

Lecture 8

20 of 50

21 of 50

22 of 50

23 of 50

Normal Probability Density Function

23

x

f(x)

Lecture 9

24 of 50

Joint table: mutually exclusive and covers sample space.

24

Single

Relationship

Complicated

Frosh

0.07 k

0.04 k

0.01 k

Soph

0.09 k

0.05 k

0.01 k

Junior

0.05 k

0.05 k

0.01 k

Senior

0.01 k

0.03 k

0.01 k

5+

0.03 k

0.03 k

0.02 k

X is dating status.

Y is year.

Each combination is mutually exclusive, and they span the sample space

Lecture 10

Chris Piech, CS109

25 of 50

25

Beer Lambert Law

Rate of muons depends on x, amount of limestone

Lecture 11

Chris Piech, CS109

26 of 50

Inference

26

Age from C14

Updated Delivery Prob

Age from Name

Hidden Chambers

Stanford Eye Test

Updating Lidar Belief

27 of 50

Inference

Inference noun

Updating one’s belief about a random variable (or multiple) based on conditional knowledge regarding another random variable (or multiple) in a probabilistic model.

TLDR: conditional probability with random variables.

28 of 50

28

def update_belief_carbon_dating(m = 900):

# pr_A[i] is P(Age = i| m = 900).

pr_A = {}

for i in range(100,10000+1):

prior = 1 / n_years # P(A = i)

likelihood = calc_likelihood(m, i) #P(M=m|A=i)

pr_A[i] = likelihood * prior

# implicitly computes the normalization constant

normalize(pr_A)

return pr_A

def update_belief_baby(prior, today = 10):

# pr_D[i] is P(D = i| No Baby Yet).

pr_D = {}

for i in range(-50,25):

# P(NoBaby | D = i)

likelihood = 0 if i < today else 1

pr_D[i] = likelihood * prior[i]

# implicitly computes the LOTP

normalize(pr_D)

return pr_D

def update_belief_name_to_age(name = 'Laura'):

# pr_age[i] is P(Age = i| name).

# prob_name_and_age is just a counting from the US

# Social Security database.

pr_age = {}

for i in range(10,110):

pr_age[i] = calc_prob_name_and_age(name, i)

# implicitly computes the normalization constant

normalize(pr_age)

return pr_age

What do you notice is the same. What is different?

Chris Piech, CS109

29 of 50

Four Prototypical Trajectories

Lecture 12

Chris Piech, CS109, 2021

30 of 50

Multinomial Random Variable?

  •  

30

Joint PMF

 

where

and

Multinomial # of ways of ordering the outcomes

Probability of each ordering is equal + mutually exclusive

Lecture 13

Chris Piech, CS109, 2021

31 of 50

Beta is the Random Variable for Probabilities

31

Used to represent a distributed belief of a probability

Lecture 14

Chris Piech, CS109, 2021

32 of 50

Central Limit Theorem

  •  

32

 

Lecture 16

Chris Piech, CS109, 2021

33 of 50

Four Prototypical Trajectories

Bootstraping allows you to:

  • Know the distribution of statistics
  • Calculate p values
  • Using computers
  • You totally could have invented it

Lecture 17

Chris Piech, CS109, 2021

34 of 50

Conditional Expectation

34

X = units in fall quarter

Y = year in school

E[X | Y] ?

Lecture 18

Chris Piech, CS109, 2021

35 of 50

Which Question is Better?

35

Lecture 19

Chris Piech, CS109, 2021

36 of 50

36

We want to choose the parameter value that maximizes the probability of the data:

How to Choose the “Best” Parameters: MLE

Likelihood

To put words into math:

Our best estimate

Log Likelihood

Lecture 20

37 of 50

37

Logistic Regression

+

z = 2.1

σ(z) = 0.7

Lecture 21

38 of 50

Gradient Ascent

Walk uphill and you will find a local maxima

(if your step size is small enough)

argmax

39 of 50

Results

39

overfitting

Model Train Accuracy Test Accuracy

-------------------------------------------------------------

Baseline 0.6031 0.5887

Naive Bayes 0.7909 0.8067

Logistic Regression 0.8169 0.8307

Decision Tree 0.8514 0.8307

Random Forest 0.8726 0.8500

Gradient Boosting 0.8611 0.8440

AdaBoost 0.8334 0.8353

BayesNet 0.8320 0.8507

Lecture 22

40 of 50

40

Calibration

New!

41 of 50

41

We Can Put Neurons Together

+

Single neuron in a hidden layer

Lecture 23

42 of 50

Lets start training a Critter

42

Lecture 25

43 of 50

What should you do next?

44 of 50

Go solve amongst the abundance of important problems

45 of 50

46 of 50

Think about intersectionality

Your side passion

Data that you have access to

Your lived experience

Thompson sampling

47 of 50

CS109

Probability Fundamentals

Single Random Variables

Probabilistic Models

Uncertainty Theory

AI

48 of 50

49 of 50

I had an important job!

I hope you think I did it justice

50 of 50

Thank you so much!