Ë
    ÷Q(h¾  ã                   ó$  — d Z ddlmZmZ ddlmZ ddlZddlm	Z	m
Z
 ddlmZ ddlmZ e G d	„ d
«      «       Zej                   f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edœZy)zM
Module contains classes for invertible (and differentiable) link functions.
é    )ÚABCÚabstractmethod)Ú	dataclassN)ÚexpitÚlogit)Úgmeané   )Úsoftmaxc                   óB   — e Zd ZU eed<   eed<   eed<   eed<   d„ Zd„ Zy)ÚIntervalÚlowÚhighÚlow_inclusiveÚhigh_inclusivec                 ó‚   — | j                   | j                  kD  r&t        d| j                   › d| j                  › d�«      ‚y)zCheck that low <= highz#One must have low <= high; got low=z, high=ú.N)r   r   Ú
ValueError)Úselfs    úP/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sklearn/_loss/link.pyÚ__post_init__zInterval.__post_init__   s?   € à�8‰8�d—i‘iÒÜØ5°d·h±h°Z¸wÀtÇyÁyÀkÐQRÐSóð ð  ó    c                 óž  — | j                   r!t        j                  || j                  «      }n t        j                  || j                  «      }t        j
                  |«      sy| j                  r!t        j                  || j                  «      }n t        j                  || j                  «      }t        t        j
                  |«      «      S )zóTest whether all values of x are in interval range.

        Parameters
        ----------
        x : ndarray
            Array whose elements are tested to be in interval range.

        Returns
        -------
        result : bool
        F)r   ÚnpÚgreater_equalr   ÚgreaterÚallr   Ú
less_equalr   ÚlessÚbool)r   Úxr   r   s       r   ÚincludeszInterval.includes    s‹   € ð ×ÒÜ×"Ñ" 1 d§h¡hÓ/‰Cä—*‘*˜Q §¡Ó)ˆCä�v‰v�cŒ{Øà×ÒÜ—=‘=  D§I¡IÓ.‰Dä—7‘7˜1˜dŸi™iÓ(ˆDô ”B—F‘F˜4“LÓ!Ð!r   N)Ú__name__Ú
__module__Ú__qualname__ÚfloatÚ__annotations__r   r   r!   © r   r   r   r      s"   … à	ƒJØ
ƒKØÓØÓòó"r   r   c                 óÄ  — dt        j                  |«      j                  z  }| j                  t         j                   k(  rd}n:| j                  dk  r| j                  d|z
  z  |z   }n| j                  d|z   z  |z   }| j
                  t         j                  k(  rd}||fS | j
                  dk  r| j
                  d|z   z  |z
  }||fS | j
                  d|z
  z  |z
  }||fS )zÔGenerate values low and high to be within the interval range.

    This is used in tests only.

    Returns
    -------
    low, high : tuple
        The returned values low and high lie within the interval.
    é
   g    _ Âr   é   g    _ B)r   ÚfinfoÚepsr   Úinfr   )ÚintervalÚdtyper,   r   r   s        r   Ú_inclusive_low_highr0   =   sâ   € ð Œr�x‰x˜‹×"Ñ"Ñ
