Geometric¶
- class paddle.distribution. Geometric ( probs ) [source]
-
Geometric distribution parameterized by probs.
In probability theory and statistics, the geometric distribution is one of discrete probability distributions, parameterized by one positive shape parameter, denoted by probs. In n Bernoulli trials, it takes k+1 trials to get the probability of success for the first time. In detail, it is: the probability that the first k times failed and the kth time succeeded. The geometric distribution is a special case of the Pascal distribution when r=1.
The probability mass function (pmf) is
\[Pr(Y=k)=(1-p)^kp\]where k is number of trials failed before seeing a success, and p is probability of success for each trial and k=0,1,2,3,4…, p belong to (0,1].
- Parameters
-
probs (Real|Tensor) – Probability parameter. The value of probs must be positive. When the parameter is a tensor, probs is probability of success for each trial.
- Returns
-
Geometric distribution for instantiation of probs.
Examples
>>> import paddle >>> from paddle.distribution import Geometric >>> geom = Geometric(0.5) >>> print(geom.mean) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 1.) >>> print(geom.variance) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 2.) >>> print(geom.stddev) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 1.41421354)
-
probs
(
value
)
probs¶
-
Probability density/mass function.
Note
This method will be deprecated in the future, please use prob instead.
- property mean
-
Mean of geometric distribution.
- property variance
-
Variance of geometric distribution.
- property stddev
-
Standard deviation of Geometric distribution.
-
pmf
(
k
)
pmf¶
-
Probability mass function evaluated at k.
\[P(X=k) = (1-p)^{k} p, \quad k=0,1,2,3,\ldots\]- Parameters
-
k (int) – Value to be evaluated.
- Returns
-
Probability.
- Return type
-
Tensor
Examples
>>> import paddle >>> from paddle.distribution import Geometric >>> geom = Geometric(0.5) >>> print(geom.pmf(2)) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.12500000)
-
log_pmf
(
k
)
log_pmf¶
-
Log probability mass function evaluated at k.
\[\log P(X = k) = \log(1-p)^k p\]- Parameters
-
k (int) – Value to be evaluated.
- Returns
-
Log probability.
- Return type
-
Tensor
Examples
>>> import paddle >>> from paddle.distribution import Geometric >>> geom = Geometric(0.5) >>> print(geom.log_pmf(2)) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, -2.07944131)
-
sample
(
shape=()
)
sample¶
-
Sample from Geometric distribution with sample shape.
- Parameters
-
shape (tuple(int)) – Sample shape.
- Returns
-
Sampled data with shape sample_shape + batch_shape + event_shape.
Examples
>>> import paddle >>> from paddle.distribution import Geometric >>> paddle.seed(2023) >>> geom = Geometric(0.5) >>> print(geom.sample((2,2))) Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[0., 0.], [1., 0.]])
-
rsample
(
shape=()
)
rsample¶
-
Generate samples of the specified shape.
- Parameters
-
shape (tuple(int)) – The shape of generated samples.
- Returns
-
A sample tensor that fits the Geometric distribution.
- Return type
-
Tensor
Examples
>>> import paddle >>> from paddle.distribution import Geometric >>> paddle.seed(2023) >>> geom = Geometric(0.5) >>> print(geom.rsample((2,2))) Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[0., 0.], [1., 0.]])
-
entropy
(
)
entropy¶
-
Entropy of dirichlet distribution.
\[H(X) = -\left[\frac{1}{p} \log p + \frac{1-p}{p^2} \log (1-p) \right]\]- Returns
-
Entropy.
- Return type
-
Tensor
Examples
>>> import paddle >>> from paddle.distribution import Geometric >>> geom = Geometric(0.5) >>> print(geom.entropy()) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 1.38629425)
-
cdf
(
k
)
cdf¶
-
Cdf of geometric distribution.
\[F(X \leq k) = 1 - (1-p)^(k+1), \quad k=0,1,2,\ldots\]- Parameters
-
k – The number of trials performed.
- Returns
-
Entropy.
- Return type
-
Tensor
Examples
>>> import paddle >>> from paddle.distribution import Geometric >>> geom = Geometric(0.5) >>> print(geom.cdf(4)) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.96875000)
-
kl_divergence
(
other
)
[source]
kl_divergence¶
-
Calculate the KL divergence KL(self || other) with two Geometric instances.
\[KL(P \| Q) = \frac{p}{q} \log \frac{p}{q} + \log (1-p) - \log (1-q)\]- Parameters
-
other (Geometric) – An instance of Geometric.
- Returns
-
The kl-divergence between two geometric distributions.
- Return type
-
Tensor
Examples
>>> import paddle >>> from paddle.distribution import Geometric >>> geom_p = Geometric(0.5) >>> geom_q = Geometric(0.1) >>> print(geom_p.kl_divergence(geom_q)) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.51082563)
- property batch_shape
-
Returns batch shape of distribution
- Returns
-
batch shape
- Return type
-
Sequence[int]
- property event_shape
-
Returns event shape of distribution
- Returns
-
event shape
- Return type
-
Sequence[int]
-
log_prob
(
value
)
log_prob¶
-
Log probability density/mass function.
-
prob
(
value
)
prob¶
-
Probability density/mass function evaluated at value.
- Parameters
-
value (Tensor) – value which will be evaluated