Probabilistic Modeling


Probabilistic Modeling

Corey
Happy Wednesday, everyone. Today we’re taking the ideas you’ve learned about probability and stretching them to describe many random variables acting together. That’s the heart of probabilistic modeling: building a useful account of a complicated world from variables that are random together.

Hope
And there’s a practical reason to care. Think about a program written by a beginner: even a few lines of code can reflect a misconception, but it can be surprisingly difficult for a computer to figure out what the student misunderstood.

Corey
Exactly. Code.org had tens of thousands of students and around 37,000 distinct solutions to a short block-based programming task. Straightforward statistics—like counting mistakes alongside features such as loops or if-statements—didn’t get very far, and deep learning wasn’t close to human performance.

Hope
The inspiration came from a different kind of problem: probabilistic models that describe how people produce handwritten characters. The idea was to build a generative model for how beginners write code, and that model did well enough to outperform humans at identifying students’ bugs.

Corey
That’s a great example of why we need more than one or two random variables. A student’s program has many interacting parts. To model something that complex, we first need a clear way to describe the probability of all those variables together.

Hope
That brings us to the central object: the joint distribution. For every possible assignment to all the random variables in a model, the joint distribution tells us the probability of that complete assignment. If you know the joint, you have complete probabilistic information about the model.

Corey
And once we have a joint distribution, we can do inference. Inference means observing some variables and updating our beliefs about others. If an elephant’s weight is observed, for instance, we can update our belief about its gender in a simple model where gender influences the distribution of weight.

Hope
A more nuanced example is visual acuity. If the variable we’re inferring has only two possible values, like a Bernoulli variable, one Bayes-theorem calculation can update our belief. But visual acuity has many possible levels, not just ‘can see’ and ‘cannot see.’ We need to update a probability for every possible level.

Corey
That means applying Bayes’ theorem repeatedly—one calculation for each possible value. In code, we might represent the possible values and their probabilities in a dictionary. A key represents one value of the random variable, and looking it up gives that value’s probability.

Hope
The terms still have their familiar meanings: the prior is our belief before the observation, the likelihood tells us how probable the observation is given a particular state, and the posterior is our updated belief after observing the evidence.

Corey
There’s a useful programming trick here. Instead of calculating each posterior probability by separately calculating the denominator in Bayes’ theorem, calculate all the numerators first and then normalize them so they sum to one.

Hope
Normalization does that missing denominator calculation for us. The sum of all the numerators is the total probability of the evidence, and dividing each numerator by that sum gives the correct posterior. You may see code that does this without explicitly writing out the denominator, but it’s still doing Bayes’ theorem correctly.

Corey
Now let’s scale up. Imagine WebMD’s symptom checker: it needs to keep track of symptoms, diseases that might cause them, and other variables—such as age or being an undergraduate—that could affect disease or symptom probabilities.

Hope
The questions can go in many directions. Given nausea and tiredness, what’s the probability of having the flu? Given a fever and sore throat, what’s the probability of having a cold? And given those same observations, what should we believe about some other variable, such as whether the person is an undergraduate?

Corey
If we had the joint distribution, we could answer any of those inference questions. But storing a joint table gets enormous quickly. With nine binary random variables, there are two to the ninth possible assignments. In general, with n binary variables, there are two to the n assignments.

Hope
One probability in a complete table can be inferred because all the probabilities must sum to one, so you might store one fewer value. But that tiny saving doesn’t solve the real problem: the table still grows exponentially as the number of variables increases.

Corey
So we need a more compact language. One option is a Bayesian network: a graph whose nodes are random variables and whose directed arrows encode hypotheses about how variables influence one another. Then we provide probabilities associated with that graph.

Hope
Let’s use a small WebMD-style example with four binary variables: flu, undergraduate, fever, and tiredness. We might represent flu as causing fever and tiredness, and undergraduate status as influencing tiredness.

Corey
It’s important to distinguish causality from information. If I tell you someone has a fever, that may change your belief that they have the flu. But in this model, the fever is evidence about the flu; it isn’t what caused the flu. The causal arrow points from flu to fever.

Hope
Choosing those arrows is partly an art. You could make reasonable arguments for extra connections—for instance, that being an undergraduate changes exposure to flu, or that fever contributes to tiredness. A model doesn’t need to capture every possible connection; it needs useful broad strokes.

Corey
And adding arrows has a cost. More arrows mean more conditional probabilities to specify and estimate. A node with two binary parents requires probabilities for four possible parent assignments; with three parents it would require eight. Too many arrows can make the model grow exponentially.

Hope
There’s also a trade-off in accuracy. A model with too few arrows may miss important relationships; one with many arrows may be difficult to estimate well. Adding an arrow doesn’t automatically make a model better if the extra probabilities are poorly estimated.

Corey
Here’s how we fill in a Bayesian network. For each variable, specify its probability given every possible assignment to its immediate parents. If a node has no parents, just specify its probability on its own.

Hope
For example, suppose the probability that a person has the flu is 0.1, and the probability that they’re an undergraduate is 0.8. Those nodes have no parents in our graph, so those are their probabilities without conditioning on anything else.

Corey
Fever has flu as a parent, so we specify the probability of fever for both possible flu values. In this example, the probability of fever given no flu is 0.05, while the probability of fever given flu is 0.7.

