o
    Õ,Ėh}  ć                   @   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__© śa/var/www/html/scripts/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   ŚrsampleE   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_probJ   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_functionY   s   

’z'GeneralizedPareto.log_survival_functionc                 C   s   t  t  |  |”” ”S ©N)r   r1   Śexpr<   ©r   r4   r   r   r   Ślog_cdfd   s   zGeneralizedPareto.log_cdfc                 C   s   t  |  |””S r=   )r   r>   r@   r?   r   r   r   Ścdfg   ó   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$   j   s   zGeneralizedPareto.icdfc                 C   s   || j  | j S r=   )r
   r   )r   Śxr   r   r   r,   u   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   Śmeanx   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   