# 分类分布

**分类分布**是单次随机抽取的离散概率分布，该抽取落在 k 个可能类别之一，其中类别 i 以概率 p_i 出现，p_i ≥ 0 且 \( \sum_{i=1}^{k} p_i = 1 \)。它也被称为广义伯努利分布或多努利分布<sup>[1](https://mathworld.wolfram.com/CategoricalDistribution.html)</sup>。服从分类分布的随机变量 X 满足：对 \( 1 \le i \le k \)，有 \( \Pr(X = i) = p_i \)<sup>[1](https://mathworld.wolfram.com/CategoricalDistribution.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 概率质量函数 | 对 \( 1 \le i \le k \)，\( \Pr(X = i) = p_i \)，其中 \( p_i \ge 0 \) 且 \( \sum_{i=1}^{k} p_i = 1 \)<sup>[1](https://mathworld.wolfram.com/CategoricalDistribution.html)</sup> |
| 与多项分布的关系 | 分类分布是试验次数为 1 的多项分布<sup>[1](https://mathworld.wolfram.com/CategoricalDistribution.html)</sup> |
| 与伯努利分布的关系 | 当 k = 2 时，将两个类别标记为 0 和 1，即退化为伯努利分布<sup>[1](https://mathworld.wolfram.com/CategoricalDistribution.html)</sup> |
| 矩（独热向量形式） | \( \mathrm{E}(X_i) = p_i \)，\( \mathrm{var}(X_i) = p_i(1 - p_i) \)，且对 \( i \ne j \) 有 \( \mathrm{cov}(X_i, X_j) = -p_i p_j \)<sup>[2](https://www.stat.umn.edu/geyer/f09/5101/notes/n1.pdf)</sup> |
| 共轭先验 | 狄利克雷分布是共轭的：以 \( \mathrm{Dirichlet}(\alpha_1, \ldots, \alpha_k) \) 为先验，在类别 i 中观测到一次后，后验为 \( \mathrm{Dirichlet}(\alpha_1, \ldots, \alpha_i + 1, \ldots, \alpha_k) \)<sup>[3](https://ermongroup.github.io/cs228-notes/learning/bayesian/)</sup> |
| 别名 | 广义伯努利分布；多努利分布<sup>[1](https://mathworld.wolfram.com/CategoricalDistribution.html)</sup> |

## 定义与参数化

分类分布描述从有限个互斥类别中抽取的一次观测。其参数是位于单纯形上的概率向量 \( \theta = (p_1, \ldots, p_k) \)，即各分量非负且总和为一<sup>[4](https://mc-stan.org/docs/2_32/functions-reference/categorical-distribution.html)</sup>。概率质量函数即为 \( \mathrm{Categorical}(y \mid \theta) = \theta_y \)，其中 \( y \in \{1, \ldots, N\} \)<sup>[4](https://mc-stan.org/docs/2_32/functions-reference/categorical-distribution.html)</sup>。

该分布可用两种等价方式表示：作为取 k 个整数值之一的标量，或作为恰好一个分量为 1、其余分量均为 0 的向量<sup>[1](https://mathworld.wolfram.com/CategoricalDistribution.html)</sup>。在向量形式中，若骰子显示第 k 面，则第 k 位为开；这就是独热编码，k 条“线路”中只有一条是热的<sup>[5](https://oa.ee.tsinghua.edu.cn/ouzhijian/pgm/pgm-pdf/Murphy%20book.pdf)</sup>。对于 k = 3，状态 1、2、3 分别编码为 (1, 0, 0)、(0, 1, 0) 和 (0, 0, 1)<sup>[5](https://oa.ee.tsinghua.edu.cn/ouzhijian/pgm/pgm-pdf/Murphy%20book.pdf)</sup>。

软件库接受两种形式的参数。PyTorch 的 `Categorical` 类通过 `probs` 或 `logits` 参数化（但不可同时使用）；`probs` 必须非负、有限且总和不为零，并沿最后一维归一化为总和 1，而 `logits` 被解释为未归一化的对数概率，可为任意实数<sup>[6](https://docs.pytorch.org/docs/stable/distributions.html)</sup>。样本为来自 \( \{0, \ldots, K-1\} \) 的整数，其中 K 为概率向量的长度<sup>[6](https://docs.pytorch.org/docs/stable/distributions.html)</sup>。Stan 提供了一种对应的对数几率参数化：\( \mathrm{CategoricalLogit}(y \mid \beta) = \mathrm{Categorical}(y \mid \mathrm{softmax}(\beta)) \)，其在内部应用 softmax 函数将任意向量转换为单纯形上的向量<sup>[4](https://mc-stan.org/docs/2_32/functions-reference/categorical-distribution.html)</sup><sup> • </sup><sup>[7](https://usermanual.wiki/Document/stanusersguide218.2054725045.pdf)</sup>。

## 与其他分布的关系

分类分布位于一族计数分布的底层。多项分布建模 n 次独立试验中各类别的计数，是 n 个独立独热（伯努利）向量之和，每个向量记录一次分类抽取<sup>[2](https://www.stat.umn.edu/geyer/f09/5101/notes/n1.pdf)</sup>。在多项分布中令 n = 1 即得到分类分布<sup>[1](https://mathworld.wolfram.com/CategoricalDistribution.html)</sup><sup> • </sup><sup>[8](https://mcube.lab.nycu.edu.tw/~cfung/docs/books/goodfellow2016deep_learning.pdf)</sup>。与两结果情形的类比是完全精确的：分类分布与多项分布的关系，等同于伯努利分布与二项分布的关系<sup>[9](https://snlp2020.github.io/notes/probability-refresher-notes.pdf)</sup>。

术语因文本而异。机器学习研究者凯文·P·墨菲（Kevin P. Murphy）在古斯塔沃·拉塞尔达（Gustavo Lacerda）的建议下，类比二项/伯努利的区分，采用了“多努利”一词<sup>[5](https://oa.ee.tsinghua.edu.cn/ouzhijian/pgm/pgm-pdf/Murphy%20book.pdf)</sup>。古德费洛（Goodfellow）、本吉奥（Bengio）和库尔维尔（Courville）所著的《深度学习》教材指出，许多文本使用“多项”一词来指代多努利分布，却未澄清其仅指 n = 1 的情形<sup>[8](https://mcube.lab.nycu.edu.tw/~cfung/docs/books/goodfellow2016deep_learning.pdf)</sup>。

## 矩

在独热向量形式中，每个分量 \( X_i \) 是指示抽取是否落在类别 i 的指示变量。由于恰好一个分量为 1，各分量之和为一，这使得向量退化：其分量不能独立变化。矩由此结构导出<sup>[2](https://www.stat.umn.edu/geyer/f09/5101/notes/n1.pdf)</sup>：

\[ \mathrm{E}(X_i) = p_i, \qquad \mathrm{var}(X_i) = p_i(1 - p_i), \qquad \mathrm{cov}(X_i, X_j) = -p_i p_j \ \text{其中 } i \ne j, \]

因此方差矩阵为 \( \mathrm{var}(X) = P - pp' \)，其中 P 是以 p_i 为对角元素的对角矩阵，pp' 为外积<sup>[2](https://www.stat.umn.edu/geyer/f09/5101/notes/n1.pdf)</sup>。负协方差是结构性的：一次试验被分配给类别 j 就不能同时被分配给类别 k<sup>[10](https://sunjh22.github.io/knowledge/statistics/probability-randomness/categorical-and-multinomial-distributions.html)</sup>。

## 贝叶斯推断

狄利克雷分布是贝塔分布在单纯形上的多元推广，是分类分布的共轭先验：以狄利克雷分布为概率向量的先验，观测数据后的后验也是狄利克雷分布<sup>[3](https://ermongroup.github.io/cs228-notes/learning/bayesian/)</sup>。对于计数为 \( x_1, \ldots, x_k \) 的多项似然和先验 \( \mathrm{Dirichlet}(\alpha_1, \ldots, \alpha_k) \)，后验为 \( \mathrm{Dirichlet}(x_1 + \alpha_1, \ldots, x_k + \alpha_k) \)，后验均值估计为 \( \hat{p}_i = (x_i + \alpha_i) / \sum_{j=1}^{k} (x_j + \alpha_j) \)<sup>[11](https://faculty.washington.edu/yenchic/20A_stat512/Lec7_Multinomial.html)</sup>。先验参数 \( \alpha_j \) 可解释为观测数据前类别 j 的伪计数<sup>[11](https://faculty.washington.edu/yenchic/20A_stat512/Lec7_Multinomial.html)</sup>。正是这种匹配形式使得分类模型的贝叶斯更新变得简便<sup>[12](https://haroldsoh.com/cs3264/notes/lecture-04.html)</sup>。

## 采样算法

从具有指定概率的分类分布中抽取样本是概率编程语言及其他机器学习应用中的核心操作<sup>[13](https://arxiv.org/abs/1906.11700)</sup>。沃克（Walker）的别名法是用于从任意指定离散分布生成随机变量的快速经典方法<sup>[14](https://www.tandfonline.com/servlet/linkout?type=Scholix&url=https%3A%2F%2Fdx.doi.org%2F10.1080%252F00031305.1979.10482697)</sup>。2019 年的一种方法将霍夫曼树视为表示分类分布的高效数据结构，给出了在 \( \mathcal{O}(\log(n)) \) 时间内生成样本以及添加、删除或修改类别的算法；实际采样时间与理论最优值的差距保持在该最优值的百分之几以内<sup>[13](https://arxiv.org/abs/1906.11700)</sup>。

## 在软件与模型中的使用

分类分布在标准概率编程和深度学习技术栈中均有实现。TensorFlow Probability 提供 `tfp.distributions.Categorical`，以表示未归一化对数概率的对数几率参数化，并提供 `tf.random.categorical` 用于抽取样本<sup>[15](https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/Categorical)</sup><sup> • </sup><sup>[16](https://www.tensorflow.org/api_docs/python/tf/random/categorical?authuser=485)</sup>。在 Stan 中，`categorical` 采样语句自 2.0 版起可用，对数几率版本自 2.4 版起可用，所有分类函数均已向量化，因此结果可以是单个整数或整数数组<sup>[4](https://mc-stan.org/docs/2_32/functions-reference/categorical-distribution.html)</sup>。

该分布也作为统计学习中的构件出现。scikit-learn 的 `CategoricalNB` 实现分类朴素贝叶斯，对每个特征在给定类别的条件下估计一个分类分布，特征 i 中类别 t 在给定类别 c 下的概率估计为 \( (N_{tic} + \alpha) / (N_c + \alpha n_i) \)，其中 \( \alpha \) 为平滑参数<sup>[17](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>。在强化学习和变分自编码器中，通过分类样本的梯度需要代理估计器，如得分函数（REINFORCE）估计器，因为样本无法直接反向传播；松弛版本如松弛独热分类分布将样本置于单纯形上，是可重参数化的<sup>[6](https://docs.pytorch.org/docs/stable/distributions.html)</sup>。

## References

1. [Categorical Distribution, Wolfram MathWorld](https://mathworld.wolfram.com/CategoricalDistribution.html)
2. [Stat 5101 Lecture Notes (Geyer), University of Minnesota](https://www.stat.umn.edu/geyer/f09/5101/notes/n1.pdf)
3. [Bayesian Learning, CS228 notes, Stanford](https://ermongroup.github.io/cs228-notes/learning/bayesian/)
4. [16.5 Categorical distribution, Stan Functions Reference](https://mc-stan.org/docs/2_32/functions-reference/categorical-distribution.html)
5. [Machine Learning: A Probabilistic Perspective (Murphy), Chapter 2 excerpt](https://oa.ee.tsinghua.edu.cn/ouzhijian/pgm/pgm-pdf/Murphy%20book.pdf)
6. [Probability distributions, torch.distributions, PyTorch documentation](https://docs.pytorch.org/docs/stable/distributions.html)
7. [Stan User's Guide](https://usermanual.wiki/Document/stanusersguide218.2054725045.pdf)
8. [Deep Learning (Goodfellow, Bengio, Courville), Chapter 3 excerpt](https://mcube.lab.nycu.edu.tw/~cfung/docs/books/goodfellow2016deep_learning.pdf)
9. [Statistical NLP course notes, probability refresher](https://snlp2020.github.io/notes/probability-refresher-notes.pdf)
10. [Categorical and Multinomial Distributions, Jiahong Sun](https://sunjh22.github.io/knowledge/statistics/probability-randomness/categorical-and-multinomial-distributions.html)
11. [UW STAT 512 Lecture 7: Multinomial distribution (Yen-Chi Chen)](https://faculty.washington.edu/yenchic/20A_stat512/Lec7_Multinomial.html)
12. [CS3264 Lecture 4 notes, Harold Soh](https://haroldsoh.com/cs3264/notes/lecture-04.html)
13. [Efficient algorithms for modifying and sampling from a categorical distribution, arXiv:1906.11700](https://arxiv.org/abs/1906.11700)
14. [On the Alias Method for Generating Random Variables from a Discrete Distribution, The American Statistician](https://www.tandfonline.com/servlet/linkout?type=Scholix&url=https%3A%2F%2Fdx.doi.org%2F10.1080%252F00031305.1979.10482697)
15. [tfp.distributions.Categorical, TensorFlow Probability](https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/Categorical)
16. [tf.random.categorical, TensorFlow](https://www.tensorflow.org/api_docs/python/tf/random/categorical?authuser=485)
17. [Naive Bayes, scikit-learn documentation](https://scikit-learn.org/stable/modules/naive_bayes.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论 › Probability theory › Probability distributions › Distribution families and classification › Discrete distribution families

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/categorical-distribution

English original: https://www.edgepedia.org/categorical-distribution
