o
    lۂi                     @   sf   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 de	ZdS )    N)NumberReal)infnan)constraintsDistribution)broadcast_allGeneralizedParetoc                       s   e Zd ZdZejejejdZdZd$ fdd	Z	d$ fdd	Z
e fd	d
Zdd Zdd Zdd Zdd Zdd Zdd Zedd Zedd Zdd Zedd Zejdd d!d"d# Z  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                    s^   t |||\| _| _| _t|tr t|tr t|tr t }n| j }t	 j
||d d S )Nvalidate_args)r   r
   r   r   
isinstancer   torchSizesizesuper__init__)selfr
   r   r   r   batch_shape	__class__ i/home/ubuntu/maya3_transcribe/venv/lib/python3.10/site-packages/torch/distributions/generalized_pareto.pyr   .   s   

zGeneralizedPareto.__init__c                    s`   |  t|}t|}| j||_| j||_| j||_tt|j	|dd | j
|_
|S )NFr   )_get_checked_instancer	   r   r   r
   expandr   r   r   r   _validate_args)r   r   	_instancenewr   r   r   r   <   s   
zGeneralizedPareto.expandc                 C   s,   |  |}tj|| jj| jjd}| |S )N)dtypedevice)_extended_shaper   randr
   r    r!   icdf)r   sample_shapeshapeur   r   r   rsampleF   s   

zGeneralizedPareto.rsamplec              	   C   s   | j r| | | |}t| jtd}t|t| j| j}d| t| }t|dk||t	||  }t
| jtrHt| jn| j }| t||| 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_scaler   r   r   log_probK   s   

  zGeneralizedPareto.log_probc                 C   sj   | j r| | | |}t| jtd}t|t| j| j}t	||  | }t|| |S )Nr)   )
r   r+   r,   r   r-   r   r.   r/   r0   r1   )r   r4   r5   r6   r7   r9   r   r   r   log_survival_functionZ   s   

z'GeneralizedPareto.log_survival_functionc                 C   s   t t | | S N)r   r1   expr<   r   r4   r   r   r   log_cdfe   s   zGeneralizedPareto.log_cdfc                 C   s   t | |S r=   )r   r>   r@   r?   r   r   r   cdfh      zGeneralizedPareto.cdfc           
      C   sz   | j }| j}| j}t|t|}t|t||}t| }||| t	| |   }|||  }	t||	|S r=   )
r
   r   r   r   r-   
zeros_liker/   r0   r1   expm1)
r   r4   r
   r   r   r6   r7   logur9   
where_zeror   r   r   r$   k   s   zGeneralizedPareto.icdfc                 C   s   || j  | j S r=   )r
   r   )r   xr   r   r   r,   v   rB   zGeneralizedPareto._zc                 C   s>   | j }|dk }t||d}| j| jd|   }t||tS )Nr*         ?)r   r   r/   r
   r   r   r   r   validr7   resultr   r   r   meany   s
   zGeneralizedPareto.meanc                 C   sL   | j }|dk }t||d}| jd d| d dd|    }t||tS )NrH   g      ?   r*   )r   r   r/   r   r   rI   r   r   r   variance   s
   "zGeneralizedPareto.variancec                 C   s$   t | j| j d }t || jS )Nr*   )r   r3   r   r   broadcast_to_batch_shape)r   ansr   r   r   entropy   s   zGeneralizedPareto.entropyc                 C   s   | j S r=   )r
   )r   r   r   r   mode   s   zGeneralizedPareto.modeFr   )is_discrete	event_dimc                 C   s2   | j }t| jdk || j| j  t}t||S )Nr   )r
   r   r/   r   r   r   r   interval)r   lowerupperr   r   r   support   s
   zGeneralizedPareto.supportr=   )__name__
__module____qualname____doc__r   realpositivearg_constraintshas_rsampler   r   r   r   r(   r;   r<   r@   rA   r$   r,   propertyrL   rN   rR   rS   dependent_propertyrY   __classcell__r   r   r   r   r	      s2    



)r2   numbersr   r   r   r   r   torch.distributionsr   r   torch.distributions.utilsr   __all__r	   r   r   r   r   <module>   s   