Chapter 11. Comparison
This chapter introduces joint distributions, which are an essential tool for working with distributions of more than one variable.
We’ll use them to solve a silly problem on our way to solving a real problem. The silly problem is figuring out how tall two people are, given only that one is taller than the other. The real problem is rating chess players (or participants in other kinds of competition) based on the outcome of a game.
To construct joint distributions and compute likelihoods for these problems, we will use outer products and similar operations. And that’s where we’ll start.
Outer Operations
Many useful operations can be expressed as the “outer product” of two
sequences, or another kind of “outer” operation. Suppose you have
sequences like x
and y
:
x
=
[
1
,
3
,
5
]
y
=
[
2
,
4
]
The outer product of these sequences is an array that contains the product of every pair of values, one from each sequence. There are several ways to compute outer products, but the one I think is the most versatile is a “mesh grid”.
NumPy provides a function called meshgrid
that computes a mesh grid.
If we give it two sequences, it returns two arrays:
import
numpy
as
np
X
,
Y
=
np
.
meshgrid
(
x
,
y
)
The first array contains copies of x
arranged in rows, where the
number of rows is the length of y
:
X
array([[1, 3, 5], [1, 3, 5]])
The second array contains copies of y
arranged in columns, where the
number of columns is the length of x
:
Y
array([[2, 2, 2], [4, 4, 4]])
Because the ...
Get Think Bayes, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.