o
    tBh/                     @   s   d Z ddlmZmZ ddlmZ ddlZddlZddl	m
Z
 eddZG dd	 d	ed
ZG dd deZG dd deZG dd deZG dd deZG dd deZeeeedZdS )z$
Distribution functions used in GLM
    )ABCMetaabstractmethod)
namedtupleN)xlogyDistributionBoundary)value	inclusivec                   @   sN   e Zd ZdZdd Zedd ZedddZd	d
 ZdddZ	dddZ
dS )ExponentialDispersionModela3  Base class for reproductive Exponential Dispersion Models (EDM).

    The pdf of :math:`Y\sim \mathrm{EDM}(y_\textrm{pred}, \phi)` is given by

    .. math:: p(y| \theta, \phi) = c(y, \phi)
        \exp\left(\frac{\theta y-A(\theta)}{\phi}\right)
        = \tilde{c}(y, \phi)
            \exp\left(-\frac{d(y, y_\textrm{pred})}{2\phi}\right)

    with mean :math:`\mathrm{E}[Y] = A'(\theta) = y_\textrm{pred}`,
    variance :math:`\mathrm{Var}[Y] = \phi \cdot v(y_\textrm{pred})`,
    unit variance :math:`v(y_\textrm{pred})` and
    unit deviance :math:`d(y,y_\textrm{pred})`.

    Methods
    -------
    deviance
    deviance_derivative
    in_y_range
    unit_deviance
    unit_deviance_derivative
    unit_variance

    References
    ----------
    https://en.wikipedia.org/wiki/Exponential_dispersion_model.
    c                 C   s<   t | jts
td| jjrt|| jjS t|| jjS )zReturns ``True`` if y is in the valid range of Y~EDM.

        Parameters
        ----------
        y : array of shape (n_samples,)
            Target values.
        z;_lower_bound attribute must be of type DistributionBoundary)	
isinstance_lower_boundr   	TypeErrorr   npgreater_equalr   greater)selfy r   u/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/_loss/glm_distribution.py
in_y_range4   s   
z%ExponentialDispersionModel.in_y_rangec                 C      dS )a  Compute the unit variance function.

        The unit variance :math:`v(y_\textrm{pred})` determines the variance as
        a function of the mean :math:`y_\textrm{pred}` by
        :math:`\mathrm{Var}[Y_i] = \phi/s_i*v(y_\textrm{pred}_i)`.
        It can also be derived from the unit deviance
        :math:`d(y,y_\textrm{pred})` as

        .. math:: v(y_\textrm{pred}) = \frac{2}{
            \frac{\partial^2 d(y,y_\textrm{pred})}{
            \partialy_\textrm{pred}^2}}\big|_{y=y_\textrm{pred}}

        See also :func:`variance`.

        Parameters
        ----------
        y_pred : array of shape (n_samples,)
            Predicted mean.
        Nr   r   y_predr   r   r   unit_varianceH       z(ExponentialDispersionModel.unit_varianceFc                 C   r   )  Compute the unit deviance.

        The unit_deviance :math:`d(y,y_\textrm{pred})` can be defined by the
        log-likelihood as
        :math:`d(y,y_\textrm{pred}) = -2\phi\cdot
        \left(loglike(y,y_\textrm{pred},\phi) - loglike(y,y,\phi)\right).`

        Parameters
        ----------
        y : array of shape (n_samples,)
            Target values.

        y_pred : array of shape (n_samples,)
            Predicted mean.

        check_input : bool, default=False
            If True raise an exception on invalid y or y_pred values, otherwise
            they will be propagated as NaN.
        Returns
        -------
        deviance: array of shape (n_samples,)
            Computed deviance
        Nr   )r   r   r   check_inputr   r   r   unit_deviance^   r   z(ExponentialDispersionModel.unit_deviancec                 C   s   d||  |  | S )a  Compute the derivative of the unit deviance w.r.t. y_pred.

        The derivative of the unit deviance is given by
        :math:`\frac{\partial}{\partialy_\textrm{pred}}d(y,y_\textrm{pred})
             = -2\frac{y-y_\textrm{pred}}{v(y_\textrm{pred})}`
        with unit variance :math:`v(y_\textrm{pred})`.

        Parameters
        ----------
        y : array of shape (n_samples,)
            Target values.

        y_pred : array of shape (n_samples,)
            Predicted mean.
        )r   )r   r   r   r   r   r   unit_deviance_derivativex   s   z3ExponentialDispersionModel.unit_deviance_derivative   c                 C   s   t || || S )a  Compute the deviance.

        The deviance is a weighted sum of the per sample unit deviances,
        :math:`D = \sum_i s_i \cdot d(y_i, y_\textrm{pred}_i)`
        with weights :math:`s_i` and unit deviance
        :math:`d(y,y_\textrm{pred})`.
        In terms of the log-likelihood it is :math:`D = -2\phi\cdot
        \left(loglike(y,y_\textrm{pred},\frac{phi}{s})
        - loglike(y,y,\frac{phi}{s})\right)`.

        Parameters
        ----------
        y : array of shape (n_samples,)
            Target values.

        y_pred : array of shape (n_samples,)
            Predicted mean.

        weights : {int, array of shape (n_samples,)}, default=1
            Weights or exposure to which variance is inverse proportional.
        )r   sumr   r   r   r   weightsr   r   r   deviance   s   z#ExponentialDispersionModel.deviancec                 C   s   ||  || S )a  Compute the derivative of the deviance w.r.t. y_pred.

        It gives :math:`\frac{\partial}{\partial y_\textrm{pred}}
        D(y, \y_\textrm{pred}; weights)`.

        Parameters
        ----------
        y : array, shape (n_samples,)
            Target values.

        y_pred : array, shape (n_samples,)
            Predicted mean.

        weights : {int, array of shape (n_samples,)}, default=1
            Weights or exposure to which variance is inverse proportional.
        )r   r!   r   r   r   deviance_derivative   s   z.ExponentialDispersionModel.deviance_derivativeNF)r   )__name__
