
Math 183 • Statistical Methods • Spring 2025
% % \providecommand{}{p_{0}} \providecommand{}{p_{1}} \providecommand{}{p_{2}} \providecommand{}{p_{12}} \providecommand{1n}{p_{1n}} % % % % $$
Random variable
A random variable is a variable which assumes values based on the outcome of a trial from a random phenomenon.
Tip
Think of a random variable as a placeholder for the different outcomes we can witness from a trial.
Support
The support of a random variable is the universe of all possible values a random variable can assume.
Every random variable has:
A support, \(\text{supp}(X)\)
A probability distribution \({\mathbb P}_X\).
Measures of central tendency and dispersion.
We will frequently encounter:
\[ X \sim \texttt{Name}(\theta) \]
Here:
Uniform Random Variable
A uniform random variable \(X\) is a random variable wherein a finite number of values are equally likely to be observed. This is denoted by \(X \sim {\text{Unif}}\left\{a, b\right\}\)
Support
\[\text{supp}(X) = \left\{a, a+1, a+2, \dots, b-1, b\right\}\]
Probability Mass Function
\[ {\mathbb P}(X=x) = \frac 1{b-a+1} \quad \text{ for all } x \in \text{supp}(X) \]
Expected Value and Variance
\[ \begin{aligned} {\mathbb E}(X) = \frac{a+b}{2} \quad {\text{Var}}(X) = \frac{(b-a+1)^2-1}{12}\\ \end{aligned} \]
Die Roll
Extension to non-quantitative support
Bernoulli Random Variable
When a random variable \(X\) can only take two possible outcomes, it is referred to as a Bernoulli random variable. This is denoted by \(X \sim {\text{Ber}}(p)\)
\[\begin{aligned}X\end{aligned} = \begin{cases} 1 & \text{with probability } p\\ 0 & \text{with probability } 1-p \end{cases} \]
Examples
Expectation and Variance
If \(X \sim {\text{Ber}}(\theta)\) then \(p_{X}(x) = p^x (1-p)^{1-x}\) and \[\begin{aligned} {\mathbb E}(X) &= (p \times 1) + \left((1-p) \times 0\right) = p\\\\ {\mathbb E}(X^2) &= (p \times 1^2) + \left((1-p) \times 0^2\right) = p\\\\ {\text{Var}}(X) &= {\mathbb E}(X^2) - {\mathbb E}(X)^2\\ &= p - p^2\\ &= p(1-p) \end{aligned}\]
Let \(X_1\) and \(X_2\) be two \(iid\) \({\text{Ber}}(p)\) random variables
Let \(Y = X_1 + X_2\). What is the probability distribution of \(Y\)?
The support is \(\text{supp}(Y) = \left\{0, 1, 2\right\}\)
\[\begin{aligned}{\mathbb P}(Y=0) &= {\mathbb P}\left(\left\{X_1=0\right\} \cap \left\{X_2=0\right\}\right)\\ &= {\mathbb P}(X_1=0) \times {\mathbb P}(X_2=0) = (1-p)^2\end{aligned}\]
\[\begin{aligned}{\mathbb P}(Y=2) &= {\mathbb P}\left(\left\{X_1=1\right\} \cap \left\{X_2=1\right\}\right)\\ &= {\mathbb P}(X_1=1) \times {\mathbb P}(X_2=1) = p^2\end{aligned}\]
\[\begin{aligned}
{\mathbb P}(Y=1)
&= {\mathbb P}\left(\left\{X_1=0 \cap X_2 = 1\right\} \cup \left\{X_1=0 \cap X_2 = 1\right\}\right)\\
&= {\mathbb P}(X_1=0) \times {\mathbb P}({X_2 = 1}) + {\mathbb P}({{X_1=0}}) \times {\mathbb P}({X_2 = 1})\\
&= (1-p) \times p + p \times (1-p)\\ & = 2p(1-p)
\end{aligned}\]
Let \(X_1\) and \(X_2\) be two \(iid\) \({\text{Ber}}(p)\) random variables
Let \(Y = X_1 + X_2\). What is the probability distribution of \(Y\)?
The support is \(\text{supp}(Y) = \left\{0, 1, 2\right\}\)
PMF for \(Y\)
The PMF for \(Y\) is given by \[ p_{Y}(y) = \begin{cases} (1-p)^2 & y=0\\ 2p(1-p) & y=1\\ p^2 & y=2 \end{cases} \]
Binomial Random Variable
\(X \sim {\text{Bin}}(n, p)\) is called a Binomial random variable it represents the # of successes in \(n\) \(iid\) trials from a \({\text{Ber}}(p)\) random variable, i.e., for \(Z_1, \dots Z_n {\stackrel{iid}{\sim}}Ber(p)\) \[ X = \# \left\{Z_i=1 : 1 \le i \le n\right\} = \sum_{i=1}^n Z_i \]
Example
\(X \sim {\text{Bin}}(n, p)\)
Support
\(\text{supp}(X) = \left\{0, 1, 2, \dots, n\right\}\)
Probability Mass Function
\[ {\mathbb P}(X=k) = {n\choose{k}} p^k (1-p)^{n-k} \quad 0 \le k \le n \]
Expected Value and Variance
\[ \begin{aligned} {\mathbb E}(X) &= n \times p\\ {\text{Var}}(X) &= n \times p(1-p)\\ \end{aligned} \]
Gilman Parking Structure
You decide to drive to campus, and now you have to deal with the daunting task of finding parking. With probability \(p=0.2\) a parking spot in the Gilman parking structure is empty. Let \(X\) be the random variable indicating the # of parking spots you need to visit before you finally find one that is empty. What the probability distribution of \(X\)?
Geometric Random Variable
A Geomteric random variable \(X \sim {\text{Geo}}(p)\) represents the number of trials it takes to observe the first success
Support
\[\text{supp}(X) = \left\{1, 2, 3, \dots\right\}\]
Probability Mass Function
\[ {\mathbb P}(X=k) = (1-p)^{k-1} p \]
Expected Value and Variance
\[ \begin{aligned} {\mathbb E}(X) = \frac 1p \quad {\text{Var}}(X) = \frac{1-p}{p^2}\\ \end{aligned} \]
Tip
You keep tossing a coin. If \(X\) denotes the number of coin tosses required to get a \(H\), then \(X \sim {\text{Geo}}({{\frac{1}{2}}})\).
Tip
Each bulb in a factory is defective with probability \(p\). If \(X\) is the number of bulbs you need to test to find a defective bulb, then \(X \sim {\text{Geo}}(p)\)
Tip
You are applying for summer internships, and with probability \(p=0.2\) a firm you have applied to gets back to you with an interview. Let \(X\) be the random variable which represents the number of firms you need to apply to before you get a call back for an interview. Then \(X \sim {\text{Geo}}(0.2)\)?
Poisson Random Variable
Suppose some event is known to occur on an ``average” \(\lambda\) times in every fixed unit of time. Then a Poisson random variable, \(X \sim {\text{Poi}}(\lambda)\), expresses the probability of a given number events actually observed in this fixed unit of time.
Example
Support
\[\text{supp}(X) = \left\{0, 1, 2, 3, \dots\right\}\]
Probability Mass Function
\[ {\mathbb P}(X=k) = \frac{\lambda^k \cdot e^{-\lambda}}{k!} \]
Expected Value and Variance
\[ \begin{aligned} {\mathbb E}(X) = \lambda, \quad {\text{Var}}(X) = \lambda\\ \end{aligned} \]
Example
You to pick an integer, \(X\), uniformly at random from \(1\) to \(10\). \(X \sim {\text{Unif}}\left\{1,10\right\}\), \[p_{X}(3) = {\mathbb P}(X=3) = 1/10.\]
Example
You to pick any number, \(X\), uniformly at random from \(1\) to \(10\). Then
\(p_{X}(3) = {\mathbb P}(X=3) =\)\(0\)
Example
You to pick any number, \(X\), uniformly at random from \(1\) to \(10\). Then, \[p_{X}(3) = {\mathbb P}(X=3) = 0\]
Since it is uniform we can still (meaningfully) assign probability to the events like \[\left\{X \le 5.5\right\}\]
In particular, the CDF \(F_{X}(x)\) can still be computed \[F_{X}(5.5) = {\mathbb P}({X \le 5.5}) = {{\frac{1}{2}}}\]
Discrete Uniform \[{\text{Unif}}\left\{1, 10\right\}\]

