# 22.8. Distributions¶ Open the notebook in Colab Open the notebook in Colab Open the notebook in Colab Open the notebook in Colab Open the notebook in SageMaker Studio Lab

Now that we have learned how to work with probability in both the discrete and the continuous setting, let’s get to know some of the common distributions encountered. Depending on the area of machine learning, we may need to be familiar with vastly more of these, or for some areas of deep learning potentially none at all. This is, however, a good basic list to be familiar with. Let’s first import some common libraries.

%matplotlib inline
from math import erf, factorial
import torch
from IPython import display
from d2l import torch as d2l

torch.pi = torch.acos(torch.zeros(1)) * 2  # Define pi in torch

%matplotlib inline
from math import erf, factorial
import numpy as np
from IPython import display
from d2l import mxnet as d2l

%matplotlib inline
from math import erf, factorial
import tensorflow as tf
import tensorflow_probability as tfp
from IPython import display
from d2l import tensorflow as d2l

tf.pi = tf.acos(tf.zeros(1)) * 2  # Define pi in TensorFlow


## 22.8.1. Bernoulli¶

This is the simplest random variable usually encountered. This random variable encodes a coin flip which comes up $$1$$ with probability $$p$$ and $$0$$ with probability $$1-p$$. If we have a random variable $$X$$ with this distribution, we will write

(22.8.1)$X \sim \mathrm{Bernoulli}(p).$

The cumulative distribution function is

(22.8.2)$\begin{split}F(x) = \begin{cases} 0 & x < 0, \\ 1-p & 0 \le x < 1, \\ 1 & x >= 1 . \end{cases}\end{split}$

The probability mass function is plotted below.

p = 0.3

