o
    Zh                     @   sf   d dl Z d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
 dgZdd ZG d	d deZdS )
    N)Tensor)constraints)Distribution)broadcast_alllazy_propertylogits_to_probsprobs_to_logitsBinomialc                 C   s    | j dd|  | j dd d S )Nr   )minmax   )clamp)x r   K/var/www/auris/lib/python3.10/site-packages/torch/distributions/binomial.py_clamp_by_zero   s    r   c                       s   e Zd ZdZejejejdZdZ	d% fdd	Z
d& fdd		Zd
d Zejddddd ZedefddZedefddZedefddZedefddZedefddZedejfddZe fddZdd  Zd!d" Zd'd#d$Z  ZS )(r	   a  
    Creates a Binomial distribution parameterized by :attr:`total_count` and
    either :attr:`probs` or :attr:`logits` (but not both). :attr:`total_count` must be
    broadcastable with :attr:`probs`/:attr:`logits`.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = Binomial(100, torch.tensor([0 , .2, .8, 1]))
        >>> x = m.sample()
        tensor([   0.,   22.,   71.,  100.])

        >>> m = Binomial(torch.tensor([[5.], [10.]]), torch.tensor([0.5, 0.8]))
        >>> x = m.sample()
        tensor([[ 4.,  5.],
                [ 7.,  6.]])

    Args:
        total_count (int or Tensor): number of Bernoulli trials
        probs (Tensor): Event probabilities
        logits (Tensor): Event log-odds
    )total_countprobslogitsT   Nc                    s   |d u |d u krt d|d ur"t||\| _| _| j| j| _nt||\| _| _| j| j| _|d ur:| jn| j| _| j }t j	||d d S )Nz;Either `probs` or `logits` must be specified, but not both.validate_args)

ValueErrorr   r   r   Ztype_asr   _paramsizesuper__init__)selfr   r   r   r   batch_shape	__class__r   r   r   5   s$   
zBinomial.__init__c                    s   |  t|}t|}| j||_d| jv r"| j||_|j|_d| jv r2| j	||_	|j	|_t
t|j|dd | j|_|S )Nr   r   Fr   )Z_get_checked_instancer	   torchSizer   expand__dict__r   r   r   r   r   _validate_args)r   r   Z	_instancenewr    r   r   r$   K   s   


zBinomial.expandc                 O   s   | j j|i |S N)r   r'   )r   argskwargsr   r   r   _newY   s   zBinomial._newr   )Zis_discreteZ	event_dimc                 C   s   t d| jS )Nr   )r   Zinteger_intervalr   r   r   r   r   support\      zBinomial.supportreturnc                 C   s   | j | j S r(   r   r   r,   r   r   r   mean`   s   zBinomial.meanc                 C   s   | j d | j  j| j dS )Nr   r   )r   r   floorr   r,   r   r   r   moded   s   zBinomial.modec                 C   s   | j | j d| j  S Nr   r0   r,   r   r   r   varianceh   s   zBinomial.variancec                 C      t | jddS NT)Z	is_binary)r   r   r,   r   r   r   r   l   r.   zBinomial.logitsc                 C   r6   r7   )r   r   r,   r   r   r   r   p   r.   zBinomial.probsc                 C   s
   | j  S r(   )r   r   r,   r   r   r   param_shapet   s   
zBinomial.param_shapec                 C   sR   |  |}t  t| j|| j|W  d    S 1 s"w   Y  d S r(   )Z_extended_shaper"   Zno_gradZbinomialr   r$   r   )r   Zsample_shapeshaper   r   r   samplex   s   

$zBinomial.samplec              	   C   s   | j r| | t| jd }t|d }t| j| d }| jt| j | jttt	| j   | }|| j | | | S r4   )
r&   Z_validate_sampler"   lgammar   r   r   log1pexpabs)r   valueZlog_factorial_nZlog_factorial_kZlog_factorial_nmkZnormalize_termr   r   r   log_prob   s   
zBinomial.log_probc                 C   sJ   t | j }| j |kstd| | d}t|| 	d S )Nz5Inhomogeneous total count not supported by `entropy`.Fr   )
intr   r   r
   NotImplementedErrorr@   enumerate_supportr"   r=   sum)r   r   r@   r   r   r   entropy   s   zBinomial.entropyc                 C   sp   t | j }| j |kstdtjd| | jj| jj	d}|
ddt| j  }|r6|d| j }|S )Nz?Inhomogeneous total count not supported by `enumerate_support`.r   )dtypedevice))r   )rA   r   r   r
   rB   r"   Zaranger   rF   rG   viewlenZ_batch_shaper$   )r   r$   r   valuesr   r   r   rC      s   zBinomial.enumerate_support)r   NNNr(   )T)__name__
__module____qualname____doc__r   Znonnegative_integerZunit_intervalrealZarg_constraintsZhas_enumerate_supportr   r$   r+   Zdependent_propertyr-   propertyr   r1   r3   r5   r   r   r   r"   r#   r8   r:   r@   rE   rC   __classcell__r   r   r    r   r	      s8    

)r"   r   Ztorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.utilsr   r   r   r   __all__r   r	   r   r   r   r   <module>   s   