Continuous Uniform \[{\text{Unif}}\left(1, 10\right)\]


\(F_{X}(2) = {\mathbb P}(X \le 2) = \frac{1}{9}\)

\(F_{X}(5) = {\mathbb P}(X \le 5) = \frac{4}{9}\)

\(F_{X}(5.5) = {\mathbb P}(X \le 5.5) = \frac{1}{2}\)

\(F_{X}(7) = {\mathbb P}(X \le 7) = \frac{6}{9}\)

\(F_{X}(10) = {\mathbb P}(X \le 10) = \frac{9}{9} = 1\)
\[ F_{X}(x) = \begin{cases} 0 & \text{ if }x \le 1\\ \\ \frac{x-1}{9} & \text{ if }1 < x \le 10\\ \\ 1 & \text{ if } 10 < x \end{cases} \]
\[ F_{X}(x) = \int_{1}^{x} f_{X}(x) dx = \int_{1}^{x} \frac{1}{9} dx \quad \quad \text{if } 1 < x \le 10 \]
where \(f_{X}(x) = 1/9\) is the probability density function
Definition: Probability Density Function (PDF)
For a continuous random variable \(X\), its probability density function (PDF), denoted by \(f_X(x)\), is a function that describes the likelihood of \(X\) taking on a specific set of values. The probability of the event \(\left\{X \le a\right\}\) is given by: \[ F_{X}(a) = P(X \leq a) = \int\limits_{-\infty}^a f_X(x) \, dx \]
Support
The support of a continuous random variable \(X\) is the set of values where the PDF is strictly positive, i.e., \[ \text{supp}(X) = \left\{x: f_{X}(x) \color{red}{>} 0\right\}. \]
Properties of the Probability Density Function (PDF)
Non-negativity: The PDF is always non-negative for all values of \(x\): \[ f_X(x) \geq 0 \]
Area Under the Curve: The total area under the curve of the PDF over its entire range is equal to 1: \[ \int_{-\infty}^{\infty} f_X(x) \, dx = 1 \]
Probabilities from Intervals: The probability that the random variable \(X\) lies in the interval \([a, b]\) is given by the area under the PDF curve from \(a\) to \(b\): \[ P(a \leq X \leq b) = \int_a^b f_X(x) \, dx \]
No Point Probability: For continuous random variables, the probability at any specific point is zero: \[ P(X = x) = 0 \]
The Fundamental Theorem of Calculus states that differentiation and integration are inverse operations. Specifically, if \(G(x)\) is an antiderivative of \(g(x)\) on an interval \([a, b]\), then:
\[ \int_a^b g(x) \, dx = G(b) - G(a) \]
Important
From the fundamental theorem of calculus: The rate of change (or the derivative) of the CDF with respect to \(x\) is the PDF: \[ f_X(x) = \frac{d}{dx} F_X(x) \]
Upper \(\alpha\)-quantile or Upper \(100\times \alpha\%\) percentile
For \(\alpha \in (0, 1)\), the upper \(\alpha\)-quantile of a distribution \(F\), is the value \(q_\alpha\) such that: \[ \begin{aligned} 1-F_X(q_\alpha) = {\mathbb P}(X > q_\alpha) = \alpha\quad\text{equivalently}\quad q_\alpha = F_X^{-1}(1-\alpha) \end{aligned} \]
#| standalone: true
#| viewerHeight: 600
#| components: viewer
#| layout: vertical
import numpy as np
import scipy
from scipy import stats
import scipy.stats as stats
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from shiny import App, render, ui
from matplotlib.patches import Patch
# Generate a random sample
# Define the UI
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.sidebar(
ui.input_slider(
"alpha",
"alpha",
min = 1e-2,
max = 1-1e-2,
value = 0.5,
step = 1e-2,
ticks=True,
animate=False
)
),
ui.output_plot("plots", height="500px")
)
)
# Define the server logic
def server(input, output, session):
@output
@render.plot
def plots():
X = stats.gamma(a=2, scale=1)
min_X, max_X = X.ppf((1e-5, 1-1e-5))
x = np.linspace(min_X-1, max_X+1, 500)
alpha = input.alpha()
q_alpha = X.ppf(1-alpha)
# Create figure and axes
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
# Histogram Plot
ax[0].plot(x, X.pdf(x), lw=1, label='')
ax[0].set_ylabel('PDF')
ax[0].fill_between(x, 0, X.pdf(x), where=(x > q_alpha), alpha=0.5)
ax[0].set_title(rf'$P(X > q_\alpha)$={1-X.cdf(q_alpha): .2f}');
ax[0].axvline(q_alpha, color='red', linestyle='--')
ax[1].plot(x, X.cdf(x), lw=1, label='')
ax[1].set_ylabel('CDF')
ax[1].axhline(X.cdf(q_alpha), color='black', linestyle='--', lw=0.5)
ax[1].axvline(q_alpha, color='red', linestyle='--')
ax[1].set_title(rf'$q_\alpha$={q_alpha: .3f}')
plt.tight_layout()
return fig
# Create the Shiny app
app = App(app_ui, server)
app
\[F_{X}(x) = {\mathbb P}(X \le x) = \sum_{y \le x} p_{X}(y)\]
\[{\mathbb E}(X) = \sum\limits_{\text{supp}(X)} x \cdot p_{X}(x)\]
\[{\mathbb E}(X^2) = \sum\limits_{\text{supp}(X)} x^2 \cdot p_{X}(x)\]
\[{\text{Var}}(X) = {\mathbb E}(X^2) - {\mathbb E}(X)^2\]
\[F_{X}(x) = {\mathbb P}(X \le x) = \int_{-\infty}^x f_{X}(x)dx\]
\[{\mathbb E}(X) = \int_{-\infty}^{\infty} x \cdot f_{X}(x) dx\]
\[{\mathbb E}(X^2) = \int_{-\infty}^{\infty} x^2 \cdot f_{X}(x) dx\]
\[{\text{Var}}(X) = {\mathbb E}(X^2) - {\mathbb E}(X)^2\]
Note
Consider a continuous random variable \(X\) with the following probability density function (PDF): \[ f_X(x) = \begin{cases} Cx^2 & \text{for } 0 < x \le 1 \\ C(2-x) & \text{for } 1 < x \le 2 \\ 0 & \text{otherwise} \end{cases} \]
For \(f_X(x)\) to be a valid probability density function, it must satisfy:
\(\int_{-\infty}^{\infty} f_X(x) \, dx = 1, \quad \text{i.e.,}\)
\[ \newcommand{\phn}{\phantom{\frac 11\!\!\!\!}} \begin{aligned} 1=& \int_0^1 Cx^2 dx + \int_1^2 C(2-x)dx\\ =& C \left[\phn\color{red}{\frac{x^3}{3}}\right]_0^1 + C^2\left[\phn\color{red}{2x - \frac{x^2}{2}}\right]_1^2 \\ =& C \left[\phn\color{red}{\frac{1^3}{3}- \frac{0^3}{3}}\right]_0^1 + C\left[\phn\color{red}{(4 - \frac{2^2}{2})-(2 - \frac{1^2}{2})}\right]_1^2 \\ =& \frac{C}{3} + \frac{C}{2} \\ \therefore C &= \frac{6}{5} = 1.2 \end{aligned}\]
\[\begin{aligned} F_{X}(1.5) &= \int_{-\infty}^{1.5}f_{X}(x)dx\\ &= \int_{-\infty}^{0}0dx + \int_0^{1}Cx^2dx + \int_{1}^{1.5}C(2-x)dx\\ &= 0 + C\left(\color{red}{\frac{1}{3}-\frac{0}{3}}\right) + C\left(\color{red}{\left(2 \times 1.5 - \frac{1.5^2}{2}\right)-\left(2 - \frac{1^2}{2}\right)}\right)dx\\ &= C \times \color{red}{\frac{1}{3}} + C \times \color{red}{0.375}\\ &= 1.2 \times 0.708\\ &= 0.85 \end{aligned}\]
\[\begin{aligned} {\mathbb E}(X) &= \int_{-\infty}^{\infty}x \cdot f_{X}(x)dx\\ &= \int_{-\infty}^{0}0dx + \int_0^{1}Cx \cdot x^2dx + \int_{1}^{2}Cx(2-x)dx + \int_{2}^\infty 0dx\\ &= C\left[\color{red}{\frac{x^4}{4}}\right]_0^1 + C\left[\color{red}{\frac{2x^2}{2}-\frac{x^3}{3}}\right]_1^2\\ &= C\left[\color{red}{\frac{1}{4}}\right] + \left[\color{red}{{4-1}-\frac{8-1}{3}}\right]\\ &= 1.2\times \left[\color{red}{\frac{1}{4}}\right] + 1.2 \times \left[\color{red}{\frac{2}{3}}\right]\\ \therefore {\mathbb E}(X) &= 1.1 \end{aligned}\]
\[\begin{aligned} {\mathbb E}(X^2) &= \int_{-\infty}^{\infty}x^2 \cdot f_{X}(x)dx\\ &= \int_0^{1}Cx^2 \cdot x^2dx + \int_{1}^{2}Cx^2(2-x)dx\\ &= C\left[\color{red}{\frac{x^5}{5}}\right]_0^1 + C\left[\color{red}{\frac{2x^3}{3}-\frac{x^4}{4}}\right]_1^2\\ &= C\left[\color{red}{\dots}\right] + C\left[\color{red}{\dots}\right]\\ \end{aligned}\]
Then
\[ {\text{Var}}(X) = {\mathbb E}(X^2)-{\mathbb E}(X)^2. \]
Definition
\(X \sim {\text{Unif}}(a, b)\) is a continuous Uniform random variable with the PDF given by \[ f_{X}(x) = \begin{cases} \frac{1}{b-a} & \text{if } a \le x \le b\\ 0 & \text{otherwise } \end{cases} \]
Support
\[ \text{supp}(X) = [a, b] = \left\{x: 0 \le x \le b\right\} \]
CDF
\[\begin{aligned} F_X(x) = {\mathbb P}(X \le x) = \begin{cases} 0 & x \le a\\ \frac{x-a}{b-a} & a < x \le b\\ 1 & b < x \end{cases} \end{aligned}\]
Expected Value and Variance
\[ {\mathbb E}(X) = \frac{b+a}{2} \quad {\text{Var}}(X) = \frac{(b-a)^2}{12} \]
#| standalone: true
#| viewerHeight: 600
#| components: viewer
#| layout: vertical
import numpy as np
import scipy
from scipy import stats
import scipy.stats as stats
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from shiny import App, render, ui
from matplotlib.patches import Patch
# Generate a random sample
# Define the UI
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.sidebar(
ui.input_slider(
"alpha",
"alpha",
min = 1e-2,
max = 1-1e-2,
value = 0.5,
step = 1e-2,
ticks=True,
animate=False
),
ui.input_slider(
"b",
"b",
min = 0.25,
max = 2.5,
value = 1.5,
step = 0.25,
ticks=True,
animate=False
),
),
ui.output_plot("plots", height="500px")
)
)
# Define the server logic
def server(input, output, session):
@output
@render.plot
def plots():
b = input.b()
Z = stats.uniform()
X = stats.uniform(loc=0, scale=b)
min_X, max_X = X.ppf((1e-5, 1-1e-5))
x = np.linspace(min_X-1, max_X+1, 500)
alpha = input.alpha()
q_alpha = X.ppf(1-alpha)
# Create figure and axes
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
# Histogram Plot
ax[0].plot(x, X.pdf(x), lw=1, label='')
ax[0].plot(x, Z.pdf(x), lw=1, label='',color='black', linestyle='--')
ax[0].set_ylabel('PDF')
ax[0].fill_between(x, 0, X.pdf(x), where=(x > q_alpha), alpha=0.5)
ax[0].set_title(rf'$P(X > q_\alpha)$={1-X.cdf(q_alpha): .2f}');
ax[0].axvline(q_alpha, color='red', linestyle='--')
ax[0].set_xlim(-1, 5)
ax[0].set_ylim(0, 4)
ax[1].plot(x, X.cdf(x), lw=1, label='')
ax[1].plot(x, Z.cdf(x), lw=1, label='', color='black', linestyle='--')
ax[1].set_ylabel('CDF')
ax[1].axhline(X.cdf(q_alpha), color='black', linestyle='--', lw=0.5)
ax[1].axvline(q_alpha, color='red', linestyle='--')
ax[1].set_title(rf'$q_\alpha$={q_alpha: .3f}')
ax[1].set_xlim(-1, 5)
plt.tight_layout()
return fig
# Create the Shiny app
app = App(app_ui, server)
app
Example
If a manufacturer claims a battery has a life of 50 to 70 hours, and any duration within this range is equally likely, then the battery life is a continuous uniform random variable \(X \sim {\text{Unif}}(50, 70)\).
Definition
\(X \sim {\text{Exp}}(\lambda)\) is a continuous exponential random variable with the PDF given by: \[ f_X(x) = \begin{cases} \lambda e^{-\lambda x} & \text{if } x \ge 0\\ 0 & \text{otherwise} \end{cases} \] Where \(\lambda > 0\) is called the rate parameter.
Support
\[ \text{supp}(X) = [0, \infty) = \{x: x \ge 0\} \]
CDF
\[\begin{aligned} F_X(x) = {\mathbb P}(X \le x) = \begin{cases} 1 - e^{-\lambda x} & x \ge 0\\ 0 & x < 0 \end{cases} \end{aligned}\]
Expected Value and Variance
\[ {\mathbb E}(X) = \frac{1}{\lambda} \quad {\text{Var}}(X) = \frac{1}{\lambda^2} \]
#| standalone: true
#| viewerHeight: 600
#| components: viewer
#| layout: vertical
import numpy as np
import scipy
from scipy import stats
import scipy.stats as stats
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from shiny import App, render, ui
from matplotlib.patches import Patch
# Generate a random sample
# Define the UI
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.sidebar(
ui.input_slider(
"alpha",
"alpha",
min = 1e-2,
max = 1-1e-2,
value = 0.5,
step = 1e-2,
ticks=True,
animate=False
),
ui.input_slider(
"lam",
"lambda",
min = 0.25,
max = 2.5,
value = 1.5,
step = 0.25,
ticks=True,
animate=False
),
),
ui.output_plot("plots", height="500px")
)
)
# Define the server logic
def server(input, output, session):
@output
@render.plot
def plots():
lam = input.lam()
Z = stats.expon()
X = stats.expon(scale=1/lam)
min_X, max_X = X.ppf((1e-5, 1-1e-5))
x = np.linspace(min_X-1, max_X+1, 500)
alpha = input.alpha()
q_alpha = X.ppf(1-alpha)
# Create figure and axes
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
# Histogram Plot
ax[0].plot(x, X.pdf(x), lw=1, label='')
ax[0].plot(x, Z.pdf(x), lw=1, label='',color='black', linestyle='--')
ax[0].set_ylabel('PDF')
ax[0].fill_between(x, 0, X.pdf(x), where=(x > q_alpha), alpha=0.5)
ax[0].set_title(rf'$P(X > q_\alpha)$={1-X.cdf(q_alpha): .2f}');
ax[0].axvline(q_alpha, color='red', linestyle='--')
ax[0].set_xlim(-1, 5)
ax[0].set_ylim(0, 4)
ax[1].plot(x, X.cdf(x), lw=1, label='')
ax[1].plot(x, Z.cdf(x), lw=1, label='', color='black', linestyle='--')
ax[1].set_ylabel('CDF')
ax[1].axhline(X.cdf(q_alpha), color='black', linestyle='--', lw=0.5)
ax[1].axvline(q_alpha, color='red', linestyle='--')
ax[1].set_title(rf'$q_\alpha$={q_alpha: .3f}')
ax[1].set_xlim(-1, 5)
plt.tight_layout()
return fig
# Create the Shiny app
app = App(app_ui, server)
app
Example
Consider a car rental shop where cars are rented at a constant rate of 3 cars per hour. The time between successive car rentals can be modeled as an exponential random variable (X (3)).
Definition
A random variable \(X \sim \mathcal{N}(\mu, \sigma^2)\) is said to follow a normal distribution where \(\mu\) is the mean and \(\sigma\) is the standard deviation. Its probability density function (PDF) is given by: \[ f_X(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2\right) \]
Support
\[ \text{supp}(X) = {\mathbb R}= (-\infty, \infty) \]
Expected Value and Variance
\[ {\mathbb E}(X) = \mu \quad {\text{Var}}(X) = \sigma^2 \]
CDF
For the standard normal distribution \(Z \sim \mathbb{N}(0,1)\) \[ F_Z(x) = {\mathbb P}(Z \le x) = \Phi(x) \]
#| standalone: true
#| viewerHeight: 600
#| components: viewer
#| layout: vertical
import numpy as np
import scipy
from scipy import stats
import scipy.stats as stats
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from shiny import App, render, ui
from matplotlib.patches import Patch
# Generate a random sample
# Define the UI
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.sidebar(
ui.input_slider(
"alpha",
"alpha",
min = 1e-2,
max = 1-1e-2,
value = 0.5,
step = 1e-2,
ticks=True,
animate=False
),
ui.input_slider(
"mu",
"mu",
min = -1.0,
max = 1.0,
value = 0.0,
step = 0.25,
ticks=True,
animate=False
),
ui.input_slider(
"sigma",
"sigma",
min = 0.25,
max = 2.0,
value = 1.5,
step = 0.25,
ticks=True,
animate=False
),
),
ui.output_plot("plots", height="500px")
)
)
# Define the server logic
def server(input, output, session):
@output
@render.plot
def plots():
mu = input.mu()
sigma = input.sigma()
Z = stats.norm()
X = stats.norm(loc=mu, scale=sigma)
min_X, max_X = X.ppf((1e-5, 1-1e-5))
x = np.linspace(min_X-1, max_X+1, 500)
alpha = input.alpha()
q_alpha = X.ppf(1-alpha)
# Create figure and axes
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
# Histogram Plot
ax[0].plot(x, X.pdf(x), lw=1, label='')
ax[0].plot(x, Z.pdf(x), lw=1, label='',color='black', linestyle='--')
ax[0].set_ylabel('PDF')
ax[0].fill_between(x, 0, X.pdf(x), where=(x > q_alpha), alpha=0.5)
ax[0].set_title(rf'$P(X > q_\alpha)$={1-X.cdf(q_alpha): .2f}');
ax[0].axvline(q_alpha, color='red', linestyle='--')
ax[0].set_xlim(-5, 5)
ax[0].set_ylim(0, 2)
ax[1].plot(x, X.cdf(x), lw=1, label='')
ax[1].plot(x, Z.cdf(x), lw=1, label='', color='black', linestyle='--')
ax[1].set_ylabel('CDF')
ax[1].axhline(X.cdf(q_alpha), color='black', linestyle='--', lw=0.5)
ax[1].axvline(q_alpha, color='red', linestyle='--')
ax[1].set_title(rf'$q_\alpha$={q_alpha: .3f}')
ax[1].set_xlim(-5, 5)
plt.tight_layout()
return fig
# Create the Shiny app
app = App(app_ui, server)
app
Example
The heights of adult men in a certain population are normally distributed with a mean of 175 cm and a standard deviation of 7 cm. If a man from this population is selected at random, his height can be modeled as a random variable \(X \sim \mathbb{N}(175, 49)\).