d2l.set_figsize()
d2l.plt.stem([0, 1], [1 - p, p], use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()

p = 0.3

d2l.set_figsize()
d2l.plt.stem([0, 1], [1 - p, p], use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()

p = 0.3

d2l.set_figsize()
d2l.plt.stem([0, 1], [1 - p, p], use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()


Now, let’s plot the cumulative distribution function (22.8.2).

x = torch.arange(-1, 2, 0.01)

def F(x):
return 0 if x < 0 else 1 if x > 1 else 1 - p

d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.')

x = np.arange(-1, 2, 0.01)

def F(x):
return 0 if x < 0 else 1 if x > 1 else 1 - p

d2l.plot(x, np.array([F(y) for y in x]), 'x', 'c.d.f.')

x = tf.range(-1, 2, 0.01)

def F(x):
return 0 if x < 0 else 1 if x > 1 else 1 - p

d2l.plot(x, tf.constant([F(y) for y in x]), 'x', 'c.d.f.')


If $$X \sim \mathrm{Bernoulli}(p)$$, then:

• $$\mu_X = p$$,

• $$\sigma_X^2 = p(1-p)$$.

We can sample an array of arbitrary shape from a Bernoulli random variable as follows.

1*(torch.rand(10, 10) < p)

tensor([[0, 0, 1, 0, 0, 0, 1, 1, 0, 0],
[1, 1, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[1, 1, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 0, 1, 0, 1]])

1*(np.random.rand(10, 10) < p)

array([[0, 0, 1, 0, 1, 1, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 1],
[0, 1, 1, 1, 0, 0, 0, 0, 1, 0],
[1, 1, 0, 0, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0]])

tf.cast(tf.random.uniform((10, 10)) < p, dtype=tf.float32)

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 1., 0., 1., 0., 1.],
[0., 0., 0., 1., 0., 0., 0., 1., 0., 1.],
[1., 0., 0., 1., 0., 1., 1., 0., 1., 0.],
[1., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 1., 0., 0., 1., 0., 1., 0., 0.],
[0., 1., 1., 1., 0., 0., 0., 0., 1., 1.],
[0., 0., 0., 1., 0., 0., 0., 0., 1., 0.],
[1., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 1., 1., 0., 0., 0., 1.],
[1., 0., 1., 0., 0., 0., 1., 0., 1., 0.]], dtype=float32)>


## 22.8.2. Discrete Uniform¶

The next commonly encountered random variable is a discrete uniform. For our discussion here, we will assume that it is supported on the integers $$\{1, 2, \ldots, n\}$$, however any other set of values can be freely chosen. The meaning of the word uniform in this context is that every possible value is equally likely. The probability for each value $$i \in \{1, 2, 3, \ldots, n\}$$ is $$p_i = \frac{1}{n}$$. We will denote a random variable $$X$$ with this distribution as

(22.8.3)$X \sim U(n).$

The cumulative distribution function is

(22.8.4)$\begin{split}F(x) = \begin{cases} 0 & x < 1, \\ \frac{k}{n} & k \le x < k+1 \text{ with } 1 \le k < n, \\ 1 & x >= n . \end{cases}\end{split}$

Let’s first plot the probability mass function.

n = 5

d2l.plt.stem([i+1 for i in range(n)], n*[1 / n], use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()

n = 5

d2l.plt.stem([i+1 for i in range(n)], n*[1 / n], use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()

n = 5

d2l.plt.stem([i+1 for i in range(n)], n*[1 / n], use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()


Now, let’s plot the cumulative distribution function (22.8.4).

x = torch.arange(-1, 6, 0.01)

def F(x):
return 0 if x < 1 else 1 if x > n else torch.floor(x) / n

d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.')

x = np.arange(-1, 6, 0.01)

def F(x):
return 0 if x < 1 else 1 if x > n else np.floor(x) / n

d2l.plot(x, np.array([F(y) for y in x]), 'x', 'c.d.f.')

x = tf.range(-1, 6, 0.01)

def F(x):
return 0 if x < 1 else 1 if x > n else tf.floor(x) / n

d2l.plot(x, [F(y) for y in x], 'x', 'c.d.f.')


If $$X \sim U(n)$$, then:

• $$\mu_X = \frac{1+n}{2}$$,

• $$\sigma_X^2 = \frac{n^2-1}{12}$$.

We can sample an array of arbitrary shape from a discrete uniform random variable as follows.

torch.randint(1, n, size=(10, 10))

tensor([[2, 1, 2, 1, 4, 1, 1, 1, 4, 4],
[1, 3, 4, 2, 1, 2, 1, 2, 4, 3],
[3, 3, 3, 3, 3, 1, 3, 1, 1, 2],
[4, 2, 4, 1, 1, 4, 3, 2, 1, 4],
[4, 4, 2, 4, 4, 1, 4, 4, 3, 1],
[4, 4, 2, 3, 4, 4, 4, 2, 3, 1],
[1, 4, 3, 4, 2, 4, 2, 3, 3, 2],
[4, 3, 4, 4, 2, 4, 3, 3, 3, 3],
[4, 2, 1, 3, 4, 1, 4, 4, 4, 1],
[3, 2, 3, 4, 3, 2, 1, 2, 4, 2]])

np.random.randint(1, n, size=(10, 10))

array([[2, 2, 1, 2, 1, 3, 1, 4, 2, 2],
[4, 4, 4, 4, 4, 3, 1, 2, 1, 4],
[4, 4, 2, 1, 1, 3, 1, 2, 3, 1],
[3, 2, 1, 3, 1, 4, 2, 4, 4, 1],
[1, 3, 1, 4, 3, 2, 4, 4, 2, 2],
[3, 1, 1, 4, 3, 3, 1, 4, 2, 3],
[3, 4, 3, 3, 2, 2, 2, 3, 4, 1],
[3, 4, 3, 1, 1, 3, 2, 3, 3, 1],
[2, 1, 4, 2, 1, 4, 1, 1, 3, 2],
[3, 3, 2, 4, 1, 2, 4, 2, 2, 2]])

tf.random.uniform((10, 10), 1, n, dtype=tf.int32)

<tf.Tensor: shape=(10, 10), dtype=int32, numpy=
array([[4, 2, 1, 2, 3, 2, 1, 4, 4, 4],
[1, 1, 3, 3, 4, 1, 1, 1, 1, 2],
[2, 2, 1, 1, 1, 1, 1, 3, 1, 3],
[2, 3, 1, 4, 4, 4, 1, 2, 3, 4],
[2, 4, 3, 4, 1, 2, 2, 4, 3, 2],
[4, 4, 2, 3, 1, 1, 3, 2, 1, 3],
[2, 3, 2, 3, 3, 1, 1, 4, 2, 1],
[3, 1, 2, 4, 3, 2, 3, 2, 3, 1],
[4, 2, 4, 2, 3, 4, 2, 1, 1, 1],
[1, 2, 2, 1, 3, 4, 2, 1, 2, 4]], dtype=int32)>


## 22.8.3. Continuous Uniform¶

Next, let’s discuss the continuous uniform distribution. The idea behind this random variable is that if we increase the $$n$$ in the discrete uniform distribution, and then scale it to fit within the interval $$[a, b]$$, we will approach a continuous random variable that just picks an arbitrary value in $$[a, b]$$ all with equal probability. We will denote this distribution as

(22.8.5)$X \sim U(a, b).$

The probability density function is

(22.8.6)$\begin{split}p(x) = \begin{cases} \frac{1}{b-a} & x \in [a, b], \\ 0 & x \not\in [a, b].\end{cases}\end{split}$

The cumulative distribution function is

(22.8.7)$\begin{split}F(x) = \begin{cases} 0 & x < a, \\ \frac{x-a}{b-a} & x \in [a, b], \\ 1 & x >= b . \end{cases}\end{split}$

Let’s first plot the probability density function (22.8.6).

a, b = 1, 3

x = torch.arange(0, 4, 0.01)
p = (x > a).type(torch.float32)*(x < b).type(torch.float32)/(b-a)
d2l.plot(x, p, 'x', 'p.d.f.')

a, b = 1, 3

x = np.arange(0, 4, 0.01)
p = (x > a)*(x < b)/(b - a)

d2l.plot(x, p, 'x', 'p.d.f.')

a, b = 1, 3

x = tf.range(0, 4, 0.01)
p = tf.cast(x > a, tf.float32) * tf.cast(x < b, tf.float32) / (b - a)
d2l.plot(x, p, 'x', 'p.d.f.')


Now, let’s plot the cumulative distribution function (22.8.7).

def F(x):
return 0 if x < a else 1 if x > b else (x - a) / (b - a)

d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.')

def F(x):
return 0 if x < a else 1 if x > b else (x - a) / (b - a)

d2l.plot(x, np.array([F(y) for y in x]), 'x', 'c.d.f.')

def F(x):
return 0 if x < a else 1 if x > b else (x - a) / (b - a)

d2l.plot(x, [F(y) for y in x], 'x', 'c.d.f.')


If $$X \sim U(a, b)$$, then:

• $$\mu_X = \frac{a+b}{2}$$,

• $$\sigma_X^2 = \frac{(b-a)^2}{12}$$.

We can sample an array of arbitrary shape from a uniform random variable as follows. Note that it by default samples from a $$U(0,1)$$, so if we want a different range we need to scale it.

(b - a) * torch.rand(10, 10) + a

tensor([[1.9620, 2.2448, 2.1361, 1.8685, 2.4743, 2.8697, 2.5755, 1.9305, 1.9610,
1.2658],
[2.5995, 1.6955, 1.9122, 2.7636, 1.5975, 1.7372, 2.2370, 2.4743, 1.0397,
1.7868],
[2.5328, 1.1215, 2.4301, 2.4364, 2.8866, 2.7998, 2.8357, 1.2320, 1.3709,
2.4165],
[1.8810, 1.5275, 2.4576, 2.5201, 2.8686, 1.4113, 2.8357, 2.8724, 2.3951,
1.2467],
[1.3033, 2.2931, 2.3270, 1.2439, 1.3615, 1.1760, 2.8228, 1.2723, 2.9406,
1.9897],
[1.4116, 2.8607, 2.5709, 2.5520, 2.4233, 2.4687, 1.1962, 1.7299, 1.5554,
1.6602],
[2.0118, 1.5296, 2.2120, 1.9139, 1.5104, 2.6513, 1.8306, 2.6043, 2.6991,
2.3040],
[2.6417, 1.1577, 1.5684, 1.7522, 2.3080, 1.7528, 2.0725, 1.7449, 2.9906,
1.1446],
[2.3181, 1.0035, 1.5626, 1.7000, 2.4376, 2.3113, 1.0061, 2.0907, 2.9202,
1.1290],
[1.9148, 2.8899, 2.3914, 2.3493, 2.4989, 1.3410, 2.8666, 1.2983, 1.7835,
1.6523]])

(b - a) * np.random.rand(10, 10) + a

array([[1.0285477 , 2.30487274, 2.46035174, 1.47624247, 1.68537456,
2.69232191, 1.50337147, 1.8378831 , 2.9353607 , 2.43238578],
[2.02866754, 1.79381366, 1.53764485, 1.29367412, 1.0623111 ,
2.97196419, 1.7807747 , 1.21532002, 2.79363285, 1.46241859],
[1.86319393, 1.8275095 , 2.92672695, 1.16540638, 1.11149087,
2.26385399, 1.64773856, 2.10487796, 2.9628376 , 1.82418303],
[1.75413145, 1.87265085, 1.52759554, 2.36805032, 2.88695684,
1.64338387, 1.70192678, 2.47550085, 1.30299629, 2.96544757],
[2.36563373, 2.6295078 , 2.49587571, 1.18509783, 2.12505834,
1.47564893, 1.8193782 , 2.41670711, 2.33170929, 1.80903901],
[1.34483351, 2.78775386, 2.63622162, 1.50505936, 1.70294457,
1.67815771, 2.24513924, 2.16103357, 1.52793397, 2.35074429],
[1.56921125, 1.43117607, 1.04328086, 2.04591615, 2.91879139,
1.28255929, 1.52137279, 1.18225911, 2.05154783, 1.17281177],
[2.87687945, 2.43658935, 1.60176438, 2.76049909, 1.55248646,
1.87612264, 1.57737067, 1.02074713, 2.65927775, 1.52863438],
[2.47472905, 1.06490744, 1.36506573, 2.97044377, 1.8895881 ,
1.93463361, 2.8757773 , 2.21910962, 1.40434818, 2.89128471],
[2.9836218 , 2.34600794, 2.42532166, 2.66987239, 1.50706557,
1.82555125, 2.54334045, 1.51825956, 1.58089099, 1.59370029]])

(b - a) * tf.random.uniform((10, 10)) + a

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[2.911767 , 1.9663024, 2.895637 , 2.8883607, 1.0008419, 1.2210989,
2.3168375, 1.0910976, 1.4136798, 2.6248972],
[2.8595521, 2.6681437, 2.5337331, 2.9347646, 1.6386368, 2.582692 ,
2.4093583, 1.5318234, 2.3109481, 1.2822199],
[1.3851976, 2.891004 , 2.3458998, 2.532693 , 1.009356 , 1.300389 ,
1.7870643, 1.7983582, 1.0286407, 1.2227287],
[2.2851343, 1.6705966, 2.8387861, 1.6952121, 2.9776907, 1.6421416,
1.3141332, 2.3460546, 1.7299359, 1.4540274],
[1.3693674, 1.4927194, 2.6860666, 1.5010288, 1.2950754, 1.5346754,
1.7484591, 2.6193628, 1.7835417, 2.9797173],
[2.4200325, 1.8951554, 1.8142483, 2.769222 , 1.1864698, 1.6085424,
2.118076 , 1.4657321, 1.1496787, 2.2762408],
[1.4261632, 2.4670355, 1.1178432, 1.5167699, 1.6689065, 2.9666533,
1.6136634, 1.5272057, 1.7493064, 1.6964726],
[2.599353 , 1.8333049, 1.3800771, 1.5584002, 2.8237827, 1.5010374,
2.4132142, 2.5196707, 1.6927857, 1.0632646],
[2.4301803, 2.409309 , 1.7334406, 1.4275393, 2.2455363, 1.5153031,
1.1871393, 2.9864538, 1.0725605, 1.2300951],
[1.7201931, 2.58567  , 2.9449644, 1.3707154, 2.6910393, 2.901303 ,
2.4058154, 2.237254 , 1.0431969, 2.1106973]], dtype=float32)>


## 22.8.4. Binomial¶

Let’s make things a little more complex and examine the binomial random variable. This random variable originates from performing a sequence of $$n$$ independent experiments, each of which has probability $$p$$ of succeeding, and asking how many successes we expect to see.

Let’s express this mathematically. Each experiment is an independent random variable $$X_i$$ where we will use $$1$$ to encode success, and $$0$$ to encode failure. Since each is an independent coin flip which is successful with probability $$p$$, we can say that $$X_i \sim \mathrm{Bernoulli}(p)$$. Then, the binomial random variable is

(22.8.8)$X = \sum_{i=1}^n X_i.$

In this case, we will write

(22.8.9)$X \sim \mathrm{Binomial}(n, p).$

To get the cumulative distribution function, we need to notice that getting exactly $$k$$ successes can occur in $$\binom{n}{k} = \frac{n!}{k!(n-k)!}$$ ways each of which has a probability of $$p^k(1-p)^{n-k}$$ of occurring. Thus the cumulative distribution function is

(22.8.10)$\begin{split}F(x) = \begin{cases} 0 & x < 0, \\ \sum_{m \le k} \binom{n}{m} p^m(1-p)^{n-m} & k \le x < k+1 \text{ with } 0 \le k < n, \\ 1 & x >= n . \end{cases}\end{split}$

Let’s first plot the probability mass function.

n, p = 10, 0.2

# Compute binomial coefficient
def binom(n, k):
comb = 1
for i in range(min(k, n - k)):
comb = comb * (n - i) // (i + 1)
return comb

pmf = torch.tensor([p**i * (1-p)**(n - i) * binom(n, i) for i in range(n + 1)])

d2l.plt.stem([i for i in range(n + 1)], pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()

n, p = 10, 0.2

# Compute binomial coefficient
def binom(n, k):
comb = 1
for i in range(min(k, n - k)):
comb = comb * (n - i) // (i + 1)
return comb

pmf = np.array([p**i * (1-p)**(n - i) * binom(n, i) for i in range(n + 1)])

d2l.plt.stem([i for i in range(n + 1)], pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()

n, p = 10, 0.2

# Compute binomial coefficient
def binom(n, k):
comb = 1
for i in range(min(k, n - k)):
comb = comb * (n - i) // (i + 1)
return comb

pmf = tf.constant([p**i * (1-p)**(n - i) * binom(n, i) for i in range(n + 1)])

d2l.plt.stem([i for i in range(n + 1)], pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()


Now, let’s plot the cumulative distribution function (22.8.10).

x = torch.arange(-1, 11, 0.01)
cmf = torch.cumsum(pmf, dim=0)

def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]

d2l.plot(x, torch.tensor([F(y) for y in x.tolist()]), 'x', 'c.d.f.')

x = np.arange(-1, 11, 0.01)
cmf = np.cumsum(pmf)

def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]

d2l.plot(x, np.array([F(y) for y in x.tolist()]), 'x', 'c.d.f.')

x = tf.range(-1, 11, 0.01)
cmf = tf.cumsum(pmf)

def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]

d2l.plot(x, [F(y) for y in x.numpy().tolist()], 'x', 'c.d.f.')


If $$X \sim \mathrm{Binomial}(n, p)$$, then:

• $$\mu_X = np$$,

• $$\sigma_X^2 = np(1-p)$$.

This follows from the linearity of expected value over the sum of $$n$$ Bernoulli random variables, and the fact that the variance of the sum of independent random variables is the sum of the variances. This can be sampled as follows.

m = torch.distributions.binomial.Binomial(n, p)
m.sample(sample_shape=(10, 10))

tensor([[0., 4., 1., 1., 3., 1., 3., 5., 2., 2.],
[2., 1., 0., 1., 2., 0., 3., 2., 2., 2.],
[1., 0., 1., 1., 1., 3., 4., 2., 2., 2.],
[3., 2., 2., 2., 0., 1., 2., 4., 1., 3.],
[5., 1., 3., 2., 0., 0., 0., 1., 0., 3.],
[2., 4., 3., 4., 2., 3., 1., 1., 3., 2.],
[2., 2., 3., 1., 1., 1., 5., 2., 2., 2.],
[2., 2., 0., 2., 2., 3., 2., 4., 1., 2.],
[2., 0., 2., 1., 3., 4., 1., 1., 4., 2.],
[1., 3., 2., 3., 3., 4., 4., 3., 2., 2.]])

np.random.binomial(n, p, size=(10, 10))

array([[3, 5, 1, 3, 2, 2, 3, 3, 2, 2],
[1, 1, 1, 2, 2, 0, 2, 2, 3, 3],
[1, 2, 0, 3, 1, 0, 1, 2, 2, 1],
[0, 2, 1, 4, 3, 2, 2, 2, 1, 2],
[0, 0, 3, 2, 2, 1, 2, 2, 0, 3],
[0, 1, 2, 5, 2, 1, 3, 1, 3, 2],
[5, 0, 3, 0, 0, 1, 1, 2, 6, 4],
[1, 2, 2, 0, 3, 3, 2, 0, 1, 4],
[2, 2, 1, 3, 2, 2, 3, 1, 1, 3],
[0, 2, 4, 6, 2, 1, 3, 2, 3, 2]])

m = tfp.distributions.Binomial(n, p)
m.sample(sample_shape=(10, 10))

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[ 8.,  6.,  7.,  7.,  5.,  4.,  5.,  7.,  9.,  6.],
[ 6.,  5.,  6.,  2.,  7.,  5.,  3.,  8.,  4.,  3.],
[ 8.,  7.,  5.,  5.,  2.,  5.,  7.,  5.,  4.,  8.],
[ 5.,  6.,  7., 10.,  4.,  5.,  6.,  6.,  1.,  5.],
[ 8.,  5.,  5.,  6.,  5.,  5.,  7.,  5.,  5.,  9.],
[ 6.,  5.,  3.,  6.,  3.,  6.,  8.,  5.,  5.,  7.],
[ 6.,  5.,  4.,  4.,  6.,  3.,  7.,  7.,  5.,  6.],
[ 6.,  4.,  7.,  6.,  8.,  3.,  6.,  7.,  5.,  4.],
[ 6.,  6.,  4.,  6.,  7.,  8.,  3.,  6.,  8.,  7.],
[ 7.,  8.,  5.,  3.,  4.,  3.,  3.,  5.,  3.,  4.]], dtype=float32)>


