3
O4W^>  ã               @   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   ú=/tmp/pip-build-e66j97b2/torch/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jƒ 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 k|d kkrt dƒ‚|d k	rPt||ƒ\| _| _| jj| jƒ| _t| jtƒ}n.t||ƒ\| _| _| jj| jƒ| _t| jtƒ}|d k	rŒ| jn| j| _|r¢t	j
ƒ }n
| j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„   | j t|ƒ}tj|ƒ}| jj|ƒ|_d| jkrD| jj|ƒ|_|j|_d| jkrd| j	j|ƒ|_	|j	|_t
t|ƒj|dd� | j|_|S )Nr   r   F)r   )Z_get_checked_instancer   r   r   r   ÚexpandÚ__dict__r   r   r   r   r   Ú_validate_args)r   r    Z	_instanceÚnew)r!   r   r   r"   ;   s    


zBinomial.expandc             O   s   | j j||ŽS )N)r   r%   )r   ÚargsÚkwargsr   r   r   Ú_newI   s    zBinomial._newc             C   s   t jd| jƒS )Nr   )r   Zinteger_intervalr   )r   r   r   r   ÚsupportL   s    zBinomial.supportc             C   s   | j | j S )N)r   r   )r   r   r   r   ÚmeanP   s    zBinomial.meanc             C   s   | j | j d| j  S )Nr   )r   r   )r   r   r   r   ÚvarianceT   s    zBinomial.variancec             C   s   t | jdd�S )NT)Ú	is_binary)r   r   )r   r   r   r   r   X   s    zBinomial.logitsc             C   s   t | jdd�S )NT)r,   )r   r   )r   r   r   r   r   \   s    zBinomial.probsc             C   s
   | j jƒ S )N)r   r   )r   r   r   r   Úparam_shape`   s    zBinomial.param_shapec             C   s¸   t jƒ �¦ tt| jjƒ ƒdƒ}| j|ƒ|f }t j| jjdƒj	|ƒƒ}| jj
ƒ |kr¢t j|| jj| jjd�}|| jjdƒk}t jjƒ r–d||j	|ƒ< n|j|dƒ |jdd�S Q R X d S )Nr   )ÚdtypeÚdeviceg        )Zdiméÿÿÿÿr0   r0   )r   Zno_gradr
   Úintr   Z_extended_shapeZ	bernoullir   Z	unsqueezer"   r	   Úaranger   r.   r/   Z_CZ_get_tracing_stateZmasked_fill_Úsum)r   Zsample_shapeÚ	max_countÚshapeZ
bernoullisr2   Úmaskr   r   r   Úsampled   s    

zBinomial.samplec             C   sŒ   | j r| j|ƒ tj| jd ƒ}tj|d ƒ}tj| j| d ƒ}| jt| jƒ | jtjtjtj	| jƒ ƒƒ  | }|| j | | | S )Nr   )
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_probs   s    
4zBinomial.log_probc             C   sp   t | jjƒ ƒ}| jjƒ |ks$tdƒ‚tjd| | jj| jj	d�}|j
ddt| jƒ  ƒ}|rl|jd| j ƒ}|S )	Nz?Inhomogeneous total count not supported by `enumerate_support`.r   )r.   r/   r0   )r0   )r   r0   )r0   )r1   r   r
   r	   ÚNotImplementedErrorr   r2   r   r.   r/   ÚviewÚlenZ_batch_shaper"   )r   r"   r   Úvaluesr   r   r   Úenumerate_supportƒ   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*   r+   r   r   r   r-   r   r   r7   r=   rB   Ú__classcell__r   r   )r!   r   r      s"   
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   