
    rh}                     l    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y)    N)NumberReal)infnan)constraintsDistribution)broadcast_allGeneralizedParetoc                   2    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j2                  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concentrationTc                 &   t        |||      \  | _        | _        | _        t	        |t
              r5t	        |t
              r%t	        |t
              rt        j                         }n| j                  j                         }t        | )  ||       y )Nvalidate_args)r	   r   r   r   
isinstancer   torchSizesizesuper__init__)selfr   r   r   r   batch_shape	__class__s         y/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/distributions/generalized_pareto.pyr   zGeneralizedPareto.__init__-   sn    3@4
0$*d0 sF#5&)=&1**,K((--/KMB    c                 j   | j                  t        |      }t        j                  |      }| j                  j                  |      |_        | 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jj-((//+.JJ%%k2	 ..55kB.{%.P!00
r   c                     | j                  |      }t        j                  || j                  j                  | j                  j
                        }| j                  |      S )N)dtypedevice)_extended_shaper   randr   r$   r%   icdf)r   sample_shapeshapeus       r   rsamplezGeneralizedPareto.rsampleE   sB    $$\2JJuDHHNN488??Kyy|r   c           	         | j                   r| j                  |       | j                  |      }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                  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    !!%(GGEN-- 2 2ELL4EFKKU__T%7%78$:L:L
	 	MEOOA..AFAq5;;y1}3M/MN$.tzz4$@DHHTZZ djjnnFV 	 zEKKMBBBr   c                    | j                   r| j                  |       | j                  |      }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    !!%(GGEN-- 2 2ELL4EFKKU__T%7%78$:L:L
	 Y]33i?{{7QB66r   c                 r    t        j                  t        j                  | j                  |                   S N)r   r6   exprB   r   r9   s     r   log_cdfzGeneralizedPareto.log_cdfd   s(    {{EIId&@&@&GHHIIr   c                 J    t        j                  | j                  |            S rD   )r   rE   rG   rF   s     r   cdfzGeneralizedPareto.cdfg   s    yye,--r   c                    | 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    hh

**--u/?/?/NOKK)GW	{{E6"ei/%++yj4>O2PPP54<'
{{7J>>r   c                 :    || j                   z
  | j                  z  S rD   )r   r   )r   xs     r   r1   zGeneralizedPareto._zu   s    DHH

**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   sW    **!KK}c:	DJJ!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   se    **#KK}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   s=    ii

#d&8&881<!!#t'8'899r   c                     | j                   S rD   )r   )r   s    r   modezGeneralizedPareto.mode   s    xx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   sT    "EDJJ9K9K,K$KS
 ##E5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 $.5::< 
C	7J.	?+ / / / /:   $[##C2 D2r   )r7   numbersr   r   r   r   r   torch.distributionsr   r   torch.distributions.utilsr	   __all__r
    r   r   <module>rx      s/         9 3 
H2 H2r   