## 22.8.5. Poisson¶

Let’s now perform a thought experiment. We are standing at a bus stop and we want to know how many buses will arrive in the next minute. Let’s start by considering $$X^{(1)} \sim \mathrm{Bernoulli}(p)$$ which is simply the probability that a bus arrives in the one minute window. For bus stops far from an urban center, this might be a pretty good approximation. We may never see more than one bus in a minute.

However, if we are in a busy area, it is possible or even likely that two buses will arrive. We can model this by splitting our random variable into two parts for the first 30 seconds, or the second 30 seconds. In this case we can write

(22.8.11)$X^{(2)} \sim X^{(2)}_1 + X^{(2)}_2,$

where $$X^{(2)}$$ is the total sum, and $$X^{(2)}_i \sim \mathrm{Bernoulli}(p/2)$$. The total distribution is then $$X^{(2)} \sim \mathrm{Binomial}(2, p/2)$$.

Why stop here? Let’s continue to split that minute into $$n$$ parts. By the same reasoning as above, we see that

(22.8.12)$X^{(n)} \sim \mathrm{Binomial}(n, p/n).$

Consider these random variables. By the previous section, we know that (22.8.12) has mean $$\mu_{X^{(n)}} = n(p/n) = p$$, and variance $$\sigma_{X^{(n)}}^2 = n(p/n)(1-(p/n)) = p(1-p/n)$$. If we take $$n \rightarrow \infty$$, we can see that these numbers stabilize to $$\mu_{X^{(\infty)}} = p$$, and variance $$\sigma_{X^{(\infty)}}^2 = p$$. This indicates that there could be some random variable we can define in this infinite subdivision limit.

