26 / 90

第 26 天:隨機變數、期望、採樣與最大概似

定義離散隨機變數與分佈,手算期望與變異數,用固定種子的抽樣建立經驗分佈,並推導包含零計數失效在內的最大概似估計。

數學與演算法基礎 480 分鐘(8 小時專注學習)
01 120 分鐘

隨機變數與機率分佈

僅在記錄預測與觀察證據後才允許輔助。

前置要求

完成第 25 天(機率基礎);如遇阻塞,使用所列恢復連結(第 21 天)。

學習目標

把離散隨機變數定義為從結果到數的函數,驗證機率分佈表,並把它與詞元上的類別分佈聯繫起來。

操作指引

隨機變數 $X$ 把樣本空間的每個結果映射為一個數;其分佈列出滿足 $\sum_x P(X=x)=1$ 的 $P(X=x)$。公平骰的 $X$ 是 $\{1,\dots,6\}$ 上的均勻分佈。必做:計算骰子的 $E[X]$,再驗證取值 $\{0,1,2,3\}$ 上的詞元型分佈 $p=[0.5,0.25,0.125,0.125]$。

自主實作與記錄

(a) 求公平骰的 $E[X]$,並解釋非整數期望為何不與結果矛盾。(b) 對 $\{0,1,2,3\}$ 上的 $p=[0.5,0.25,0.125,0.125]$,驗證 $\sum p = 1$ 並求 $P(X\ge 2)$。(c) 把 $p$ 解釋為四個詞元類型上的類別分佈:哪個是最可能的詞元?

獨立掌握標準

驗證正規化 $\sum p = 1$、算出 $P(X\ge2)=0.25$,並能把期望解釋為區別於任何單一結果的平均。

權威來源資源與權利證明

linking CC BY-NC-SA 4.0 noncommercial share-alike

Introductory Statistics 2e

作者 / 機構: OpenStax, Rice University (Barbara Illowsky, Susan Dean) | 原始出處連結 ↗

對應章節 / 時間戳: Chapter 4: Discrete Random Variables (probability distribution functions)

驗證版本與日期: 2e web edition (Verified: 2026-10-30)

入選理由:

Open textbook treatment of discrete random variables and distribution tables.

來源前置條件:

High-school algebra; Day 25 probability foundations

安全回退方案(離線彈性):

All required definitions, tables, and worked answers are reproduced locally in this lesson.

對比候選資源與選型分析 (3)
MIT OCW 6.041 Probabilistic Systems Analysis ↗ compared_deferred [CC BY-NC-SA 4.0]

Rigorous but paced for a full graduate semester; exceeds the depth needed for Days 25-27 training-objective foundations.

Khan Academy Statistics and Probability ↗ compared_alternative [CC BY-NC-SA 3.0]

Accessible video-first coverage; noncommercial/share-alike terms and video pacing make it a supplementary rather than primary link.

Harvard Stat 110 Introduction to Probability (Blitzstein & Hwang) ↗ compared_alternative [CRC Press copyright (book); free lecture videos]

Strong intuition-building course; the printed textbook is paywalled, so it complements rather than replaces the openly licensed OpenStax text.

linking Stanford copyright; public course notes No text extraction or redistribution permitted

CS229 Review of Probability Theory

作者 / 機構: Stanford CS229 course staff (Arian Maleki, Tom Do) | 原始出處連結 ↗

對應章節 / 時間戳: Section 2.4-2.5: Random variables, distributions, expectation

驗證版本與日期: 2020 public course notes (Verified: 2026-10-30)

入選理由:

ML-oriented definition of random variables bridging to categorical token distributions.

來源前置條件:

Multivariate calculus notation exposure

安全回退方案(離線彈性):

Self-contained definitions and derivations in activity instructions and offline guide.

對比候選資源與選型分析 (2)
OpenStax Introductory Statistics 2e ↗ compared_alternative [CC BY-NC-SA 4.0]

Co-selected: provides the gentler beginner narrative; CS229 provides the ML-oriented notation bridge.

MIT OCW 18.05 Introduction to Probability and Statistics ↗ compared_alternative [CC BY-NC-SA 4.0]

Strong full-course treatment with noncommercial/share-alike terms; heavier than the compact CS229 reference for this three-day block.

