Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section 2.2 — Multiple random variables

This notebook contains all the code examples from Section 2.2 Multiple random variables in the No Bullshit Guide to Statistics.

Notebook setup


[notice] A new release of pip is available: 26.0.1 -> 26.1.2
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>

Definitions

Loading...
<Figure size 500x300 with 1 Axes>
<Figure size 500x300 with 1 Axes>
0.3
[0.06 0.12 0.06 0.03 0.03]
[0.2 0.4 0.2 0.1 0.1]
Loading...

Marginal distributions

Conditional distributions

Joint probability distributions

Example 1: two coin tosses

Pr(heads,heads) = 0.25
Pr(heads,tails) = 0.25
Pr(tails,heads) = 0.25
Pr(tails,tails) = 0.25
Loading...
<Figure size 500x300 with 1 Axes>
<Figure size 500x300 with 1 Axes>

TODO: show tree diagram

0.75

Example 2: rolling two dice

Loading...
<Figure size 500x300 with 1 Axes>
<Figure size 500x300 with 1 Axes>
<Figure size 500x300 with 1 Axes>

Marginal distribution functions

Loading...
<Figure size 700x400 with 3 Axes>

Conditional distributions

<Figure size 720x300 with 3 Axes>

The conditional distribution fX∣Y(x∣y=b)f_{X|Y}(x|y=b) is obtained as follows.

X 1 0.2 2 0.4 3 0.2 4 0.1 5 0.1 Name: b, dtype: float64
<Figure size 720x300 with 3 Axes>

The conditional distribution fY∣X(y∣x=5)f_{Y|X}(y|x=5) is obtained as follows.

Y a 0.090909 b 0.272727 c 0.181818 d 0.454545 Name: 5, dtype: float64

Example 3: coin-dependent dice roll

Pr(1,heads) = 0.0833
Pr(2,heads) = 0.0833
Pr(3,heads) = 0.0833
Pr(4,heads) = 0.0833
Pr(5,heads) = 0.0833
Pr(6,heads) = 0.0833
Pr(1,tails) = 0.125
Pr(2,tails) = 0.125
Pr(3,tails) = 0.125
Pr(4,tails) = 0.125
Pr(5,tails) = 0.0
Pr(6,tails) = 0.0
1.0
[0.08333333333333333, 0.08333333333333333, 0.08333333333333333, 0.08333333333333333, 0.08333333333333333, 0.08333333333333333]
[0.125, 0.125, 0.125, 0.125, 0.0, 0.0]
[0.20833333333333331, 0.20833333333333331, 0.20833333333333331, 0.20833333333333331, 0.08333333333333333, 0.08333333333333333]
[0.20833333333333331, 0.20833333333333331, 0.20833333333333331, 0.20833333333333331, 0.08333333333333333, 0.08333333333333333]
0.9999999999999999
Loading...
<Figure size 500x300 with 1 Axes>
<Figure size 500x300 with 1 Axes>

Example 4: diagnostic test

Pr({T=0,V=0}) = 0.8 * 0.97 = 0.776
Pr({T=1,V=0}) = 0.2 * 0.97 = 0.194
Pr({T=0,V=1}) = 0.1 * 0.03 = 0.003
Pr({T=1,V=1}) = 0.9 * 0.03 = 0.027
<Figure size 500x300 with 1 Axes>
<Figure size 500x300 with 1 Axes>
Loading...
0.779
0.221
Pr({T=0}) = 0.779
Pr({T=1}) = 0.221

Bayes’ rule

Example 4: diagnostic test (continued)

We’re now interested in fV∣T(v∣t)f_{V|T}(v|t), which we can obtain using Bayes’ theorem.

fV∣T(v∣t)=fT∣V(t∣v)fV(v)fT∣V(t∣0)fV(0)+fT∣V(t∣1)fV(1).f_{V|T}(v|t) = \frac{ f_{T|V}(t|v) f_V(v) }{ f_{T|V}(t|0) f_V(0) + f_{T|V}(t|1) f_V(1)}.

We’ll now define the function fVgivenT that computes the numerator and denominator in this fraction, and returns the ratio.

The probability of the patient having the virus fV∣T(1∣t=1)f_{V|T}(1|t=1), given they tested positive is:

0.12217194570135746

Intuitively, this percentage is measuring the ratio of the probability of the outcome fVT(1,1)f_{VT}(1,1) divided by sum of the probabilities of all outcomes where positive test can occur: fVT(1,1)+fVT(0,1)f_{VT}(1,1)+f_{VT}(0,1).

0.12217194570135746

Multivariable expectations

Covariance

Correlation

Independent random variables

Formulas for independent random variables

Independent, identically distributed variables

Example 5: tossing a coin nn times

Example 6: rolling a die nn times

Discussion

Dependence and independence

Exercises