This should not come as too much of a surprise, since in the real world we can just count the number of bus arrivals, however it is nice to see that our mathematical model is well defined. This discussion can be made formal as the law of rare events.

Following through this reasoning carefully, we can arrive at the following model. We will say that $$X \sim \mathrm{Poisson}(\lambda)$$ if it is a random variable which takes the values $$\{0,1,2, \ldots\}$$ with probability

(22.8.13)$p_k = \frac{\lambda^ke^{-\lambda}}{k!}.$

The value $$\lambda > 0$$ is known as the rate (or the shape parameter), and denotes the average number of arrivals we expect in one unit of time.

We may sum this probability mass function to get the cumulative distribution function.

(22.8.14)$\begin{split}F(x) = \begin{cases} 0 & x < 0, \\ e^{-\lambda}\sum_{m = 0}^k \frac{\lambda^m}{m!} & k \le x < k+1 \text{ with } 0 \le k. \end{cases}\end{split}$

Let’s first plot the probability mass function (22.8.13).

lam = 5.0

xs = [i for i in range(20)]
pmf = torch.tensor([torch.exp(torch.tensor(-lam)) * lam**k
/ factorial(k) for k in xs])

d2l.plt.stem(xs, pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()

lam = 5.0

xs = [i for i in range(20)]
pmf = np.array([np.exp(-lam) * lam**k / factorial(k) for k in xs])

d2l.plt.stem(xs, pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()

lam = 5.0

xs = [i for i in range(20)]
pmf = tf.constant([tf.exp(tf.constant(-lam)).numpy() * lam**k
/ factorial(k) for k in xs])

d2l.plt.stem(xs, pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()


Now, let’s plot the cumulative distribution function (22.8.14).

x = torch.arange(-1, 21, 0.01)
cmf = torch.cumsum(pmf, dim=0)
def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]

d2l.plot(x, torch.tensor([F(y) for y in x.tolist()]), 'x', 'c.d.f.')

x = np.arange(-1, 21, 0.01)
cmf = np.cumsum(pmf)
def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]

d2l.plot(x, np.array([F(y) for y in x.tolist()]), 'x', 'c.d.f.')

x = tf.range(-1, 21, 0.01)
cmf = tf.cumsum(pmf)
def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]

