Probability for Computer Scientists


Probability for Computer Scientists

Anna
Welcome to CS109, a course about probability for computer scientists. Before we get into the math, let’s set the scene: the instructor, Chris, teaches in both computer science and education, and his research includes using AI for social good, especially in education and some medical work.

Michael
That combination fits the course’s big idea: probability is not just abstract math. It helps us understand how computer systems learn, how algorithms work, and how we can reason about uncertain situations in the world.

Anna
The main course resource is the website, cs109.stanford.edu. It has the syllabus, course structure, teaching team information, and practical details. Students can also use the course reader for written explanations, and there’s an ACE companion course for students who want extra examples and more units.

Michael
What background should students have? CS106B is the important programming prerequisite. The course assumes students can code and are familiar with ideas such as recursion, hash tables, and binary trees. Python will be used to build things with the math; students who are new to Python can use the review sessions to catch up.

Anna
CS103 is a less essential prerequisite. The course uses a little material from it, mainly early on, and some familiarity with sets and notation is helpful. The math background matters more: students are expected to have seen material such as partial derivatives, and multivariate integration will be taught in CS109 if they have not seen it before. If you’re unsure about your preparation, the teaching team is open to discussing it.

Michael
The course is five units for undergraduates, with a workload of roughly fifteen hours a week using Stanford’s estimate of about three hours per unit. A large share of that time is independent work on assignments, alongside lectures and sections. Graduate students can choose fewer units, but that does not reduce the workload.

Anna
Assignments are a major part of the course: students will make things, derive results, and practice probability. There is also a midterm and a final. The final date is difficult to change, with limited university exceptions, while the midterm has more flexibility if students contact the course team near the date. If another class has an exam at the same time, it’s best to raise the conflict early.

Michael
Lectures are recorded and are expected to be released about twenty-four hours later, which can help if someone is sick or needs to revisit a concept. But the instructor encouraged students to attend when possible: coming to lecture is associated with better grades, though that observation does not establish that lecture attendance causes the difference. The course also planned a small participation incentive, with details to come later.

Anna
Sections give students a chance to work through concepts with teaching assistants before tackling problem sets on their own. Working office hours are another support: students can bring their problems, work there, and ask questions as they arise. They were scheduled to begin on Thursday, and students can also use the Ed Q&A forum or contact the course team by email. Private concerns can go directly to the instructor or head TA.

Michael
Now for the intellectual motivation. Early AI researchers sometimes expected human-level intelligence to be easy to achieve by writing down enough rules. In the 1950s, Herbert Simon predicted that machines would be able to do any work a person could do within twenty years. But the world turned out to be far more complex than those expectations allowed.

Anna
Machine translation made that difficulty vivid. Simon’s group tried to translate between English and Russian by encoding rules, but language is full of complexity and ambiguity. A sentence could come back with a very different meaning—one famous example turned ‘the spirit is willing, but the flesh is weak’ into something like ‘the vodka is good, but the meat is rotten.’

Michael
That kind of disappointment contributed to an AI winter, when funding for AI research dried up. Later, a series of milestones showed that the field was changing: computers became strong at chess, a Stanford self-driving car project helped demonstrate autonomous driving, and a computer system won at Jeopardy. Speech recognition and translation also improved dramatically.

Anna
AI’s impact extends beyond games and familiar demonstrations. Protein folding is the problem of predicting a protein’s three-dimensional structure from its amino-acid sequence. Better predictions can support drug development and understanding the body; the lecture noted that researchers had produced structures for every known sequence in a major database.

Michael
And AI also appears in everyday tools, such as route planning and recommendation systems. Recent image-generation systems offered another striking example: given text, a model can represent a probability distribution over images related to that text, then sample images from it. The examples made the point that these systems generate images rather than simply retrieving a matching photograph.

