§
    ¨éUg¦  ã                   óL  — 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dS )zM
Module contains classes for invertible (and differentiable) link functions.
é    )ÚABCÚabstractmethod)Ú	dataclassN)ÚexpitÚlogit)Úgmeané   )Úsoftmaxc                   óD   — e Zd ZU eed<   eed<   eed<   eed<   d„ Zd„ ZdS )ÚIntervalÚlowÚhighÚlow_inclusiveÚhigh_inclusivec                 óf   — | j         | j        k    r t          d| j         › d| j        › d�¦  «        ‚dS )zCheck that low <= highz#One must have low <= high; got low=z, high=ú.N)r   r   Ú
ValueError)Úselfs    úQ/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/sklearn/_loss/link.pyÚ__post_init__zInterval.__post_init__   sE   € àŒ8�d”iÒÐÝØS°d´hÐSÐSÀtÄyÐSÐSÐSñô ð ð  Ðó    c                 ó`  — | j         rt          j        || j        ¦  «        }nt          j        || j        ¦  «        }t          j        |¦  «        sdS | 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Ñ/Ô/ˆCˆCå”*˜Q ¤Ñ)Ô)ˆCåŒv�c‰{Œ{ð 	Ø�5àÔð 	)Ý”=  D¤IÑ.Ô.ˆDˆDå”7˜1˜dœiÑ(Ô(ˆDõ •B”F˜4‘L”LÑ!Ô!Ð!r   N)Ú__name__Ú
__module__Ú__qualname__ÚfloatÚ__annotations__r   r   r!   © r   r   r   r      s^   € € € € € € à	€J€J�JØ
€K€K�KØÐÐÑØÐÐÑðð ð ð"ð "ð "ð "ð "r   r   c                 óT  — 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}n,| j        dk     r| j        d|z   z  |z
  }n| 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˜‰ŒÔ"Ñ
"€CØ„|�œ�wÒÐØˆˆØ	Œ˜Ò	Ð	ØŒl˜a #™gÑ&¨Ñ,ˆˆàŒl˜a #™gÑ&¨Ñ,ˆà„}�œÒÐØˆˆØ	Œ˜Ò	Ð	ØŒ}  C¡Ñ(¨3Ñ.ˆˆàŒ}  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dS )Ú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                 ó   — dS )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.linkn   ó   € € € r   c                 ó   — dS )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   X   s€   € € € € € ðð ð €Mð
 �h ¤˜w¨¬°°uÑ=Ô=€Oàðð ð ñ „^ðð* ðð ð ñ „^ðð ð r   r2   c                   ó   — e Zd ZdZdd„ZeZdS )ÚIdentityLinkz"The identity link function g(x)=x.Nc                 ó8   — |�t          j        ||¦  «         |S |S r=   )r   Úcopytor4   s      r   r7   zIdentityLink.linkž   s#   € Øˆ?ÝŒI�c˜6Ñ"Ô"Ð"ØˆJàˆMr   r=   )r"   r#   r$   r>   r7   r<   r'   r   r   rB   rB   ›   s/   € € € € € Ø,Ð,ðð ð ð ð €G€G€Gr   rB   c                   óH   — e Zd ZdZ edej        dd¦  «        Zdd„Zdd„Z	dS )ÚLogLinkz"The log link function g(x)=log(x).r   FNc                 ó.   — t          j        ||¬¦  «        S ©N©r6   )r   Úlogr4   s      r   r7   zLogLink.link­   s   € ÝŒv�f #Ð&Ñ&Ô&Ð&r   c                 ó.   — t          j        ||¬¦  «        S rH   )r   Úexpr:   s      r   r<   zLogLink.inverse°   s   € ÝŒv�n¨#Ð.Ñ.Ô.Ð.r   r=   )
r"   r#   r$   r>   r   r   r-   r@   r7   r<   r'   r   r   rF   rF   ¨   sY   € € € € € Ø,Ð,à�h˜q "¤&¨%°Ñ7Ô7€Oð'ð 'ð 'ð 'ð/ð /ð /ð /ð /ð /r   rF   c                   ó>   — e Zd ZdZ edddd¦  «        Zdd„Zdd„ZdS )	Ú	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   ´   sW   € € € € € Ø0Ð0à�h˜q ! U¨EÑ2Ô2€Oð&ð &ð &ð &ð.ð .ð .ð .ð .ð .r   rN   c                   ó>   — e Zd ZdZ edddd¦  «        Zdd„Zdd„ZdS )	ÚHalfLogitLinkzZHalf the logit link function g(x)=1/2 * logit(x).

    Used for the exponential loss.
    r   r*   FNc                 ó2   — 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]   € € € € € ðð ð
 �h˜q ! U¨EÑ2Ô2€Oðð ð ð ð
.ð .ð .ð .ð .ð .r   rU   c                   óH   — e Zd ZdZdZ edddd¦  «        Zd„ Zd
d„Zd
d	„Z	dS )Ú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                 ó~   — 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  s=   € å�6 Ð"Ñ"Ô"ˆÝŒv�f˜r ! ! !¥R¤Z -Ô0Ñ0°cÐ:Ñ:Ô:Ð:r   c                 óx   — |€t          |d¬¦  «        S t          j        ||¦  «         t          |d¬¦  «         |S )NT)ÚcopyF)r
   r   rD   r:   s      r   r<   zMultinomialLogit.inverse
  sD   € Øˆ;Ý˜>°Ð5Ñ5Ô5Ð5åŒI�c˜>Ñ*Ô*Ð*Ý�C˜eÐ$Ñ$Ô$Ð$ØˆJr   r=   )
r"   r#   r$   r>   r?   r   r@   r_   r7   r<   r'   r   r   rY   rY   Ñ   su   € € € € € ð+ð +ðZ €MØ�h˜q ! U¨EÑ2Ô2€OðOð 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   