d2l.plot(x, [F(y) for y in x.numpy().tolist()], 'x', 'c.d.f.')


As we saw above, the means and variances are particularly concise. If $$X \sim \mathrm{Poisson}(\lambda)$$, then:

• $$\mu_X = \lambda$$,

• $$\sigma_X^2 = \lambda$$.

This can be sampled as follows.

m = torch.distributions.poisson.Poisson(lam)
m.sample((10, 10))

tensor([[ 9.,  3.,  6.,  4.,  7.,  5.,  4.,  3.,  5.,  5.],
[ 2.,  3.,  3.,  3.,  7.,  5.,  6.,  9.,  5.,  2.],
[ 9.,  4.,  5.,  2.,  5.,  6.,  9.,  5.,  4.,  2.],
[ 2.,  5.,  4.,  6.,  6.,  1.,  6.,  3.,  2.,  4.],
[ 6.,  4.,  4.,  3.,  4.,  2.,  8.,  2.,  2.,  2.],
[10.,  6.,  6.,  6.,  1.,  4.,  2.,  8.,  3.,  3.],
[ 4.,  4.,  7.,  3., 14.,  7.,  3., 10.,  3.,  7.],
[ 4.,  6.,  8.,  5.,  6.,  4.,  4.,  2.,  3.,  3.],
[ 5.,  7.,  4.,  5.,  9.,  4.,  9.,  4.,  2.,  5.],
[ 5.,  6.,  4.,  5.,  6.,  7.,  2.,  6.,  7.,  6.]])

np.random.poisson(lam, size=(10, 10))

array([[ 2,  8,  4,  6,  8,  1,  5,  5,  7,  2],
[ 4,  8,  5,  0,  5,  7,  5, 11,  5,  5],
[ 5,  8,  4,  3,  7,  7,  5,  2,  5,  4],
[ 4,  4,  5,  0,  4,  7,  4,  9,  6,  5],
[ 4,  7,  6,  5, 10,  4,  6,  4,  7,  8],
[ 4,  7,  5,  6,  6,  6,  4, 10,  6,  7],
[ 8,  3,  5,  8,  7,  6,  7,  6,  8,  8],
[ 4,  8,  6, 10,  2,  5,  2,  8,  6,  9],
[12,  6,  6,  2,  5,  5,  4,  5,  6,  3],
[ 4,  2,  4,  2,  7,  5,  6,  9,  2,  4]])

m = tfp.distributions.Poisson(lam)
m.sample((10, 10))

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[ 2.,  3.,  6.,  6.,  7.,  6.,  4.,  5.,  9.,  5.],
[12.,  4.,  5.,  5.,  2.,  2.,  4.,  6.,  7., 10.],
[ 9.,  4.,  5.,  3.,  6.,  2.,  5.,  2.,  7.,  6.],
[ 3.,  1.,  2.,  4.,  8.,  6.,  5.,  7.,  7.,  6.],
[ 7.,  1.,  1.,  6.,  7.,  5.,  3.,  5.,  5.,  9.],
[ 8.,  6.,  6.,  4., 10.,  3.,  6.,  6.,  8.,  4.],
[ 5.,  4.,  4., 10.,  6.,  2.,  4.,  9.,  4.,  3.],
[12.,  6., 10.,  4.,  4.,  5., 14.,  4.,  4.,  3.],
[ 2.,  2.,  7.,  3.,  6.,  7.,  5., 10.,  4.,  1.],
[ 8.,  3.,  3.,  5., 10.,  2.,  4.,  4.,  3.,  2.]], dtype=float32)>


