⚙️ Setup¶
import numpy as np
rng = np.random.default_rng(421)
joint = np.array([[0.10, 0.05, 0.05],
[0.05, 0.20, 0.10],
[0.05, 0.10, 0.30]]) # rows = X in {0,1,2}, cols = Y in {0,1,2}
print("joint sums to", joint.sum())
joint sums to 1.0
print(f"P(X=2, Y=2) = {joint[2,2]:.2f}")
print(f"P(X=Y) = {np.trace(joint):.2f}")
P(X=2, Y=2) = 0.30 P(X=Y) = 0.60
Answer: P(X=2, Y=2) is the bottom-right cell, 0.30. P(X=Y) sums the diagonal: 0.10 + 0.20 + 0.30 = 0.60.
pX = joint.sum(axis=1); pY = joint.sum(axis=0)
print(f"P(X): {pX.round(2)}")
print(f"P(Y): {pY.round(2)}")
P(X): [0.2 0.35 0.45] P(Y): [0.2 0.35 0.45]
Answer: summing each row gives P(X) = [0.20, 0.35, 0.45]; summing each column gives P(Y) = [0.20, 0.35, 0.45]. Both marginals are valid distributions summing to 1.
pX = joint.sum(axis=1)
cond = joint[2]/pX[2]
print(f"P(Y | X=2) = {cond.round(3)}")
P(Y | X=2) = [0.111 0.222 0.667]
Answer: take the X=2 row [0.05, 0.10, 0.30] and divide by P(X=2)=0.45, giving P(Y | X=2) ≈ [0.111, 0.222, 0.667]. Given X=2, Y is most likely 2 as well, the variables are positively associated.
pX=joint.sum(axis=1); pY=joint.sum(axis=0)
indep = np.outer(pX,pY)
print(f"product of marginals[0,0] = {indep[0,0]:.3f}, actual joint[0,0] = {joint[0,0]:.3f}")
print(f"independent? {np.allclose(joint, indep)}")
product of marginals[0,0] = 0.040, actual joint[0,0] = 0.100 independent? False
Answer: the product of marginals gives 0.04 in the top-left cell, but the actual joint has 0.10, so the joint does not factor into the marginals. X and Y are dependent (they tend to move together, as the conditional in Challenge 3 showed).
cov=[[1,0.6],[0.6,1]]
xy=rng.multivariate_normal([0,0], cov, size=5000)
print(f"sample correlation = {np.corrcoef(xy.T)[0,1]:.3f} (target 0.6)")
print(f"sample covariance matrix:\n{np.cov(xy.T).round(3)}")
sample correlation = 0.587 (target 0.6) sample covariance matrix: [[0.995 0.579] [0.579 0.98 ]]
Answer: the recovered correlation is close to the target 0.6, and the off-diagonal of the sample covariance matrix is near 0.6 as well. Correlation is the standardized covariance, a unitless measure of how two variables move together, and it is read straight off the joint distribution.