
     h/                        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	e
          Z G d de          Z G d de          Z G d de          Z G d de          Z G 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                   \    e Zd ZdZd Zed             Zedd            Zd ZddZ	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                     t          | j        t                    st          d          | j        j        rt          j        || j        j                  S t          j        || 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ys     Z/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/sklearn/_loss/glm_distribution.py
in_y_rangez%ExponentialDispersionModel.in_y_range4   sp     $+-ABB 	M   & 	:#At'8'>???:a!2!8999    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.
        N r   y_preds     r   unit_variancez(ExponentialDispersionModel.unit_varianceH         r   Fc                     dS )  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_inputs       r   unit_deviancez(ExponentialDispersionModel.unit_deviance^   r   r   c                 >    d||z
  z  |                      |          z  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   s      r   unit_deviance_derivativez3ExponentialDispersionModel.unit_deviance_derivativex   s&      QZ 4#5#5f#=#===r      c                 X    t          j        ||                     ||          z            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   weightss       r   deviancez#ExponentialDispersionModel.deviance   s)    , vg 2 21f = ==>>>r   c                 4    ||                      ||          z  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(   s       r   deviance_derivativez.ExponentialDispersionModel.deviance_derivative   s    " 66q&AAAAr   NF)r%   )__name__
__module____qualname____doc__r   r   r   r!   r$   r*   r,   r   r   r   r   r      s         8: : :(   ^*    ^2> > >$? ? ? ?0B B B B B Br   r   )	metaclassc                   ^    e Zd ZdZd
dZed             Zej        d             Zd Z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                     || _         d S Npowerr   r8   s     r   __init__zTweedieDistribution.__init__   s    


r   c                     | j         S r6   )_power)r   s    r   r8   zTweedieDistribution.power   s
    {r   c                    t          |t          j                  s"t          d                    |                    |dk    r"t          t          j         d          | _        njd|cxk     rdk     rn nt          d          d|cxk    rdk     rn nt          dd          | _        n$|dk    rt          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   
ValueErrorr<   r9   s     r   r8   zTweedieDistribution.power   s    
 %.. 	XHOOPUVVWWWA:: 4bfW N N ND]]]]]]]]]Q   %^^^^!^^^^^ 4Q$ G G GDaZZ 4Q% H H HD r   c                 6    t          j        || 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   r8   r   s     r   r   z!TweedieDistribution.unit_variance   s     x
+++r   Fc                    | j         }|rd                    |          }|dk     r+|dk                                    rt          |dz             n|dk    rnd|cxk     rdk     rn nt          d          d|cxk    rdk     rFn nC|dk                                     s|dk                                    rt          |dz             nP|dk    rC|dk                                    s|dk                                    rt          |dz             nt          |dk     r|dt	          j         t	          j        |d          d|z
            d|z
  d|z
  z  z  |t	          j         |d|z
            z  d|z
  z  z
  t	          j         |d|z
            d|z
  z  z   z  }n|dk    r	||z
  dz  }n|dk     rt          d          |dk    rdt          |||z            |z
  |z   z  }n|dk    r$dt	          j        ||z            ||z  z   dz
  z  }nhdt	          j         |d|z
            d|z
  d|z
  z  z  |t	          j         |d|z
            z  d|z
  z  z
  t	          j         |d|z
            d|z
  z  z   z  }|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.r>   z,non-negative y and strictly positive y_pred.zstrictly positive y and y_pred.)r8   rA   anyrC   r   maximumr   log)r   r   r   r    pmessagedevs          r   r!   z!TweedieDistribution.unit_deviance   s	   0 J 	!PWW  
 1uuaK$$&& L$W/J%JKKKLaQ Q   a!E;;== Vq[$5$5$7$7 $"PP   aF<<>> Rfk%6%6%8%8 R$W/P%PQQQR ! q55Aq))1q511a!eA5FGbhvq1u---Q78(61q5))QU34CC !VVv:!#CCUUM   !VVuQF
++a/&89CC!VVrvfqj))AJ6:;CCAE""q1uQ&78bhvq1u---Q78(61q5))QU34C
 
r   N)r   r-   )
r.   r/   r0   r1   r:   propertyr8   setterr   r!   r   r   r   r4   r4      s         2      X \  \4	, 	, 	,T T T T T Tr   r4   c                   "     e Zd ZdZ fdZ xZS )NormalDistributionz1Class for the Normal (aka Gaussian) distribution.c                 L    t                                          d           d S )Nr   r7   superr:   r   	__class__s    r   r:   zNormalDistribution.__init__W  $    q!!!!!r   r.   r/   r0   r1   r:   __classcell__rT   s   @r   rO   rO   T  s>        ;;" " " " " " " " "r   rO   c                   "     e Zd ZdZ fdZ xZS )PoissonDistributionz*Class for the scaled Poisson distribution.c                 L    t                                          d           d S )Nr%   r7   rQ   rS   s    r   r:   zPoissonDistribution.__init__^  rU   r   rV   rX   s   @r   rZ   rZ   [  s>        44" " " " " " " " "r   rZ   c                   "     e Zd ZdZ fdZ xZS )GammaDistributionz!Class for the Gamma distribution.c                 L    t                                          d           d S )Nr>   r7   rQ   rS   s    r   r:   zGammaDistribution.__init__e  rU   r   rV   rX   s   @r   r]   r]   b  s>        ++" " " " " " " " "r   r]   c                   "     e Zd ZdZ fdZ xZS )InverseGaussianDistributionz>Class for the scaled InverseGaussianDistribution distribution.c                 L    t                                          d           d S )N   r7   rQ   rS   s    r   r:   z$InverseGaussianDistribution.__init__l  rU   r   rV   rX   s   @r   r`   r`   i  s>        HH" " " " " " " " "r   r`   )normalpoissongammazinverse-gaussian)r1   abcr   r   collectionsr   r?   numpyr   scipy.specialr   r   r   r4   rO   rZ   r]   r`   EDM_DISTRIBUTIONSr   r   r   <module>rk      s    ( ' ' ' ' ' ' ' " " " " " "            "z"8:PQQ \B \B \B \B \B7 \B \B \B \B~[ [ [ [ [4 [ [ [|" " " " ", " " "" " " " "- " " "" " " " "+ " " "" " " " ""5 " " " !"3	    r   