Ë
    ˆ\;j×  ã                   óF   — d dl Z d dlmZ d dlmZ d dlmZ  G d„ de«      Zy)é    N)ÚNormal)ÚExpTransform)ÚTransformedDistributionc                   óT   ‡ — e Zd ZdZˆ fd„Zed„ «       Zed„ «       Zd„ Zd„ Z	d„ Z
ˆ xZS )Ú	LogNormalan  The LogNormal distribution with location `loc` and `scale` parameters.

    .. math::

        X \sim Normal(\mu, \sigma)

        Y = exp(X) \sim LogNormal(\mu, \sigma)


    Due to LogNormal distribution is based on the transformation of Normal distribution, we call that :math:`Normal(\mu, \sigma)` is the underlying distribution of :math:`LogNormal(\mu, \sigma)`

    Mathematical details

    The probability density function (pdf) is

    .. math::
        pdf(x; \mu, \sigma) = \frac{1}{\sigma x \sqrt{2\pi}}e^{(-\frac{(ln(x) - \mu)^2}{2\sigma^2})}

    In the above equation:

    * :math:`loc = \mu`: is the means of the underlying Normal distribution.
    * :math:`scale = \sigma`: is the stddevs of the underlying Normal distribution.

    Args:
        loc(int|float|list|tuple|numpy.ndarray|Tensor): The means of the underlying Normal distribution.
        scale(int|float|list|tuple|numpy.ndarray|Tensor): The stddevs of the underlying Normal distribution.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.distribution import LogNormal

            >>> # Define a single scalar LogNormal distribution.
            >>> dist = LogNormal(loc=0., scale=3.)
            >>> # Define a batch of two scalar valued LogNormals.
            >>> # The underlying Normal of first has mean 1 and standard deviation 11, the underlying Normal of second 2 and 22.
            >>> dist = LogNormal(loc=[1., 2.], scale=[11., 22.])
            >>> # Get 3 samples, returning a 3 x 2 tensor.
            >>> dist.sample((3, ))

            >>> # Define a batch of two scalar valued LogNormals.
            >>> # Their underlying Normal have mean 1, but different standard deviations.
            >>> dist = LogNormal(loc=1., scale=[11., 22.])

            >>> # Complete example
            >>> value_tensor = paddle.to_tensor([0.8], dtype="float32")

            >>> lognormal_a = LogNormal([0.], [1.])
            >>> lognormal_b = LogNormal([0.5], [2.])
            >>> sample = lognormal_a.sample((2, ))
            >>> # a random tensor created by lognormal distribution with shape: [2, 1]
            >>> entropy = lognormal_a.entropy()
            >>> print(entropy)
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [1.41893852])
            >>> lp = lognormal_a.log_prob(value_tensor)
            >>> print(lp)
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [-0.72069150])
            >>> p = lognormal_a.probs(value_tensor)
            >>> print(p)
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0.48641577])
            >>> kl = lognormal_a.kl_divergence(lognormal_b)
            >>> print(kl)
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0.34939718])
    c                 óÜ   •— t        ||¬«      | _        | j                  j                  | _        | j                  j                  | _        t        ‰| �  | j                  t        «       g«       y )N)ÚlocÚscale)r   Ú_baser	   r
   ÚsuperÚ__init__r   )Úselfr	   r
   Ú	__class__s      €úfG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/distribution/lognormal.pyr   zLogNormal.__init__\   sI   ø€ Ü ¨5Ô1ˆŒ
Ø—:‘:—>‘>ˆŒØ—Z‘Z×%Ñ%ˆŒ
Ü‰Ñ˜Ÿ™¤l£nÐ%5Õ6ó    c                 óˆ   — t        j                  | j                  j                  | j                  j                  dz  z   «      S )zYMean of lognormal distribuion.

        Returns:
            Tensor: mean value.
        é   )ÚpaddleÚexpr   ÚmeanÚvariance©r   s    r   r   zLogNormal.meanb   s/   € ô �z‰z˜$Ÿ*™*Ÿ/™/¨D¯J©J×,?Ñ,?À!Ñ,CÑCÓDÐDr   c                 óÜ   — t        j                  | j                  j                  «      t        j                  d| j                  j
                  z  | j                  j                  z   «      z  S )zbVariance of lognormal distribution.

        Returns:
            Tensor: variance value.
        r   )r   Úexpm1r   r   r   r   r   s    r   r   zLogNormal.variancek   sN   € ô �|‰|˜DŸJ™J×/Ñ/Ó0´6·:±:Ø�—
‘
—‘Ñ $§*¡*×"5Ñ"5Ñ5ó4
ñ 
ð 	
r   c                 ód   — | j                   j                  «       | j                   j                  z   S )a¦  Shannon entropy in nats.

        The entropy is

        .. math::

            entropy(\sigma) = 0.5 \log (2 \pi e \sigma^2) + \mu

        In the above equation:

        * :math:`loc = \mu`: is the mean of the underlying Normal distribution.
        * :math:`scale = \sigma`: is the stddevs of the underlying Normal distribution.

        Returns:
          Tensor: Shannon entropy of lognormal distribution.

        )r   Úentropyr   r   s    r   r   zLogNormal.entropyv   s$   € ð$ �z‰z×!Ñ!Ó# d§j¡j§o¡oÑ5Ð5r   c                 óJ   — t        j                  | j                  |«      «      S )zÂProbability density/mass function.

        Args:
          value (Tensor): The input tensor.

        Returns:
          Tensor: probability.The data type is same with :attr:`value` .

        )r   r   Úlog_prob)r   Úvalues     r   ÚprobszLogNormal.probsŠ   s   € ô �z‰z˜$Ÿ-™-¨Ó.Ó/Ð/r   c                 óL   — | j                   j                  |j                   «      S )a
  The KL-divergence between two lognormal distributions.

        The probability density function (pdf) is

        .. math::

            KL\_divergence(\mu_0, \sigma_0; \mu_1, \sigma_1) = 0.5 (ratio^2 + (\frac{diff}{\sigma_1})^2 - 1 - 2 \ln {ratio})

        .. math::

            ratio = \frac{\sigma_0}{\sigma_1}

        .. math::

            diff = \mu_1 - \mu_0

        In the above equation:

        * :math:`loc = \mu_0`: is the means of current underlying Normal distribution.
        * :math:`scale = \sigma_0`: is the stddevs of current underlying Normal distribution.
        * :math:`loc = \mu_1`: is the means of other underlying Normal distribution.
        * :math:`scale = \sigma_1`: is the stddevs of other underlying Normal distribution.
        * :math:`ratio`: is the ratio of scales.
        * :math:`diff`: is the difference between means.

        Args:
            other (LogNormal): instance of LogNormal.

        Returns:
            Tensor: kl-divergence between two lognormal distributions.

        )r   Úkl_divergence)r   Úothers     r   r"   zLogNormal.kl_divergence–   s   € ðB �z‰z×'Ñ'¨¯©Ó4Ð4r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r    r"   Ú__classcell__)r   s   @r   r   r      sH   ø„ ñDôL7ð ñEó ðEð ñ
ó ð
ò6ò(
0ö!5r   r   )r   Úpaddle.distribution.normalr   Úpaddle.distribution.transformr   Ú,paddle.distribution.transformed_distributionr   r   © r   r   Ú<module>r.      s"   ðó Ý -Ý 6Ý Pôb5Ð'õ b5r   