
    קgM	                     <    d dl Z d dlmZ dgZ G d de          ZdS )    N)DistributionExponentialFamilyc                   J    e Zd ZdZed             Zd Zed             Zd ZdS )r   a  
    ExponentialFamily is the abstract base class for probability distributions belonging to an
    exponential family, whose probability mass/density function has the form is defined below

    .. math::

        p_{F}(x; \theta) = \exp(\langle t(x), \theta\rangle - F(\theta) + k(x))

    where :math:`\theta` denotes the natural parameters, :math:`t(x)` denotes the sufficient statistic,
    :math:`F(\theta)` is the log normalizer function for a given family and :math:`k(x)` is the carrier
    measure.

    Note:
        This class is an intermediary between the `Distribution` class and distributions which belong
        to an exponential family mainly to check the correctness of the `.entropy()` and analytic KL
        divergence methods. We use this class to compute the entropy and KL divergence using the AD
        framework and Bregman divergences (courtesy of: Frank Nielsen and Richard Nock, Entropies and
        Cross-entropies of Exponential Families).
    c                     t           )zv
        Abstract method for natural parameters. Returns a tuple of Tensors based
        on the distribution
        NotImplementedErrorselfs    Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/distributions/exp_family.py_natural_paramsz!ExponentialFamily._natural_params   
     "!    c                     t           )z
        Abstract method for log normalizer function. Returns a log normalizer based on
        the distribution and input
        r   )r
   natural_paramss     r   _log_normalizerz!ExponentialFamily._log_normalizer&   s
    
 "!r   c                     t           )zp
        Abstract method for expected carrier measure, which is required for computing
        entropy.
        r   r	   s    r   _mean_carrier_measurez'ExponentialFamily._mean_carrier_measure-   r   r   c                 V   | j          }d | j        D             } | j        | }t          j                            |                                |d          }||z  }t          ||          D ];\  }}|||z                      | j	        dz                                 d          z  }<|S )z_
        Method to compute the entropy using Bregman divergence of the log normalizer.
        c                 Z    g | ](}|                                                                 )S  )detachrequires_grad_).0ps     r   
<listcomp>z-ExponentialFamily.entropy.<locals>.<listcomp>:   s,    MMM1188::,,..MMMr   T)create_graph)r   )
r   r   r   torchautogradgradsumzipreshape_batch_shape)r
   resultnparams	lg_normal	gradientsnpgs          r   entropyzExponentialFamily.entropy5   s     ,,MM8LMMM(D('2	N''	t'TT	)),, 	J 	JEBrAv&&t'85'@AAEEbIIIFFr   N)	__name__
__module____qualname____doc__propertyr   r   r   r+   r   r   r   r   r   	   sr         ( " " X"" " " " " X"    r   )r    torch.distributions.distributionr   __all__r   r   r   r   <module>r3      s^     9 9 9 9 9 9 
7 7 7 7 7 7 7 7 7 7r   