import matplotlib.pyplot as plt
import numpy as np
import scipy.stats
import seaborn as sns
from matplotlib.animation import FuncAnimation
import IPython.displayGraduate Quantitative Economics and Datascience
To formalize probability always be careful to separate
Probability space is a triple \((\Omega, {\mathcal{A}}, \mathbb{P})\):
Set \(\Omega\) of possible outcomes and \(\omega \in \Omega\) is a particular outcome
Subsets \(A \subseteq \Omega\) are events
\setminus) is event of not being eitherA probability measure \(\mathbb{P}\) assigning a probability to each event (next slides)
The collection of all possible events is \({\mathcal{A}}\) where \(A \in {\mathcal{A}}\)
Probability Measure is a function which assigns a numerical value on the likelihood of an event
For us, \(\mathbb{P}: {\mathcal{A}}\rightarrow [0,1]\)
Will see denoted as a function, \(\mu(A)\) for integrals in advanced uses
Random Variable: \(X(\omega)\) assigns a numerical value to a particular outcome
\(X : \Omega \rightarrow {\mathbb{R}}\), but could be vector or matrix valued
Or \(X(\omega = E) = \$40,000\) if employed, \(X(\omega = U) = \$15,000\) if unemployed. Useful for finding average incomes
Random variables defined on \(\Omega\), and inherit the probability measure
Probability measure \(\mathbb{P}\) on the measurable space \((\Omega, {\mathcal{A}})\) must satisfy axioms:
These imply other results such as:
Discrete probability spaces have a finite (or countable) number of outcomes
When convenient, we can number the outcomes arbitrarily as \(n = 1,\ldots N\) (or \(\infty\)) and then work with \(\Omega = \{1, \ldots N\}\) and \(\omega \in \Omega\)
Axioms especially simple because we use \(\mathbb{P}(\omega = n) = p_n\),
A random variable attaches a value \(x_n\) to each outcome. If several outcomes share a value, add their probabilities to get the probability of that value
\(X\) takes exactly three possible values, with probabilities
\[ 0.2,\quad 0.5,\quad p. \]
What must \(p\) be?
A. \(0.2\)
B. \(0.3\)
C. \(0.5\)
D. \(0.7\)
\[ p_n \equiv \mathbb{P}(X = x_n) \]
\[ F(t) \equiv \mathbb{P}(X \leq t) = \sum_{n:\,x_n \leq t} p_n \]
\[ \begin{aligned} {\mathbb{E}}[X] &= \sum_{n=1}^N x_n \mathbb{P}(X = x_n) = p^{\top} x\\ {\mathbb{E}}[f(X)] &= \sum_{n=1}^N f(x_n) \mathbb{P}(X = x_n) = p^{\top} f(x) \end{aligned} \]
E(X) = 34500.0
E(f(X)) = 182.2474487139159
Sorted x = [10000 15000 40000]
CDF(X) = [0.1 0.2 1. ]
Sort the values and their probabilities together before taking cumulative sums
scipy.stats.rv_discreterv_discrete takes distinct values and their probabilities; it sorts them internallyE(X) = 34500.0
E(f(X)) = 182.2474487139159
CDF(X) = [0.1 0.2 1. ]
Samples of X = [40000 40000 40000 40000 15000]

The binomial distribution counts successes in \(N\) independent trials, each with success probability \(\theta\). For \(n = 0, \ldots, N\),
\[ \begin{aligned} \mathbb{P}(X = n) &= \binom{N}{n} \theta^n (1-\theta)^{N-n}\\ {\mathbb{E}}[X] &= \sum_{n=0}^N n \binom{N}{n} \theta^n (1-\theta)^{N-n} = N \theta \end{aligned} \]
\[ \mathbb{P}(X \leq n) = \sum_{i=0}^n \binom{N}{i} \theta^i (1-\theta)^{N-i} \]

