
    Ǆg4                     h    d dl Z d dl 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y)	    N)inf)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        Ze
d        Zd fd	Zd fd	Zd	 Z ej                   d
d      d        Ze
d        Ze
d        Ze
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_countc                 4    | j                   | j                  z  S N)r   r   selfs    g/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/distributions/multinomial.pymeanzMultinomial.mean5   s    zzD,,,,    c                 T    | j                   | j                  z  d| j                  z
  z  S )N   r   r   r   s    r   variancezMultinomial.variance9   s$    $**,DJJ??r   r   c                 (   t        |t              st        d      || _        t	        ||      | _        t        || j                        | _        | j
                  j                  }| j
                  j                  dd  }t        | 1  |||       y )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&   zMultinomial.__init__=   s    +s+%&RSS&'eFC!kL''33''33BC8kOr   c                 "   | j                  t        |      }t        j                  |      }| 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   r-   zMultinomial.expandG   s    ((i@jj-**,,33K@k3()) 	) 	
 "00
r   c                 :     | j                   j                  |i |S r   )r!   _new)r   argskwargss      r   r2   zMultinomial._newR   s     %t  %%t6v66r   T)is_discrete	event_dimc                 @    t        j                  | j                        S r   )r   multinomialr   r   s    r   supportzMultinomial.supportU   s    &&t'7'788r   c                 .    | j                   j                  S r   )r!   r   r   s    r   r   zMultinomial.logitsY   s      '''r   c                 .    | j                   j                  S r   )r!   r   r   s    r   r   zMultinomial.probs]   s      &&&r   c                 .    | j                   j                  S r   )r!   r$   r   s    r   r$   zMultinomial.param_shapea   s      ,,,r   c                 $   t        j                  |      }| j                  j                  t        j                  | j                  f      |z         }t        t        |j                                     }|j                  |j                  d              |j                  | }|j                  | j                  |            j                         }|j                  d|t        j                  |             |j!                  | j"                        S )Nr   r   )r+   r,   r!   sampler   listrangedimappendpoppermuter0   _extended_shapezero_scatter_add_	ones_liketype_asr   )r   sample_shapesamplesshifted_idxcountss        r   r>   zMultinomial.samplee   s    zz,/##**JJ((*+l:

 5/0;??1-.!'//;/T11,?@FFHB)AB~~djj))r   c                    t        j                  | j                        }| j                  j	                         }||z  t        j
                  |dz         z
  }| j                  j                  d      dd  }t        j                  | j                  j                  |            }t        j
                  |dz         }||z  j                  ddg      }||z   S )Nr   F)r-   r   r   )r+   tensorr   r!   entropylgammar"   enumerate_supportexplog_probsum)r   ncat_entropyterm1r9   binomial_probsweightsterm2s           r   rP   zMultinomial.entropys   s    LL))*''//1K%,,q1u"55..22%2@D4>>#:#:7#CD,,w{+')..2w7u}r   c                    | j                   r| j                  |       t        | j                  |      \  }}|j	                  t
        j                        }t        j                  |j                  d      dz         }t        j                  |dz         j                  d      }d||dk(  |t         k(  z  <   ||z  j                  d      }||z
  |z   S )N)memory_formatr   r   r   )
r.   _validate_sampler   r   cloner+   contiguous_formatrQ   rU   r   )r   valuer   log_factorial_nlog_factorial_xs
log_powerss         r   rT   zMultinomial.log_prob   s    !!%(%dkk59E,C,CD,,uyy}q'89 <<	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-   r2   dependent_propertyr9   r   r   r$   r+   r,   r>   rP   rT   __classcell__)r(   s   @r   r	   r	      s    #H !, 3 3{?V?VWO- - @ @P	7 $[##B9 C9 ( ( ' ' - - #-%**, *	?r   )r+   r   torch.distributionsr   r   torch.distributions.binomialr    torch.distributions.distributionr   torch.distributions.utilsr   __all__r	    r   r   <module>rv      s.      8 1 9 3 /|?, |?r   