
    }Yh                         d dl mZ d dlZd dlmZmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZ dgZ G d	 de          ZdS )
    )OptionalN)infTensor)Categoricalconstraints)Binomial)Distribution)broadcast_allMultinomialc                       e Zd ZU dZej        ej        dZee	d<   e
defd            Ze
defd            Z	 	 	 	 dded	ee         d
ee         dee         ddf
 fdZd fd	Zd Z ej        dd          d             Ze
defd            Ze
defd            Ze
dej        fd            Z ej                    fdZd Zd Z xZS )r   a`  
    Creates a Multinomial distribution parameterized by :attr:`total_count` and
    either :attr:`probs` or :attr:`logits` (but not both). The innermost dimension of
    :attr:`probs` indexes over categories. All other dimensions index over batches.

    Note that :attr:`total_count` need not be specified if only :meth:`log_prob` is
    called (see example below)

    .. note:: The `probs` argument must be non-negative, finite and have a non-zero sum,
              and it will be normalized to sum to 1 along the last dimension. :attr:`probs`
              will return this normalized value.
              The `logits` argument will be interpreted as unnormalized log probabilities
              and can therefore be any real number. It will likewise be normalized so that
              the resulting probabilities sum to 1 along the last dimension. :attr:`logits`
              will return this normalized value.

    -   :meth:`sample` requires a single shared `total_count` for all
        parameters and samples.
    -   :meth:`log_prob` allows different `total_count` for each parameter and
        sample.

    Example::

        >>> # xdoctest: +SKIP("FIXME: found invalid values")
        >>> m = Multinomial(100, torch.tensor([ 1., 1., 1., 1.]))
        >>> x = m.sample()  # equal probability of 0, 1, 2, 3
        tensor([ 21.,  24.,  30.,  25.])

        >>> Multinomial(probs=torch.tensor([1., 1., 1., 1.])).log_prob(x)
        tensor([-4.1338])

    Args:
        total_count (int): number of trials
        probs (Tensor): event probabilities
        logits (Tensor): event log probabilities (unnormalized)
    probslogitstotal_countreturnc                      | j         | j        z  S N)r   r   selfs    q/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/distributions/multinomial.pymeanzMultinomial.mean8   s    zD,,,    c                 6    | j         | j        z  d| j        z
  z  S )N   r   r   r   s    r   variancezMultinomial.variance<   s    $*,DJ??r   r   Nr   r   validate_argsc                 H   t          |t                    st          d          || _        t	          ||          | _        t          || j                  | _        | j        j	        }| j        j
        dd          }t                                          |||           d S )Nz*inhomogeneous total_count is not supportedr   r   r   )
isinstanceintNotImplementedErrorr   r   _categoricalr   r   	_binomialbatch_shapeparam_shapesuper__init__)r   r   r   r   r   r&   event_shape	__class__s          r   r)   zMultinomial.__init__@   s     +s++ 	T%&RSSS&'eFCCC!kLLL'3'3BCC8kOOOOOr   c                 4   |                      t          |          }t          j        |          }| j        |_        | j                            |          |_        t          t          |                              || j	        d           | j
        |_
        |S )NFr    )_get_checked_instancer   torchSizer   r$   expandr(   r)   r*   _validate_args)r   r&   	_instancenewr+   s       r   r0   zMultinomial.expandP   s    ((i@@j--*,33K@@k3(() 	) 	
 	
 	
 "0
r   c                 &     | j         j        |i |S r   )r$   _new)r   argskwargss      r   r5   zMultinomial._new[   s    %t %t6v666r   T)is_discrete	event_dimc                 4    t          j        | j                  S r   )r   multinomialr   r   s    r   supportzMultinomial.support^   s    &t'7888r   c                     | j         j        S r   )r$   r   r   s    r   r   zMultinomial.logitsb   s     ''r   c                     | j         j        S r   )r$   r   r   s    r   r   zMultinomial.probsf   s     &&r   c                     | j         j        S r   )r$   r'   r   s    r   r'   zMultinomial.param_shapej   s     ,,r   c                 N   t          j        |          }| j                            t          j        | j        f          |z             }t          t          |                                                    }|                    |	                    d                      |j
        | }|                    |                     |                                                    }|                    d|t          j        |                     |                    | j                  S )Nr   r   )r.   r/   r$   sampler   listrangedimappendpoppermuter3   _extended_shapezero_scatter_add_	ones_liketype_asr   )r   sample_shapesamplesshifted_idxcountss        r   rA   zMultinomial.samplen   s    z,//#**J(*++l:
 

 5//00;??1--...!'/;/T11,??@@FFHHB)A)ABBB~~dj)))r   c                    t          j        | j                  }| j                                        }||z  t          j        |dz             z
  }| j                            d          dd          }t          j        | j        	                    |                    }t          j        |dz             }||z  
                    ddg          }||z   S )Nr   F)r0   r   r   )r.   tensorr   r$   entropylgammar%   enumerate_supportexplog_probsum)r   ncat_entropyterm1r<   binomial_probsweightsterm2s           r   rS   zMultinomial.entropy|   s    L)**'//11K%,q1u"5"55.22%2@@D4>#:#:7#C#CDD,w{++')..2w77u}r   c                    | j         r|                     |           t          | j        |          \  }}|                    t
          j                  }t          j        |                    d          dz             }t          j        |dz                                 d          }d||dk    |t           k    z  <   ||z                      d          }||z
  |z   S )N)memory_formatr   r   r   )
r1   _validate_sampler
   r   cloner.   contiguous_formatrT   rX   r   )r   valuer   log_factorial_nlog_factorial_xs
log_powerss         r   rW   zMultinomial.log_prob   s     	)!!%(((%dk599E,CDD,uyy}}q'899 <	2266r::23
v#~./un))"--
!11J>>r   )r   NNNr   )__name__
__module____qualname____doc__r   simplexreal_vectorarg_constraintsr"   __annotations__propertyr   r   r   r   boolr)   r0   r5   dependent_propertyr<   r   r   r.   r/   r'   rA   rS   rW   __classcell__)r+   s   @r   r   r      s+        # #J !, 3{?VWWO-f - - - X- @& @ @ @ X@
 "&#'(,P PP P  	P
  ~P 
P P P P P P 	 	 	 	 	 	7 7 7 $[#BBB9 9 CB9 ( ( ( ( X( 'v ' ' ' X' -UZ - - - X- #-%*,, * * * *  	? 	? 	? 	? 	? 	? 	?r   )typingr   r.   r   r   torch.distributionsr   r   torch.distributions.binomialr    torch.distributions.distributionr	   torch.distributions.utilsr
   __all__r    r   r   <module>r{      s                   8 8 8 8 8 8 8 8 1 1 1 1 1 1 9 9 9 9 9 9 3 3 3 3 3 3 /C? C? C? C? C?, C? C? C? C? C?r   