02 120 分鐘

期望、變異數與分佈表計算練習

獨立掌握檢查(無 AI 輔助)。

前置要求

完成上一項活動;如遇阻塞,使用所列恢復連結。

學習目標

在無輔助下對離散分佈計算 $E[X]$、$E[X^2]$、$\mathrm{Var}(X)=E[X^2]-E[X]^2$ 與標準差。

操作指引

先逐行算分佈表再合併。必做:下面的四型分佈、一顆公平骰和一個 Bernoulli(0.25) 變數。計算前先記錄預測值,能取精確分數處保留分數。

自主實作與記錄

對 $\{0,1,2,3\}$ 上的 $p=[0.5,0.25,0.125,0.125]$,求 $E[X]$、$E[X^2]$、$\mathrm{Var}(X)$、$\sigma_X$。再求公平骰($E[X^2]=91/6$)與 $\mathrm{Bernoulli}(0.25)$ 的 $\mathrm{Var}$。

獨立掌握標準

所有數值與獨立算出的參考分數/小數一致;變異數以平方單位報告,$\sigma$ 以原單位報告。

權威來源資源與權利證明

linking CC BY-NC-SA 4.0 noncommercial share-alike

Introductory Statistics 2e

作者 / 機構: OpenStax, Rice University (Barbara Illowsky, Susan Dean) | 原始出處連結 ↗

對應章節 / 時間戳: Chapter 4: Discrete Random Variables (mean, expected value, standard deviation)

驗證版本與日期: 2e web edition (Verified: 2026-10-30)

入選理由:

Step-by-step open examples of expected value and variance from distribution tables.

來源前置條件:

High-school algebra; Day 25 probability foundations

安全回退方案(離線彈性):

All required definitions, tables, and worked answers are reproduced locally in this lesson.

對比候選資源與選型分析 (3)
MIT OCW 6.041 Probabilistic Systems Analysis ↗ compared_deferred [CC BY-NC-SA 4.0]

Rigorous but paced for a full graduate semester; exceeds the depth needed for Days 25-27 training-objective foundations.

Khan Academy Statistics and Probability ↗ compared_alternative [CC BY-NC-SA 3.0]

Accessible video-first coverage; noncommercial/share-alike terms and video pacing make it a supplementary rather than primary link.

Harvard Stat 110 Introduction to Probability (Blitzstein & Hwang) ↗ compared_alternative [CRC Press copyright (book); free lecture videos]

Strong intuition-building course; the printed textbook is paywalled, so it complements rather than replaces the openly licensed OpenStax text.

linking Stanford copyright; public course notes No text extraction or redistribution permitted

CS229 Review of Probability Theory

作者 / 機構: Stanford CS229 course staff (Arian Maleki, Tom Do) | 原始出處連結 ↗

對應章節 / 時間戳: Section 2.5-2.6: Expectation, variance, common discrete distributions

驗證版本與日期: 2020 public course notes (Verified: 2026-10-30)

入選理由:

Concise reference for Var(X)=E[X^2]-E[X]^2 and Bernoulli variance used in ML derivations.

來源前置條件:

Multivariate calculus notation exposure

安全回退方案(離線彈性):

Self-contained definitions and derivations in activity instructions and offline guide.

對比候選資源與選型分析 (2)
OpenStax Introductory Statistics 2e ↗ compared_alternative [CC BY-NC-SA 4.0]

Co-selected: provides the gentler beginner narrative; CS229 provides the ML-oriented notation bridge.

MIT OCW 18.05 Introduction to Probability and Statistics ↗ compared_alternative [CC BY-NC-SA 4.0]

Strong full-course treatment with noncommercial/share-alike terms; heavier than the compact CS229 reference for this three-day block.

03 120 分鐘

採樣、經驗分佈與最大概似

僅在記錄預測與觀察證據後才允許輔助。

前置要求

完成上一項活動;如遇阻塞,使用所列恢復連結。

學習目標

用固定種子的抽樣建立經驗分佈,用標準誤 $\sigma/\sqrt n$ 與真分佈比較,並推導伯努利與一元文法模型的最大概似估計,包括零計數失效。

操作指引