## 22.8.6. Gaussian¶

Now Let’s try a different, but related experiment. Let’s say we again are performing $$n$$ independent $$\mathrm{Bernoulli}(p)$$ measurements $$X_i$$. The distribution of the sum of these is $$X^{(n)} \sim \mathrm{Binomial}(n, p)$$. Rather than taking a limit as $$n$$ increases and $$p$$ decreases, Let’s fix $$p$$, and then send $$n \rightarrow \infty$$. In this case $$\mu_{X^{(n)}} = np \rightarrow \infty$$ and $$\sigma_{X^{(n)}}^2 = np(1-p) \rightarrow \infty$$, so there is no reason to think this limit should be well defined.

However, not all hope is lost! Let’s just make the mean and variance be well behaved by defining

(22.8.15)$Y^{(n)} = \frac{X^{(n)} - \mu_{X^{(n)}}}{\sigma_{X^{(n)}}}.$

This can be seen to have mean zero and variance one, and so it is plausible to believe that it will converge to some limiting distribution. If we plot what these distributions look like, we will become even more convinced that it will work.

p = 0.2
ns = [1, 10, 100, 1000]
d2l.plt.figure(figsize=(10, 3))
for i in range(4):
n = ns[i]
pmf = torch.tensor([p**i * (1-p)**(n-i) * binom(n, i)
for i in range(n + 1)])
d2l.plt.subplot(1, 4, i + 1)
d2l.plt.stem([(i - n*p)/torch.sqrt(torch.tensor(n*p*(1 - p)))
for i in range(n + 1)], pmf,
use_line_collection=True)
d2l.plt.xlim([-4, 4])
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.title("n = {}".format(n))
d2l.plt.show()

p = 0.2
ns = [1, 10, 100, 1000]
d2l.plt.figure(figsize=(10, 3))
for i in range(4):
n = ns[i]
pmf = np.array([p**i * (1-p)**(n-i) * binom(n, i) for i in range(n + 1)])
d2l.plt.subplot(1, 4, i + 1)
d2l.plt.stem([(i - n*p)/np.sqrt(n*p*(1 - p)) for i in range(n + 1)], pmf,
use_line_collection=True)
d2l.plt.xlim([-4, 4])
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.title("n = {}".format(n))
d2l.plt.show()

p = 0.2
ns = [1, 10, 100, 1000]
d2l.plt.figure(figsize=(10, 3))
for i in range(4):
n = ns[i]
pmf = tf.constant([p**i * (1-p)**(n-i) * binom(n, i)
for i in range(n + 1)])
d2l.plt.subplot(1, 4, i + 1)
d2l.plt.stem([(i - n*p)/tf.sqrt(tf.constant(n*p*(1 - p)))
for i in range(n + 1)], pmf,
use_line_collection=True)
d2l.plt.xlim([-4, 4])
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.title("n = {}".format(n))
d2l.plt.show()


One thing to note: compared to the Poisson case, we are now dividing by the standard deviation which means that we are squeezing the possible outcomes into smaller and smaller areas. This is an indication that our limit will no longer be discrete, but rather continuous.

A derivation of what occurs is beyond the scope of this document, but the central limit theorem states that as $$n \rightarrow \infty$$, this will yield the Gaussian Distribution (or sometimes normal distribution). More explicitly, for any $$a, b$$:

(22.8.16)$\lim_{n \rightarrow \infty} P(Y^{(n)} \in [a, b]) = P(\mathcal{N}(0,1) \in [a, b]),$

where we say a random variable is normally distributed with given mean $$\mu$$ and variance $$\sigma^2$$, written $$X \sim \mathcal{N}(\mu, \sigma^2)$$ if $$X$$ has density

(22.8.17)$p_X(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}.$

Let’s first plot the probability density function (22.8.17).

mu, sigma = 0, 1

x = torch.arange(-3, 3, 0.01)
p = 1 / torch.sqrt(2 * torch.pi * sigma**2) * torch.exp(
-(x - mu)**2 / (2 * sigma**2))

d2l.plot(x, p, 'x', 'p.d.f.')

mu, sigma = 0, 1

x = np.arange(-3, 3, 0.01)
p = 1 / np.sqrt(2 * np.pi * sigma**2) * np.exp(-(x - mu)**2 / (2 * sigma**2))

d2l.plot(x, p, 'x', 'p.d.f.')

mu, sigma = 0, 1

x = tf.range(-3, 3, 0.01)
p = 1 / tf.sqrt(2 * tf.pi * sigma**2) * tf.exp(
-(x - mu)**2 / (2 * sigma**2))

d2l.plot(x, p, 'x', 'p.d.f.')


Now, let’s plot the cumulative distribution function. It is beyond the scope of this appendix, but the Gaussian c.d.f. does not have a closed-form formula in terms of more elementary functions. We will use erf which provides a way to compute this integral numerically.

def phi(x):
return (1.0 + erf((x - mu) / (sigma * torch.sqrt(torch.tensor(2.))))) / 2.0

d2l.plot(x, torch.tensor([phi(y) for y in x.tolist()]), 'x', 'c.d.f.')

def phi(x):
return (1.0 + erf((x - mu) / (sigma * np.sqrt(2)))) / 2.0

d2l.plot(x, np.array([phi(y) for y in x.tolist()]), 'x', 'c.d.f.')

def phi(x):
return (1.0 + erf((x - mu) / (sigma * tf.sqrt(tf.constant(2.))))) / 2.0

d2l.plot(x, [phi(y) for y in x.numpy().tolist()], 'x', 'c.d.f.')


Keen-eyed readers will recognize some of these terms. Indeed, we encountered this integral in Section 22.5. Indeed we need exactly that computation to see that this $$p_X(x)$$ has total area one and is thus a valid density.

