
    Ǆg[                     j    d dl Z d dlmc 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e      Zy)    N)constraints)Distribution)broadcast_alllazy_propertylogits_to_probsprobs_to_logitsNegativeBinomialc                   N    e Zd ZdZ ej
                  d       ej                  dd      ej                  dZej                  Z
d fd	Zd fd	Zd Zed	        Zed
        Zed        Zed        Zed        Zed        Zed        Z ej0                         fdZd Z x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logitsc                    |d u |d u k(  rt        d      |Dt        ||      \  | _        | _        | j                  j	                  | j                        | _        nCt        ||      \  | _        | _        | j                  j	                  | j
                        | _        || j                  n| j
                  | _        | j                  j                         }t        | %  ||       y )Nz;Either `probs` or `logits` must be specified, but not both.validate_args)

ValueErrorr   r   r   type_asr   _paramsizesuper__init__)selfr   r   r   r   batch_shape	__class__s         m/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/distributions/negative_binomial.pyr   zNegativeBinomial.__init__&   s    TMv~.M   k51 
#//77

CD
 k62 #//77DD$)$5djj4;;kk&&(MB    c                    | j                  t        |      }t        j                  |      }| j                  j                  |      |_        d| j                  v r1| j                  j                  |      |_        |j                  |_        d| j                  v r1| j                  j                  |      |_	        |j                  |_        t        t        |/  |d       | j                  |_        |S )Nr   r   Fr   )_get_checked_instancer	   torchSizer   expand__dict__r   r   r   r   r   _validate_args)r   r   	_instancenewr   s       r   r"   zNegativeBinomial.expand<   s    (()99Ejj-**11+>dmm#

))+6CICJt}}$++K8CJCJ-k-O!00
r   c                 :     | j                   j                  |i |S N)r   r&   )r   argskwargss      r   _newzNegativeBinomial._newJ   s    t{{///r   c                 Z    | j                   t        j                  | j                        z  S r(   )r   r    expr   r   s    r   meanzNegativeBinomial.meanM   s     %))DKK"888r   c                     | j                   dz
  | j                  j                         z  j                         j	                  d      S )N   r   )min)r   r   r-   floorclampr.   s    r   modezNegativeBinomial.modeQ   s:    !!A%)::AACIIcIRRr   c                 \    | j                   t        j                  | j                         z  S r(   )r/   r    sigmoidr   r.   s    r   variancezNegativeBinomial.varianceU   s     yy5==$++666r   c                 0    t        | j                  d      S NT)	is_binary)r   r   r.   s    r   r   zNegativeBinomial.logitsY   s    tzzT::r   c                 0    t        | j                  d      S r:   )r   r   r.   s    r   r   zNegativeBinomial.probs]   s    t{{d;;r   c                 6    | j                   j                         S r(   )r   r   r.   s    r   param_shapezNegativeBinomial.param_shapea   s    {{!!r   c                     t         j                  j                  | j                  t        j                  | j
                         d      S )NF)concentrationrater   )r    distributionsGammar   r-   r   r.   s    r   _gammazNegativeBinomial._gammae   s@     ""((**DKK<( ) 
 	
r   c                     t        j                         5  | j                  j                  |      }t        j                  |      cd d d        S # 1 sw Y   y xY w)N)sample_shape)r    no_gradrD   samplepoisson)r   rF   rA   s      r   rH   zNegativeBinomial.samplen   sC    ]]_ 	';;%%<%@D==&	' 	' 	's   1AAc                    | j                   r| j                  |       | j                  t        j                  | j
                         z  |t        j                  | j
                        z  z   }t        j                  | j                  |z          t        j                  d|z         z   t        j                  | j                        z   }|j                  | j                  |z   dk(  d      }||z
  S )Nr   r   )	r$   _validate_sampler   F
logsigmoidr   r    lgammamasked_fill)r   valuelog_unnormalized_problog_normalizations       r   log_probzNegativeBinomial.log_probs   s    !!%( $ 0 01<<[[L4
 !
ALL--!.
 \\$**U233ll3;'(ll4++,- 	 .99u$+S
 %'888r   )NNNr(   )__name__
__module____qualname____doc__r   greater_than_eqhalf_open_intervalrealarg_constraintsnonnegative_integersupportr   r"   r+   propertyr/   r5   r8   r   r   r   r>   rD   r    r!   rH   rS   __classcell__)r   s   @r   r	   r	      s     3{2215///S9""O
 --GC,0 9 9 S S 7 7 ; ; < < " " 
 
 #-%**, '
9r   )r    torch.nn.functionalnn
functionalrL   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   r   r   __all__r	    r   r   <module>rh      s6       + 9  
v9| v9r   