a
    ŗ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__© śT/var/www/auris/lib/python3.9/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   )Z_get_checked_instancer	   r   r   r
   Śexpandr   r   r   r   Ś_validate_args)r   r   Z	_instanceŚnewr   r   r   r   ;   s    
zGeneralizedPareto.expandc                 C   s,   |   |”}tj|| jj| jjd}|  |”S )N)ŚdtypeŚdevice)Z_extended_shaper   Zrandr
   r   r   Śicdf)r   Z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rt | 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_nonzeroZ	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*   r+   r,   )r   r/   r0   r1   r2   r4   r   r   r   Ślog_survival_functionY   s    

’z'GeneralizedPareto.log_survival_functionc                 C   s   t  t  |  |”” ”S ©N)r   r,   Śexpr6   ©r   r/   r   r   r   Ślog_cdfd   s    zGeneralizedPareto.log_cdfc                 C   s   t  |  |””S r7   )r   r8   r:   r9   r   r   r   Ścdfg   s    zGeneralizedPareto.cdfc           
      C   sz   | j }| j}| j}t |t |””}t |t |”|”}t | ”}||| t 	| | ”  }|||  }	t ||	|”S r7   )
r
   r   r   r   r(   Z
zeros_liker*   r+   r,   Śexpm1)
r   r/   r
   r   r   r1   r2   Zlogur4   Z
where_zeror   r   r   r    j   s    zGeneralizedPareto.icdfc                 C   s   || j  | j S r7   )r
   r   )r   Śxr   r   r   r'   u   s    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   Zvalidr2   Śresultr   r   r   Śmeanx   s
    zGeneralizedPareto.meanc                 C   sL   | j }|dk }t ||d”}| jd d| d dd|    }t ||t”S )Nr>   g      Š?é   r%   )r   r   r*   r   r   r?   r   r   r   Śvariance   s
    "zGeneralizedPareto.variancec                 C   s$   t  | j”| j d }t  || j”S )Nr%   )r   r.   r   r   Zbroadcast_toZ_batch_shape)r   Zansr   r   r   Śentropy   s    zGeneralizedPareto.entropyc                 C   s   | j S r7   )r
   )r   r   r   r   Śmode   s    zGeneralizedPareto.modeFr   )Zis_discreteZ	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.support)N)N)Ś__name__Ś
__module__Ś__qualname__Ś__doc__r   ŚrealZpositiveZarg_constraintsZhas_rsampler   r   r   r   r#   r5   r6   r:   r;   r    r'   ŚpropertyrA   rC   rD   rE   Zdependent_propertyrI   Ś__classcell__r   r   r   r   r	      s0   ż



)r-   Znumbersr   r   r   r   r   Ztorch.distributionsr   r   Ztorch.distributions.utilsr   Ś__all__r	   r   r   r   r   Ś<module>   s   