Ambiguity and uncertainty abound in natural language. In this lesson, we'll learn the basics of probability which will help us to quantify that uncertainty.
After completing this lesson, you'll be able to ...
Probability is a measure of how likely or unlikely something is to happen.
It is a way of quantifying uncertainty about an event by assigning it a value ranging from 0 to 1.
If an outcome is impossible, its probability is 0.
If an outcome is certain, its probability is 1.
The closer a probability is to 1, the more likely it is to happen.
We measure probability by counting and normalizing (dividing by some total) to ensure that we get a value between 0 and 1.
Probabilities can describe continous and discrete events. Continuous events are those that can take an infinite number of values (ex. formant values for various speech signals, heights of people at various ages, etc.). We'll focus on discrete events here, as it much of what we'll discuss in this course and later ones falls into this category (ex. classification tasks, text generation, etc.).
Discrete events are those with a countable number of possible outcomes. As an example, imagine a six-sided "dice"3 where each side represents one of six possible outcomes of a roll (1,2,3,4,5,6):
🎲However many times we roll the "dice", each result will match one of those six possible outcomes (i.e., we'll never roll a 2.743).
To calculate the probability of rolling a 3 with a fair six-sided "dice", we simply count the number of faces that show a 3 and divide by the total number of faces:
More generally, ...
In the example above, refers to a discrete random variable. As we saw, discrete simply means a countable number of possible values (outcomes). A random variable just means that the sum of probabilities over all possible outcomes (values) of the variable sum to 1 to form a probability distribution.
is known as a marginal (simple) probability.
If we didn't know the "dice" was fair, we could figure this out by rolling many many times:
If we could roll the "dice" an infinite number of times, we could calculate the true probability of each possible outcome.
To use terminology from probability theory, each roll is a trial / experiment /event. Each event has a particular outcome.
At the end of a series of trials, we are left with a distribution of possible outcomes. In the case of a fair six-sided "dice", we have a uniform distribution: each outcome is equally likely or equiprobable.
Though it isn't uniform, the frequency of characters and token types also form a distribution:
What kinds of words occur most frequently? What is their grammatical category? Do they form a natural class?
The Zipfian distribution has been demonstrated to hold for many human languages. What does this tell us about the nature of human language?
What about cases where we're interested in the union of two or more possible events ?
If the two events are disjoint (no overlap), then is simply .
If the two events are mutually exclusive (i.e., they both can't happen at the same time) or disjoint (their intersection is empty), this turns out to just be a matter of addition:
We're not limited to pairs of events.
For example, let's say we want to calculate the probability of rolling an even number. There are three outcomes that satisfy this condition:
Note that will never be smaller than or .
If the events have some overlap, we need to avoid counting the overlap twice, so we need to subtract once:
will work in all cases. What will be if and are disjoint?
We've looked at single outcomes and disjunctions of outcomes. What about the probability of some outcome not happening?
In other words, in an event with discrete outcomes (ex. rolling a "dice"), the probability of something happening is 1 (i.e., the sum of all possible outcomes2). The chance of a particular outcome not occuring is then the sum of the probability of every other event.
Let's apply this to an example.
For a fair six-sided "dice", what is the probability of not rolling a 6?
Let's try combining this rule with one we saw previously...
For a fair six-sided "dice", what is the probability of not rolling a 2 or 4 ()?
Joint probability measures the likelihood of two events occurring simultaneously.
The joint probability of event and event is commonly represented using one of the following formats:
It's the fraction of outcomes where and are both true:
If two events are independent, that means that the two events do not influence one another.
The joint probability of two independent events is defined as the product of their individual probabilities:
Let's see why.
For a fair six-sided "dice", what is the probability of rolling a 2 and then rolling a 5?
While we're interested in one particular outcome of two rolls, it's important to remember that each roll is independent: the outcome of one roll does not depend in any way on another.
There are 36 possible outcomes of two rolls:
from itertools import product
# 1, 2, ..., 6
roll_outcomes = range(1,7)
# i.e., $6^{2}$ possible outcomes
print(sum(1 for outcome in product(roll_outcomes, repeat=2)))Only one of them shows a 2 followed by a 5:
from itertools import product
# 1, 2, ..., 6
roll_outcomes = range(1,7)
# i.e., 6^{2}
for outcome in product(roll_outcomes, repeat=2):
if outcome == (2, 5):
print(outcome)When asking for the probability of such an event, we're simply asking for the portion of outcomes that match our criteria (i.e., 1 out of 36):
To solve this another way, let's use the definition of joint probabilities:
Ok, let's try applying this to measure the probability of more than one possible outcomes.
What is the probability of (rolling a 2 and then a 5) OR (rolling a 5 and then a 2)?
We know from the last example that there are 36 possible outcomes for two rolls. We're interested in 2 of those 36 possibilities:
from itertools import product
# 1, 2, ..., 6
roll_outcomes = range(1,7)
# the subset of the $6^{2}$ outcomes that match our criteria
print(
sum(
1 for outcome in product(roll_outcomes, repeat=2) \
if outcome == (2, 5) or outcome == (5, 2)
)
)Let's apply our definition of joint probabilities:
Now let's combine those (i.e., ):
Related to our last example is the idea of independence.
If , and are completely independent. In other words, if the occurence of one event, , has no effect on the likelihood of another event, , then the two events are independent.
Examples include rolls of a "dice" and coin flips. Now that you know how to caculate marginal and joint probabilities, you can test independence on less obvious cases.
We talked about independent events, but what about cases where one outcome affects another?
: What is the probability of given ?
In other words, assuming has occurred, what is the probability of then occurring?
Note that this is a question about the portion of outcomes (rather than all outcomes) that coincided with .
For example, if a roll of the "dice" results in a 2, what is the probability that the sum after a second roll will be less than 4?
How do we go about caculating ? If we're feeling lazy, we could write something like the following:
from itertools import product
# 1, 2, ..., 6
roll_outcomes = range(1,7)
# the subset of the $6^{2}$ outcomes that match our criteria
valid_outcomes = lambda: (outcome for outcome in product(roll_outcomes, repeat=2) \
# criteria for A
if outcome[0] == 2 \
# criteria for B
and sum(outcome) < 4
)
vo = list(valid_outcomes())
print(f"valid outcomes:\t{vo}")
print(f"num. valid outcomes:\t{len(vo)}")Of the 36 possible outcomes of two rolls of a six-sided "dice" (), the only time the sum of two rolls will be less than 4 when the first roll is 2 is the ordered pair (2, 1). is thus .
We also know .
That's all we need to determine :
This hopefully makes intuitive sense. There are only six possible options for the second roll. Only one of those six meets our criteria (). We shouldn't treat this as a case out of 36, because the outcome of the first roll has constrained the space of outcomes we need to consider.
What if we wanted to calculate ?
from itertools import product
# 1, 2, ..., 6
roll_outcomes = range(1,7)
# the subset of the 6^{2} outcomes that match our criteria
valid_outcomes = lambda: (outcome for outcome in product(roll_outcomes, repeat=2) \
# criteria for A
if outcome[0] == 2 \
# criteria for B
and sum(outcome) >= 4
)
vo = list(valid_outcomes())
print(f"valid outcomes:\t{vo}")
print(f"num. valid outcomes:\t{len(vo)}")
Another way of saying is . Using this reformulation, we can apply our rule for here:
We get the same answer. Coincidence? Unlikely! :)
There a few other terms that can help us to make sense of conditional probabilities. In the context of , represents the likelihood before we receive some important new information. For this reason, it's referred to as the prior (prior = before). In the same vein, is known as the posterior as it represents our updated estimate of the likelihood after incorporating some important new information.
You now know the basics of estimating probabilities. In the next lesson, we'll apply this information to estimate the probability of sequences. Before moving on, though, let's practice ...
Using a single fair "dice", you've rolled 6 three times in a row. What is the probability of rolling a 6 on the next roll?
Using a fair "dice", what is the probability of rolling a 3 four times in a row?
Using a fair "dice", what is the probability of not rolling a 3 within six rolls?
Is ever smaller than ? Why or why not?
Determine whether the following describe dependent or independent events:
Give an example of a pair of words with strong or weak dependence
Give an example of a sequence of words that would have a probability of 0.