o
    h                     @   sv   d dl mZmZ 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 )    )OptionalUnionN)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   b/home/www/facesmatcher.com/frenv_anti/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)de
eef dee dee d	ee d
df
 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   Nr   r   r   validate_argsreturnc                    s   |d u |d u krt d|d ur"t||\| _| _| j| j| _n|d us(J 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.r   )

ValueErrorr   r   r   Ztype_asr   _paramsizesuper__init__)selfr   r   r   r   batch_shape	__class__r   r   r    7   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'   T   s   


zBinomial.expandc                 O   s   | j j|i |S N)r   r*   )r!   argskwargsr   r   r   _newb   s   zBinomial._newr   )Zis_discreteZ	event_dimc                 C   s   t d| jS )Nr   )r   Zinteger_intervalr   r!   r   r   r   supporte      zBinomial.supportc                 C   s   | j | j S r+   r   r   r/   r   r   r   meani   s   zBinomial.meanc                 C   s   | j d | j  j| j dS )Nr   r   )r   r   floorr   r/   r   r   r   modem   s   zBinomial.modec                 C   s   | j | j d| j  S Nr   r2   r/   r   r   r   varianceq   s   zBinomial.variancec                 C      t | jddS NT)Z	is_binary)r
   r   r/   r   r   r   r   u   r1   zBinomial.logitsc                 C   r8   r9   )r	   r   r/   r   r   r   r   y   r1   zBinomial.probsc                 C   s
   | j  S r+   )r   r   r/   r   r   r   param_shape}   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   sample   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 r6   )
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   NotImplementedErrorrB   enumerate_supportr%   r?   sum)r!   r   rB   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   )rC   r   r   r   rD   r%   Zaranger   rH   rI   viewlenZ_batch_shaper'   )r!   r'   r   valuesr   r   r   rE      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   rC   r   boolr    r'   r.   Zdependent_propertyr0   propertyr3   r5   r7   r   r   r   r%   r&   r:   r<   rB   rG   rE   __classcell__r   r   r#   r   r      sT    


)typingr   r   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   