"€CØ‡|�|œŸ™�wÒØ‰Ø	�‰˜Ò	Ø�l‰l˜a #™gÑ&¨Ñ,‰à�l‰l˜a #™gÑ&¨Ñ,ˆà‡}�}œŸ™ÒØˆð �ˆ9Ðð 
�‰˜Ò	Ø�}‰}  C¡Ñ(¨3Ñ.ˆð �ˆ9Ðð �}‰}  C¡Ñ(¨3Ñ.ˆà�ˆ9Ðr   c                   óx   — e Zd ZdZdZ eej                   ej                  dd«      Ze	dd„«       Z
e	dd„«       Zy)ÚBaseLinka   Abstract base class for differentiable, invertible link functions.

    Convention:
        - link function g: raw_prediction = g(y_pred)
        - inverse link h: y_pred = h(raw_prediction)

    For (generalized) linear models, `raw_prediction = X @ coef` is the so
    called linear predictor, and `y_pred = h(raw_prediction)` is the predicted
    conditional (on X) expected value of the target `y_true`.

    The methods are not implemented as staticmethods in case a link function needs
    parameters.
    FNc                  ó   — y)aX  Compute the link function g(y_pred).

        The link function maps (predicted) target values to raw predictions,
        i.e. `g(y_pred) = raw_prediction`.

        Parameters
        ----------
        y_pred : array
            Predicted target values.
        out : array
            A location into which the result is stored. If provided, it must
            have a shape that the inputs broadcast to. If not provided or None,
            a freshly-allocated array is returned.

        Returns
        -------
        out : array
            Output array, element-wise link function.
        Nr'   ©r   Úy_predÚouts      r   ÚlinkzBaseLink.linko   ó   � r   c                  ó   — y)aŒ  Compute the inverse link function h(raw_prediction).

        The inverse link function maps raw predictions to predicted target
        values, i.e. `h(raw_prediction) = y_pred`.

        Parameters
        ----------
        raw_prediction : array
            Raw prediction values (in link space).
        out : array
            A location into which the result is stored. If provided, it must
            have a shape that the inputs broadcast to. If not provided or None,
            a freshly-allocated array is returned.

        Returns
        -------
        out : array
            Output array, element-wise inverse link function.
        Nr'   ©r   Úraw_predictionr6   s      r   ÚinversezBaseLink.inverse…   r8   r   ©N)r"   r#   r$   Ú__doc__Úis_multiclassr   r   r-   Úinterval_y_predr   r7   r<   r'   r   r   r2   r2   Y   sP   „ ñð €Mñ
  §¡˜w¨¯©°°uÓ=€Oàòó ðð* òó ñr   r2   c                   ó   — e Zd ZdZdd„ZeZy)ÚIdentityLinkz"The identity link function g(x)=x.Nc                 ó:   — |�t        j                  ||«       |S |S r=   )r   Úcopytor4   s      r   r7   zIdentityLink.linkŸ   s   € Øˆ?Ü�I‰I�c˜6Ô"ØˆJàˆMr   r=   )r"   r#   r$   r>   r7   r<   r'   r   r   rB   rB   œ   s   „ Ù,óð �Gr   rB   c                   óJ   — e Zd ZdZ edej                  dd«      Zdd„Zdd„Z	y)ÚLogLinkz"The log link function g(x)=log(x).r   FNc                 ó0   — t        j                  ||¬«      S ©N©r6   )r   Úlogr4   s      r   r7   zLogLink.link®   s   € Ü�v‰v�f #Ô&Ð&r   c                 ó0   — t        j                  ||¬«      S rH   )r   Úexpr:   s      r   r<   zLogLink.inverse±   s   € Ü�v‰v�n¨#Ô.Ð.r   r=   )
r"   r#   r$   r>   r   r   r-   r@   r7   r<   r'   r   r   rF   rF   ©   s#   „ Ù,á˜q "§&¡&¨%°Ó7€Oó'ô/r   rF   c                   ó6   — e Zd ZdZ edddd«      Zdd„Zdd„Zy)	Ú	LogitLinkz&The logit link function g(x)=logit(x).r   r*   FNc                 ó   — t        ||¬«      S rH   ©r   r4   s      r   r7   zLogitLink.linkº   s   € Ü�V Ô%Ð%r   c                 ó   — t        ||¬«      S rH   ©r   r:   s      r   r<   zLogitLink.inverse½   s   € Ü�^¨Ô-Ð-r   r=   ©r"   r#   r$   r>   r   r@   r7   r<   r'   r   r   rN   rN   µ   s   „ Ù0á˜q ! U¨EÓ2€Oó&ô.r   rN   c                   ó6   — e Zd ZdZ edddd«      Zdd„Zdd„Zy)	ÚHalfLogitLinkzZHalf the logit link function g(x)=1/2 * logit(x).

    Used for the exponential loss.
    r   r*   FNc                 ó*   — t        ||¬«      }|dz  }|S )NrI   g      à?rP   r4   s      r   r7   zHalfLogitLink.linkÉ   s   € Ü�F Ô$ˆØˆs‰
ˆØˆ
r   c                 ó    — t        d|z  |«      S )Nr	   rR   r:   s      r   r<   zHalfLogitLink.inverseÎ   s   € Ü�Q˜Ñ'¨Ó-Ð-r   r=   rS   r'   r   r   rU   rU   Á   s#   „ ññ
 ˜q ! U¨EÓ2€Oóô