Definition
If \(Z_1, Z_2, \ldots, Z_k\) are independent standard normal random variables, then the sum of their squares: \[ X = Z_1^2 + Z_2^2 + \ldots + Z_k^2 \] is distributed as a chi-squared distribution with \(k\) degrees of freedom, denoted as \[X \sim {\chi^2}(k). \] The probability density function (PDF) is given by: \[ f_X(x) = \frac{1}{2^{k/2} \Gamma(k/2)} x^{k/2 - 1} e^{-x/2} \]
Support
\[ \text{supp}(X) = {\mathbb R}_+ = [0, \infty) = \{x: x \ge 0\} \]
Expected Value and Variance
\[ {\mathbb E}(X) = k \quad {\text{Var}}(X) = 2k \]
#| standalone: true
#| viewerHeight: 600
#| components: viewer
#| layout: vertical
import numpy as np
import scipy
from scipy import stats
import scipy.stats as stats
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from shiny import App, render, ui
from matplotlib.patches import Patch
# Generate a random sample
# Define the UI
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.sidebar(
ui.input_slider(
"alpha",
"alpha",
min = 1e-2,
max = 1-1e-2,
value = 0.5,
step = 1e-2,
ticks=True,
animate=False
),
ui.input_slider(
"k",
"k",
min = 1,
max = 7,
value = 3,
step = 1,
ticks=True,
animate=False
),
),
ui.output_plot("plots", height="500px")
)
)
# Define the server logic
def server(input, output, session):
@output
@render.plot
def plots():
k = input.k()
Z = stats.chi2(df=1)
X = stats.chi2(df=k)
min_X, max_X = X.ppf((1e-5, 1-1e-5))
x = np.linspace(min_X-1, max_X+1, 500)
alpha = input.alpha()
q_alpha = X.ppf(1-alpha)
# Create figure and axes
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
# Histogram Plot
ax[0].plot(x, X.pdf(x), lw=1, label='')
ax[0].plot(x, Z.pdf(x), lw=1, label='',color='black', linestyle='--')
ax[0].set_ylabel('PDF')
ax[0].fill_between(x, 0, X.pdf(x), where=(x > q_alpha), alpha=0.5)
ax[0].set_title(rf'$P(X > q_\alpha)$={1-X.cdf(q_alpha): .2f}');
ax[0].axvline(q_alpha, color='red', linestyle='--')
ax[0].set_xlim(-1, 15)
ax[0].set_ylim(0, 1)
ax[1].plot(x, X.cdf(x), lw=1, label='')
ax[1].plot(x, Z.cdf(x), lw=1, label='', color='black', linestyle='--')
ax[1].set_ylabel('CDF')
ax[1].axhline(X.cdf(q_alpha), color='black', linestyle='--', lw=0.5)
ax[1].axvline(q_alpha, color='red', linestyle='--')
ax[1].set_title(rf'$q_\alpha$={q_alpha: .3f}')
ax[1].set_xlim(-1, 15)
plt.tight_layout()
return fig
# Create the Shiny app
app = App(app_ui, server)
app
Example
If \(X_1, X_2, \dots, X_n\) are a sample taken from a distribution which follows a normal distribution \(\mathbb{N}(\mu, \sigma^2)\). Then the sample variance \[{{\text{Var}}(X_1, X_2, \dots, X_n)} = \frac{1}{(n-1)}\sum_{i=1}^n(X_i - \overline X)^2\] will follow the rescaled \(\frac{\sigma^2}{(n-1)} \cdot {\chi^2}(n-1)\) distribution.