Our choice of working with coin flips made computations shorter, but nothing about that choice was fundamental. Indeed, if we take any collection of independent identically distributed random variables $$X_i$$, and form

(22.8.18)$X^{(N)} = \sum_{i=1}^N X_i.$

Then

(22.8.19)$\frac{X^{(N)} - \mu_{X^{(N)}}}{\sigma_{X^{(N)}}}$

will be approximately Gaussian. There are additional requirements needed to make it work, most commonly $$E[X^4] < \infty$$, but the philosophy is clear.

The central limit theorem is the reason why the Gaussian is fundamental to probability, statistics, and machine learning. Whenever we can say that something we measured is a sum of many small independent contributions, we can assume that the thing being measured will be close to Gaussian.

There are many more fascinating properties of Gaussians, and we would like to discuss one more here. The Gaussian is what is known as a maximum entropy distribution. We will get into entropy more deeply in Section 22.11, however all we need to know at this point is that it is a measure of randomness. In a rigorous mathematical sense, we can think of the Gaussian as the most random choice of random variable with fixed mean and variance. Thus, if we know that our random variable has some mean and variance, the Gaussian is in a sense the most conservative choice of distribution we can make.

To close the section, let’s recall that if $$X \sim \mathcal{N}(\mu, \sigma^2)$$, then:

• $$\mu_X = \mu$$,

• $$\sigma_X^2 = \sigma^2$$.

We can sample from the Gaussian (or standard normal) distribution as shown below.

torch.normal(mu, sigma, size=(10, 10))

tensor([[ 0.1677, -0.7462,  1.1029, -1.0665,  0.0036, -0.8823,  1.5826, -0.8944,
1.5103, -1.2203],
[-0.3716,  2.2532, -1.3558,  1.0863,  0.5990, -1.8327, -0.6022, -0.0933,
-1.5177,  2.6853],
[ 0.6901,  1.5855, -0.4401,  0.6621,  0.2587,  0.3516, -0.2432, -0.3704,
0.0109,  0.6213],
[ 2.5237, -2.7913,  0.8006, -0.2412,  0.7282,  0.8062, -0.2894,  0.7135,
-0.6108, -0.8372],
[ 0.5409, -1.4337,  0.2972,  1.2728, -1.0889,  1.7792, -0.2540,  0.0330,
-0.5963, -1.7805],
[ 0.2239,  0.7997,  1.6492,  1.0243, -2.2255, -0.3796, -0.1375, -0.3982,
0.2152,  0.5650],
[ 0.2160,  1.5258,  0.8421,  1.5865,  0.0431,  0.2344,  0.1951,  1.4344,
0.0029, -1.9691],
[-1.7597,  0.0362, -1.2524, -0.0547, -1.8852, -2.2572, -0.1579, -0.5615,
0.8205,  0.6424],
[-0.3875,  0.8489, -0.7034, -0.7598,  0.4008,  0.0971,  0.1265, -0.3023,
-1.0155, -0.1641],
[-0.2039,  0.4160, -0.3035, -0.0136, -1.7823, -0.5321, -0.6687,  0.3651,
-0.8252, -1.0663]])

np.random.normal(mu, sigma, size=(10, 10))

array([[-0.29503104,  1.12416252,  1.24335892, -0.58107367,  0.96869114,
-0.87705009, -1.51239443, -0.43508646,  1.10116388, -0.10169488],
[ 1.59608965,  0.1550536 , -0.50141912, -1.48585871, -0.68418802,
0.63249986,  0.77455925,  0.59183081, -0.38287086,  0.76908111],
[-0.17704357, -0.76367173, -0.83963509,  2.99918685, -0.24399479,
1.89737549,  0.95696642,  0.28627525, -0.74799976,  0.7174262 ],
[-0.09609771, -0.09511743, -0.72166984, -3.94976978, -0.44709017,
-0.42781109,  2.39014964,  0.21640664, -0.48967074,  1.89582179],
[-0.04901966,  0.64749678,  0.88051717,  0.53333149,  1.11387094,
-0.99904278, -0.61145601,  0.19218998, -0.12338616, -0.40579713],
[-1.63742669, -0.47681313,  0.11139641,  1.00511925, -0.33664355,
-1.20425574,  0.64463781, -0.76726346, -0.02765596,  1.48007446],
[-0.42501804, -1.95908889, -0.70233561,  0.88339171, -0.71526497,
0.22647059, -1.28546053,  0.89240439, -0.76769509, -2.24336261],
[-1.63482717, -0.20401733, -0.17189122,  0.78484013, -1.22278884,
0.20456232, -0.6118326 , -0.8252671 ,  0.16003692, -0.60762541],
[ 0.05251489,  0.50649076, -0.20104584,  1.33660758,  0.15185705,
0.47339565, -0.09000228, -0.98819232, -1.13180671, -0.17428252],
[-0.35822591,  0.46789343,  0.68622364, -0.79342449,  1.07385749,
-0.83993488, -0.72010177, -0.40384159, -0.91538875,  0.47845156]])