.r   rU   c                   ó@   — e Zd ZdZdZ edddd«      Zd„ Zd
d„Zd
d	„Z	y)ÚMultinomialLogitaš  The symmetric multinomial logit function.

    Convention:
        - y_pred.shape = raw_prediction.shape = (n_samples, n_classes)

    Notes:
        - The inverse link h is the softmax function.
        - The sum is over the second axis, i.e. axis=1 (n_classes).

    We have to choose additional constraints in order to make

        y_pred[k] = exp(raw_pred[k]) / sum(exp(raw_pred[k]), k=0..n_classes-1)

    for n_classes classes identifiable and invertible.
    We choose the symmetric side constraint where the geometric mean response
    is set as reference category, see [2]:

    The symmetric multinomial logit link function for a single data point is
    then defined as

        raw_prediction[k] = g(y_pred[k]) = log(y_pred[k]/gmean(y_pred))
        = log(y_pred[k]) - mean(log(y_pred)).

    Note that this is equivalent to the definition in [1] and implies mean
    centered raw predictions:

        sum(raw_prediction[k], k=0..n_classes-1) = 0.

    For linear models with raw_prediction = X @ coef, this corresponds to
    sum(coef[k], k=0..n_classes-1) = 0, i.e. the sum over classes for every
    feature is zero.

    Reference
    ---------
    .. [1] Friedman, Jerome; Hastie, Trevor; Tibshirani, Robert. "Additive
        logistic regression: a statistical view of boosting" Ann. Statist.
        28 (2000), no. 2, 337--407. doi:10.1214/aos/1016218223.
        https://projecteuclid.org/euclid.aos/1016218223

    .. [2] Zahid, Faisal Maqbool and Gerhard Tutz. "Ridge estimation for
        multinomial logit models with symmetric side constraints."
        Computational Statistics 28 (2013): 1017-1034.
        http://epub.ub.uni-muenchen.de/11001/1/tr067.pdf
    Tr   r*   Fc                 ó`   — |t        j                  |d¬«      d d …t         j                  f   z
  S )Nr*   ©Úaxis)r   ÚmeanÚnewaxis)r   r;   s     r   Úsymmetrize_raw_predictionz*MultinomialLogit.symmetrize_raw_prediction  s%   € Ø¤§¡¨¸QÔ ?ÂÄ2Ç:Á:ÀÑ NÑNÐNr   Nc                 óz   — t        |d¬«      }t        j                  ||d d …t        j                  f   z  |¬«      S )Nr*   r[   rI   )r   r   rJ   r^   )r   r5   r6   Úgms       r   r7   zMultinomialLogit.link  s1   € ä�6 Ô"ˆÜ�v‰v�f˜r¢!¤R§Z¡Z -Ñ0Ñ0°cÔ:Ð:r   c                 ój   — |€t        |d¬«      S t        j                  ||«       t        |d¬«       |S )NT)ÚcopyF)r
   r   rD   r:   s      r   r<   zMultinomialLogit.inverse  s1   € Øˆ;Ü˜>°Ô5Ð5ä�I‰I�c˜>Ô*Ü�C˜eÕ$ØˆJr   r=   )
r"   r#   r$   r>   r?   r   r@   r_   r7   r<   r'   r   r   rY   rY   Ò   s/   „ ñ+ðZ €MÙ˜q ! U¨EÓ2€OòOó;ô
r   rY   )ÚidentityrJ   r   Ú
half_logitÚmultinomial_logit)r>   Úabcr   r   Údataclassesr   Únumpyr   Úscipy.specialr   r   Úscipy.statsr   Úutils.extmathr
   r   Úfloat64r0   r2   rB   rF   rN   rU   rY   Ú_LINKSr'   r   r   ú<module>ro      sª   ðñ÷ $Ý !ã ß &Ý å #ð ÷'"ð '"ó ð'"ðT )+¯
©
ó ô8@ˆsô @ôF
�8ô 
ô	/ˆhô 	/ô	.�ô 	.ô.�Hô .ô"?�xô ?ðF ØØØØ)ñ
�r   