執行:python3 -c "import random, collections; random.seed(26); s=[random.randint(1,6) for _ in range(100)]; print(round(sum(s)/100,4), collections.Counter(s))". 骰子樣本均值的標準誤為 $\sigma/\sqrt n \approx 1.7078/10 \approx 0.1708$。60 次拋擲 36 次正面的硬幣概似為 $p^{36}(1-p)^{24}$,在 $\hat p = 36/60$ 處最大。一元文法的 MLE 為 $\hat p(w) = \mathrm{count}(w)/N$。

自主實作與記錄

(a) 報告定種子樣本均值,並給出一個偏離 $1/6$ 的經驗頻率。(b) 令對數概似導數為零,推導 60 次拋擲中 36 次正面的 $\hat p$。(c) 在語料 "the cat sat on the mat the cat ran"($N=9$,六種已見類型)中給出 'the' 與 'dog' 的一元文法 MLE。若模型給留出詞元賦 0,會壞在哪裡?

獨立掌握標準

如實報告定種子數值(均值 $3.47$、3 點偏少),由對數概似推出 $\hat p = 0.6$,並能解釋零計數失效與加 1 平滑的對策。

權威來源資源與權利證明

linking CC BY-NC-SA 4.0 noncommercial share-alike

Introductory Statistics 2e

作者 / 機構: OpenStax, Rice University (Barbara Illowsky, Susan Dean) | 原始出處連結 ↗

對應章節 / 時間戳: Chapter 1: Sampling and Data (sampling, variation); Chapter 4 (estimating parameters)

驗證版本與日期: 2e web edition (Verified: 2026-10-30)

入選理由:

Open coverage of sampling variability and estimation that motivates the MLE and standard-error work here.

來源前置條件:

High-school algebra; Day 25 probability foundations

安全回退方案(離線彈性):

All required definitions, simulations, and worked answers are reproduced locally in this lesson.

對比候選資源與選型分析 (3)
MIT OCW 6.041 Probabilistic Systems Analysis ↗ compared_deferred [CC BY-NC-SA 4.0]

Rigorous but paced for a full graduate semester; exceeds the depth needed for Days 25-27 training-objective foundations.

Khan Academy Statistics and Probability ↗ compared_alternative [CC BY-NC-SA 3.0]

Accessible video-first coverage; noncommercial/share-alike terms and video pacing make it a supplementary rather than primary link.

Harvard Stat 110 Introduction to Probability (Blitzstein & Hwang) ↗ compared_alternative [CRC Press copyright (book); free lecture videos]

Strong intuition-building course; the printed textbook is paywalled, so it complements rather than replaces the openly licensed OpenStax text.

linking PSF License Version 2

Python 3 random: Generate pseudo-random numbers

作者 / 機構: Python Software Foundation | 原始出處連結 ↗

對應章節 / 時間戳: random.seed, random.randint, random.sample, random.choices

驗證版本與日期: Python 3.12 documentation (Verified: 2026-10-30)

入選理由:

Normative reference for the seeded draws used to build empirical distributions.

來源前置條件:

Basic Python syntax

安全回退方案(離線彈性):

Use the installed interpreter's help(random) and the included deterministic commands.

對比候選資源與選型分析 (2)
NumPy random Generator documentation ↗ compared_deferred [BSD 3-Clause]

Faster vectorized sampling but adds a third-party dependency before the tensor track; deferred to the models-and-training days.

Python secrets module documentation ↗ compared_rejected_for_days_25_27 [PSF License Version 2]

Cryptographically secure randomness is unnecessary for reproducible statistical pedagogy and obscures the seed-based determinism the exercises require.

04 120 分鐘

獨立掌握檢查:估計與採樣

獨立掌握檢查(無 AI 輔助)。

前置要求

完成上一項活動;如遇阻塞,使用所列恢復連結。

學習目標

獨立求解未見過的估計題,並說明每個結果背後的統計假設。

操作指引