tf.random.normal((10, 10), mu, sigma)

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[-0.5782399 ,  0.6925044 , -0.25757247, -0.9818466 , -1.162834  ,
0.09520584,  1.239011  , -0.39943317,  0.29457876, -1.347272  ],
[ 0.22124176,  0.23621187,  1.6238133 , -0.23927781,  0.6302711 ,
-0.47449523,  1.2005731 ,  1.6169496 ,  0.6767891 , -2.0386577 ],
[-1.97832   , -1.1735806 ,  0.81817   ,  0.8048171 ,  0.7374495 ,
0.14954728,  1.6957926 , -0.05951545,  0.7055154 ,  0.5457118 ],
[ 1.0189906 ,  0.04332988,  2.0034375 ,  0.27378228,  1.7257762 ,
-2.61181   ,  0.1456913 ,  0.455217  ,  0.03366793,  1.7066848 ],
[-0.76928663,  0.13084716,  0.20099415,  0.23222515, -0.31574747,
-0.7776461 , -0.4249022 ,  0.6409535 ,  0.4799244 , -0.15538965],
[ 0.4489961 ,  0.09703924,  0.13132775, -0.47556046,  0.257628  ,
-1.9942493 ,  0.9173814 , -0.11729906,  1.1560944 , -0.2900136 ],
[-0.298577  , -0.15425491, -0.29060996,  1.9694098 , -2.1493902 ,
0.5926153 ,  0.18088488, -0.21830912, -1.0513645 , -0.5529293 ],
[-0.6848689 ,  0.57063514,  0.17949228, -0.1710157 , -1.7538719 ,
0.9257719 ,  0.26414004,  0.8745741 , -0.48321187, -0.29466563],
[-0.24861577, -0.1331137 , -1.1672456 ,  1.5880164 , -0.28820592,
-0.43423542, -0.36654526, -0.674431  , -1.25202   , -1.5960987 ],
[-1.5859227 , -0.474229  ,  0.74373263,  0.2929317 ,  0.32807645,
-1.9258341 ,  0.60911334, -0.78564906,  0.8394166 ,  1.200828  ]],
dtype=float32)>


## 22.8.7. Exponential Family¶

One shared property for all the distributions listed above is that they all belong to which is known as the exponential family. The exponential family is a set of distributions whose density can be expressed in the following form:

(22.8.20)$p(\mathbf{x} \mid \boldsymbol{\eta}) = h(\mathbf{x}) \cdot \mathrm{exp} \left( \boldsymbol{\eta}^{\top} \cdot T(\mathbf{x}) - A(\boldsymbol{\eta}) \right)$

As this definition can be a little subtle, let’s examine it closely.

First, $$h(\mathbf{x})$$ is known as the underlying measure or the base measure. This can be viewed as an original choice of measure we are modifying with our exponential weight.

Second, we have the vector $$\boldsymbol{\eta} = (\eta_1, \eta_2, ..., \eta_l) \in \mathbb{R}^l$$ called the natural parameters or canonical parameters. These define how the base measure will be modified. The natural parameters enter into the new measure by taking the dot product of these parameters against some function $$T(\cdot)$$ of $$\mathbf{x}= (x_1, x_2, ..., x_n) \in \mathbb{R}^n$$ and exponentiated. The vector $$T(\mathbf{x})= (T_1(\mathbf{x}), T_2(\mathbf{x}), ..., T_l(\mathbf{x}))$$ is called the sufficient statistics for $$\boldsymbol{\eta}$$. This name is used since the information represented by $$T(\mathbf{x})$$ is sufficient to calculate the probability density and no other information from the sample $$\mathbf{x}$$’s are required.

Third, we have $$A(\boldsymbol{\eta})$$, which is referred to as the cumulant function, which ensures that the above distribution (22.8.20) integrates to one, i.e.,

(22.8.21)$A(\boldsymbol{\eta}) = \log \left[\int h(\mathbf{x}) \cdot \mathrm{exp} \left(\boldsymbol{\eta}^{\top} \cdot T(\mathbf{x}) \right) d\mathbf{x} \right].$

To be concrete, let’s consider the Gaussian. Assuming that $$\mathbf{x}$$ is an univariate variable, we saw that it had a density of

(22.8.22)\begin{split}\begin{aligned} p(x \mid \mu, \sigma) &= \frac{1}{\sqrt{2 \pi \sigma^2}} \cdot \mathrm{exp} \left\{ \frac{-(x-\mu)^2}{2 \sigma^2} \right\} \\ &= \frac{1}{\sqrt{2 \pi}} \cdot \mathrm{exp} \left\{ \frac{\mu}{\sigma^2}x -\frac{1}{2 \sigma^2} x^2 - \left( \frac{1}{2 \sigma^2} \mu^2 +\log(\sigma) \right) \right\}. \end{aligned}\end{split}

This matches the definition of the exponential family with:

• underlying measure: $$h(x) = \frac{1}{\sqrt{2 \pi}}$$,

• natural parameters: $$\boldsymbol{\eta} = \begin{bmatrix} \eta_1 \\ \eta_2 \end{bmatrix} = \begin{bmatrix} \frac{\mu}{\sigma^2} \\ \frac{1}{2 \sigma^2} \end{bmatrix}$$,

• sufficient statistics: $$T(x) = \begin{bmatrix}x\\-x^2\end{bmatrix}$$, and

• cumulant function: $$A({\boldsymbol\eta}) = \frac{1}{2 \sigma^2} \mu^2 + \log(\sigma) = \frac{\eta_1^2}{4 \eta_2} - \frac{1}{2}\log(2 \eta_2)$$.

It is worth noting that the exact choice of each of above terms is somewhat arbitrary. Indeed, the important feature is that the distribution can be expressed in this form, not the exact form itself.

As we allude to in Section 4.1.2.2, a widely used technique is to assume that the final output $$\mathbf{y}$$ follows an exponential family distribution. The exponential family is a common and powerful family of distributions encountered frequently in machine learning.

## 22.8.8. Summary¶

• Bernoulli random variables can be used to model events with a yes/no outcome.

• Discrete uniform distributions model selects from a finite set of possibilities.

• Continuous uniform distributions select from an interval.

• Binomial distributions model a series of Bernoulli random variables, and count the number of successes.

• Poisson random variables model the arrival of rare events.

• Gaussian random variables model the result of adding a large number of independent random variables together.

• All the above distributions belong to exponential family.

## 22.8.9. Exercises¶

1. What is the standard deviation of a random variable that is the difference $$X-Y$$ of two independent binomial random variables $$X, Y \sim \mathrm{Binomial}(16, 1/2)$$.

2. If we take a Poisson random variable $$X \sim \mathrm{Poisson}(\lambda)$$ and consider $$(X - \lambda)/\sqrt{\lambda}$$ as $$\lambda \rightarrow \infty$$, we can show that this becomes approximately Gaussian. Why does this make sense?

3. What is the probability mass function for a sum of two discrete uniform random variables on $$n$$ elements?