## Marginal Distributions¶

Joint distribution tables partition the entire outcome space according to the values of the pair $(X, Y)$.

In our example, we are tossing a coin three times. $X$ is the number of heads in the first two tosses and $Y$ the number of heads in the last two tosses. Here once again is the joint distribution table for $X$ and $Y$.

joint_dist

X=0 X=1 X=2
Y=2 0.000 0.125 0.125
Y=1 0.125 0.250 0.125
Y=0 0.125 0.125 0.000

The space has been partitioned into 9 pieces, each with its own chance. The total of all the chances is 1 as we saw in the previous section.

### Partitioning $\{X = x \}$ According to $Y$¶

Now look along the column labeled X = 0. In every cell of that column, the value of $X$ is 0 and $Y$ is some element in the range of $Y$. So the column X=0 partitions the event $\{X = 0\}$ according to the value of $Y$, and displays the probability of each piece of the partition.

Indeed for every $x$, $$\{X = x \} = \bigcup_{\text{all } y} \{X = x, Y = y\}$$ and this is a disjoint union. So by the addition rule,

$$P(X = x) = \sum_{\text{all } y} P(X = x, Y = y)$$

That is, $P(X = x)$ is the sum of the probabilities in the column X=x. Because $P(X = x)$ is the generic term in the distribution of $X$, we have learned that we can derive the distribution of $X$ from the joint distribution of $X$ and $Y$.

To find the numerical values of the distribution of $X$, we will use a method called marginal which takes the name X as its argument and operates on a joint distribution object. The reason for using the word "marginal" will become clear as soon as we see the output.

joint_dist.marginal('X')

X=0 X=1 X=2
Y=2 0.000 0.125 0.125
Y=1 0.125 0.250 0.125
Y=0 0.125 0.125 0.000
Sum: Marginal of X 0.250 0.500 0.250

Now at the bottom of the table you have all the column sums, which constitute the distribution of $X$. These sums appear in the margin of the table, and hence the distribution is called marginal.

This is just a new name for the probability distribution of $X$, the number of heads in the first two tosses.

### Both Marginals¶

What you can do for $X$, you can do as well for $Y$ by looking along the rows.

joint_dist.marginal('Y')

X=0 X=1 X=2 Sum: Marginal of Y
Y=2 0.000 0.125 0.125 0.25
Y=1 0.125 0.250 0.125 0.50
Y=0 0.125 0.125 0.000 0.25

$Y$ is also the number of heads in two tosses of a coin (the last two of three tosses), so the probabilities in the margin make sense.

You can also get both marginals at once:

joint_dist.both_marginals()

X=0 X=1 X=2 Sum: Marginal of Y
Y=2 0.000 0.125 0.125 0.25
Y=1 0.125 0.250 0.125 0.50
Y=0 0.125 0.125 0.000 0.25
Sum: Marginal of X 0.250 0.500 0.250 1.00

The bottom right corner cell is the sum of all the probabilities in the table, and also the sum of all the probabilities in each of the margins. Reassuringly, it's 1.

Compare the two marginal distributions. They are the same (possible values and the corresponding probabilities). So $X \stackrel{d}{=} Y$.