CS109: Probability for Computer Scientists
1
Chris Gregg
Summer 2026
We are in a paradigm-changing world…
2
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
CS109 is a stepping stone to understanding this new world
Probability is the core concept in machine learning and artificial intelligence algorithms!
This class is an applied mathematics course on probability
You will be seeing and writing code (in Python)
We will build on many concepts that lead to machine learning
Today:
We will discuss course logistics
We will start to talk about sample spaces, event spaces, and the meaning of a probability.
3
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Who is Chris Gregg
4
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Who is Chris Gregg
5
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Chris and Probability
6
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Why is CS109 important?
We are seeing a huge surge in statistics, predictions, and probabilistic models shared through global news, governing bodies, and social media.
The technological and social innovation we develop during this time will strongly influence how we solve interesting problems impacting the lives of countless people across the globe.
7
National Weather
Service Alerts
https://www.weather.gov/
World Politics
https://abcnews.go.com/538
https://www.nytimes.com/
https://www.economist.com/
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Course Logistics
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Course Staff
TA: Justin Choo (oliver88@stanford.edu)
9
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Course Mechanics (light version: see the syllabus for details)
10
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Course Components
Lecture: MTuWTh 10:30am-11:45am, CoDa B80
11
10% | 7 Assignments (online, web app) |
30% | 2 Midterms (in class, on paper)�Monday July 13th Monday August 3rd |
35% | Final (in person, on paper) 3 hour exam, Saturday August 15th, 8:30am-11:30am There will not be alternate exam times – do not take another course with an overlapping final exam! |
25% | Participation / In-class work |
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Course Components
Written portion:
for typesetting, Tutorial on CS109 website
Coding portion in Python:
Review Session TBD
Late Policy:
All students given 2-hour grace period (but 2-hours and 1 second is actually late).
On the pset app, you can grant yourself up to two late days per assignment, up to five total. After that, you must contact the head CA
12
20% | 7 Assignments (online, web app) |
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Course Components
We will have more information about both exams as they near.
If you have an OAE accommodation, please fill out the following form and upload your letter: https://forms.gle/49uNkj5c2EG8TjVi9
13
30% | 2 Midterms (in class, on paper)�Monday July 13th Monday August 3rd |
35% | Final (in person, on paper) 3 hour exam, Saturday August 15th, 8:30am-11:30am There will not be alternate exam times – do not take another course with an overlapping final exam! |
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Course Components
We will not have regular sections for in-person students. We will have a section for CGOE students.
Class time will be spent on a short recap of the lecture slides, and then we will spend the bulk of the time working on practice problems covering the material.
14
25% | Participation / In-class work |
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
CS109 Contest
15
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
If you have questions:
16
Q&A forum
All announcements
“Working” office hours
start on Wednesday
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Course Reader: https://probabilitycoders.stanford.edu/sum26
17
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
How this class will work:
18
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
The Story of Modern AI
19
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Modern AI
or, How we learned to combine
probability and programming
20
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Brief History
21
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Early Optimism in the 1950s
22
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Early Optimism in the 1950s
23
“Machines will be capable, within twenty years, of doing any work a man can do.”
–Herbert Simon, 1952
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Underwhelming Results 1950s to 1980s
24
The world is too complex
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
25
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
The (Second!) AI Winter Started to Thaw in the late 1990s
26
1997: Deep Blue
2005: Stanley
2011: Watson
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Progression was Incredible!
Speech Recognition is Almost Perfect Today!
27
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
The last remaining board game
Humans conquered by AlphaGo in 2015
28
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Translation is AI-driven (and amazing)
29
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Self-Driving Cars (right outside in Palo Alto!)
30
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
In 2022, everything changed, again:
AI that seems to understand language
31
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
We Now Live in a World of AI
32
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
There are problems that seemed virtually impossible for a computer a short time ago
33
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
The Chihuahua or Muffin Challenge
34
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
The Chihuahua or Muffin Challenge
35
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
The Chihuahua or Muffin Challenge
36
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
This is what a human sees: This is what a computer sees:
37
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Why is it easy for humans?
38
About 30% of your cortex is used from vision
3% is used to process hearing
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Let’s start a bit easier…digit recognition:
39
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Classification: Make a Harry Potter Sorting Hat
40
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Classification: Make a Harry Potter Sorting Hat
41
That is an image of a one
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Classification: Make a Harry Potter Sorting Hat
42
That is an image of a zero
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Classification: Make a Harry Potter Sorting Hat
43
That is an image of a zero
* Sometimes it will be wrong!
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Classification is Hard to Program
44
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
In this class, we will learn the math that underpins classification
45
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
So all of probability is to inform machine learning?
46
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
So all of probability is to inform machine learning?
No.
47
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Probability is more than just machine learning!
48
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Algorithms and Probability
E.g., Raytracing E.g., HashMaps
49
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Understanding the world and building tools
E.g., CT Scanner: uses probability to determine if body material is bone, muscle, or fat.
50
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Philosophy and Ethics
51
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Most Desired Skill by PhD Students
Most CS PhD students list their highest desiderata upon
graduation as:
“Better understanding of probability”
52
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Learning Real Skills in CS109
53
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Learning Real Skills in CS109
Learning Real Skills in CS109
Recent exam question:
Patient sees a series of letters of different font size, and for each, answers correct or incorrect
54
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Learning Real Skills in CS109
Recent exam question (part 2):
A patient has answered 20 “letter sizes” and got a few
correct. What is your belief in how well they can see?
55
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Learning Real Skills in CS109
Now state of the art for eye exam theory!
56
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Learning Real Skills in CS109
57
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
CS109 Provides a Foundation for Your Future
58
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
CS109 Provides a Foundation for Your Future
But, it is not always intuitive
59
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Unintuitive Probability
60
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Unintuitive Probability
61
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Unintuitive Probability
62
The right answer is 9%
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
CS109 View of Probability:
Teach you how to write programs to numerically solve probability problems
63
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
CS109 View of Probability:
Teach you the theory you need to do the math
64
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
CS109 Major Topics
65
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Let’s Dive in to Some Probability!
66
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Events and the Sample Space
67
Note: When Ross uses: ⊂, he really means: ⊆
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Events and the Sample Space
68
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
What is a probability?
69
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
A probability is a number between 0 and 1
70
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Our belief that an event E might occur
71
P(E)
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
What is a probability?
72
The event we care about
How many times does it occur?
Out of (close to) infinite trials
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
What is a probability?
73
n is the number of trials
The “event” E is that you hit the target
Hit: 0
Thrown: 0
P(E)≈
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
What is a probability?
74
n is the number of trials
The “event” E is that you hit the target
Hit: 0
Thrown: 1
P(E)≈ 0.00
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
What is a probability?
75
n is the number of trials
The “event” E is that you hit the target
Hit: 1
Thrown: 2
P(E)≈ 0.50
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
What is a probability?
76
n is the number of trials
The “event” E is that you hit the target
Hit: 11
Thrown: 24
P(E)≈ 0.46
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
77
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
What is a probability (in a Dataset)?
78
Trial | Value |
1 | Rainy |
2 | Sunny |
3 | Rainy |
4 | Cloudy |
5 | Rainy |
6 | Sunny |
7 | Sunny |
8 | Sunny |
… | |
10000 | Cloudy |
Let E be the event that it is Sunny
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
What is a probability (in a Dataset)?
79
Trial | Value |
1 | Rainy |
2 | Sunny |
3 | Rainy |
4 | Cloudy |
5 | Rainy |
6 | Sunny |
7 | Sunny |
8 | Sunny |
… | |
10000 | Cloudy |
Let E be the event that it is Sunny
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Equally Likely Outcomes
80
Some sample spaces have equally likely outcomes.
If we have equally likely outcomes, then P(Each outcome)
Therefore
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Not Everything is Equally Likely
81
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Not Everything is Equally Likely
82
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Not Everything is Equally Likely
83
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Solve probabilities using math!
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
The Axioms of Probability
(Kolmogorov, 1933)
86
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Axioms of Proability
87
Recall: S = all possible outcomes. E = the event.
P(E ∪ F) = P(E) + P(F)
Kolmogorov
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Mutually Exclusive Events
88
If events are mutually exclusive, probability of OR is simple:
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Mutually Exclusive Events
89
If events are mutually exclusive, probability of OR is simple:
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
If events are mutually exclusive, the probability of OR is easy!
90
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
91
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Many times it is easier to calculate P(EC)
Provable Identity #1:
P(EC) = 1 - P(E)
92
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sources of Probability:
A dice story
93
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Die = 7?
Roll two 6-sided dice. What is the probability that the sum = 7?
Let E be the event that the sum is 7
94
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Die = 7?
Roll two 6-sided dice. What is the probability that the sum = 7?
Let E be the event that the sum is 7
95
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Die = 7?
96
cgregg % python3 dice_soln.py
After 10000000 trials
P(E) = 0.1666913
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Die = 2?
Roll two 6-sided dice. What is the probability that the sum = 2?
Let E be the event that the sum is 2
97
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Die = 2?
Roll two 6-sided dice. What is the probability that the sum = 2?
Let E be the event that the sum is 2
98
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Other ways to make a Sample Space?
99
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Dice: Three options for the sample space
100
Value of a die
Value of a die
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Dice: Bug: Die are Indistinct
101
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Dice: Three options for the sample space
102
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Dice: Bug: Just look at the sum
103
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Sum of Two Dice: Three options for the sample space
104
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026
Next time:
Conditional Probability
and Bayes’ Theorem
105
Mehran Sahami, Chris Piech, Lisa Yan, Jerry Cain, Juliette Woodrow, and Chris Gregg, Spring 2026