a
    ßñhö  ã                   @   s`   d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZ dd„ ZG dd	„ d	eƒZdS )
é    )ÚNumberN)Úconstraints)ÚDistribution)Úbroadcast_allÚprobs_to_logitsÚlazy_propertyÚlogits_to_probsc                 C   s    | j dd�|  | j dd� d S )Nr   )Úmin)Úmaxé   )Úclamp)Úx© r   ú\/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/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„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZe ¡ fdd„Zdd„ Zd dd„Z‡  ZS )!ÚBinomiala·  
    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::

        >>> 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rPt||ƒ\| _| _| j | j¡| _t| jtƒ}n.t||ƒ\| _| _| j | j¡| _t| jtƒ}|d urŒ| jn| j| _|r¢t	 
¡ }n
| j ¡ }tt| ƒj||d� d S )Nz;Either `probs` or `logits` must be specified, but not both.©Úvalidate_args)Ú
ValueErrorr   r   r   Ztype_asr   Ú
isinstancer   Ú_paramÚtorchÚSizeÚsizeÚsuperr   Ú__init__)Úselfr   r   r   r   Z	is_scalarÚbatch_shape©Ú	__class__r   r   r   (   s    

zBinomial.__init__c                    s„   |   t|¡}t |¡}| j |¡|_d| jv rD| j |¡|_|j|_d| jv rd| j	 |¡|_	|j	|_t
t|ƒj|dd� | j|_|S )Nr   r   Fr   )Z_get_checked_instancer   r   r   r   ÚexpandÚ__dict__r   r   r   r   r   Ú_validate_args)r    r!   Z	_instanceÚnewr"   r   r   r$   ;   s    


zBinomial.expandc                 O   s   | j j|i |¤ŽS ©N)r   r'   )r    ÚargsÚkwargsr   r   r   Ú_newI   s    zBinomial._newc                 C   s   t  d| j¡S )Nr   )r   Zinteger_intervalr   ©r    r   r   r   ÚsupportL   s    zBinomial.supportc                 C   s   | j | j S r(   ©r   r   r,   r   r   r   ÚmeanP   s    zBinomial.meanc                 C   s   | j | j d| j  S ©Nr   r.   r,   r   r   r   ÚvarianceT   s    zBinomial.variancec                 C   s   t | jdd�S ©NT)Z	is_binary)r   r   r,   r   r   r   r   X   s    zBinomial.logitsc                 C   s   t | jdd�S r2   )r   r   r,   r   r   r   r   \   s    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 sD0    Y  d S r(   )Z_extended_shaper   Zno_gradZbinomialr   r$   r   )r    Zsample_shapeÚshaper   r   r   Úsampled   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 r0   )
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_probi   s    
ÿþzBinomial.log_probc                 C   sp   t | j ¡ ƒ}| j ¡ |ks$tdƒ‚tjd| | jj| jj	d�}| 
ddt| jƒ  ¡}|rl| d| j ¡}|S )Nz?Inhomogeneous total count not supported by `enumerate_support`.r   )ÚdtypeÚdevice)éÿÿÿÿ)r   )Úintr   r
   r	   ÚNotImplementedErrorr   Zaranger   r<   r=   ÚviewÚlenZ_batch_shaper$   )r    r$   r   Úvaluesr   r   r   Úenumerate_supporty   s    zBinomial.enumerate_support)r   NNN)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Znonnegative_integerZunit_intervalÚrealZarg_constraintsZhas_enumerate_supportr   r$   r+   Zdependent_propertyr-   Úpropertyr/   r1   r   r   r   r3   r   r   r5   r;   rD   Ú__classcell__r   r   r"   r   r      s0   þ





r   )Znumbersr   r   Ztorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.utilsr   r   r   r   r   r   r   r   r   r   Ú<module>   s   