Anna
So how do we begin to explain what’s under the hood? Start with classification: given an input, assign it to a category. A computer might classify a picture as a Chihuahua or a muffin, or recognize a handwritten digit as a zero or a one. The task sounds easy to people, but an algorithm begins with pixel values, not with our rich experience of seeing an object.

Michael
That difference matters. Human vision depends on extensive processing; the lecture pointed out that a substantial part of the brain is devoted to seeing. What feels immediate to us can require a complicated computation, so writing a list of rules for every possible Chihuahua, muffin, zero, or one is not a realistic solution.

Anna
One alternative is to build artificial neurons as simple probability machines. In a cartoon model, a neuron receives inputs, gives those inputs different weights, adds the weighted values, and passes the result through a squashing function. We can interpret its output as a probability of firing—or not firing.

Michael
A single artificial neuron is not the whole story. The important step is combining many of these units into a neural network. Networks can have input layers, hidden layers, and an output layer; for image recognition, the inputs can be pixel values and the final output can represent probabilities for possible classifications.

Anna
But where does a network’s intelligence come from? Not from the mere fact that we put the units together. It depends on the weights. Well-chosen weights can give meaningful probability estimates, while random weights generally will not. The challenge is to find good weights without having someone manually adjust every one.

Michael
That leads to the second idea: learning from examples. Give a network an example, let it make a prediction, and compare that prediction with the correct answer. If the network says a digit is a one when the example is actually a zero, we measure how wrong it was with a loss.

Anna
Then the learning algorithm adjusts the weights to reduce that loss. Because the network makes probability-based claims, we can express how its predictions should change mathematically. The course will build up to understanding derivatives of loss with respect to weights and using them to find changes that improve predictions.

Michael
Those formulas may look intimidating at first, but the promise of CS109 is that students will develop the probability and mathematical tools to understand them, derive them, and use them. The goal is not just to recognize a formula; it’s to understand what it means and how it supports learning.

Anna
The applications are broad. The course points to problems such as detecting heart disease, predicting which movies people like, and analyzing DNA. One Stanford example used machine-learning models to help distinguish possible skin cancer from non-cancer, showing how tools like these can be applied to consequential problems.

Michael
It’s also important to be realistic about what AI still struggles with. In one-shot learning, a person sees one example of a symbol and can often recognize another instance of it. Many machine-learning systems, by contrast, need millions of examples. That gap reminds us that AI progress does not mean every hard problem is solved.

Anna
Probability is useful beyond machine learning, too. It supports the analysis of algorithms, including reasoning about how an algorithm works and how to make it faster. It also helps with conditional-probability problems such as recommendation systems and with making decisions when outcomes are uncertain.

Michael
There’s a human and ethical side as well. We need people who can build useful technology, but we also need people who understand it well enough to notice how it might be misused. That includes thinking about fairness in AI and how ethical principles intersect with law.

Anna
The lecture gave a real-world example of probability in action: designing a better eye exam. A traditional exam asks a series of fixed questions about which letters someone can see. A probabilistic approach can maintain a distribution over how well the person sees, use the answers so far to decide what to show next, and adapt the exam as evidence arrives.

Michael
In the results described in the lecture, the adaptive exam was more accurate and could be much shorter than the comparison exams. The project began as a CS109 final-exam problem and later became research published in major scientific venues. That’s a powerful example of how course ideas can grow into work with real-world use.

Anna
A classic probability example shows why intuition alone can mislead us. Suppose a person tests positive for Zika. The test correctly gives a positive result for people who have Zika ninety percent of the time, but it also gives a positive result for people without Zika seven percent of the time. Since the disease is rare, the probability that a person actually has it after a positive result can still be low; the lecture’s example gives about nine percent.

Michael
The key is to distinguish the test result from the underlying condition. A positive test is evidence, not certainty, and the rarity of the condition matters when interpreting that evidence. Probability gives us a systematic way to update our beliefs as new information arrives—for example, if the person feels better or worse the next day.