Take independent, identically distributed random variables \(X_1, X_2, \ldots\) with \({\mathbb{E}}|X_n| < \infty\) and \({\mathbb{E}}[X_n] = \mu\). A finite variance is not needed
The sample mean \(\bar{X}_N \equiv \frac{1}{N}\sum_{n=1}^N X_n\) is itself a random variable
The weak law says that for any \(\epsilon > 0\)
\[ \lim_{N\to\infty} \mathbb{P}(|\bar{X}_N - \mu| > \epsilon) = 0 \]
Powerful and frequently used, but remember the assumptions!
mu, sigma = 0, 1
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
for n_draws, ax in zip([20, 100], axes):
np.random.seed(42)
samples = np.random.normal(mu, sigma, n_draws)
t = np.arange(1, n_draws + 1)
sample_means = np.cumsum(samples) / t
ax.scatter(t, samples, label="Draws", alpha=0.6, s=10)
ax.plot(t, sample_means, label="Sample mean", lw=2)
ax.axhline(mu, color="r", linestyle="--", label="True mean")
ax.vlines(t, sample_means, samples, color="gray", lw=0.5)
ax.set(title=f"$N = {n_draws}$", xlabel="Draw number")
axes[0].legend()
plt.show()
\[ p(x) = \frac{\alpha x_m^{\alpha}}{x^{\alpha+1}},\quad x \geq x_m \]
n_draws = 100
np.random.seed(42)
dist = scipy.stats.pareto(1.5)
samples = dist.rvs(size=n_draws)
t = np.arange(1, n_draws + 1)
sample_means = np.cumsum(samples) / t
fig, ax = plt.subplots(figsize=(11, 5))
ax.scatter(t, samples, label="Draws", alpha=0.6, s=10)
ax.plot(t, sample_means, label="Sample mean", lw=2)
ax.axhline(dist.mean(), color="r", linestyle="--", label="True mean")
ax.vlines(t, sample_means, samples, color="gray", lw=0.5)
ax.set(xlabel="Draw number")
ax.legend()
plt.show()
The Central Limit Theorem (CLT) is a classic result in statistics
Again, assume IID observations with \({\mathbb{E}}[X_n] = \mu\) and \(0 < \operatorname{Var}[X_n] = \sigma^2 < \infty\)
Define the sample mean \(\bar{X}_N \equiv \frac{1}{N} \sum_{n=1}^N X_n\)
Then the CLT is
\[ \sqrt{N} \left( \bar{X}_N - \mu \right) \xrightarrow{d} {\mathcal{N}}(0, \sigma^2) \]
fig, ax = plt.subplots()
def update(n):
ax.clear()
data = dist.rvs((10000, n))
Y = np.sqrt(n) * (data.mean(axis=1) - mu)
ax.set_xlim(-3 * s, 3 * s)
ax.set_ylim(0, 0.5)
ax.hist(Y, bins=60, alpha=0.5, density=True)
ax.set_title(f"CLT for $N = {n}$")
x = np.linspace(-3 * s, 3 * s, 100)
pdf = scipy.stats.norm.pdf(x, 0, s)
ax.plot(x, pdf, "r-", lw=2, label=r"$\mathcal{N}(0, \sigma^2)$")
ax.legend()
frames = [1, 2, 3, 5, 10, 20, 50, 100, 500, 2000]
ani = FuncAnimation(fig, update, frames=frames, interval=500, repeat=False)
plt.close()
IPython.display.HTML(ani.to_html5_video())The observations are IID Pareto draws with \(\alpha = 1.5\).
The mean is finite, but the variance is infinite.
Which result applies to their sample mean?
A. Both the LLN and the usual finite-variance CLT
B. The usual CLT, but not the LLN
C. The LLN, but not the usual finite-variance CLT
D. Neither result
Let \(X,Y\) be two discrete random variables that take values:
\[ X\in\{1,\ldots,I\},\quad Y\in\{1,\ldots,J\} \]
Then their joint distribution is described by a matrix
\[ P\equiv[\mathbb{P}(X=i,Y=j)]_{i=1\ldots I, j=1,\ldots J}\in {\mathbb{R}}^{I\times J} \]
Which fulfills the key axioms of probability
\[ \begin{aligned} p_{ij}\equiv\mathbb{P}(X=i,Y=j) &\geq 0\\ \sum_{i=1}^{I}\sum_{j=1}^{J}p_{ij}=1 \end{aligned} \]
The joint distribution induces marginal distributions
\[ \begin{aligned} \mathbb{P}(X=i)& = \sum_{j=1}^{J}p_{ij} = \mu_i, \quad i=1,\ldots,I\\ \mathbb{P}(Y=j)&= \sum_{i=1}^{I}p_{ij} = \nu_j, \quad j=1,\ldots,J \end{aligned} \]
The marginal distributions are also probability distributions
\[ P = \begin{bmatrix} 0.05 & 0.07 & 0.02 & 0.01 \\ 0.04 & 0.10 & 0.06 & 0.03 \\ 0.08 & 0.09 & 0.07 & 0.04 \\ 0.02 & 0.03 & 0.02 & 0.01 \\ 0.09 & 0.08 & 0.04 & 0.05 \end{bmatrix},\quad \mu = \begin{bmatrix} 0.15 \\ 0.23 \\ 0.28 \\ 0.08 \\ 0.26 \end{bmatrix},\quad \nu = \begin{bmatrix} 0.28 & 0.37 & 0.21 & 0.14 \end{bmatrix} \]
For \(\mathbb{P}(B) > 0\), conditional probabilities are defined by
\[ \mathbb{P}(A \,|\, B)=\frac{\mathbb{P}(A \cap B)}{\mathbb{P}(B)} \]
\(A \cap B\) is the event that both \(A\) and \(B\) occur, i.e., the intersection
The conditional probability is the probability of \(A\) given \(B\) has occurred
For discrete random variables with \(\mathbb{P}(Y=j) > 0\), the conditional distribution is
\[ \mathbb{P}(X=i|Y=j)=\frac{p_{ij}}{\sum_{k=1}^{I}p_{kj}} =\frac{\mathbb{P}(X=i, Y=j)}{\mathbb{P}(Y=j)} \]
\[ \sum_{i=1}^{I}\mathbb{P}(X=i\,|\,Y=j) =\frac{\sum_{i=1}^{I}p_{ij}}{\sum_{k=1}^{I}p_{kj}}=1 \]
This table gives \(\mathbb{P}(X=i, Y=j)\).
| \(Y=1\) | \(Y=2\) | |
|---|---|---|
| \(X=1\) | 0.10 | 0.20 |
| \(X=2\) | 0.30 | 0.40 |
What is \(\mathbb{P}(X=1\,|\,Y=1)\)?
A. \(0.10\)
B. \(0.25\)
C. \(0.30\)
D. \(0.40\)
Law of Total Probability is a useful identity for conditional probabilities
Then for any event \(B\),
\[ \mathbb{P}(B) = \sum_{i=1}^{N} \mathbb{P}(B \cap A_i) = \sum_{i=1}^{N} \mathbb{P}(B \,|\, A_i) \mathbb{P}(A_i) \]
Random variables \(X\) and \(Y\) with marginals \(\mu\) and \(\nu\) are statistically independent if
\[ \mathbb{P}(X=i,Y=j)=\mu_i \nu_j,\text{ for all } i, j \]
When \(X\) and \(Y\) are independent and \(\nu_j = \mathbb{P}(Y=j) > 0\),
\[ \begin{aligned} \mathbb{P}(X=i\,|\,Y=j) &=\frac{\mathbb{P}(X=i, Y=j)}{\mathbb{P}(Y=j)} =\frac{\mu_i \nu_j}{\sum_{k=1}^{I}\mu_k \nu_j} =\frac{\mu_i \nu_j}{\nu_j\sum_{k=1}^{I}\mu_k}=\frac{\mu_i \nu_j}{\nu_j\times 1}=\mu_i \\ \end{aligned} \]
Thus independence implies the conditional distributions are the marginals:
Let \(X, Y, Z\) be discrete random variables; condition only on values with positive probability
Common notation for independence is
\[ \begin{aligned} X &\perp\!\!\!\perp Y\\ \mathbb{P}(X=x, Y=y) &= \mathbb{P}(X=x) \mathbb{P}(Y=y) \end{aligned} \]
Common notation for conditional independence
\[ \begin{aligned} X &\perp\!\!\!\perp Y \,|\, Z\\ \mathbb{P}(X=x, Y=y\,|\, Z=z) &= \mathbb{P}(X=x\,|\, Z=z) \mathbb{P}(Y=y\,|\, Z=z) \end{aligned} \]
Central to causal inference and treatment effects
Simpson’s paradox is a warning on composition effects
Let \(S\) be the event of admission, \(G\) gender, and \(D\) department. By the law of total probability
\[ \mathbb{P}(S \,|\, G) = \sum_{d} \mathbb{P}(S \,|\, G, D=d)\, \mathbb{P}(D=d \,|\, G) \]
So \(\mathbb{P}(S \,|\, G=\text{men}) > \mathbb{P}(S \,|\, G=\text{women})\) is consistent with \(\mathbb{P}(S \,|\, G=\text{men}, D=d) \leq \mathbb{P}(S \,|\, G=\text{women}, D=d)\) for every department \(d\), if women apply to departments with low admission rates
| Total Applicants | Admitted | Men Applicants | Men Admitted | Women Applicants | Women Admitted |
|---|---|---|---|---|---|
| 12,763 | 41% | 8,442 | 44% | 4,321 | 35% |
| Dept | All Applicants | Admitted | Men Applicants | Men Admitted | Women Applicants | Women Admitted |
|---|---|---|---|---|---|---|
| A | 933 | 64% | 825 | 62% | 108 | 82% |
| B | 585 | 63% | 560 | 63% | 25 | 68% |
| C | 918 | 35% | 325 | 37% | 593 | 34% |
| D | 792 | 34% | 417 | 33% | 375 | 35% |
| E | 584 | 25% | 191 | 28% | 393 | 24% |
| F | 714 | 6% | 373 | 6% | 341 | 7% |
Overall, \(\mathbb{P}(\text{Admitted} \,|\, \text{Men}) = 0.44\) and \(\mathbb{P}(\text{Admitted} \,|\, \text{Women}) = 0.35\)
But this is different when conditioning on departments!
Two groups apply to the same two departments.
Group A has a higher admission rate than group B within each department.
Must group A have the higher overall admission rate?
A. Yes: higher in every department means higher overall
B. No: the groups can apply to different departments in different proportions
C. No: group B must have the higher overall rate
D. The overall rates must be equal
For \(\mathbb{P}(A) > 0\) and \(\mathbb{P}(B) > 0\), Bayes’ Law gives
\[ \mathbb{P}(A \,|\, B)=\frac{\mathbb{P}(B \,|\, A)\mathbb{P}(A)}{\mathbb{P}(B)} \]
Sometimes:
\(A\) is the event of being unemployed, \(B\) is the event of getting a cash transfer
Let \(U\) mean unemployed and \(T\) mean receiving a cash transfer. Suppose
\(\mathbb{P}(U)=0.20\), \(\mathbb{P}(T\,|\,U)=0.80\), and \(\mathbb{P}(T\,|\,U^c)=0.20\).
1. Find the total probability of receiving a transfer:
\[ \begin{aligned} \mathbb{P}(T) &= \mathbb{P}(T\,|\,U)\mathbb{P}(U) + \mathbb{P}(T\,|\,U^c)\mathbb{P}(U^c) \\ &= 0.80(0.20) + 0.20(0.80) = 0.32. \end{aligned} \]
2. Apply Bayes’ law to find the unemployed share among recipients:
\[ \mathbb{P}(U\,|\,T) = \frac{\mathbb{P}(T\,|\,U)\mathbb{P}(U)}{\mathbb{P}(T)} = \frac{0.80(0.20)}{0.32} = 0.50. \]
So 50% of transfer recipients are unemployed.
For discrete random variables with \(\mathbb{P}(X=i) > 0\) and \(\mathbb{P}(Y=j) > 0\), Bayes’ Law gives
\[ \mathbb{P}(X=i\,|\,Y=j)=\frac{\mathbb{P}(X=i,Y=j)}{\mathbb{P}(Y=j)}=\frac{\mathbb{P}(Y=j\,|\,X=i)\mathbb{P}(X=i)}{\mathbb{P}(Y=j)} \]
If \(X\) and \(Y\) are independent
\[ \mathbb{P}(X=i\,|\,Y=j) = \mathbb{P}(X=i) \]
\[ {\mathbb{E}}[X\,|\,Y=j] = \sum_{i=1}^{I} i\, \mathbb{P}(X=i\,|\,Y=j) \]
If \(X\) and \(Y\) are independent then
Let \(A, B\) be constants and \(X, Y\) random variables, with compatible dimensions and finite expectations for the expressions below
Expectations are linear operators, which gives us some useful properties
\({\mathbb{E}}[X Y] \neq {\mathbb{E}}[X] {\mathbb{E}}[Y]\) in general
\({\mathbb{E}}[f(X)] = f({\mathbb{E}}[X])\) is not guaranteed for nonlinear \(f\)
Jensen’s Inequality: If \(f(\cdot)\) is a convex function, then \({\mathbb{E}}[f(X)] \geq f({\mathbb{E}}[X])\)
\(X\) equals \(0\) or \(2\), each with probability \(1/2\).
What are \({\mathbb{E}}[X]\) and \({\mathbb{E}}[X^2]\)?
A. \({\mathbb{E}}[X]=1\), \({\mathbb{E}}[X^2]=1\)
B. \({\mathbb{E}}[X]=1\), \({\mathbb{E}}[X^2]=2\)
C. \({\mathbb{E}}[X]=2\), \({\mathbb{E}}[X^2]=2\)
D. \({\mathbb{E}}[X]=2\), \({\mathbb{E}}[X^2]=4\)
Let \(\{A_1, \ldots, A_N\}\) partition \(\Omega\), with \(\mathbb{P}(A_i) > 0\) and \({\mathbb{E}}|X| < \infty\)
Law of Total Expectations
\[ {\mathbb{E}}[X] = \sum_{i=1}^{N} {\mathbb{E}}[X \,|\, A_i] \mathbb{P}(A_i) \]
Same properties all hold e.g. \({\mathbb{E}}[A X + B Y \,|\, Z] = A {\mathbb{E}}[X \,|\, Z] + B {\mathbb{E}}[Y \,|\, Z]\)
\({\mathbb{E}}[X \,|\, Y]\) is a function of \(Y\), so it is itself a random variable
Law of Iterated Expectations
\[ {\mathbb{E}}\left[{\mathbb{E}}[X\,|\,Y]\right] = {\mathbb{E}}[X] \]
Income \(X\) has this distribution:
| Income | Probability |
|---|---|
| \(\$10{,}000\) | \(0.10\) |
| \(\$15{,}000\) | \(0.10\) |
| \(\$40{,}000\) | \(0.80\) |
What is \(\mathbb{P}(X \leq \$15{,}000)\)?
A. \(0.10\)
B. \(0.20\)
C. \(0.80\)
D. \(0.90\)
This table gives \(\mathbb{P}(X=i, Y=j)\).
| \(Y=1\) | \(Y=2\) | |
|---|---|---|
| \(X=1\) | 0.10 | 0.40 |
| \(X=2\) | 0.10 | 0.40 |
Are \(X\) and \(Y\) independent?
A. Yes: every joint probability equals the product of its marginals
B. No: \(X\) and \(Y\) have different marginal distributions
C. No: the four joint probabilities are not equal
D. We cannot tell from a joint table
Monthly income \(X\) is in thousands of dollars.
| Group | Population share | Mean income |
|---|---|---|
| Unemployed | \(0.25\) | \(1\) |
| Employed | \(0.75\) | \(3\) |
What is \({\mathbb{E}}[X]\)?
A. \(2\)
B. \(2.5\)
C. \(3\)
D. \(4\)
np.set_printoptions(precision=3)
P = np.array([[0.05, 0.07, 0.02, 0.01],
[0.04, 0.1, 0.06, 0.03],
[0.08, 0.09, 0.07, 0.04],
[0.02, 0.03, 0.02, 0.01],
[0.09, 0.08, 0.04, 0.05]])
I, J = P.shape
print(f"I = {I}, J = {J}")
print(f"sum = 1? {np.isclose(P.sum(), 1.0)}")
print(f"p_ij >= 0? {np.all(P >= 0)}")
margin_x = P.sum(axis=1) # sum over j
margin_y = P.sum(axis=0) # sum over i
print(f"P(X=i) = {margin_x}")
print(f"Sum_i P(X=i) = {margin_x.sum()}")
print(f"P(Y=j) = {margin_y}")
print(f"Sum_j P(Y=j) = {margin_y.sum()}")I = 5, J = 4
sum = 1? True
p_ij >= 0? True
P(X=i) = [0.15 0.23 0.28 0.08 0.26]
Sum_i P(X=i) = 0.9999999999999999
P(Y=j) = [0.28 0.37 0.21 0.14]
Sum_j P(Y=j) = 1.0
# cond_x_y[j, i] = P(X=i | Y=j);
cond_x_y = (P / margin_y).T # shape (J, I)
# cond_y_x[i, j] = P(Y=j | X=i)
cond_y_x = (P.T / margin_x).T # shape (I, J)
print(f"P(X=·|Y=1)=\n{cond_x_y[0, :]}")
print(f"P(X=·|Y=2)=\n{cond_x_y[1, :]}")
print(f"sum_i P(X=i|Y=2)={cond_x_y[1, :].sum():.2f}")
print(f"P(Y=·|X=1)=\n{cond_y_x[0, :]}")
print(f"sum_j P(Y=j|X=1)={cond_y_x[0, :].sum():.2f}")P(X=·|Y=1)=
[0.179 0.143 0.286 0.071 0.321]
P(X=·|Y=2)=
[0.189 0.27 0.243 0.081 0.216]
sum_i P(X=i|Y=2)=1.00
P(Y=·|X=1)=
[0.333 0.467 0.133 0.067]
sum_j P(Y=j|X=1)=1.00
margin_x = P.sum(axis=1)
margin_y = P.sum(axis=0)
cond_x_y = (P / margin_y).T
cond_y_x = (P.T / margin_x).T
# Give RV values to states
vals_x = np.arange(I) + 1
vals_y = np.arange(J) + 1
y_cond = 0 # Y = 1
E_x_y1 = cond_x_y[y_cond, :] @ vals_x
# non-broadcasted version
# E_x_y1 = sum(vals_x[i] * cond_x_y[y_cond, i]
# for i in range(I))
print(f"E(X | Y = {y_cond+1})={E_x_y1}")E(X | Y = 1)=3.214285714285715
# Vectorized version of E[X|Y=j] for all j
E_x_y = (cond_x_y * vals_x).sum(axis=1) # shape (J,)
E_y_x = (cond_y_x * vals_y).sum(axis=1) # shape (I,)
# Law of Iterated Expectations:
# E_X should equal sum_j E[X|Y=j] P(Y=j)
E_X_direct = vals_x @ margin_x
E_X_iter = E_x_y @ margin_y # sum over j
print("E(X | Y = j) =", E_x_y)
print("E(Y | X = i) =", E_y_x)
print(f"E(X) = {E_X_direct:.3g}")
print(f"E(E(X|Y)) = {E_X_iter:.3g}")E(X | Y = j) = [3.214 2.865 3. 3.429]
E(Y | X = i) = [1.933 2.348 2.25 2.25 2.192]
E(X) = 3.07
E(E(X|Y)) = 3.07
\[ \mathbb{P}(X=1\,|\,Y=2) = \frac{\mathbb{P}(Y=2\,|\,X=1)\mathbb{P}(X=1)}{\mathbb{P}(Y=2)} \]
x = 1
y = 2
p_y_x = cond_y_x[x-1, y-1]
p_x = margin_x[x-1]
p_y = margin_y[y-1]
p_x_y = cond_x_y[y-1, x-1]
p_bayes = p_y_x * p_x / p_y
print(f"P(Y={y}|X={x}) = {p_y_x:.2g}")
print(f"P(X={x}) = {p_x:.2g}")
print(f"P(Y={y}) = {p_y:.2g}")
print(f"P(X={x}|Y={y})={p_x_y:.2g}")
print(f"P(Y={y}|X={x})P(X={x})\
/P(Y={y})={p_bayes:.2g}")P(Y=2|X=1) = 0.47
P(X=1) = 0.15
P(Y=2) = 0.37
P(X=1|Y=2)=0.19
P(Y=2|X=1)P(X=1)/P(Y=2)=0.19
[x[i, i+1] for i in range(5)]), or a combination are usually clearer and often fast enough