本檢查期間不使用 AI 與參考資料,只做四則運算。先記錄預測,解出全部四題,再複盤並對錯題走恢復連結。 保留首次無協助嘗試且不做改動;此後的錯題複盤和任何協助式補救是單獨階段,遺漏的概念須通過一次答案事先不展示的新同等無協助檢查,才算已補救。 第二次檢查(僅在補救之後;無協助、無提示):(a) 對 $\{0,1,2,3\}$ 上的 $p=[0.2,0.3,0.4,0.1]$ 求 $E[X]$ 與 $\mathrm{Var}(X)$。(b) 求 $P(\text{擲 3 次至少一個 6})$。(c) 15 次拋擲 9 次正面:給出 $\hat p$ 及其近似標準誤差。(d) 用一句話解釋有限樣本比例為何不必等於真 $p$。本卡片不給出第二次檢查的答案:按與掌握標準相同的標準評分(數值正確,寫明 i.i.d. 與有限樣本假設),並只在做完後用另一種方法(直接計算 $\mathrm{Var}=E[(X-E[X])^2]$,或列舉全部 $6^3$ 種結果)核對。(a) 出錯用 day-26-act-01(分布表的有效性)和 day-26-act-02(期望、變異數)補救,(b) 用 day-25-act-02(獨立性)和 day-25-act-01(計數),(c)-(d) 用 day-26-act-03(抽樣、標準誤差、最大概似)。

自主實作與記錄

(a) 對 $\{0,1,2,3\}$ 上的 $p=[0.1,0.2,0.4,0.3]$ 求 $E[X]$ 與 $\mathrm{Var}(X)$。(b) 求 $P(\text{擲 4 次至少一個 6})$。(c) 20 次拋擲 14 次正面:給出 $\hat p$,並舉一個真 $p$ 仍可能不同的理由。(d) 用一句話解釋有限樣本均值為何不必等於 $E[X]$。

獨立掌握標準

獨立數值與 $1.9$、$0.89$、$0.5177$、$0.7$ 一致;陳述 i.i.d. 與有限樣本假設;錯題接入恢復連結,且每個遺漏概念須經一次新的同等無協助檢查才算補救。

權威來源資源與權利證明

linking CC BY-NC-SA 4.0 noncommercial share-alike

Introductory Statistics 2e

作者 / 機構: OpenStax, Rice University (Barbara Illowsky, Susan Dean) | 原始出處連結 ↗

對應章節 / 時間戳: Chapters 1 and 4 review: sampling, distributions, expectation, variance

驗證版本與日期: 2e web edition (Verified: 2026-10-30)

入選理由:

Open review coverage aligned with this day's assessment scope.

來源前置條件:

High-school algebra; Day 25 probability foundations

安全回退方案(離線彈性):

The assessment is self-contained with original items and worked reference answers.

對比候選資源與選型分析 (3)
MIT OCW 6.041 Probabilistic Systems Analysis ↗ compared_deferred [CC BY-NC-SA 4.0]

Rigorous but paced for a full graduate semester; exceeds the depth needed for Days 25-27 training-objective foundations.

Khan Academy Statistics and Probability ↗ compared_alternative [CC BY-NC-SA 3.0]

Accessible video-first coverage; noncommercial/share-alike terms and video pacing make it a supplementary rather than primary link.

Harvard Stat 110 Introduction to Probability (Blitzstein & Hwang) ↗ compared_alternative [CRC Press copyright (book); free lecture videos]

Strong intuition-building course; the printed textbook is paywalled, so it complements rather than replaces the openly licensed OpenStax text.

linking Stanford copyright; public course notes No text extraction or redistribution permitted

CS229 Review of Probability Theory

作者 / 機構: Stanford CS229 course staff (Arian Maleki, Tom Do) | 原始出處連結 ↗

對應章節 / 時間戳: Sections 2.4-3: random variables, expectation, variance, maximum likelihood

驗證版本與日期: 2020 public course notes (Verified: 2026-10-30)

入選理由:

Reference for estimation and MLE concepts this assessment checks without assistance.

來源前置條件:

Multivariate calculus notation exposure

安全回退方案(離線彈性):

The assessment is self-contained with original items and worked reference answers.

對比候選資源與選型分析 (2)
OpenStax Introductory Statistics 2e ↗ compared_alternative [CC BY-NC-SA 4.0]

Co-selected: provides the gentler beginner narrative; CS229 provides the ML-oriented notation bridge.

MIT OCW 18.05 Introduction to Probability and Statistics ↗ compared_alternative [CC BY-NC-SA 4.0]

Strong full-course treatment with noncommercial/share-alike terms; heavier than the compact CS229 reference for this three-day block.