a
    h                     @   s   d dl mZmZ d dlZd dlm  mZ d dlm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G d	d deZdS )
    )OptionalUnionN)Tensor)constraints)Distribution)Gamma)broadcast_alllazy_propertylogits_to_probsprobs_to_logitsNegativeBinomialc                       s  e Zd ZdZededdejdZej	Z
d!eeef ee ee ee dd fdd	Zd" fd
d	Zdd ZeedddZeedddZeedddZeedddZeedddZeejdddZeedddZe fddZdd  Z   Z!S )#r   ao  
    Creates a Negative Binomial distribution, i.e. distribution
    of the number of successful independent and identical Bernoulli trials
    before :attr:`total_count` failures are achieved. The probability
    of success of each Bernoulli trial is :attr:`probs`.

    Args:
        total_count (float or Tensor): non-negative number of negative Bernoulli
            trials to stop, although the distribution is still valid for real
            valued count
        probs (Tensor): Event probabilities of success in the half open interval [0, 1)
        logits (Tensor): Event log-odds for probabilities of success
    r                 ?)total_countprobslogitsN)r   r   r   validate_argsreturnc                    s   |d u |d u krt d|d urDt||\| _| _| j| j| _n.|d usPJ 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__ S/var/www/auris/lib/python3.9/site-packages/torch/distributions/negative_binomial.pyr   +   s&    
zNegativeBinomial.__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   torchSizer   expand__dict__r   r   r   r   r   _validate_args)r   r   Z	_instancenewr   r   r   r"   H   s    


zNegativeBinomial.expandc                 O   s   | j j|i |S N)r   r%   )r   argskwargsr   r   r   _newV   s    zNegativeBinomial._new)r   c                 C   s   | j t| j S r&   )r   r    expr   r   r   r   r   meanY   s    zNegativeBinomial.meanc                 C   s    | j d | j   jddS )N   r   )min)r   r   r*   floorclampr+   r   r   r   mode]   s    zNegativeBinomial.modec                 C   s   | j t| j  S r&   )r,   r    Zsigmoidr   r+   r   r   r   variancea   s    zNegativeBinomial.variancec                 C   s   t | jddS NT)Z	is_binary)r   r   r+   r   r   r   r   e   s    zNegativeBinomial.logitsc                 C   s   t | jddS r3   )r
   r   r+   r   r   r   r   i   s    zNegativeBinomial.probsc                 C   s
   | j  S r&   )r   r   r+   r   r   r   param_shapem   s    zNegativeBinomial.param_shapec                 C   s   t | jt| j ddS )NF)Zconcentrationrater   )r   r   r    r*   r   r+   r   r   r   _gammaq   s
    zNegativeBinomial._gammac                 C   sD   t  ( | jj|d}t |W  d    S 1 s60    Y  d S )N)sample_shape)r    Zno_gradr6   sampleZpoisson)r   r7   r5   r   r   r   r8   z   s    
zNegativeBinomial.samplec                 C   s~   | j r| | | jt| j  |t| j  }t| j|  td|  t| j }|| j| dkd}|| S )Nr   r   )	r$   Z_validate_sampler   FZ
logsigmoidr   r    lgammaZmasked_fill)r   valueZlog_unnormalized_probZlog_normalizationr   r   r   log_prob   s"    

zNegativeBinomial.log_prob)NNN)N)"__name__
__module____qualname____doc__r   Zgreater_than_eqZhalf_open_intervalrealZarg_constraintsZnonnegative_integerZsupportr   r   floatr   boolr   r"   r)   propertyr,   r1   r2   r	   r   r   r    r!   r4   r   r6   r8   r<   __classcell__r   r   r   r   r      sD   
   
)typingr   r   r    Ztorch.nn.functionalnnZ
functionalr9   r   Ztorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.gammar   Ztorch.distributions.utilsr   r	   r
   r   __all__r   r   r   r   r   <module>   s   