Hope
Because fever is binary, once we know the probability of fever being one, we know the probability of it being zero: subtract from one. So we only need to specify one probability for each parent assignment.

Corey
Tiredness has two parents, flu and undergraduate status. Since each can be zero or one, there are four possible assignments to its parents, and we need a probability of tiredness for each one. For example, the model might assign an 0.8 probability of tiredness to an undergraduate without the flu.

Hope
That’s a conditional probability table: it tells us the probability of a node’s value for each configuration of its parents. In this four-variable example, the network can be specified with eight numbers: one each for flu and undergraduate, two for fever, and four for tiredness.

Corey
That’s smaller than listing the probabilities of all sixteen complete assignments in a joint table. More generally, the size of the network’s conditional tables depends on how many parents each node has, especially the node with the most parents.

Hope
The compactness comes from assumptions about independence. In this graph, flu and undergraduate status have no connection between them, so the model treats them as independent. The arrows therefore say not only what influences what, but also which relationships we’re leaving out.

Corey
A Bayesian network must be acyclic: you can’t have a chain of arrows that loops back on itself. For example, undergraduate status causing tiredness and tiredness causing undergraduate status would make a cycle, which isn’t allowed in this model.

Hope
Once we’ve specified the network, we can calculate the probability of a complete assignment by multiplying the probability of each variable given its parents. That factorization is the key assumption that turns the graph and its conditional tables into a joint distribution.

Corey
Think of the generative story behind that calculation. First, sample the variables with no parents, such as flu and undergraduate status. Then sample fever using the value of flu, and sample tiredness using the values of flu and undergraduate status.

Hope
Each run produces a whole pretend person, with a value for every variable. Some generated people will have a fever and some won’t; some will have the flu and some won’t. Repeating the process gives samples from the joint distribution represented by the Bayesian network.

Corey
That’s different from inference. Sampling generates a complete assignment without starting from observations. Inference starts with evidence—say, that someone is an undergraduate and has a fever—and asks what that evidence tells us about the flu.

Hope
Where do the numbers in the conditional tables come from? They might come from an expert’s reasonable estimates, or they might be learned from data. For instance, records of people visiting a clinic could help estimate how often undergraduates report tiredness in each relevant situation.

Corey
The inference payoff is that a joint distribution lets us answer any conditional probability question. Start with the definition of conditional probability: the probability of the target and the evidence together, divided by the probability of the evidence.

Hope
The numerator is a joint assignment, so our network can calculate it. The denominator may leave out a variable we haven’t specified—for example, flu. To calculate it, consider both cases, flu equals one and flu equals zero, and add their probabilities.

Corey
That adding-up process is called marginalization. To find the probability of some variables without specifying the others, sum the joint probabilities over all assignments to the variables you’re leaving out.

Hope
So the full recipe is: represent the model with a Bayesian network, use its factorization to calculate joint probabilities, then use marginalization and conditional probability to answer inference questions. If you can compute the joint, you can work out any such probability question.

Corey
Let’s connect that to independence. For two events, independence means the probability that both happen equals the probability of one times the probability of the other. The same idea applies to random variables, but the equation must hold for every possible pair of values.

Hope
That makes independence a strong claim: knowing the value of one variable tells you nothing about the other. If there’s even one assignment where the equation fails, the variables aren’t independent.

Corey
Consider two dice. Let D1 be the first die and S be the sum. The event D1 equals one has probability one-sixth, and the event S equals seven also has probability one-sixth. The probability that both happen is one-thirty-sixth, so those two particular events are independent.

Hope
But that doesn’t mean the random variables D1 and S are independent. If D1 equals one and S equals five, the joint probability is not the product of those two event probabilities. Independence of the variables requires the equation to work for every pair of values, not just one.

Corey
How might we look for relationships when we have data rather than a model already in hand? One first step is to examine how variables vary together. Imagine a dataset where people rate how much they like many music genres.

Hope
A scatter plot can give us a first impression. If knowing a value of X helps us predict Y, the variables seem related. If the points form a diffuse cloud and knowing X doesn’t help much with Y, they may look more independent.

Corey
A numerical measure of how two variables vary together is covariance. For a particular pair of values, look at how far X is from its expectation and how far Y is from its expectation, then multiply those deviations.

Hope
If both values are above their means, or both are below their means, the product is positive. If one is above its mean and the other is below, the product is negative. Covariance averages those products across the joint distribution.

Corey
One equivalent way to calculate it is the expectation of the product of X and Y, minus the product of their expectations. In a scatter plot with positive and negative contributions that tend to cancel, covariance can be close to zero.

Hope
In the music example, liking punk and liking rock had high covariance, as did liking metal and rock. Liking metal or hard rock had negative covariance with liking pop. That tells us which preferences tend to move together, though it doesn’t by itself tell us a complete causal story.

Corey
Covariance can be a starting point for designing a model from data: examine which variables co-vary, look for plausible structure, and then build a Bayesian network to express a hypothesis about how the world works. That design takes judgment, but it gives us a model a computer can use.

Hope
And that’s the bigger lesson: a joint distribution gives us the complete probabilistic picture, Bayesian networks offer a more compact way to represent it, and inference lets us answer questions once we observe evidence. Understanding how variables relate is what makes it possible to model complicated situations—from symptoms to student code.

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