__module____qualname____doc__r   r   r   r   r   r#   r$   r   r   r   r   r	      s    

r	   )	metaclassc                   @   sF   e Zd ZdZdddZedd Zejdd Zdd	 ZdddZ	dS )TweedieDistributiona  A class for the Tweedie distribution.

    A Tweedie distribution with mean :math:`y_\textrm{pred}=\mathrm{E}[Y]`
    is uniquely defined by it's mean-variance relationship
    :math:`\mathrm{Var}[Y] \propto y_\textrm{pred}^power`.

    Special cases are:

    ===== ================
    Power Distribution
    ===== ================
    0     Normal
    1     Poisson
    (1,2) Compound Poisson
    2     Gamma
    3     Inverse Gaussian

    Parameters
    ----------
    power : float, default=0
            The variance power of the `unit_variance`
            :math:`v(y_\textrm{pred}) = y_\textrm{pred}^{power}`.
            For ``0<power<1``, no distribution exists.
    r   c                 C   s
   || _ d S Npowerr   r.   r   r   r   __init__   s   
zTweedieDistribution.__init__c                 C   s   | j S r,   )_powerr   r   r   r   r.      s   zTweedieDistribution.powerc                 C   s   t |tjstd||dkrttj dd| _n0d|  k r(dk r)t	d d|  kr3dk r=n ntddd| _n|dkrItddd| _nt	|| _
d S )	Nz*power must be a real number, input was {0}r   F)r   r   z?Tweedie distribution is only defined for power<=0 and power>=1.   T)r
   numbersRealr   formatr   r   Infr   
ValueErrorr1   r/   r   r   r   r.      s   
c                 C   s   t || jS )zCompute the unit variance of a Tweedie distribution
        v(y_	extrm{pred})=y_	extrm{pred}**power.

        Parameters
        ----------
        y_pred : array of shape (n_samples,)
            Predicted mean.
        )r   r.   r   r   r   r   r      s   	z!TweedieDistribution.unit_varianceFc                 C   s  | j }|rfd|}|dk r|dk rt|d nK|dkr nFd|  k r-dk r.td d|  kr8dk rMn n|dk  sF|dk rLt|d n|dkrd|dk s]|dk rct|d nt|dk rdt t|dd| d| d|   |t |d|  d|   t |d| d|    }|S |dkr|| d }|S |dk rtd|dkrdt||| | |  }|S |dkrdt|| ||  d  }|S dt |d| d| d|   |t |d|  d|   t |d| d|    }|S )	r   z>Mean Tweedie deviance error with power={} can only be used on r   zstrictly positive y_pred.r   z;Tweedie deviance is only defined for power<=0 and power>=1.r3   z,non-negative y and strictly positive y_pred.zstrictly positive y and y_pred.)r.   r6   anyr8   r   maximumr   log)r   r   r   r   pmessagedevr   r   r   r      st   &
z!TweedieDistribution.unit_devianceN)r   r%   )
r&   r'   r(   r)   r0   propertyr.   setterr   r   r   r   r   r   r+      s    


r+   c                           e Zd ZdZ fddZ  ZS )NormalDistributionz1Class for the Normal (aka Gaussian) distribution.c                       t  jdd d S )Nr   r-   superr0   r2   	__class__r   r   r0   W     zNormalDistribution.__init__r&   r'   r(   r)   r0   __classcell__r   r   rF   r   rB   T      rB   c                       rA   )PoissonDistributionz*Class for the scaled Poisson distribution.c                    rC   )Nr   r-   rD   r2   rF   r   r   r0   ^  rH   zPoissonDistribution.__init__rI   r   r   rF   r   rL   [  rK   rL   c                       rA   )GammaDistributionz!Class for the Gamma distribution.c                    rC   )Nr3   r-   rD   r2   rF   r   r   r0   e  rH   zGammaDistribution.__init__rI   r   r   rF   r   rM   b  rK   rM   c                       rA   )InverseGaussianDistributionz>Class for the scaled InverseGaussianDistribution distribution.c                    rC   )N   r-   rD   r2   rF   r   r   r0   l  rH   z$InverseGaussianDistribution.__init__rI   r   r   rF   r   rN   i  rK   rN   )normalpoissongammazinverse-gaussian)r)   abcr   r   collectionsr   r4   numpyr   scipy.specialr   r   r	   r+   rB   rL   rM   rN   EDM_DISTRIBUTIONSr   r   r   r   <module>   s(    
   
