Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 5th No.18】Add Binomial and Poisson API -part #57856

Merged
merged 29 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update docs
  • Loading branch information
NKNaN committed Dec 19, 2023
commit 5bf911a744dd3afbd88ec4f08e2059433027b595
21 changes: 10 additions & 11 deletions python/paddle/distribution/binomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Binomial(distribution.Distribution):
r"""
The Binomial distribution with size `total_count` and `probability` parameters.

In probability theory and statistics, the binomial distribution is the most basic probability distribution,
In probability theory and statistics, the binomial distribution is the most basic discrete probability distribution defined on :math:`[0, n] \cap \mathbb{N}`,
which can be viewed as the number of times a potentially unfair coin is tossed to get heads, and the result
of its random variable can be viewed as the sum of a series of independent Bernoulli experiments.

Expand All @@ -38,15 +38,14 @@ class Binomial(distribution.Distribution):

In the above equation:

* :math:`total_count = n`: is the size.
* :math:`probability = p`: is the probability.
* :math:`total_count = n`: is the size, meaning the total number of Bernoulli experiments.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* :math:`total_count = n`: is the size, meaning the total number of Bernoulli experiments.
* :math:`total\_count = n`: is the size, meaning the total number of Bernoulli experiments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

* :math:`probability = p`: is the probability of the event happening in one Bernoulli experiments.

Args:
total_count(int|Tensor): The size of Binomial distribution, meaning the number of independent bernoulli
trials with probability parameter p. The random variable counts the number of success
among n independent bernoulli trials. The data type of `total_count` will be convert to float32.
probability(float|Tensor): The probability of Binomial distribution, meaning the probability of success
for each individual bernoulli trial. The data type of `probability` will be convert to float32.
total_count(int|Tensor): The size of Binomial distribution which should be greater than 0, meaning the number of independent bernoulli
trials with probability parameter :math:`p`. The data type of :attr:`total_count` will be convert to float32.
probability(float|Tensor): The probability of Binomial distribution which should reside in [0, 1], meaning the probability of success
for each individual bernoulli trial. The data type of :attr:`probability` will be convert to float32.

Examples:
.. code-block:: python
Expand Down Expand Up @@ -141,13 +140,13 @@ def variance(self):
return self.total_count * self.probability * (1 - self.probability)

def sample(self, shape=()):
"""Generate binomial samples of the specified shape.
"""Generate binomial samples of the specified shape. The final shape would be ``shape+batch_shape`` .

Args:
shape (Sequence[int], optional): Prepended shape of the generated samples.

Returns:
Tensor: A tensor with prepended dimensions shape. The data type is float32.
Tensor: Sampled data with shape `sample_shape` + `batch_shape`.
"""
if not isinstance(shape, Iterable):
raise TypeError('sample shape must be Iterable object.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterable 和 Sequence 语义略有区别 https://stackoverflow.com/questions/72157296/what-is-the-difference-between-type-hinting-a-variable-as-an-iterable-versus-a-s ,此处应该是只需要传入Sequence

Expand Down Expand Up @@ -259,7 +258,7 @@ def prob(self, value):
return paddle.exp(self.log_prob(value))

def kl_divergence(self, other):
r"""The KL-divergence between two binomial distributions.
r"""The KL-divergence between two binomial distributions with the same :attr:`total_count`.

The probability density function (pdf) is

Expand Down
12 changes: 6 additions & 6 deletions python/paddle/distribution/poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class Poisson(distribution.Distribution):
r"""
The Poisson distribution with occurrence rate parameter: `rate`.

In probability theory and statistics, the Poisson distribution is the most basic probability
distribution, which is used to describe the probability distribution of the number of random
In probability theory and statistics, the Poisson distribution is the most basic discrete probability
distribution defined on the nonnegative integer set, which is used to describe the probability distribution of the number of random
events occurring per unit time.

The probability mass function (pmf) is
Expand All @@ -41,7 +41,7 @@ class Poisson(distribution.Distribution):
* :math:`rate = \lambda`: is the mean occurrence rate.

Args:
rate(int|float|Tensor): The mean occurrence rate of Poisson distribution, meaning the expected occurrence
rate(int|float|Tensor): The mean occurrence rate of Poisson distribution which should be greater than 0, meaning the expected occurrence
times of an event in a fixed time interval. The data type of `rate` will be convert to float32.

Examples:
Expand Down Expand Up @@ -133,13 +133,13 @@ def variance(self):
return self.rate

def sample(self, shape=()):
"""Generate poisson samples of the specified shape.
"""Generate poisson samples of the specified shape. The final shape would be ``shape+batch_shape`` .

Args:
shape (Sequence[int], optional): Prepended shape of the generated samples.

Returns:
Tensor: A tensor with prepended dimensions shape. The data type is float32.
Tensor: Sampled data with shape `sample_shape` + `batch_shape`.
"""
if not isinstance(shape, Iterable):
raise TypeError('sample shape must be Iterable object.')
Expand Down Expand Up @@ -245,7 +245,7 @@ def prob(self, value):
return paddle.exp(self.log_prob(value))

def kl_divergence(self, other):
r"""The KL-divergence between two poisson distributions.
r"""The KL-divergence between two poisson distributions with the same `batch_shape`.

The probability density function (pdf) is

Expand Down