
    [Ths                     l    S SK r S SK JrJr  S SKJrJr  S SKJr  S SKJ	r	  S SK
Jr  S/r " S S\	5      rg)	    N)infTensor)Categoricalconstraints)Binomial)Distribution)broadcast_allMultinomialc                   t  ^  \ rS rSr% Sr\R                  \R                  S.r\	\
S'   \S\4S j5       r\S\4S j5       rSU 4S	 jjrSU 4S
 jjrS r\R$                  " SSS9S 5       r\S\4S j5       r\S\4S j5       r\S\R.                  4S j5       r\R.                  " 5       4S jrS rS rSrU =r$ )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                 4    U R                   U R                  -  $ N)r   r   selfs    W/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributions/multinomial.pymeanMultinomial.mean6   s    zzD,,,,    c                 T    U R                   U R                  -  SU R                  -
  -  $ )N   r   r   r   s    r   varianceMultinomial.variance:   s$    $**,DJJ??r   r   c                   > [        U[        5      (       d  [        S5      eXl        [	        X#S9U l        [        XR                  S9U l        U R
                  R                  nU R
                  R                  SS  n[        TU ]1  XVUS9  g )Nz*inhomogeneous total_count is not supportedr   r   validate_args)
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+   Multinomial.__init__>   sz    +s++%&RSS&'eC!kL''33''33BC8Or   c                   > U R                  [        U5      n[        R                  " U5      nU R                  Ul        U R
                  R                  U5      Ul        [        [        U]#  XR                  SS9  U R                  Ul
        U$ )NFr!   )_get_checked_instancer
   torchSizer   r&   expandr*   r+   r,   _validate_args)r   r(   	_instancenewr-   s       r   r3   Multinomial.expandH   s}    ((i@jj-**,,33K@k3()) 	) 	
 "00
r   c                 :    U R                   R                  " U0 UD6$ r   )r&   _new)r   argskwargss      r   r9   Multinomial._newS   s      %%t6v66r   T)is_discrete	event_dimc                 B    [         R                  " U R                  5      $ r   )r   multinomialr   r   s    r   supportMultinomial.supportV   s    &&t'7'788r   c                 .    U R                   R                  $ r   )r&   r   r   s    r   r   Multinomial.logitsZ   s      '''r   c                 .    U R                   R                  $ r   )r&   r   r   s    r   r   Multinomial.probs^   s      &&&r   c                 .    U R                   R                  $ r   )r&   r)   r   s    r   r)   Multinomial.param_shapeb   s      ,,,r   c                 *   [         R                  " U5      nU R                  R                  [         R                  " U R                  45      U-   5      n[        [        UR                  5       5      5      nUR                  UR                  S5      5        UR                  " U6 nUR                  U R                  U5      5      R                  5       nUR                  SU[         R                  " U5      5        UR!                  U R"                  5      $ )Nr   r    )r1   r2   r&   sampler   listrangedimappendpoppermuter6   _extended_shapezero_scatter_add_	ones_liketype_asr   )r   sample_shapesamplesshifted_idxcountss        r   rJ   Multinomial.samplef   s    zz,/##**JJ((*+l:

 5/0;??1-.//;/T11,?@FFHB)AB~~djj))r   c                    [         R                  " U R                  5      nU R                  R	                  5       nX-  [         R
                  " US-   5      -
  nU R                  R                  SS9SS  n[         R                  " U R                  R                  U5      5      n[         R
                  " US-   5      nXV-  R                  SS/5      nX7-   $ )Nr   F)r3   r   r    )r1   tensorr   r&   entropylgammar'   enumerate_supportexplog_probsum)r   ncat_entropyterm1rA   binomial_probsweightsterm2s           r   r]   Multinomial.entropyt   s    LL))*''//1%,,q1u"55..22%2@D4>>#:#:7#CD,,w{+)..2w7}r   c                    U R                   (       a  U R                  U5        [        U R                  U5      u  p!UR	                  [
        R                  S9n[
        R                  " UR                  S5      S-   5      n[
        R                  " US-   5      R                  S5      nSX!S:H  U[        * :H  -  '   X!-  R                  S5      nX4-
  U-   $ )N)memory_formatr    r   r   )
r4   _validate_sampler	   r   cloner1   contiguous_formatr^   rb   r   )r   valuer   log_factorial_nlog_factorial_xs
log_powerss         r   ra   Multinomial.log_prob   s    !!%(%dkk59E,C,CD,,uyy}q'89 <<	266r:23
v#~./n))"-
1J>>r   )r'   r&   r   )r   NNNr   )__name__
__module____qualname____firstlineno____doc__r   simplexreal_vectorarg_constraintsr$   __annotations__propertyr   r   r   r+   r3   r9   dependent_propertyrA   r   r   r1   r2   r)   rJ   r]   ra   __static_attributes____classcell__)r-   s   @r   r
   r
      s   #J !, 3 3{?V?VWO-f - - @& @ @P	7 ##B9 C9 ( ( ( 'v ' ' -UZZ - - #(**, *	? 	?r   )r1   r   r   torch.distributionsr   r   torch.distributions.binomialr    torch.distributions.distributionr   torch.distributions.utilsr	   __all__r
    r   r   <module>r      s.      8 1 9 3 /}?, }?r   