香港中文大學數學系
2026年6月27日
隨機變量的期望值 (expected value / expectation) 是概率實驗中所有可能結果按概率加權的平均值。
方差 (Variance) 量化了隨機變量的數值圍繞其期望值的離散程度。
標準差 (Standard deviation) 是方差的正平方根,其單位與隨機變量相同。
假設 \(X\) 是一個離散隨機變量,其支撐集為 \(\{x_1,x_2,\dots,x_n\}\),且概率質量函數 (PMF) 為 \(P(X=x_i)=p_i\),其中 \(\sum_{i=1}^n p_i = 1,\ p_i\ge 0\)。
期望值: \[ \mathbb{E}[X] = \mu_X = \sum_{i=1}^n x_i \, p_i \]
方差(兩個等價公式): \[ \operatorname{Var}(X) = \sigma_X^2 = \mathbb{E}\big[(X-\mu_X)^2\big] = \sum_{i=1}^n (x_i-\mu_X)^2 p_i \] \[ \operatorname{Var}(X) = \mathbb{E}[X^2] - \big(\mathbb{E}[X]\big)^2,\quad \mathbb{E}[X^2]=\sum_{i=1}^n x_i^2 p_i \]
標準差: \[ \operatorname{SD}(X) = \sigma_X = \sqrt{\operatorname{Var}(X)} \]
給定離散變量 \(X\) 的 PMF 表格:
\[ \begin{array}{c|cccc|c} x & 0 & 1 & 2 & 3 & \textrm{總和} \\ \hline P(X=x) & 0.010 & 0.840 & 0.145 & 0.005 & 1 \end{array} \]
步驟 1:計算 \(\mathbb{E}[X]\) \[ \begin{aligned} \mathbb{E}[X] &= (0)(0.010) + (1)(0.840) + (2)(0.145) + (3)(0.005) \\ &= 0 + 0.840 + 0.290 + 0.015 \\ &= 1.145 \end{aligned} \]
步驟 2:計算 \(\mathbb{E}[X^2]\)(用於方差捷徑公式) \[ \begin{aligned} \mathbb{E}[X^2] &= (0^2)(0.010) + (1^2)(0.840) + (2^2)(0.145) + (3^2)(0.005) \\ &= 0 + (1)(0.840) + (4)(0.145) + (9)(0.005) \\ &= 0 + 0.840 + 0.580 + 0.045 \\ &= 1.465 \end{aligned} \]
步驟 3:計算方差 \(\operatorname{Var}(X)\) \[ \begin{aligned} \operatorname{Var}(X) &= \mathbb{E}[X^2] - \big(\mathbb{E}[X]\big)^2 \\ &= 1.465 - (1.145)^2 \\ &= 1.465 - 1.311025 \\ &= 0.153975 \end{aligned} \] 透過直接計算平方偏差和進行驗證: \[ \begin{aligned} \sum (x_i-\mu_X)^2 p_i &= (0-1.145)^2(0.010) + (1-1.145)^2(0.840) \\ &\quad + (2-1.145)^2(0.145) + (3-1.145)^2(0.005) \\ &= (1.311025)(0.010) + (0.021025)(0.840) \\ &\quad + (0.731025)(0.145) + (3.441025)(0.005) \\ &= 0.01311025 + 0.017661 + 0.105998625 + 0.017205125 \\ &= 0.153975 \quad (\text{與捷徑公式結果一致}) \end{aligned} \]
步驟 4:計算標準差 \(\sigma_X\) \[ \sigma_X = \sqrt{0.153975} \approx 0.39239648 \]
import math
# 定義隨機變量的數值與概率
x_vals = [0, 1, 2, 3]
p_vals = [0.010, 0.840, 0.145, 0.005]
# 步驟 1: 期望值 E[X]
E_X = sum(x * p for x, p in zip(x_vals, p_vals))
# 步驟 2: E[X^2] 用於方差捷徑公式
E_X2 = sum((x**2) * p for x, p in zip(x_vals, p_vals))
# 步驟 3: 方差
Var_X = E_X2 - E_X**2
# 步驟 4: 標準差
SD_X = math.sqrt(Var_X)
# 印出高精度結果
print(f"E(X) = {E_X:.7f}")## E(X) = 1.1450000
## Var(X) = 0.1539750
## SD(X) = 0.3923965
假設 \(X\) 是一個連續隨機變量,其概率密度函數 (PDF) 為 \(f_X(t)\),支撐區間為 \([a,b]\),並滿足 \(\displaystyle \int_a^b f_X(t)dt =1,\ f_X(t)\ge0\)。
期望值: \[ \mathbb{E}[X] = \mu_X = \int_{-\infty}^{\infty} t\, f_X(t) dt \]
方差: \[ \operatorname{Var}(X) = \mathbb{E}\big[(X-\mu_X)^2\big] = \int_{-\infty}^{\infty} (t-\mu_X)^2 f_X(t)dt = \mathbb{E}[X^2] - (\mathbb{E}[X])^2,\quad \mathbb{E}[X^2]=\int_{-\infty}^\infty t^2 f_X(t)dt \]
標準差: \[ \sigma_X = \sqrt{\operatorname{Var}(X)} \]
假設 \(a,b\in\mathbb{R}\) 為常數,\(X,Y\) 為隨機變量:
\(\mathbb{E}[aX + b] = a\mathbb{E}[X] + b\);\(\mathbb{E}[X+Y]=\mathbb{E}[X]+\mathbb{E}[Y]\)
\(\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X)\);\(\operatorname{Var}(b)=0\)(常數的方差為零)
標準化隨機變量 (Standardized random variable):定義 \(Z = \dfrac{X-\mu_X}{\sigma_X}\)。根據性質 1 和 2: \[ \mathbb{E}[Z] = \mathbb{E}\left[\frac{X-\mu_X}{\sigma_X}\right] = \frac{1}{\sigma_X}\big(\mathbb{E}[X]-\mu_X\big)=0 \] \[ \operatorname{Var}(Z) = \operatorname{Var}\left(\frac{X-\mu_X}{\sigma_X}\right) = \frac{1}{\sigma_X^2}\operatorname{Var}(X) = \frac{\sigma_X^2}{\sigma_X^2}=1 \] \(Z\) 的平均值永遠為 \(0\),方差為 \(1\)。
定義 \(Y=4X+2\)。計算 \(\mathbb{E}[X],\operatorname{Var}(X),\mathbb{E}[Y],\operatorname{Var}(Y)\)。
逐步解析積分
步驟 1:計算 \(\mathbb{E}[X]\) \[ \mathbb{E}[X] = \int_0^1 t\cdot f(t) dt = \int_0^1 t\cdot 3t^2 dt = 3\int_0^1 t^3 dt = 3\left[\frac{t^4}{4}\right]_0^1 = \frac{3}{4} = 0.75 \]
步驟 2:計算 \(\mathbb{E}[X^2]\) \[ \mathbb{E}[X^2] = \int_0^1 t^2\cdot 3t^2 dt = 3\int_0^1 t^4 dt =3\left[\frac{t^5}{5}\right]_0^1 = \frac{3}{5}=0.6 \]
步驟 3:\(X\) 的方差 \[ \operatorname{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 = \frac{3}{5} - \left(\frac{3}{4}\right)^2 = \frac{3}{5} - \frac{9}{16} = \frac{48 - 45}{80} = \frac{3}{80} = 0.0375 \]
步驟 4:\(Y=4X+2\) 的期望值(線性性質) \[ \mathbb{E}[Y] = \mathbb{E}[4X+2] =4\mathbb{E}[X]+2 =4\cdot \frac{3}{4}+2 = 3+2=5 \]
步驟 5:\(Y=4X+2\) 的方差(方差縮放性質) \[ \operatorname{Var}(Y) = 4^2 \operatorname{Var}(X) =16\cdot \frac{3}{80} = \frac{48}{80}=\frac{3}{5}=0.6 \] \(\mathbb{E}[Y^2]\) 的完整積分驗證(交叉核對): \[ \begin{aligned} \mathbb{E}[Y^2] &= \int_0^1 (4t+2)^2 \cdot 3t^2 dt = 3\int_0^1 (16t^2+16t+4)t^2 dt \\ &=3\int_0^1 (16t^4+16t^3+4t^2)dt =3\left[ \frac{16t^5}{5}+\frac{16t^4}{4}+\frac{4t^3}{3} \right]_0^1 \\ &=3\left(\frac{16}{5}+4+\frac{4}{3}\right) =3\left(\frac{48+60+20}{15}\right) =3\cdot \frac{128}{15}=\frac{128}{5}=25.6 \end{aligned} \] \[ \operatorname{Var}(Y)=\mathbb{E}[Y^2]-(\mathbb{E}[Y])^2 = 25.6 - 25 =0.6 \quad (\text{與利用性質所得的結果一致}) \]
import sympy as sp
# 定義符號變數
t = sp.Symbol('t', real=True)
# PDF 函數
f = 3 * t**2
# 支撐區間 [0,1]
a, b = 0, 1
# E[X]
E_X = sp.integrate(t * f, (t, a, b))
# E[X^2]
E_X2 = sp.integrate(t**2 * f, (t, a, b))
Var_X = E_X2 - E_X**2
# Y = 4X + 2
Y_expr = 4*t + 2
E_Y = sp.integrate(Y_expr * f, (t, a, b))
E_Y2 = sp.integrate(Y_expr**2 * f, (t, a, b))
Var_Y = E_Y2 - E_Y**2
# 印出精確符號值與小數數值
print("=== X = continuous, f(t)=3t^2, 0<=t<=1 ===")## === X = continuous, f(t)=3t^2, 0<=t<=1 ===
## E[X] exact: 3/4, decimal: 0.75
## Var(X) exact: 3/80, decimal: 0.0375
##
## === Y = 4X +2 ===
## E[Y] exact: 5, decimal: 5.0
## Var(Y) exact: 3/5, decimal: 0.6
對於兩個離散隨機變量 \(X,Y\),其聯合 PMF 為 \(p(x,y)=P(X=x,Y=y)\): \[ \sum_{x}\sum_{y} p(x,y)=1,\quad p(x,y)\ge0 \] 邊際 PMF (Marginal PMFs): \[ p_X(x)=\sum_y p(x,y),\quad p_Y(y)=\sum_x p(x,y) \] 對於連續隨機變量,其聯合 PDF 為 \(f(x,y)\): \[ \iint_{\mathbb{R}^2} f(x,y)dxdy=1,\ f(x,y)\ge0 \] 邊際分佈: \[ f_X(x)=\int_{-\infty}^\infty f(x,y)dy,\quad f_Y(y)=\int_{-\infty}^\infty f(x,y)dx \] 聯合期望值:
\(\mathbb{E}[g(X,Y)]=\sum_x\sum_y g(x,y)p(x,y)\)(離散)
\(\iint g(x,y)f(x,y)dxdy\)(連續)
\[ \operatorname{Cov}(X,Y) = \mathbb{E}\big[(X-\mu_X)(Y-\mu_Y)\big] = \mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y] \] 性質:
\(\operatorname{Cov}(X,X)=\operatorname{Var}(X)\)
\(\operatorname{Cov}(aX+b,cY+d)=ac\operatorname{Cov}(X,Y)\)
\(\operatorname{Cov}(X_1+X_2,Y)=\operatorname{Cov}(X_1,Y)+\operatorname{Cov}(X_2,Y)\)
\[ \rho_{XY} = \frac{\operatorname{Cov}(X,Y)}{\sigma_X \sigma_Y},\quad -1\le \rho_{XY}\le 1 \]
\(\rho=1\):完全正線性關係
\(\rho=-1\):完全負線性關係
\(\rho=0\):不相關(無線性關聯;獨立必然不相關,反之則不然)
對於隨機向量 \(\mathbf{X} = \begin{bmatrix}X_1\\X_2\\\vdots\\X_k\end{bmatrix}\),平均數向量 \(\boldsymbol{\mu}=\begin{bmatrix}\mu_1\\\mu_2\\\vdots\\\mu_k\end{bmatrix},\ \mu_i=\mathbb{E}[X_i]\)。
協方差矩陣 \(\boldsymbol{\Sigma}\in\mathbb{R}^{k\times k}\): \[ \boldsymbol{\Sigma} = \begin{bmatrix} \operatorname{Var}(X_1) & \operatorname{Cov}(X_1,X_2) & \dots & \operatorname{Cov}(X_1,X_k) \\ \operatorname{Cov}(X_2,X_1) & \operatorname{Var}(X_2) & \dots & \operatorname{Cov}(X_2,X_k) \\ \vdots & \vdots & \ddots & \vdots \\ \operatorname{Cov}(X_k,X_1) & \operatorname{Cov}(X_k,X_2) & \dots & \operatorname{Var}(X_k) \end{bmatrix} \] 性質:對稱 (\(\boldsymbol{\Sigma}=\boldsymbol{\Sigma}^T\)),半正定。
從統計學教科書中選擇一個連續 PDF \(f(x)\)(例如均勻分佈、指數分佈、正態分佈、貝塔分佈)。重複以下步驟:
驗證 \(\int f(x)dx=1\)(有效 PDF 檢查)
計算 \(\mathbb{E}[X]=\int x f(x)dx\)
計算 \(\mathbb{E}[X^2]=\int x^2 f(x)dx\)
\(\operatorname{Var}(X)=\mathbb{E}[X^2]-(\mathbb{E}[X])^2\)
\(\sigma_X=\sqrt{\operatorname{Var}(X)}\)
在 SymPy Python 程式碼中實作計算,以獲得符號/精確結果。
# 引入符號計算庫 SymPy
import sympy as sp
# --------------------------
# 步驟 1: 定義符號變數與 PDF 參數
# --------------------------
# 隨機變量 X 的實數變數
x = sp.Symbol("x", real=True)
# 分佈的超參數:mu (平均值), sigma > 0 (標準差)
mu = sp.Symbol("mu", real=True)
sigma = sp.Symbol("sigma", positive=True)
# 定義正態分佈 PDF f(x)
f_x = (1 / (sigma * sp.sqrt(2 * sp.pi))) * sp.exp(-((x - mu) ** 2) / (2 * sigma ** 2))
print("=== Defined Normal PDF f(x) ===")## === Defined Normal PDF f(x) ===
## 2
## -(-mu + x)
## ------------
## 2
## ___ 2*sigma
## \/ 2 *e
## -------------------
## ____
## 2*\/ pi *sigma
# --------------------------
# 步驟 1: 驗證 PDF 有效性:f(x) 在完整支撐集上的積分 = 1
# 正態分佈的支撐集:(-∞, +∞)
# --------------------------
integral_f = sp.integrate(f_x, (x, -sp.oo, sp.oo))
print("Step 1: Integral of f(x) from -∞ to +∞")## Step 1: Integral of f(x) from -∞ to +∞
## 1
if integral_f == 1:
print("Conclusion: ∫f(x)dx = 1, this is a valid probability density function.\n")
else:
print("Conclusion: Integral not equal to 1, invalid PDF.\n")## Conclusion: ∫f(x)dx = 1, this is a valid probability density function.
# --------------------------
# 步驟 2: 計算期望值 E[X] = ∫x*f(x) dx
# --------------------------
E_X = sp.integrate(x * f_x, (x, -sp.oo, sp.oo))
print("Step 2: E[X] = ∫x f(x) dx")## Step 2: E[X] = ∫x f(x) dx
## mu
# --------------------------
# 步驟 3: 計算二階原點矩 E[X²] = ∫x²*f(x) dx
# --------------------------
E_X2 = sp.integrate((x ** 2) * f_x, (x, -sp.oo, sp.oo))
print("Step 3: E[X²] = ∫x² f(x) dx")## Step 3: E[X²] = ∫x² f(x) dx
## 2 2
## mu + sigma
# --------------------------
# 步驟 4: 方差 Var(X) = E[X²] - (E[X])²
# 符號化簡
# --------------------------
Var_X = sp.simplify(E_X2 - (E_X) ** 2)
print("Step 4: Var(X) = E[X²] - (E[X])²")## Step 4: Var(X) = E[X²] - (E[X])²
## 2
## sigma
# --------------------------
# 步驟 5: 標準差 σ_X = sqrt(Var(X))
# --------------------------
sigma_X = sp.simplify(sp.sqrt(Var_X))
print("Step 5: σ_X = sqrt(Var(X))")## Step 5: σ_X = sqrt(Var(X))
## sigma
import sympy as sp
# 符號
x = sp.Symbol("x", real=True)
a = sp.Symbol("a", real=True)
b = sp.Symbol("b", real=True)
# 均勻分佈 PDF
f_x = 1 / (b - a)
support_lower = a
support_upper = b
# 步驟 1: f(x) 在 [a,b] 上的積分
int_f = sp.integrate(f_x, (x, a, b))
print("Step1 ∫f(x)dx =", int_f)## Step1 ∫f(x)dx = -a/(-a + b) + b/(-a + b)
## Step2 E[X] = a/2 + b/2
## Step3 E[X²] = a**2/3 + a*b/3 + b**2/3
## Step4 Var(X) = a**2/12 - a*b/6 + b**2/12
## Step5 σ_X = sqrt(3*a**2 - 6*a*b + 3*b**2)/6
當同時分析兩個或多個隨機變量時,我們研究它們的聯合概率分佈,它描述了多個事件同時發生的概率。給定一個聯合分佈和一個邊際分佈,我們可以解出單一隨機變量剩餘的邊際分佈。
聯合 PMF 的定義
假設 \(X,Y\) 是離散隨機變量,其支撐值分別為 \(x_1,x_2,\dots,x_s\) 和 \(y_1,y_2,\dots,y_t\)。 定義為: \[ p_{ij}=p(x_i,y_j) = \mathbb{P}(X=x_i,\ Y=y_j),\quad x_i,y_j\in\mathbb{R},\ i=1,\dots,s,\ j=1,\dots,t \]
聯合概率表佈局
邊際 PMF 公式 從聯合 PMF 中,單一變量的邊際分佈可以透過對另一個變量求和來獲得: \[ \mathbb{P}(X=x_i) = \sum_{j=1}^t \mathbb{P}(X=x_i,Y=y_j) = \sum_{j=1}^t p_{ij} \] \[ \mathbb{P}(Y=y_j) = \sum_{i=1}^s \mathbb{P}(X=x_i,Y=y_j) = \sum_{i=1}^s p_{ij} \] 邊際分佈是從聯合表格中提取的、單一變量的單變量概率分佈。
離散聯合 PMF 的性質
非負性:對於所有有效的 \(x_i,y_j\),\(p(x_i,y_j)\ge 0\)。
總概率和為 1:\(\sum_{i=1}^s \sum_{j=1}^t p(x_i,y_j) = 1\)。
矩形區域概率:\(\mathbb{P}(a<X<b,\ c<Y<d) = \displaystyle\sum_{\substack{a<x_i<b \\ c<y_j<d}} p(x_i,y_j)\)。
問題陳述
袋子內容物:
抽出 2 個球,不放回。
定義:
\(X\) = 抽出的藍球數量
\(Y\) = 抽出的紅球數量
任務:
推導聯合 PMF \(p(x,y)=\mathbb{P}(X=x,Y=y)\) 的公式。
建立完整的聯合概率表。
計算 \(\mathbb{P}(X+Y\le 1)\)。
找出 \(X\) 的邊際分佈。
找出 \(Y\) 的邊際分佈。
步驟 1:聯合 PMF 公式(超幾何)
抽出 2 個球的總方式數:\(\binom{8}{2}\)。 若有 \(x\) 個藍球、\(y\) 個紅球,剩下的 \(2-x-y\) 個球必為綠球。 有效範圍:\(x=0,1,2;\ y=0,1,2;\ x+y\le 2\)。 \[ p(x,y) = \frac{\binom{3}{x}\binom{2}{y}\binom{3}{2-x-y}}{\binom{8}{2}} \] 先計算分母: \[ \binom{8}{2} = \frac{8!}{2!6!} = \frac{8\cdot7}{2}=28 \]
個別聯合概率計算 \[\begin{align*} p(0,0) &= \frac{\binom{3}{0}\binom{2}{0}\binom{3}{2}}{28} = \frac{1\cdot1\cdot3}{28} = \frac{3}{28} \\ p(0,1) &= \frac{\binom{3}{0}\binom{2}{1}\binom{3}{1}}{28} = \frac{1\cdot2\cdot3}{28} = \frac{6}{28} = \frac{3}{14} \\ p(0,2) &= \frac{\binom{3}{0}\binom{2}{2}\binom{3}{0}}{28} = \frac{1\cdot1\cdot1}{28} = \frac{1}{28} \\ p(1,0) &= \frac{\binom{3}{1}\binom{2}{0}\binom{3}{1}}{28} = \frac{3\cdot1\cdot3}{28} = \frac{9}{28} \\ p(1,1) &= \frac{\binom{3}{1}\binom{2}{1}\binom{3}{0}}{28} = \frac{3\cdot2\cdot1}{28} = \frac{6}{28} = \frac{3}{14} \\ p(1,2) &= \binom{3}{1}\binom{2}{2}\binom{3}{-1} \quad (\text{無效,為零}) = 0 \\ p(2,0) &= \frac{\binom{3}{2}\binom{2}{0}\binom{3}{0}}{28} = \frac{3\cdot1\cdot1}{28} = \frac{3}{28} \\ p(2,1) &= p(2,2) = 0 \quad (\text{球數不足,綠球數量為負}) \end{align*}\]
步驟 2:完整的聯合概率表
步驟 3:計算 \(\mathbb{P}(X+Y\le 1)\)
滿足 \(x+y\le1\) 的有效 \((x,y)\) 數對:\((0,0),\ (0,1),\ (1,0)\) \[ \begin{aligned} \mathbb{P}(X+Y\le 1) &= p(0,0)+p(0,1)+p(1,0) \\ &= \frac{3}{28} + \frac{3}{14} + \frac{9}{28} \\ &= \frac{3}{28} + \frac{6}{28} + \frac{9}{28} \\ &= \frac{18}{28} = \frac{9}{14} \end{aligned} \]
步驟 4:\(X\) 的邊際分佈
\[ \begin{aligned} \mathbb{P}(X=0) &= p(0,0)+p(0,1)+p(0,2) = \frac{3}{28}+\frac{6}{28}+\frac{1}{28}=\frac{10}{28}=\frac{5}{14} \\ \mathbb{P}(X=1) &= p(1,0)+p(1,1)+p(1,2) = \frac{9}{28}+\frac{6}{28}+0=\frac{15}{28} \\ \mathbb{P}(X=2) &= p(2,0)+p(2,1)+p(2,2) = \frac{3}{28}+0+0=\frac{3}{28} \end{aligned} \]步驟 5:\(Y\) 的邊際分佈
\[ \begin{aligned} \mathbb{P}(Y=0) &= p(0,0)+p(1,0)+p(2,0) = \frac{3}{28}+\frac{9}{28}+\frac{3}{28}=\frac{15}{28} \\ \mathbb{P}(Y=1) &= p(0,1)+p(1,1)+p(2,1) = \frac{6}{28}+\frac{6}{28}+0=\frac{12}{28}=\frac{3}{7} \\ \mathbb{P}(Y=2) &= p(0,2)+p(1,2)+p(2,2) = \frac{1}{28}+0+0=\frac{1}{28} \end{aligned} \]import math
from fractions import Fraction
# 定義組合函數
def comb(n, k):
if k < 0 or k > n:
return 0
return math.comb(n, k)
# 參數設定
n_blue = 3
n_red = 2
n_green = 3
total = n_blue + n_red + n_green
draw = 2
denominator = comb(total, draw)
# 步驟 1: 計算所有的聯合概率 p(x,y)
joint_probs = {}
for x in [0,1,2]:
for y in [0,1,2]:
z = draw - x - y
num = comb(n_blue, x) * comb(n_red, y) * comb(n_green, z)
prob = Fraction(num, denominator)
joint_probs[(x,y)] = prob
# 印出聯合概率表
print("=== Joint Probability Table p(x,y) ===")## === Joint Probability Table p(x,y) ===
for x in [0,1,2]:
row = []
for y in [0,1,2]:
row.append(str(joint_probs[(x,y)]))
print(f"x={x}: y0={row[0]}, y1={row[1]}, y2={row[2]}")## x=0: y0=3/28, y1=3/14, y2=1/28
## x=1: y0=9/28, y1=3/14, y2=0
## x=2: y0=3/28, y1=0, y2=0
# 步驟 2: 計算 P(X+Y <= 1)
p_sum_le1 = joint_probs[(0,0)] + joint_probs[(0,1)] + joint_probs[(1,0)]
print(f"\nP(X+Y <= 1) = {p_sum_le1}, decimal = {float(p_sum_le1):.6f}")##
## P(X+Y <= 1) = 9/14, decimal = 0.642857
# 步驟 3: 邊際分佈 X
marg_x = {}
for x in [0,1,2]:
s = sum(joint_probs[(x,y)] for y in [0,1,2])
marg_x[x] = s
print("\n=== Marginal Distribution X ===")##
## === Marginal Distribution X ===
## P(X=0) = 5/14, decimal = 0.357143
## P(X=1) = 15/28, decimal = 0.535714
## P(X=2) = 3/28, decimal = 0.107143
# 步驟 4: 邊際分佈 Y
marg_y = {}
for y in [0,1,2]:
s = sum(joint_probs[(x,y)] for x in [0,1,2])
marg_y[y] = s
print("\n=== Marginal Distribution Y ===")##
## === Marginal Distribution Y ===
## P(Y=0) = 15/28, decimal = 0.535714
## P(Y=1) = 3/7, decimal = 0.428571
## P(Y=2) = 1/28, decimal = 0.035714
連續聯合 PDF 的定義與性質
對於兩個連續隨機變量 \(X\) 和 \(Y\),我們用聯合概率密度函數 (joint PDF) \(f(x,y)\) 來描述它們的聯合行為。概率計算需要在 \((x,y)\) 平面的區域上進行雙重積分。
非負性條件: \[ f(x,y) \ge 0 \quad \text{對於所有實數 } x,y \]
總概率歸一化(全平面的積分等於 1): \[ \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} f(x,y)\,dx\,dy = 1 \]
矩形區域概率: \[ \mathbb{P}(a<X<b,\ c<Y<d) = \int_{c}^{d} \int_{a}^{b} f(x,y)\,dx\,dy \]
一般平面區域概率:對於任何二維面積 \(A \subset \mathbb{R}^2\), \[ \mathbb{P}\big\{(X,Y)\in A\big\} = \iint_A f(x,y)\,dx\,dy \]
邊際概率密度函數(對另一個變量進行積分消去): \[ f_X(x) = \int_{-\infty}^{\infty} f(x,y)\,dy,\qquad f_Y(y) = \int_{-\infty}^{\infty} f(x,y)\,dx \]
問題陳述
連續隨機變量 \(X,Y\) 的聯合 PDF 為 \[ f(x,y) = \begin{cases} x+y & 0<x<1,\ 0<y<1 \\ 0 & \text{其他情況} \end{cases} \] 在支撐區域 \((0,1)\times(0,1)\) 上計算邊際密度函數 \(f_X(x)\) 和 \(f_Y(y)\)。
\(f_X(x)\) 的逐步解析推導
根據邊際 PDF 的定義: \[ f_X(x) = \int_{-\infty}^{\infty} f(x,y)\,dy \] 在 \(0<y<1\) 範圍之外,\(f(x,y)=0\),因此我們將積分界限限制在 \(y=0\) 到 \(y=1\): \[ f_X(x) = \int_{0}^{1} (x+y)\,dy,\quad 0<x<1 \] 對 \(y\) 逐項積分(將 \(x\) 視為常數): \[ \begin{aligned} \int_{0}^{1} (x+y)\,dy &= \int_{0}^{1} x\,dy + \int_{0}^{1} y\,dy \\ &= x\big[y\big]_{0}^{1} + \left[\frac{1}{2}y^2\right]_{0}^{1} \\ &= x(1-0) + \left(\frac{1}{2}(1)^2 - \frac{1}{2}(0)^2\right) \\ &= x + \frac{1}{2} \end{aligned} \] \(X\) 的最終邊際分佈: \[ f_X(x) = \begin{cases} x+\dfrac12 & 0<x<1 \\ 0 & \text{其他地方} \end{cases} \]
\(f_Y(y)\) 的逐步解析推導
透過積分消去 \(x\) 的對稱計算: \[ f_Y(y) = \int_{-\infty}^{\infty} f(x,y)\,dx = \int_{0}^{1} (x+y)\,dx,\quad 0<y<1 \] 對 \(x\) 逐項積分(將 \(y\) 視為常數): \[ \begin{aligned} \int_{0}^{1} (x+y)\,dx &= \int_{0}^{1} x\,dx + \int_{0}^{1} y\,dx \\ &= \left[\frac12 x^2\right]_{0}^{1} + y\big[x\big]_{0}^{1} \\ &= \left(\frac12(1)^2 - \frac12(0)^2\right) + y(1-0) \\ &= \frac12 + y \end{aligned} \] \(Y\) 的最終邊際分佈: \[ f_Y(y) = \begin{cases} y+\dfrac12 & 0<y<1 \\ 0 & \text{其他地方} \end{cases} \]
一致性檢查(驗證邊際積分等於 1)
檢查 \(\int_0^1 f_X(x)dx = 1\): \[ \int_{0}^{1}\left(x+\frac12\right)dx = \left[\frac12 x^2 + \frac12 x\right]_0^1 = \left(\frac12+\frac12\right)-0 = 1 \] 檢查 \(\int_0^1 f_Y(y)dy = 1\): \[ \int_{0}^{1}\left(y+\frac12\right)dy = \left[\frac12 y^2 + \frac12 y\right]_0^1 = \left(\frac12+\frac12\right)-0 = 1 \] 這兩個邊際 PDF 都滿足有效密度函數的歸一化法則。
import sympy as sp
# 定義符號變數
x, y = sp.Symbol('x'), sp.Symbol('y')
# 在 0<x<1, 0<y<1 上定義聯合 PDF
f = x + y
# 計算邊際 PDF f_X(x) = f 對 y 積分,範圍 0 到 1
f_X = sp.integrate(f, (y, 0, 1))
# 計算邊際 PDF f_Y(y) = f 對 x 積分,範圍 0 到 1
f_Y = sp.integrate(f, (x, 0, 1))
# 印出符號結果
print("Marginal PDF f_X(x) =", f_X)## Marginal PDF f_X(x) = x + 1/2
## Marginal PDF f_Y(y) = y + 1/2
# 驗證 f_X 的歸一化積分
norm_X = sp.integrate(f_X, (x, 0, 1))
print("\nIntegral of f_X(x) over [0,1] =", norm_X)##
## Integral of f_X(x) over [0,1] = 1
# 驗證 f_Y 的歸一化積分
norm_Y = sp.integrate(f_Y, (y, 0, 1))
print("Integral of f_Y(y) over [0,1] =", norm_Y)## Integral of f_Y(y) over [0,1] = 1
矩陣表示法提供了一種緊湊的方式來量化一組隨機變量之間兩兩的關係。協方差矩陣匯集了該組中每一對隨機變量的方差(自我離散)和協方差(交叉線性關聯)。
總體協方差矩陣的定義
設 \(\{X_1,X_2,\dots,X_p\}\) 為包含 \(p\) 個隨機變量的集合。協方差矩陣 \(\boldsymbol{\Sigma}\in\mathbb{R}^{p\times p}\) 針對每一個元素定義如下: \[ \boldsymbol{\Sigma}_{ij}= \begin{cases} \operatorname{Var}(X_i) & i=j \quad (\text{主對角線:}X_i \text{ 的方差})\\ \operatorname{Cov}(X_i,X_j) & i\neq j \quad (\text{非對角線:}X_i \text{ 與 } X_j \text{ 之間的協方差}) \end{cases} \] 完整矩陣展開: \[ \boldsymbol{\Sigma}= \begin{bmatrix} \operatorname{Var}(X_1) & \operatorname{Cov}(X_1,X_2) & \dots & \operatorname{Cov}(X_1,X_p)\\ \operatorname{Cov}(X_2,X_1) & \operatorname{Var}(X_2) & \dots & \operatorname{Cov}(X_2,X_p)\\ \vdots & \vdots & \ddots & \vdots\\ \operatorname{Cov}(X_p,X_1) & \operatorname{Cov}(X_p,X_2) & \dots & \operatorname{Var}(X_p) \end{bmatrix} = \begin{bmatrix} \sigma_1^2 & \sigma_{12} & \dots & \sigma_{1p}\\ \sigma_{21} & \sigma_2^2 & \dots & \sigma_{2p}\\ \vdots & \vdots & \ddots & \vdots\\ \sigma_{p1} & \sigma_{p2} & \dots & \sigma_p^2 \end{bmatrix} \] 關鍵性質:協方差是對稱的 \(\operatorname{Cov}(X_i,X_j)=\operatorname{Cov}(X_j,X_i)\),因此 \(\boldsymbol{\Sigma}=\boldsymbol{\Sigma}^T\)(對稱方陣)。
簡單解釋:對角線元素衡量個別變量的離散程度;非對角線元素衡量兩個不同變量之間的線性共變關係。
給定樣本數據集
5 個變量 (\(A,B,C,D,E\)),每個變量有 \(n=6\) 個樣本觀測值:
\[ \begin{array}{c|cccccc} Variable & Obs1 & Obs2 & Obs3 & Obs4 & Obs5 & Obs6 \\ \hline A & 1 & 2 & 3 & 4 & 5 & 6 \\ B & 2 & 3 & 5 & 6 & 1 & 9 \\ C & 3 & 5 & 5 & 5 & 10 & 8 \\ D & 10 & 20 & 30 & 40 & 50 & 55 \\ E & 7 & 8 & 9 & 4 & 6 & 10 \end{array} \] 樣本協方差公式(無偏,分母為 \(n-1\)): \[ \widehat{\operatorname{Cov}}(X_i,X_j)=\frac{1}{n-1}\sum_{k=1}^n \big(X_{i,k}-\bar{X}_i\big)\big(X_{j,k}-\bar{X}_j\big),\quad \widehat{\operatorname{Var}}(X_i)=\widehat{\operatorname{Cov}}(X_i,X_i) \] \(n=6\),所以 \(n-1=5\)。
步驟 1:計算每個變量的樣本平均值
\[ \begin{aligned} \bar{A}&=\frac{1+2+3+4+5+6}{6}=\frac{21}{6}=3.5 \\ \bar{B}&=\frac{2+3+5+6+1+9}{6}=\frac{26}{6}\approx4.3333333 \\ \bar{C}&=\frac{3+5+5+5+10+8}{6}=\frac{36}{6}=6 \\ \bar{D}&=\frac{10+20+30+40+50+55}{6}=\frac{205}{6}\approx34.1666667 \\ \bar{E}&=\frac{7+8+9+4+6+10}{6}=\frac{44}{6}\approx7.3333333 \end{aligned} \]
步驟 2:中心化數據矩陣(每個元素減去列平均值)
中心化列 = 原始列 \(-\) 該列平均值: \[ \begin{aligned} \tilde{A}&=[1-3.5,\ 2-3.5,\ 3-3.5,\ 4-3.5,\ 5-3.5,\ 6-3.5] =[-2.5,\ -1.5,\ -0.5,\ 0.5,\ 1.5,\ 2.5] \\ \tilde{B}&=\left[2-\tfrac{26}{6},\ 3-\tfrac{26}{6},\ 5-\tfrac{26}{6},\ 6-\tfrac{26}{6},\ 1-\tfrac{26}{6},\ 9-\tfrac{26}{6}\right] =\left[-\tfrac{14}{6},\ -\tfrac{8}{6},\ \tfrac{4}{6},\ \tfrac{10}{6},\ -\tfrac{20}{6},\ \tfrac{28}{6}\right] \\ \tilde{C}&=[3-6,\ 5-6,\ 5-6,\ 5-6,\ 10-6,\ 8-6] =[-3,\ -1,\ -1,\ -1,\ 4,\ 2] \\ \tilde{D}&=\left[10-\tfrac{205}{6},\ 20-\tfrac{205}{6},\ 30-\tfrac{205}{6},\ 40-\tfrac{205}{6},\ 50-\tfrac{205}{6},\ 55-\tfrac{205}{6}\right] =\left[-\tfrac{145}{6},\ -\tfrac{85}{6},\ -\tfrac{25}{6},\ \tfrac{35}{6},\ \tfrac{95}{6},\ \tfrac{125}{6}\right] \\ \tilde{E}&=\left[7-\tfrac{44}{6},\ 8-\tfrac{44}{6},\ 9-\tfrac{44}{6},\ 4-\tfrac{44}{6},\ 6-\tfrac{44}{6},\ 10-\tfrac{44}{6}\right] =\left[\tfrac{2}{6},\ \tfrac{8}{6},\ \tfrac{14}{6},\ -\tfrac{20}{6},\ -\tfrac{8}{6},\ \tfrac{16}{6}\right] \end{aligned} \]
步驟 3:透過中心化矩陣計算樣本協方差矩陣的公式
令 \(\widetilde{\mathbf{X}}\) 為 \(5\times6\) 的中心化數據矩陣。無偏樣本協方差矩陣為: \[ \widehat{\boldsymbol{\Sigma}}=\frac{1}{n-1}\widetilde{\mathbf{X}}\,\widetilde{\mathbf{X}}^T \] \(n-1=5\),因此將外積乘以 \(\tfrac15\)。
驗證數值樣本協方差矩陣(與 Sage 輸出一致) \[ \widehat{\boldsymbol{\Sigma}}= \begin{bmatrix} 3.5000000 & 3.0000000 & 0.4000000 & 32.5000000 & 0.4000000 \\ 3.0000000 & 8.6666667 & 0.4000000 & 25.3333333 & 2.4666667 \\ 0.4000000 & 0.4000000 & 38.0000000 & 0.4000000 & 0.4000000 \\ 32.5000000 & 25.3333333 & 0.4000000 & 304.1666667 & 1.3333333 \\ 0.4000000 & 2.4666667 & 0.4000000 & 1.3333333 & 4.6666667 \end{bmatrix} \] 驗證原因:此矩陣是對稱的,對角線元素是樣本方差,非對角線元素是樣本協方差,以無偏的 \(n-1\) 縮放計算,與 Python 結果一致。
import numpy as np
# 步驟 1: 定義數據集矩陣 (行 = 變量 A,B,C,D,E; 列 = 觀測值)
data = np.array([
[1, 2, 3, 4, 5, 6], # A
[2, 3, 5, 6, 1, 9], # B
[3, 5, 5, 5, 10, 8], # C
[10, 20, 30, 40, 50, 55],# D
[7, 8, 9, 4, 6, 10] # E
])
# 步驟 2: NumPy 內建 cov 函數 (rowvar=True: 行為變量,ddof=1 為無偏 n-1)
cov_matrix = np.cov(data, rowvar=True, ddof=1)
# 印出格式化至小數點後 7 位的結果,以便與 Sage 匹配
print("Sample Covariance Matrix (unbiased, ddof=1):")## Sample Covariance Matrix (unbiased, ddof=1):
## [[ 3.5 3. 4. 32.5 0.4 ]
## [ 3. 8.6666667 0.4 25.3333333 2.4666667]
## [ 4. 0.4 6.4 38. 0.4 ]
## [ 32.5 25.3333333 38. 304.1666667 1.3333333]
## [ 0.4 2.4666667 0.4 1.3333333 4.6666667]]
# 選擇性的手動計算,以驗證公式 1/(n-1)*X_centered @ X_centered.T
n_vars, n_samples = data.shape
data_centered = data - np.mean(data, axis=1, keepdims=True)
cov_manual = (1/(n_samples - 1)) * data_centered @ data_centered.T
print("\nManual Centered Matrix Calculation Match Check:")##
## Manual Centered Matrix Calculation Match Check:
## True