Anna
That’s why the course starts with fundamentals before moving deeper into AI and decision-making. Probability is a way to understand uncertainty, and a strong foundation lets us tackle more complicated models later. The course also emphasizes applications, because creating things with mathematics can make the ideas more concrete.

Michael
And the first foundation is counting. Counting is useful for probability because it helps us reason about possible outcomes. It’s also useful in computer science when we need to understand how much work an algorithm may do. We’ll begin with experiments, outcomes, and events.

Anna
An experiment is something we do that can produce outcomes. Roll one die, and the possible outcomes are one through six. If the event we care about is rolling an even number, three outcomes satisfy it: two, four, and six. So counting an event means counting how many outcomes satisfy its description.

Michael
For two dice, there are thirty-six possible ordered outcomes: six possibilities for the first die and six for the second. Rather than listing them all, we can use the first of two basic counting rules: the step rule. If an experiment can be broken into steps, and the number of choices at one step does not depend on what happened at another, we multiply the numbers of choices.

Anna
The lecture’s image example makes that rule vivid. A computer color can be represented using red, green, and blue channels, each with 256 possible values. Choosing a color is three steps, so there are 256 times 256 times 256 possible colors—about 17 million—for one pixel.

Michael
If an image has twelve pixels and each pixel can independently take any of those colors, there are about 17 million choices for each of twelve steps. The total is therefore 17 million to the power of twelve, roughly 5.8 times ten to the power of seventy-seven. That’s close to the estimated number of atoms in the observable universe, around ten to the power of eighty.

Anna
And a larger image has a staggering number of possibilities. The exact count is 17 million to the power of the number of pixels. The point is not just to marvel at a huge number: counting helps explain why searching all possibilities can be computationally difficult.

Michael
The 256 possible values in a color channel also follow from the step rule. Each channel uses eight bits, and each bit can be zero or one. That gives two choices at each of eight steps, or two to the power of eight, which is 256.

Anna
The second basic counting rule is the sum rule. When outcomes can come from set A or set B, and the sets have no outcomes in common, we add their sizes. For example, if there are two toys in one category and three in another, with no toy belonging to both, there are five toys total.

Michael
The ‘no overlap’ condition is essential. Consider six-bit strings that are valid if they start with zero-one or end with one-zero. There are sixteen strings that start with zero-one: those first two bits are fixed, and the remaining four positions each have two choices. There are also sixteen that end with one-zero.

Anna
It’s tempting to add sixteen and sixteen and announce thirty-two, but some strings satisfy both conditions. Those strings have been counted twice. The two groups overlap in four strings, so the correct total is sixteen plus sixteen minus four, or twenty-eight.

Michael
That gives us the general rule called inclusion-exclusion. To count outcomes that are in A or B, add the number in A to the number in B, then subtract the number in both. For two sets, that is the size of A plus the size of B minus the size of their intersection.

Anna
So our two core ideas are: count across independent steps by multiplying, and count across non-overlapping alternatives by adding. If alternatives overlap, inclusion-exclusion corrects the double count. These simple rules are a foundation for the wider study of combinatorics and probability.

Michael
One final challenge is counting distinct orderings. If four letters are all different, choose the first in four ways, the second in three, the third in two, and the last in one. The total is four times three times two times one, or twenty-four.

Anna
Notice the subtlety: the number of available choices changes after each pick, but it changes in a predictable way. No matter which letter you choose first, three letters remain; no matter which two you choose, two remain. That lets us multiply the number of choices at each step.

Michael
The word B-O-B-A is trickier because the B appears twice. If we treat the two Bs as different, we count arrangements that look identical as if they were distinct. The lecture leaves that as a challenge: use the counting tools to find the number of unique orderings, and then try a harder word such as Mississippi.

Anna
That’s a good place to pause: CS109 will move from counting into probability, uncertainty, and applications such as machine learning. The central message is that these ideas are both mathematically powerful and practically useful—and the first step is to make the counting foundation rock solid.

Ask anything
We've read this whole study set, so questions about it get the best answers.