o
    ei3                     @   s|   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 )	    N)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$deeB dedB d	edB d
edB ddf
 fddZd% fdd	Z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def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logitsNr   r   r   validate_argsreturnc                    s   |d u |d u krt d|d ur"t||\| _| _| j| j| _n|d u r*tdt||\| _| _| j| j| _|d urB| jn| j| _| j }t	 j
||d d S )Nz;Either `probs` or `logits` must be specified, but not both.zlogits is unexpectedly Noner   )
ValueErrorr   r   r   type_asAssertionErrorr   _paramsizesuper__init__)selfr   r   r   r   batch_shape	__class__ o/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/distributions/negative_binomial.pyr   +   s(   
zNegativeBinomial.__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   )_get_checked_instancer
   torchSizer   expand__dict__r   r   r   r   r   _validate_args)r   r   	_instancenewr   r   r   r#   K   s   


zNegativeBinomial.expandc                 O   s   | j j|i |S N)r   r'   )r   argskwargsr   r   r   _newY   s   zNegativeBinomial._newc                 C   s   | j t| j S r(   )r   r!   expr   r   r   r   r   mean\   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!   sigmoidr   r-   r   r   r   varianced   s   zNegativeBinomial.variancec                 C      t | jddS NT)	is_binary)r	   r   r-   r   r   r   r   h      zNegativeBinomial.logitsc                 C   r6   r7   )r   r   r-   r   r   r   r   l   r9   zNegativeBinomial.probsc                 C   s
   | j  S r(   )r   r   r-   r   r   r   param_shapep   s   
zNegativeBinomial.param_shapec                 C   s   t | jt| j ddS )NF)concentrationrater   )r   r   r!   r,   r   r-   r   r   r   _gammat   s
   zNegativeBinomial._gammac                 C   sD   t   | jj|d}t |W  d    S 1 sw   Y  d S )N)sample_shape)r!   no_gradr=   samplepoisson)r   r>   r<   r   r   r   r@   }   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%   _validate_sampler   F
logsigmoidr   r!   lgammamasked_fill)r   valuelog_unnormalized_problog_normalizationr   r   r   log_prob   s"   

zNegativeBinomial.log_prob)NNNr(   ) __name__
__module____qualname____doc__r   greater_than_eqhalf_open_intervalrealarg_constraintsnonnegative_integersupportr   floatboolr   r#   r+   propertyr.   r3   r5   r   r   r   r!   r"   r:   r   r=   r@   rJ   __classcell__r   r   r   r   r
      sN    
 )r!   torch.nn.functionalnn
functionalrC   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.gammar   torch.distributions.utilsr   r   r   r	   __all__r
   r   r   r   r   <module>   s   