Skip to content

Commit 56fd31e

Browse files
authored
【Hackathon 5th No. 18】Add Binomial and Poisson API (#6325)
1 parent d527f66 commit 56fd31e

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
.. _cn_api_paddle_distribution_Binomial:
2+
3+
Binomial
4+
-------------------------------
5+
6+
.. py:class:: paddle.distribution.Binomial(total_count, probs)
7+
8+
9+
在概率论和统计学中,Binomial 是一种最基本的离散型概率分布,定义在 :math:`[0, n] \cap \mathbb{N}` 上,可以看作是扔一枚硬币(可能是不公平的硬币)被扔出正面的次数,
10+
它的随机变量的取值可以看作是一系列独立的伯努利实验结果的总和。
11+
12+
其概率质量函数(pmf)为:
13+
14+
.. math::
15+
16+
pmf(x; n, p) = \frac{n!}{x!(n-x)!}p^{x}(1-p)^{n-x}
17+
18+
其中:
19+
20+
- :math:`n` 表示伯努利实验次数。
21+
- :math:`p` 表示每次伯努利实验中事件发生的概率。
22+
23+
参数
24+
:::::::::
25+
26+
- **total_count** (int|Tensor) - 即上述公式中 :math:`n` 参数,大于零,表示伯努利实验次数。如果 :attr:`total_count` 的输入数据类型是 int 则会被转换
27+
成数据类型为 paddle 全局默认数据类型的 1-D Tensor,否则将转换成与 :attr:`probs` 相同的数据类型。
28+
29+
- **probs** (float|Tensor) - 即上述公式中 :math:`p` 参数,在 [0, 1] 区间内,表示每次伯努利实验中事件发生的概率。如果 :attr:`probs` 的输
30+
入数据类型是 float 则会被转换为 paddle 全局默认数据类型的 1-D Tensor。
31+
32+
33+
代码示例
34+
:::::::::
35+
36+
COPY-FROM: paddle.distribution.Binomial
37+
38+
属性
39+
:::::::::
40+
41+
mean
42+
'''''''''
43+
44+
Binomial 分布的均值
45+
46+
**返回**
47+
48+
Tensor,均值
49+
50+
variance
51+
'''''''''
52+
53+
Binomial 分布的方差
54+
55+
**返回**
56+
57+
Tensor,方差
58+
59+
方法
60+
:::::::::
61+
62+
prob(value)
63+
'''''''''
64+
65+
计算 value 的概率。
66+
67+
**参数**
68+
69+
- **value** (Tensor) - 待计算值。
70+
71+
**返回**
72+
73+
Tensor,value 的概率。数据类型与 :attr:`probs` 相同。
74+
75+
76+
log_prob(value)
77+
'''''''''
78+
79+
计算 value 的对数概率。
80+
81+
**参数**
82+
83+
- **value** (Tensor) - 待计算值。
84+
85+
**返回**
86+
87+
Tensor,value 的对数概率。数据类型与 :attr:`probs` 相同。
88+
89+
90+
sample()
91+
'''''''''
92+
93+
从 Binomial 分布中生成满足特定形状的样本数据。最终生成样本形状为 ``shape+batch_shape`` 。
94+
95+
**参数**
96+
97+
- **shape** (Sequence[int],可选):采样次数。
98+
99+
**返回**
100+
101+
Tensor:样本数据。其维度为 :math:`\text{sample shape} + \text{batch shape}` 。
102+
103+
entropy()
104+
'''''''''
105+
106+
计算 Binomial 分布的信息熵。
107+
108+
.. math::
109+
110+
\mathcal{H}(X) = - \sum_{x \in \Omega} p(x) \log{p(x)}
111+
112+
**返回**
113+
114+
类别分布的信息熵,数据类型与 :attr:`probs` 相同。
115+
116+
kl_divergence(other)
117+
'''''''''
118+
119+
相对于另一个类别分布的 KL 散度,两个分布需要有相同的 :attr:`total_count`。
120+
121+
**参数**
122+
123+
- **other** (Binomial) - 输入的另一个类别分布。
124+
125+
**返回**
126+
127+
相对于另一个类别分布的 KL 散度,数据类型与 :attr:`probs` 相同。

docs/api/paddle/distribution/Overview_cn.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ paddle.distribution 目录下包含飞桨框架支持的随机变量的概率分
2323
" :ref:`Distribution <cn_api_paddle_distribution_Distribution>` ", "Distribution 概率分布抽象基类"
2424
" :ref:`ExponentialFamily <cn_api_paddle_distribution_ExponentialFamily>` ", "ExponentialFamily 指数型分布族基类"
2525
" :ref:`Bernoulli <cn_api_paddle_distribution_Bernoulli>` ", "Bernoulli 概率分布类"
26+
" :ref:`Binomial <cn_api_paddle_distribution_Binomial>` ", "Binomial 概率分布类"
2627
" :ref:`ContinuousBernoulli <cn_api_paddle_distribution_ContinuousBernoulli>` ", "ContinuousBernoulli 概率分布类"
2728
" :ref:`Categorical <cn_api_paddle_distribution_Categorical>` ", "Categorical 概率分布类"
2829
" :ref:`Cauchy <cn_api_paddle_distribution_Cauchy>` ", "Cauchy 概率分布类"
@@ -36,6 +37,7 @@ paddle.distribution 目录下包含飞桨框架支持的随机变量的概率分
3637
" :ref:`TransfomedDistribution <cn_api_paddle_distribution_TransformedDistribution>` ", "TransformedDistribution 概率分布类"
3738
" :ref:`Laplace <cn_api_paddle_distribution_Laplace>`", "Laplace 概率分布类"
3839
" :ref:`LogNormal <cn_api_paddle_distribution_LogNormal>` ", "LogNormal 概率分布类"
40+
" :ref:`Poisson <cn_api_paddle_distribution_Poisson>` ", "Poisson 概率分布类"
3941
" :ref:`Gumbel <cn_api_paddle_distribution_Gumbel>` ", "Gumbel 概率分布类"
4042
" :ref:`Geometric <cn_api_paddle_distribution_Geometric>` ", "Geometric 概率分布类"
4143

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
.. _cn_api_paddle_distribution_Poisson:
2+
3+
Poisson
4+
-------------------------------
5+
6+
.. py:class:: paddle.distribution.Poisson(rate)
7+
8+
9+
在概率论和统计学中,Poisson 是一种最基本的离散型概率分布,定义在非负整数集上,用来描述单位时间内随机事件发生次数的概率分布。
10+
11+
其概率质量函数(pmf)为:
12+
13+
.. math::
14+
15+
pmf(x; \lambda) = \frac{e^{-\lambda} \cdot \lambda^x}{x!}
16+
17+
其中,:math:`\lambda` 表示事件平均发生率。
18+
19+
20+
参数
21+
:::::::::
22+
23+
- **rate** (int|float|Tensor) - 即上述公式中 :math:`\lambda` 参数,大于零,表示事件平均发生率,即单位时间内的事件发生次数。如果输入数据类型不是 int 或 float
24+
:attr:`rate` 的数据类型会被转换成数据类型为 paddle 全局默认数据类型的 1-D Tensor
25+
26+
代码示例
27+
:::::::::
28+
29+
COPY-FROM: paddle.distribution.Poisson
30+
31+
属性
32+
:::::::::
33+
34+
mean
35+
'''''''''
36+
37+
Poisson 分布的均值
38+
39+
**返回**
40+
41+
Tensor,均值
42+
43+
variance
44+
'''''''''
45+
46+
Poisson 分布的方差
47+
48+
**返回**
49+
50+
Tensor,方差
51+
52+
方法
53+
:::::::::
54+
55+
prob(value)
56+
'''''''''
57+
58+
计算 value 的概率。
59+
60+
**参数**
61+
62+
- **value** (Tensor) - 待计算值。
63+
64+
**返回**
65+
66+
Tensor,value 的概率。数据类型与 :attr:`rate` 相同。
67+
68+
69+
log_prob(value)
70+
'''''''''
71+
72+
计算 value 的对数概率。
73+
74+
**参数**
75+
76+
- **value** (Tensor) - 待计算值。
77+
78+
**返回**
79+
80+
Tensor,value 的对数概率。数据类型与 :attr:`rate` 相同。
81+
82+
83+
sample()
84+
'''''''''
85+
86+
从 Poisson 分布中生成满足特定形状的样本数据。最终生成样本形状为 ``shape+batch_shape`` 。
87+
88+
**参数**
89+
90+
- **shape** (Sequence[int],可选):采样次数。
91+
92+
**返回**
93+
94+
Tensor,样本数据。其维度为 :math:`\text{sample shape} + \text{batch shape}` 。
95+
96+
entropy()
97+
'''''''''
98+
99+
计算 Poisson 分布的信息熵。
100+
101+
.. math::
102+
103+
\mathcal{H}(X) = - \sum_{x \in \Omega} p(x) \log{p(x)}
104+
105+
**返回**
106+
107+
类别分布的信息熵,数据类型与 :attr:`rate` 相同。
108+
109+
kl_divergence(other)
110+
'''''''''
111+
112+
相对于另一个类别分布的 KL 散度,两个分布需要有相同的 :math:`\text{batch shape}`。
113+
114+
**参数**
115+
116+
- **other** (Poisson) - 输入的另一个类别分布。
117+
118+
**返回**
119+
120+
相对于另一个类别分布的 KL 散度,数据类型与 :attr:`rate` 相同。

0 commit comments

Comments
 (0)