
    }Yh}                     t    d dl Z d dlmZmZ d dlZd dlmZmZ d dlmZm	Z	 d dl
mZ dgZ G d de	          ZdS )    N)NumberReal)infnan)constraintsDistribution)broadcast_allGeneralizedParetoc                   $    e Zd ZdZej        ej        ej        dZdZd fd	Z	d fd	Z
 ej                    fdZd Zd	 Zd
 Zd Zd Zd Zed             Zed             Zd Zed             Z ej        dd          d             Z xZS )r
   a  
    Creates a Generalized Pareto distribution parameterized by :attr:`loc`, :attr:`scale`, and :attr:`concentration`.

    The Generalized Pareto distribution is a family of continuous probability distributions on the real line.
    Special cases include Exponential (when :attr:`loc` = 0, :attr:`concentration` = 0), Pareto (when :attr:`concentration` > 0,
    :attr:`loc` = :attr:`scale` / :attr:`concentration`), and Uniform (when :attr:`concentration` = -1).

    This distribution is often used to model the tails of other distributions. This implementation is based on the
    implementation in TensorFlow Probability.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = GeneralizedPareto(torch.tensor([0.1]), torch.tensor([2.0]), torch.tensor([0.4]))
        >>> m.sample()  # sample from a Generalized Pareto distribution with loc=0.1, scale=2.0, and concentration=0.4
        tensor([ 1.5623])

    Args:
        loc (float or Tensor): Location parameter of the distribution
        scale (float or Tensor): Scale parameter of the distribution
        concentration (float or Tensor): Concentration parameter of the distribution
    )locscaleconcentrationTNc                 n   t          |||          \  | _        | _        | _        t	          |t
                    r>t	          |t
                    r)t	          |t
                    rt          j                    }n| j                                        }t                      
                    ||           d S )Nvalidate_args)r	   r   r   r   
isinstancer   torchSizesizesuper__init__)selfr   r   r   r   batch_shape	__class__s         x/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/distributions/generalized_pareto.pyr   zGeneralizedPareto.__init__-   s    3@4
 4
0$*d0 sF##	*5&))	* =&11	*
  *,,KK(--//KMBBBBB    c                    |                      t          |          }t          j        |          }| j                            |          |_        | j                            |          |_        | j                            |          |_        t          t          |          	                    |d           | j
        |_
        |S )NFr   )_get_checked_instancer
   r   r   r   expandr   r   r   r   _validate_args)r   r   	_instancenewr   s       r   r   zGeneralizedPareto.expand;   s    (():IFFj--(//+..J%%k22	 .55kBB%%..{%.PPP!0
r   c                     |                      |          }t          j        || j        j        | j        j                  }|                     |          S )N)dtypedevice)_extended_shaper   randr   r$   r%   icdf)r   sample_shapeshapeus       r   rsamplezGeneralizedPareto.rsampleE   sC    $$\22JuDHN48?KKKyy||r   c           	         | j         r|                     |           |                     |          }t          j        | j        t          j        d                    }t          j        |t          j        | j                  | j                  }d|z  t          j        |          z   }t          j        |dk    ||t          j	        ||z            z            }t          | j        t                    rt          j        | j                  n| j                                        }| t          j        |||          z
  S )N           r   )r    _validate_sample_zr   iscloser   tensorwhere	ones_likelog1pr   r   r   mathlog)r   valuezeq_zero	safe_concywhere_nonzero	log_scales           r   log_probzGeneralizedPareto.log_probJ   s    	)!!%(((GGENN- 2EL4E4EFFKU_T%788$:L
 
	 	MEOA...AFAq5;y1}3M3M/MNN$.tz4$@$@VDHTZ   djnnFVFV 	 zEKMBBBBr   c                    | j         r|                     |           |                     |          }t          j        | j        t          j        d                    }t          j        |t          j        | j                  | j                  }t          j	        ||z             |z  }t          j        || |          S )Nr.   )
r    r0   r1   r   r2   r   r3   r4   r5   r6   )r   r9   r:   r;   r<   r>   s         r   log_survival_functionz'GeneralizedPareto.log_survival_functionY   s     	)!!%(((GGENN- 2EL4E4EFFKU_T%788$:L
 
	 Y]333i?{7QB666r   c                 v    t          j        t          j        |                     |                               S N)r   r6   exprB   r   r9   s     r   log_cdfzGeneralizedPareto.log_cdfd   s-    {EId&@&@&G&GHHHIIIr   c                 P    t          j        |                     |                    S rD   )r   rE   rG   rF   s     r   cdfzGeneralizedPareto.cdfg   s    ye,,---r   c                 r   | j         }| j        }| j        }t          j        |t          j        |                    }t          j        |t          j        |          |          }t          j        |           }|||z  t          j	        | |z            z  z   }|||z  z
  }	t          j        ||	|          S rD   )
r   r   r   r   r2   
zeros_liker4   r5   r6   expm1)
r   r9   r   r   r   r;   r<   logur>   
where_zeros
             r   r(   zGeneralizedPareto.icdfj   s    h
*-u/?/N/NOOK)G)GWW	{E6""ei/%+yj4>O2P2PPP54<'
{7J>>>r   c                 &    || j         z
  | j        z  S rD   )r   r   )r   xs     r   r1   zGeneralizedPareto._zu   s    DH
**r   c                     | j         }|dk     }t          j        ||d          }| j        | j        d|z
  z  z   }t          j        ||t
                    S )Nr/         ?)r   r   r4   r   r   r   r   r   validr<   results        r   meanzGeneralizedPareto.meanx   sR    *!K}c::	DJ!i-88{5&#...r   c                     | j         }|dk     }t          j        ||d          }| j        dz  d|z
  dz  dd|z  z
  z  z  }t          j        ||t                    S )NrR   g      ?   r/   )r   r   r4   r   r   rS   s        r   variancezGeneralizedPareto.variance   sb    *#K}d;;	Q1y=Q"6!a)m:K"LM{5&#...r   c                 ~    t          j        | j                  | j        z   dz   }t          j        || j                  S )Nr/   )r   r8   r   r   broadcast_to_batch_shape)r   anss     r   entropyzGeneralizedPareto.entropy   s6    i
##d&881<!#t'8999r   c                     | j         S rD   )r   )r   s    r   modezGeneralizedPareto.mode   s	    xr   Fr   )is_discrete	event_dimc                     | j         }t          j        | j        dk     || j        | j        z  z
  t
                    }t          j        ||          S )Nr   )r   r   r4   r   r   r   r   interval)r   loweruppers      r   supportzGeneralizedPareto.support   sM    "EDJ9K,K$KS
 
 #E5111r   rD   )__name__
__module____qualname____doc__r   realpositivearg_constraintshas_rsampler   r   r   r   r,   r@   rB   rG   rI   r(   r1   propertyrV   rY   r^   r`   dependent_propertyrg   __classcell__)r   s   @r   r
   r
      s        0 %$) O
 KC C C C C C      $.5:<<    
C C C	7 	7 	7J J J. . .	? 	? 	?+ + + / / X/ / / X/: : :   X $[#CCC2 2 DC2 2 2 2 2r   )r7   numbersr   r   r   r   r   torch.distributionsr   r   torch.distributions.utilsr	   __all__r
    r   r   <module>rx      s                              9 9 9 9 9 9 9 9 3 3 3 3 3 3 
H2 H2 H2 H2 H2 H2 H2 H2 H2 H2r   