ó
    §ñ:i¦  ã                   ó&  • S r SSKJrJr  SSKJr  SSKrSSKJ	r	J
r
  SSKJr  SSKJr  \ " S	 S
5      5       r\R                   4S jr " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r\\\\\S.rg)zM
Module contains classes for invertible (and differentiable) link functions.
é    )ÚABCÚabstractmethod)Ú	dataclassN)ÚexpitÚlogit)Úgmeané   )Úsoftmaxc                   óJ   • \ rS rSr% \\S'   \\S'   \\S'   \\S'   S rS rSr	g	)
ÚIntervalé   ÚlowÚhighÚlow_inclusiveÚhigh_inclusivec                 ó„   • U R                   U R                  :”  a&  [        SU R                    SU R                   S35      eg)zCheck that low <= highz#One must have low <= high; got low=z, high=Ú.N)r   r   Ú
ValueError)Úselfs    ÚU/srv/projetos/modelo_ml_acdoc/venv/lib/python3.13/site-packages/sklearn/_loss/link.pyÚ__post_init__ÚInterval.__post_init__   s?   € à�8‰8�d—i‘iÓÜØ5°d·h±h°Z¸wÀtÇyÁyÀkÐQRÐSóð ð  ó    c                 óÀ  • U R                   (       a!  [        R                  " XR                  5      nO [        R                  " XR                  5      n[        R
                  " U5      (       d  gU R                  (       a!  [        R                  " XR                  5      nO [        R                  " XR                  5      n[        [        R
                  " U5      5      $ )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   ÚincludesÚInterval.includes   s…   € ð ××Ü×"Ò" 1§h¡hÓ/‰Cä—*’*˜Q§¡Ó)ˆCä�vŠv�c�{‰{Øà××Ü—=’= §I¡IÓ.‰Dä—7’7˜1Ÿi™iÓ(ˆDô ”B—F’F˜4“LÓ!Ð!r   © N)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__ÚfloatÚ__annotations__r!   r   r#   Ú__static_attributes__r%   r   r   r   r      s"   ‡ à	ƒJØ
ƒKØÓØÓòõ"r   r   c                 óÈ  • S[         R                  " U5      R                  -  nU R                  [         R                  * :X  a  SnO;U R                  S:  a  U R                  SU-
  -  U-   nOU R                  SU-   -  U-   nU R
                  [         R                  :X  a  SnX44$ U R
                  S:  a  U R
                  SU-   -  U-
  nX44$ U R
                  SU-
  -  U-
  nX44$ )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Údtyper1   r   r   s        r   Ú_inclusive_low_highr5   <   sÜ   € ð Œr�xŠx˜‹×"Ñ"Ñ
"€CØ‡|�|œŸ™�wÓØ‰Ø	�‰˜Ó	Ø�l‰l˜a #™gÑ&¨Ñ,‰à�l‰l˜a #™gÑ&¨Ñ,ˆà‡}�}œŸ™ÓØˆð ˆ9Ðð 
�‰˜Ó	Ø�}‰}  C¡Ñ(¨3Ñ.ˆð ˆ9Ðð �}‰}  C¡Ñ(¨3Ñ.ˆàˆ9Ðr   c                   ó„   • \ rS rSrSrSr\" \R                  * \R                  SS5      r	\
SS j5       r\
SS j5       rSrg)	ÚBaseLinkéX   aÜ  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                 ó   • g)aà  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   ÚlinkÚBaseLink.linkn   ó   � r   c                 ó   • g)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_predictionr<   s      r   ÚinverseÚBaseLink.inverse„   r?   r   r%   ©N)r&   r'   r(   r)   Ú__doc__Úis_multiclassr   r   r2   Úinterval_y_predr   r=   rC   r,   r%   r   r   r7   r7   X   sP   † ñð €Mñ
  §¡˜w¨¯©°°uÓ=€Oàóó ðð* óó ór   r7   c                   ó&   • \ rS rSrSrSS jr\rSrg)ÚIdentityLinké›   z"The identity link function g(x)=x.Nc                 ó<   • Ub  [         R                  " X!5        U$ U$ rE   )r   Úcopytor:   s      r   r=   ÚIdentityLink.linkž   s   € Ø‰?Ü�IŠI�cÔ"ØˆJàˆMr   r%   rE   )r&   r'   r(   r)   rF   r=   rC   r,   r%   r   r   rJ   rJ   ›   s   † Ù,ôð ƒGr   rJ   c                   óV   • \ rS rSrSr\" S\R                  SS5      rS	S jr	S	S jr
Srg)
ÚLogLinké¨   z"The log link function g(x)=log(x).r   FNc                 ó*   • [         R                  " XS9$ ©N©r<   )r   Úlogr:   s      r   r=   ÚLogLink.link­   s   € Ü�vŠv�fÑ&Ð&r   c                 ó*   • [         R                  " XS9$ rS   )r   ÚexprA   s      r   rC   ÚLogLink.inverse°   s   € Ü�vŠv�nÑ.Ð.r   r%   rE   )r&   r'   r(   r)   rF   r   r   r2   rH   r=   rC   r,   r%   r   r   rP   rP   ¨   s#   † Ù,á˜q "§&¡&¨%°Ó7€Oô'÷/r   rP   c                   óB   • \ rS rSrSr\" SSSS5      rS
S jrS
S jrS	r	g)Ú	LogitLinké´   z&The logit link function g(x)=logit(x).r   r/   FNc                 ó   • [        XS9$ rS   ©r   r:   s      r   r=   ÚLogitLink.link¹   s   € Ü�VÑ%Ð%r   c                 ó   • [        XS9$ rS   ©r   rA   s      r   rC   ÚLogitLink.inverse¼   s   € Ü�^Ñ-Ð-r   r%   rE   ©
r&   r'   r(   r)   rF   r   rH   r=   rC   r,   r%   r   r   r[   r[   ´   s   † Ù0á˜q ! U¨EÓ2€Oô&÷.r   r[   c                   óB   • \ rS rSrSr\" SSSS5      rS
S jrS
S jrS	r	g)ÚHalfLogitLinkéÀ   zRHalf the logit link function g(x)=1/2 * logit(x).

Used for the exponential loss.
r   r/   FNc                 ó"   • [        XS9nUS-  nU$ )NrT   g      à?r^   r:   s      r   r=   ÚHalfLogitLink.linkÈ   s   € Ü�FÑ$ˆØˆs‰
ˆØˆ
r   c                 ó    • [        SU-  U5      $ )Nr	   ra   rA   s      r   rC   ÚHalfLogitLink.inverseÍ   s   € Ü�Q˜Ñ'¨Ó-Ð-r   r%   rE   rc   r%   r   r   re   re   À   s#   † ññ
 ˜q ! U¨EÓ2€Oô÷
.r   re   c                   óL   • \ rS rSrSrSr\" SSSS5      rS rSS	 jr	SS
 jr
Srg)ÚMultinomialLogitéÑ   a  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                 ó\   • U[         R                  " USS9S S 2[         R                  4   -
  $ )Nr/   ©Úaxis)r   ÚmeanÚnewaxis)r   rB   s     r   Úsymmetrize_raw_predictionÚ*MultinomialLogit.symmetrize_raw_prediction  s%   € Ø¤§¢¨¸QÑ ?ÂÄ2Ç:Á:ÀÑ NÑNÐNr   Nc                 ón   • [        USS9n[        R                  " XS S 2[        R                  4   -  US9$ )Nr/   ro   rT   )r   r   rU   rr   )r   r;   r<   Úgms       r   r=   ÚMultinomialLogit.link  s/   € ä�6 Ñ"ˆÜ�vŠv�f¢!¤R§Z¡Z -Ñ0Ñ0°cÑ:Ð:r   c                 ó`   • Uc
  [        USS9$ [        R                  " X!5        [        USS9  U$ )NT)ÚcopyF)r
   r   rM   rA   s      r   rC   ÚMultinomialLogit.inverse
  s/   € Ø‰;Ü˜>°Ñ5Ð5ä�IŠI�cÔ*Ü�C˜eÒ$ØˆJr   r%   rE   )r&   r'   r(   r)   rF   rG   r   rH   rs   r=   rC   r,   r%   r   r   rl   rl   Ñ   s/   † ñ+ðZ €MÙ˜q ! U¨EÓ2€OòOô;÷
r   rl   )ÚidentityrU   r   Ú
half_logitÚmultinomial_logit)rF   Úabcr   r   Údataclassesr   Únumpyr   Úscipy.specialr   r   Úscipy.statsr   Úutils.extmathr
   r   Úfloat64r5   r7   rJ   rP   r[   re   rl   Ú_LINKSr%   r   r   Ú<module>r†      sª   ðñ÷ $Ý !ã ß &Ý å #ð ÷'"ð '"ó ð'"ðT )+¯
©
ô ô8@ˆsô @ôF
�8ô 
ô	/ˆhô 	/ô	.�ô 	.ô.�Hô .ô"?�xô ?ðF ØØØØ)ñ
�r   