geometric.py 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numbers

import numpy as np

import paddle
from paddle.distribution import distribution, uniform
from paddle.fluid import framework


class Geometric(distribution.Distribution):
    r"""
    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 trials to get the probability of success for the first time.
    In detail, it is: the probability that the first k-1 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

    .. math::
            Pr(Y=k)=(1-p)^kp

    where k is number of trials performed and p is probability of success for each trial and k=0,1,2,3,4..., p belong to (0,1].

    Args:
        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:

        .. code-block:: python

            import paddle
            from paddle.distribution import Geometric

            geom = Geometric(0.5)

            geom.mean
            # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            #        [2.])

            geom.variance
            # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            #        [2.])

            geom.stddev
            # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            #        [1.41421354])
    """

    def __init__(self, probs):
        if isinstance(probs, (numbers.Real, paddle.Tensor, framework.Variable)):
            if isinstance(probs, numbers.Real):
                probs = paddle.full(
                    shape=(), fill_value=probs, dtype=paddle.float32
                )

            all_ones = paddle.full(
                shape=probs.shape, fill_value=1, dtype=probs.dtype
            )
            all_zeros = paddle.full(
                shape=probs.shape, fill_value=0, dtype=probs.dtype
            )
            all_false = paddle.full(
                shape=probs.shape, fill_value=False, dtype=bool
            )

            lessthen_0 = probs <= all_zeros
            morethen_1 = probs > all_ones

        else:
            raise TypeError(
                f"Expected type of probs is Number.Real|Tensor|framework.Variable, but got {type(probs)}"
            )

        if paddle.equal_all(lessthen_0, all_false) and paddle.equal_all(
            morethen_1, all_false
        ):
            batch_shape = tuple(probs.shape)
        else:
            raise ValueError(
                "Expected parameter probs of distribution Geometric to satisfy the"
                "constraint Interval(lower_bound=0.0, upper_bound=1.0)"
            )

        self.probs = probs
        super().__init__(batch_shape)

    @property
    def mean(self):
        """Mean of geometric distribution."""
        return 1.0 / self.probs

    @property
    def variance(self):
        """Variance of geometric distribution."""
        return paddle.to_tensor(
            (1.0 / self.probs - 1.0) / self.probs,
            dtype=self.probs.dtype,
        )

    @property
    def stddev(self):
        """Standard deviation of Geometric distribution."""
        return paddle.sqrt(self.variance)

    def pmf(self, k):
        r"""Probability mass funciotn evaluated at k.

        .. math::

            P(X=k) = (1-p)^{k-1} p, \quad k=1,2,3,\ldots

        Args:
            k (int): Value to be evaluated.

        Returns:
            Tensor: Probability.

        Examples:

            .. code-block:: python

                import paddle
                from paddle.distribution import Geometric

                geom = Geometric(0.5)
                geom.pmf(2)
                # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [0.25000000])
        """
        if isinstance(k, (numbers.Integral, framework.Variable)):
            return paddle.pow((1.0 - self.probs), k - 1.0) * self.probs
        else:
            raise TypeError(
                f"Expected type of k is number.Real|framework.Variable, but got {type(k)}"
            )

    def log_pmf(self, k):
        r"""Log probability mass function evaluated at k.

        .. math::
            \log P(X = k) = \log(1-p)^k p

        Args:
            k (int): Value to be evaluated.

        Returns:
            Tensor: Log probability.

        Examples:

            .. code-block:: python

                import paddle
                from paddle.distribution import Geometric

                geom = Geometric(0.5)
                geom.log_pmf(2)
                # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [-1.38629436])
        """
        if isinstance(k, (numbers.Integral, framework.Variable)):
            return paddle.log(self.pmf(k))
        else:
            raise TypeError(
                f"Expected type of k is number.Real|framework.Variable, but got {type(k)}"
            )

    def sample(self, shape=()):
        """Sample from Geometric distribution with sample shape.

        Args:
            shape (tuple(int)): Sample shape.

        Returns:
            Sampled data with shape `sample_shape` + `batch_shape` + `event_shape`.

        Examples:

            .. code-block:: python

                import paddle
                from paddle.distribution import Geometric

                geom = Geometric(0.5)
                geom.sample((2,2))
                # Tensor(shape=[2, 2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [[[4.28128004],
                #         [0.53546447]],
                #         [[0.88012987],
                #         [0.54070371]]])
        """
        with paddle.no_grad():
            return self.rsample(shape)

    def rsample(self, shape=()):
        """Generate samples of the specified shape.

        Args:
            shape(tuple(int)): The shape of generated samples.

        Returns:
            Tensor: A sample tensor that fits the Geometric distribution.

        Examples:

            .. code-block:: python

                import paddle
                from paddle.distribution import Geometric
                geom = Geometric(0.5)
                geom.rsample((2,2))
                # Tensor(shape=[2, 2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [[[2.90974379],
                #         [1.28049409]],
                #         [[4.60141420],
                #         [2.98836184]]])

        """
        shape = distribution.Distribution._extend_shape(
            self, sample_shape=shape
        )
        tiny = np.finfo(dtype='float32').tiny

        sample_uniform = uniform.Uniform(low=float(tiny), high=float(1))

        new_t = sample_uniform.sample(list(shape))
        return paddle.log(new_t) / paddle.log1p(-(self.probs))

    def entropy(self):
        r"""Entropy of dirichlet distribution.

        .. math::

            H(X) = -\left[\frac{1}{p} \log p + \frac{1-p}{p^2} \log (1-p) \right]

        Returns:
            Tensor: Entropy.

        Examples:

            .. code-block:: python

                import paddle
                from paddle.distribution import Geometric

                geom = Geometric(0.5)
                geom.entropy()
                # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [1.38629436])
        """
        x = (1.0 - self.probs) * paddle.log(1.0 - self.probs)
        y = self.probs * paddle.log(self.probs)

        return -(x + y) / self.probs

    def cdf(self, k):
        r"""Cdf of geometric distribution.

        .. math::

            F(X \leq k) = 1 - (1-p)^k, \quad k=0,1,2,\ldots

        Args:
            k: The number of trials performed.

        Returns:
            Tensor: Entropy.

        Examples:

            .. code-block:: python

                import paddle
                from paddle.distribution import Geometric

                geom = Geometric(0.5)
                geom.cdf(4)
                # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [0.93750000])
        """
        if isinstance(k, (numbers.Integral, framework.Variable)):
            return 1.0 - paddle.pow((1.0 - self.probs), k)
        else:
            raise TypeError(
                f"Expected type of k is number.Real|framework.Variable, but got {type(k)}"
            )

    def kl_divergence(self, other):
        r"""Calculate the KL divergence KL(self || other) with two Geometric instances.

        .. math::

            KL(P \| Q) = \frac{p}{q} \log \frac{p}{q} + \log (1-p) - \log (1-q)

        Args:
            other (Geometric): An instance of Geometric.

        Returns:
            Tensor: The kl-divergence between two geometric distributions.

        Examples:

            .. code-block:: python

                import paddle
                from paddle.distribution import Geometric

                geom_p = Geometric(0.5)
                geom_q = Geometric(0.1)
                geom_p.kl_divergence(geom_q)
                # Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                #        [0.51082563])
        """
        if isinstance(other, Geometric):
            p, q = self.probs, other.probs
            return p * paddle.log(p / q) + (1.0 - p) * paddle.log(
                (1.0 - p) / (1.0 - q)
            )
        else:
            raise TypeError(
                f"Exected type of other is geometric.Geometric, but got {type(other)}"
            )