
    ȅie                         S SK r S SK Jr  S SKJr  S SKJr  S SKJrJrJ	r	J
r
  S SKJr  S SKJrJr  S/r " S	 S\5      rg)
    N)Tensor)constraints)Distribution)broadcast_alllazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_NumberNumber	Geometricc            	       p  ^  \ rS rSrSr\R                  \R                  S.r\R                  r
   SS\\-  S-  S\\-  S-  S\S-  SS4U 4S	 jjjrSU 4S
 jjr\S\4S j5       r\S\4S j5       r\S\4S j5       r\S\4S j5       r\S\4S j5       r\R0                  " 5       4S jrS rS rSrU =r$ )r      a<  
Creates a Geometric distribution parameterized by :attr:`probs`,
where :attr:`probs` is the probability of success of Bernoulli trials.

.. math::

    P(X=k) = (1-p)^{k} p, k = 0, 1, ...

.. note::
    :func:`torch.distributions.geometric.Geometric` :math:`(k+1)`-th trial is the first success
    hence draws samples in :math:`\{0, 1, \ldots\}`, whereas
    :func:`torch.Tensor.geometric_` `k`-th trial is the first success hence draws samples in :math:`\{1, 2, \ldots\}`.

Example::

    >>> # xdoctest: +IGNORE_WANT("non-deterministic")
    >>> m = Geometric(torch.tensor([0.3]))
    >>> m.sample()  # underlying Bernoulli has 30% chance 1; 70% chance 0
    tensor([ 2.])

Args:
    probs (Number, Tensor): the probability of sampling `1`. Must be in range (0, 1]
    logits (Number, Tensor): the log-odds of sampling `1`.
)probslogitsNr   r   validate_argsreturnc           
      z  > US L US L :X  a  [        S5      eUb  [        U5      u  U l        O Uc  [        S5      e[        U5      u  U l        Ub  UOUn[        U[        5      (       a  [        R                  " 5       nOUc  [        S5      eUR                  5       n[        T	U ]-  XSS9  U R                  (       a  Ub}  U R                  nUS:  nUR                  5       (       dV  UR                  U)    n[        S[        U5      R                    S[#        UR$                  5       S['        U 5       S	U 35      eg g g )
Nz;Either `probs` or `logits` must be specified, but not both.zlogits is unexpectedly Nonez$probs_or_logits is unexpectedly Noner   r   zExpected parameter probs (z
 of shape z) of distribution z* to be positive but found invalid values:
)
ValueErrorr   r   AssertionErrorr   
isinstancer   torchSizesizesuper__init___validate_argsalldatatype__name__tupleshaperepr)
selfr   r   r   probs_or_logitsbatch_shapevaluevalidinvalid_value	__class__s
            W/home/james-whalen/.local/lib/python3.13/site-packages/torch/distributions/geometric.pyr   Geometric.__init__2   sH    TMv~.M  )%0MTZ~$%BCC*62NT[#(#4%&ow//**,K&$%KLL)..0KB5#4JJEAIE99;; %

E6 2 U,,-Zekk8J7K L''+Dzl 3AANQ  	 $5    c                 f  > U R                  [        U5      n[        R                  " U5      nSU R                  ;   a   U R
                  R                  U5      Ul        SU R                  ;   a   U R                  R                  U5      Ul        [        [        U]'  USS9  U R                  Ul
        U$ )Nr   r   Fr   )_get_checked_instancer   r   r   __dict__r   expandr   r   r   r   )r&   r(   	_instancenewr,   s       r-   r3   Geometric.expandY   s    ((I>jj-dmm#

))+6CIt}}$++K8CJi&{%&H!00
r/   c                 &    SU R                   -  S-
  $ Ng      ?r   r&   s    r-   meanGeometric.meand   s    TZZ#%%r/   c                 B    [         R                  " U R                  5      $ N)r   
zeros_liker   r:   s    r-   modeGeometric.modeh   s    

++r/   c                 @    SU R                   -  S-
  U R                   -  $ r8   r9   r:   s    r-   varianceGeometric.variancel   s    djj 3&$**44r/   c                 *    [        U R                  SS9$ NT)	is_binary)r	   r   r:   s    r-   r   Geometric.logitsp   s    tzzT::r/   c                 *    [        U R                  SS9$ rF   )r   r   r:   s    r-   r   Geometric.probst   s    t{{d;;r/   c                    U R                  U5      n[        R                  " U R                  R                  5      R
                  n[        R                  " 5          [        R                  R                  5       (       aM  [        R                  " X R                  R                  U R                  R                  S9nUR                  US9nO+U R                  R                  U5      R                  US5      nUR                  5       U R                  * R                  5       -  R!                  5       sS S S 5        $ ! , (       d  f       g = f)N)dtypedevice)min   )_extended_shaper   finfor   rL   tinyno_grad_C_get_tracing_staterandrM   clampr5   uniform_loglog1pfloor)r&   sample_shaper$   rR   us        r-   sampleGeometric.samplex   s    $$\2{{4::++,11]]_xx**,,JJuJJ,<,<TZZEVEVWGGG%JJNN5)224;EEG

{1133::< __s   CD::
Ec                 $   U R                   (       a  U R                  U5        [        XR                  5      u  pUR	                  [
        R                  S9nSX"S:H  US:H  -  '   X* R                  5       -  U R                  R                  5       -   $ )N)memory_formatr   rO   )	r   _validate_sampler   r   cloner   contiguous_formatrZ   rY   )r&   r)   r   s      r-   log_probGeometric.log_prob   sv    !!%($UJJ7%*A*AB-.zeqj)*~~''$**..*:::r/   c                 Z    [        U R                  U R                  SS9U R                  -  $ )Nnone)	reduction)r
   r   r   r:   s    r-   entropyGeometric.entropy   s(    ,T[[$**PVWjj	
r/   )r   r   )NNNr>   )r"   
__module____qualname____firstlineno____doc__r   unit_intervalrealarg_constraintsnonnegative_integersupportr   r   boolr   r3   propertyr;   r@   rC   r   r   r   r   r   r^   re   rj   __static_attributes____classcell__)r,   s   @r-   r   r      s(   4 !, 9 9[EUEUVO--G )-)-%)	%%% $&% d{	%
 
% %N	 &f & & ,f , , 5& 5 5 ; ; ; <v < < #(**, 
=;
 
r/   )r   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   r   r	   torch.nn.functionalr
   torch.typesr   r   __all__r    r/   r-   <module>r      s=      + 9  A ' -|
 |
r/   