
    P3j?                        d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZmZ ddlmZ e G d d	             Z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)	dataclass)ulp)gmean)_expit_logitget_namespace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    ?/DATA/.local/lib/python3.12/site-packages/sklearn/_loss/link.py__post_init__zInterval.__post_init__   s?    88dii5dhhZwtyykQRS       c                    t        |      \  }}| j                  r|j                  || j                        }n|j	                  || j                        }|j                  |      sy| j                  r|j                  || j                        }n|j                  || j                        }t        |j                  |            S )zTest 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
   r   greater_equalr   greaterallr   
less_equalr   lessbool)r   xxp_r   r   s         r   includeszInterval.includes    s     a A""1dhh/C**Q)Cvvc{==DII.D771dii(D BFF4L!!r   N)__name__
__module____qualname__float__annotations__r    r   r$    r   r   r   r      s"    	J
K"r   r   c                    dt        d      z  }| j                  t        d       k(  rd}n:| j                  dk  r| j                  d|z
  z  |z   }n| j                  d|z   z  |z   }| j                  t        d      k(  rd}n:| j                  dk  r| j                  d|z   z  |z
  }n| j                  d|z
  z  |z
  }t        |      t        |      fS )zGenerate values low and high to be within the interval range.

    This is used in tests only.

    Returns
    -------
    low, high : tuple of floats
        The returned values low and high lie within the interval.
    
      infg    _r   g    _B)r   r   r(   r   )intervalepsr   r   s       r   _inclusive_low_highr1   >   s     s1v+C||e}$		lla#g&,lla#g&,}}e$		}}C(3.}}C(3.:uT{""r   c                   d    e Zd ZdZdZ e ed        ed      dd      Zed        Z	e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.
    Fr.   c                      y)an  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.

        Returns
        -------
        array
            Output array, element-wise link function.
        Nr*   r   y_preds     r   linkzBaseLink.linkp       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).

        Returns
        -------
        array
            Output array, element-wise inverse link function.
        Nr*   r   raw_predictions     r   inversezBaseLink.inverse   r8   r   N)r%   r&   r'   __doc__is_multiclassr   r(   interval_y_predr   r7   r<   r*   r   r   r3   r3   Z   sP     M
 e}eElE5IO "  r   r3   c                       e Zd ZdZd ZeZy)IdentityLinkz"The identity link function g(x)=x.c                     |S Nr*   r5   s     r   r7   zIdentityLink.link   s    r   N)r%   r&   r'   r=   r7   r<   r*   r   r   rA   rA      s    , Gr   rA   c                   >    e Zd ZdZ ed ed      dd      Zd Zd Zy)LogLinkz"The log link function g(x)=log(x).r   r.   Fc                 @    t        |      \  }}|j                  |      S rC   )r
   log)r   r6   r"   r#   s       r   r7   zLogLink.link   s    f%Avvf~r   c                 @    t        |      \  }}|j                  |      S rC   )r
   expr   r;   r"   r#   s       r   r<   zLogLink.inverse   s    n-Avvn%%r   N)	r%   r&   r'   r=   r   r(   r?   r7   r<   r*   r   r   rE   rE      s#    ,q%,u=O&r   rE   c                   2    e Zd ZdZ edddd      Zd Zd Zy)	LogitLinkz&The logit link function g(x)=logit(x).r   r-   Fc                     t        |      S rC   r	   r5   s     r   r7   zLogitLink.link   s    f~r   c                     t        |      S rC   r   r:   s     r   r<   zLogitLink.inverse   s    n%%r   Nr%   r&   r'   r=   r   r?   r7   r<   r*   r   r   rL   rL      s    0q!UE2O&r   rL   c                   2    e Zd ZdZ edddd      Zd Zd Zy)HalfLogitLinkzZHalf the logit link function g(x)=1/2 * logit(x).

    Used for the exponential loss.
    r   r-   Fc                     dt        |      z  S )Ng      ?rN   r5   s     r   r7   zHalfLogitLink.link   s    VF^##r   c                     t        d|z        S )N   rP   r:   s     r   r<   zHalfLogitLink.inverse   s    a.())r   NrQ   r*   r   r   rS   rS      s#    
 q!UE2O$*r   rS   c                   <    e Zd ZdZdZ edddd      Zd Zd Z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                 X    t        |      \  }}||j                  |d      d d d f   z
  S Nr-   )axis)r
   meanrJ   s       r   symmetrize_raw_predictionz*MultinomialLogit.symmetrize_raw_prediction   s1    n-AQ ?4 HHHr   c                 n    t        |      \  }}t        |d      }|j                  ||d d d f   z        S rZ   )r
   r   rG   )r   r6   r"   r#   gms        r   r7   zMultinomialLogit.link   s8    f%A6"vvfr!T'{*++r   c                     t        |      S rC   r   r:   s     r   r<   zMultinomialLogit.inverse  s    ~&&r   N)
r%   r&   r'   r=   r>   r   r?   r]   r7   r<   r*   r   r   rX   rX      s/    +Z Mq!UE2OI,'r   rX   )identityrG   logit
half_logitmultinomial_logitN)r=   abcr   r   dataclassesr   mathr   scipy.statsr   sklearn.utils._array_apir   r	   r
   sklearn.utils.extmathr   r   r1   r3   rA   rE   rL   rS   rX   _LINKSr*   r   r   <module>rl      s    $ !   B B ) (" (" ("V#88s 8v8 &h &	& 	&*H *<'x <'@ )
r   