
    Mpj                     f   S SK r S SKrS SKrS SKrS SKJrJr  S SKJr  S SK	J
r
JrJrJr  S SKJr  S SKJr  S SKJr  S SKJrJr  S S	KJrJrJrJrJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(  S S
K)J*r*  S SK+J,r,J-r-J.r.  S SK+J/r0  S SK1J2r2J3r3J4r4  \5" \Rl                  " 5       5      r7\7 V s/ s H	  o " 5       PM     sn r8\8\'" SS9\&" SS9\$" SS9\$" S S9\$" SS9\$" SS9\$" SS9\%" S S9\%" SS9\%" SS9\%" SS9/-  r8S r9 SS jr:S r;\Rx                  R{                  S\8\9S9S 5       r>\#" 5       / SQ\R~                  * \R~                  /4\" 5       / SQ\R~                  * \R~                  /4\'" 5       / SQ\R~                  * \R~                  /4\&" 5       / SQ\R~                  * \R~                  /4\!" 5       SS/\R~                  * SS \R~                  /4\" 5       SS/\R~                  * SS S \R~                  /4\$" SS9SS/\R~                  * \R~                  /4\$" S S9SS/\R~                  * \R~                  /4\$" S!S9SS/\R~                  * SS \R~                  /4\$" SS9SS/\R~                  * SS S \R~                  /4\$" S"S9SS/\R~                  * SS S \R~                  /4\%" SS9SS/\R~                  * \R~                  /4\%" S S9/ S#Q\R~                  * \R~                  /4\%" S!S9SS/\R~                  * SS \R~                  /4\%" SS9SS/\R~                  * SS S \R~                  /4\%" S"S9SS/\R~                  * SS S \R~                  /4\" 5       / S$Q\R~                  * S%S\R~                  /4\" 5       / \R~                  * S%S&\R~                  /4/r@\!" 5       S // 4\&" 5       S // 4\$" SS9/ S'Q/ 4\$" S S9S(S // 4\$" S!S9S // 4\%" SS9/ S'Q/ 4\%" S S9S(S // 4\%" S!S9S // 4\" 5       S S// 4\" 5       / S)Q/ 4/
rA\!" 5       / S /4\$" SS9/ / S*Q4\$" S S9/ / S*Q4\$" S!S9/ S /4\%" SS9/ / S*Q4\%" S S9/ S*Q/ 4\%" S!S9/ S /4\" 5       / S S/4\" 5       SS+/S S/4/	rB\Rx                  R{                  S,\@\A-   5      S- 5       rC\Rx                  R{                  S.\@\B-   5      S/ 5       rD\Rx                  R{                  S0/ \#" 5       S1S2S3S4S4P\" 5       S1S2S5S1S4P\'" S+S9S1S2SS+S4P\'" SS9S1S2SSS4P\'" SS9S2S1S1S6S4P\&" S+S"S79S1S2S8SS4P\&" S+S"S79S1SS9SS4P\!" 5       S9\R                  " S45      S4S\R                  " S45      -  -
  SS44P\" 5       S9\R                  " S45      \R                  " S45      S+-   S+S+4P\$" S"S9S9\R                  " S45      S:SS4P\%" SS9S9S5SS\R                  " S5      -  -
  SS4P\%" SS9S9S5\R                  " S5      S+-
  SS4P\%" S"S9S9S5S;SS4P\" 5       S\R                  " S45      \R                  " S45      S\R                  " S45      -  -
  SS4P\" 5       S<S=S S S 4P\" 5       S1S=S>S%S 4P\" 5       S<S?S S S 4P\" 5       S1S?S@S%S 4P\" 5       S1SASBS%S 4P\" 5       S1SCSDSESF4P\" 5       S<SC/SF/S"-  Q7P\" 5       S1SGSHSESI4P\" 5       S<SG/SI/S"-  Q7P\" 5       S<SJSDSKSF4P\" 5       S1SJ/SL/S"-  Q7P\" 5       S<SBSBSSM4P\" 5       S<SNSNSSO4P\" 5       S1S>S S S 4P\" 5       S<S>S>SS 4P\" 5       S1SNS SPSO4P\" 5       S1SQS S S 4P\" 5       S1S@S S S 4P\" 5       S<S@S@SS 4P\" S"SR9S</ SSQ\" / SSQ5      ST-
  SS4P\" S"SR9S1/ SSQ\" / SSQ5      S+-
  SS4P\" S"SR9S9/ SSQ\" / SSQ5      SU-
  SS4P\" S"SR9S9/ SVQ\" / SVQ5      SW-
  SS4P\9S9SX 5       rG\Rx                  R{                  S\75      \Rx                  R{                  SYSZS[/5      \Rx                  R{                  S\\R                  \R                  /5      \Rx                  R{                  S]\R                  \R                  /5      \Rx                  R{                  S^SS/5      \Rx                  R{                  S_SS/5      \Rx                  R{                  S`SS/5      \Rx                  R{                  SaSS/5      Sb 5       5       5       5       5       5       5       5       rJ\Rx                  R{                  S\8\9S9\Rx                  R{                  S^SSc/5      Sd 5       5       rK\Rx                  R{                  S\8\9S9\Rx                  R{                  S^SSc/5      Se 5       5       rL\Rx                  R{                  S\8\9S9\Rx                  R{                  S^SfSg/5      Sh 5       5       rM\Rx                  R{                  S\8\9S9Si 5       rN\Rx                  R{                  S\8\9S9\Rx                  R{                  S^SSc/5      Sj 5       5       rO\Rx                  R{                  S\8\9S9\Rx                  R{                  S^SSc/5      Sk 5       5       rP\Rx                  R{                  Sl/ SmQ5      \4Sn 5       5       rQ\Rx                  R{                  S\8\9S9\Rx                  R{                  S^SSc/5      So 5       5       rR\Rx                  R{                  Sp\#" 5       \R                  Sq4\" 5       \R                  Sq4\'" SS9Sr Sq4\!" 5       \R                  Ss4\" 5       \R                  St4\$" 5       \R                  St4\" 5       \R                  Su4/5      Sv 5       rUSw rVSx rWSy rX\Rx                  R{                  Sz\R                  " / S{Q5      \R                  " / S|Q5      45      \Rx                  R{                  S}\R                  " / S~Q5      \R                  " / SQ5      45      S 5       5       rZ\Rx                  R{                  S\8\9S9S 5       r[\Rx                  R{                  S\75      \Rx                  R{                  S^SSc/5      \Rx                  R{                  S\R                  \R                  45      \Rx                  R{                  SS5      S 5       5       5       5       r\\Rx                  R{                  S\75      \Rx                  R{                  SS\R                  0S\R                   S34/5      S 5       5       r^\Rx                  R{                  S\'SS0\_S4\'SS 0\`S4\'SS&0\`S4\&SS0\_S4\&SS 0\`S4\&SS&0\`S4/5      S 5       ra\Rx                  R{                  S\8\9S9S 5       rb\Rx                  R{                  S/ SQ5      S 5       rc\Rx                  R{                  S\\4\ \4\"\!4// SQS9\Rx                  R{                  S/ SQ5      \Rx                  R{                  SSZS[/5      \Rx                  R{                  S\." 5       5      S 5       5       5       5       rd\Rx                  R{                  S\." 5       5      S 5       reS rfgs  sn f )    N)assert_allcloseassert_array_equal)approx)LinearConstraintminimizeminimize_scalarnewton)	logsumexp)config_context)_loss)IdentityLink_inclusive_low_high)_LOSSESAbsoluteErrorBaseLossHalfBinomialLossHalfBinomialLossArrayAPIHalfGammaLossHalfMultinomialLossHalfMultinomialLossArrayAPIHalfPoissonLossHalfPoissonLossArrayAPIHalfSquaredErrorHalfTweedieLossHalfTweedieLossIdentity	HuberLossPinballLoss	_log1pexp)assert_all_finite)_atol_for_typemove_to)yield_namespace_device_dtype_combinationsdevice)_array_api_for_testscreate_memmap_backed_dataskip_if_32bitg      ?)quantileg      ?      power            @c                    [        U [        5      (       a  U nUR                  R                  n[        U[        5      (       a  USUR
                  R                   S3-  nU$ [        U[        5      (       a  USUR                   3-  nU$ [        US5      (       a8  [        UR
                  S5      (       a  USUR
                  R                   S3-  nU$ [        U 5      $ )Nz
(quantile=)clossr+   z(power=)
isinstancer   	__class____name__r   r1   r(   r   hasattrr+   str)paramlossnames      Y/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/sklearn/_loss/tests/test_loss.pyloss_instance_namer;   G   s    %""~~&&dK((j!4!4 5Q77D
 	 i((j00D  T7##

G(D(Dgdjj../q11D5z    c                 
   [         R                  R                  U5      nU R                  (       a  [         R                  " XR
                  45      nUR                  US   US   XR
                  -  S9UR                  SS& [         R                  " U5      R                  [        5      U R
                  -  nXv4$ [        U R                  [        5      (       aN  [        U R                  5      u  p[         R                   " XS   /5      n[         R"                  " XS   /5      n	X4nUR                  US   US   US9n[        U R$                  5      u  p['        XS   5      n[)        XS   5      n	UR                  XUS9nU R$                  R*                  S:X  a&  U R$                  R,                  (       a  SUSSUS-  2'   U R$                  R.                  S:X  a&  U R$                  R0                  (       a  SUSSUS-  2'   Xv4$ )z9Random generate y_true and raw_prediction in valid range.r   r,   )lowhighsizeNr@      )nprandomRandomStateis_multiclassempty	n_classesuniformflatarangeastypefloatr2   linkr   r   interval_y_predamaxamininterval_y_truemaxminr>   low_inclusiver?   high_inclusive)
r8   	n_samplesy_bound	raw_boundseedrngraw_predictiony_truer>   r?   s
             r:   random_y_true_raw_predictionr^   V   s    ))


%C9nn"=>!$!1^^+ "- "
A
 9%,,U3dnnD, !!' dii..+D,@,@AIC''3!-.C77DA,/0DI!9Q<i % 
 ((<(<=	#qz"4$SY7##q(T-A-A-O-O*+F&yA~&'$$)d.B.B.Q.Q,-F1(a()!!r<   c                     [         R                  " XS9nU " USU-  -
  5      nU " X-
  5      nU " X-   5      nU " USU-  -   5      nU* SU-  -   SU-  -
  U-   SU-  -  $ )z2Helper function for numerical (first) derivatives.)
fill_valuer-            (@)rC   	full_like)funcxepsh
f_minus_2h
f_minus_1h	f_plus_1h	f_plus_2hs           r:   numerical_derivativerl   {   ss     	Q'Aa!a%iJaeJQUIQQYIJY&Z7*DPSTTr<   r8   )idsc                    U R                   (       a1  Sn[        R                  " [        R                  " SUS-
  US9S5      nO,[	        U R
                  5      u  p4[        R                  " X4SS9nU R
                  R                  (       a(  [        R                  X R
                  R                  4   nU R
                  R                  (       a(  [        R                  X R
                  R                  4   nU R                  U5      (       d   eUR                  S   n[	        U R                  5      u  p4U R                   (       aa  [        R                  " UW45      n[        R                  " X4US9USS2S4'   SSUSS2S4   -
  -  USS2S4'   SSUSS2S4   -
  -  USS2S4'   O[        R                  " X4US9nU R                  U5      (       d   eU R                   R!                  U5      nU R#                  X'S	9  g)
z4Test interval ranges of y_true and y_pred in losses.rB   r   r,   num
   N      ?r-   r]   r\   )rF   rC   tilelinspacer   rR   rU   r_r>   rV   r?   in_y_true_rangeshaperO   rG   in_y_pred_rangerN   r8   )r8   rH   r]   r>   r?   ny_predr\   s           r:   test_loss_boundaryr|      s    	Q	A9EqI'(<(<=	SB/ ))v337778**v338889''''QA#D$8$89IC1i.){{3!4q!ta&A,./q!ta&A,./q!tSA.'''' YY^^F+NIIVI;r<   )r   皙?d   r~   r   皙      ?rB   )r   r   r   r~   r   )r~   rr   ?g?)r}   r   r   r}   )              ?r-   )r   r   r   rr   z!loss, y_true_success, y_true_failc                     U H0  nU R                  [        R                  " U/5      5      (       a  M0   e   U H0  nU R                  [        R                  " U/5      5      (       d  M0   e   g)z-Test boundaries of y_true for loss functions.N)rw   rC   array)r8   y_true_successy_true_failys       r:   test_loss_boundary_y_truer      Z     ##BHHaSM2222 ''!6666 r<   z!loss, y_pred_success, y_pred_failc                     U H0  nU R                  [        R                  " U/5      5      (       a  M0   e   U H0  nU R                  [        R                  " U/5      5      (       d  M0   e   g)z-Test boundaries of y_pred for loss functions.N)ry   rC   r   )r8   y_pred_successy_pred_failr   s       r:   test_loss_boundary_y_predr      r   r<   zDloss, y_true, raw_prediction, loss_true, gradient_true, hessian_truer   g      @ra      g      @g      п)r(   deltag      @       @g      ȿg      ?r   g@xg@xDg     @g     @@g     Bg     B@g     B%   ggT`G<g33333sBg33333sB@g)Pv.<g     B@g?gU`G<g0fՍ<gm@g       +g       gm@rH   )皙?rr   333333?r   r   )g     @r   _eG|>r   c                    U " [         R                  " U/5      [         R                  " U/5      S9nU R                  [         R                  " U/5      [         R                  " U/5      S9nU R                  [         R                  " U/5      [         R                  " U/5      S9u  pU R	                  [         R                  " U/5      [         R                  " U/5      S9u  pU[        USSS9:X  d   eU[        USSS9:X  d   eUb3  U[        USSS9:X  d   eU	[        USSS9:X  d   eU
[        USSS9:X  d   eUb  U[        USSS9:X  d   egg)z7Test losses, gradients and hessians at specific values.rs   V瞯<relabsN)rC   r   gradientloss_gradientgradient_hessianr   )r8   r]   r\   	loss_truegradient_truehessian_trueloss1grad1loss2grad2grad3hesss               r:   test_loss_on_specific_valuesr      s^   x &*288^DT;UVEMMxx!"((N;K2L  E %%xx!"((N;K2L & LE ''xx!"((N;K2L ( KE F9%U;;;;F9%U;;;; }%UCCCC}%UCCCC}%UCCCCvl5AAAA  r<   readonly_memmapFTdtype_in	dtype_outsample_weightout1out2	n_threadsc           	         U " 5       n Sn[        U USSSS9u  pU	R                  U5      n	U
R                  U5      n
Ub  [        R                  " S/U-  US9nUb  [        R                  " XS9nUb  [        R                  " XS9nU(       a$  [        U	5      n	[        U
5      n
Ub  [        U5      nU R                  U	U
UUUS	9nUb  XL d   e U R                  U	U
UUUS
9nUb  XL d   e U R                  U	U
UUUUS9u  pUb  XL d   e Ub  XL d   e Ub%  U R                  (       a  [        R                  " XS9nU R                  U	U
UUUUS9u  pUb  XL d   e Ub  XL d   e U " XUS9  U R                  XS9  U R                  XS9  [        U S5      (       a  U R                  U
S9  [        U S5      (       a+  U R                  U	U
UUUUS9u  pUb  XL d   e Ub  XL d   eggg)a   Test acceptance of dtypes, readonly and writeable arrays in loss functions.

Check that loss accepts if all input arrays are either all float32 or all
float64, and all output arrays are either all float32 or all float64.

Also check that input arrays can be readonly, e.g. memory mapped.
   r}   r   rq   *   r8   rW   rX   rY   rZ   Nr   dtype)r]   r\   r   loss_outr   )r]   r\   r   gradient_outr   )r]   r\   r   r   r   r   )r]   r\   r   r   hessian_outr   r]   r\   r   r]   r   predict_proba)r\   gradient_proba)r]   r\   r   r   	proba_outr   )r^   rL   rC   r   
empty_liker&   r8   r   r   rF   r   fit_intercept_onlyconstant_to_optimal_zeror5   r   r   )r8   r   r   r   r   r   r   r   rW   r]   r\   lgrg   ps                  r:   test_loss_dtyper     s   $ 6DI9F ]]8$F#**84N #!2(C}}V5}}^=*622>B$5mDM		%# 	 	A (1922d%# 	 	A (1922d%#  DA (1922d(1922dD..}}^=  %# ! DA (1922d(1922d]S6G!!!Mt_%%.9t%&&"")' # 
 !,qy66$ ,qy66$y 'r<   rangec                    [        U SSSSS9u  p#US:X  a0  [        R                  " SUR                  S   UR                  S   S	9n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n	U R                  UUUUS
9  U R                  R                  UUUUS
9  [        XE5        U R                  UUUUS9  U R                  R                  UUUUS9  [        Xg5        U R                  R                  UUUUUS9  U R                  R                  UUUUUS9  [        XE5        [        Xg5        U R                  UUUUUS9  U R                  R                  UUUUU	S9  [        Xg5        [        X5        g)z:Test that Python and Cython functions return same results.   r   r   r   r   r   r,   r   ro   r]   r\   r   r   r]   r\   r   r   r]   r\   r   r   r   r]   r\   r   r   r   N)r^   rC   ru   rx   r   r8   r1   r   r   r   r   )
r8   r   r]   r\   out_l1out_l2out_g1out_g2out_h1out_h2s
             r:   test_loss_same_as_C_functionsr     s    :F Av||AFLLOL]]6"F]]6"F]]>*F]]>*F]]>*F]]>*FII%#	   	JJOO%#	   F#MM%#	   	JJ%#	   F#JJ%#   	JJ%#   F#F#%#   	JJ%#    F#F#r<   c                 X   [        U SSSUS9u  p4US:X  a0  [        R                  " SUR                  S   UR                  S   S9n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n	[        R                  " U5      n
U R                  UUUUS	9nU R                  UUUUS
9nU R                  UUUUUS9u  pU R                  UUUU	U
S9u  nn[        X5        [        X5        [        R                  " X5      (       d   e[        X5        [        R                  " X5      (       d   e[        X5        [        X5        [        X5        [        R                  " X5      (       d   e[        X5        [        R                  " X5      (       d   e[        X5        [        R                  " X5      (       d   e[        U S5      (       a  U R                  (       d   e[        R                  " U5      n[        R                  " U5      nU R                  UUUUUS9u  nn[        UU5        [        UU5        [        UU5        [        [        R                  " USS9SSS9  gg)zTest that loss and gradient are the same across different functions.

Also test that output arguments contain correct results.
r   r   r   r   r   r,   r   ro   r   r   r   r   r   r]   r\   r   r   r   axisdy=)rtolN)r^   rC   ru   rx   r   r8   r   r   r   r   r   shares_memoryr5   rF   r   sum)r8   r   global_random_seedr]   r\   r   r   r   r   out_g3out_h3l1g1l2g2g3h3out_g4	out_probag4probas                        r:    test_loss_gradients_are_the_samer   ?  s    :F Av||AFLLOL]]6"F]]6"F]]>*F]]>*F]]>*F]]>*F	%#	 
 
B 
%#	 
 
B %#   FB ""%# # FB Br"B''''r"B''''BBr"B''''r"B''''r"B''''t%&&!!!!~.MM.1	'')' ( 
	E 	F#By)u1-qu= 'r<   onesrD   c           
      8   Sn[        U USSUS9u  pEUS:X  a$  [        R                  " U[        R                  S9nOK[        R                  R                  U5      nUR                  US9R                  [        R                  5      n[        U R                  UUUS9UU R                  UUS	S9-  5        U R                  UUS	S9u  pxU R                  UUUS9u  p[        Xq-  U	5        U R                  (       d  [        X-  U
5        O[        XS	S	2S	4   -  U
5        U R                  UUS	S9u  pU R                  UUUS9u  pU R                  (       d  [        X-  U
5        [        X-  U5        g	[        XS	S	2S	4   -  U
5        [        XS	S	2S	4   -  U5        g	)
zTest sample weights in loss, gradients and hessians.

Make sure that passing sample weights to loss, gradient and hessian
computation methods is equivalent to multiplying by the weights.
r   r   r   r   r   )rx   r   rA   r   N)r^   rC   r   float64rD   rE   normalrL   r   r8   r   rF   r   )r8   r   r   rW   r]   r\   r[   lossesr   	losses_swgradient_swhessian
hessian_sws                r:   test_sample_weight_multipliesr     s    I9F irzzBii##$67

	
299"**E		)' 	 	

 	
)))  
	
 ))% * F
 "//%# 0 I
 F*I60+>D#99;G--% . H
 #33%# 4 K
 0+>/<D#99;G4"88*Er<   c                    [        U SSSSS9u  pUR                  S:X  a  USS2S4   n[        U R                  XS9U R                  XS95        [        U R	                  XS9U R	                  XS95        [        U R                  XS9U R                  XS95        [        U R                  XS9U R                  XS95        gg)	z5Test that reshaped raw_prediction gives same results.r   r   r   r   r   r,   Nrs   )r^   ndimr   r8   r   r   r   )r8   r]   r\   raw_prediction_2ds       r:   test_graceful_squeezingr     s     :F a*1d73IIVIFIIVIC	
 	fOfL	
 	MMMJMMMG	
 	!!!R!!!O	
  r<   c                    U R                   (       d  [        R                  " / SQ5      n[        U R                  [
        5      (       a  SnU R                  R                  nU R                  R                  (       d  XC-   nU R                  R                  nU R                  R                  (       d  XS-
  n[        R                  " X$U5      nU R                  R                  U5      nO[        R                  " U R                  5      R                  [         5      n[        R"                  " U R                  U R                  4[        R$                  " S5      * [         S9n[        R$                  " S5      UR&                  SSU R                  S-   2'   US:X  a0  [        R(                  " SUR*                  S   UR*                  S   S	9nU R-                  UUUS
9nU R/                  XaS9n[1        Xx* SSS9  g)zrTest value of perfect predictions.

Loss of y_pred = y_true plus constant_to_optimal_zero should sums up to
zero.
)r   r   r   r~   rB   rq   绽|=rq   )rx   r`   r   Nr,   r   r   ro   r   r   +=r   )atolr   )rF   rC   r   r2   rN   r   rO   r>   rU   r?   rV   clipinverserK   rH   rL   rM   fullexprJ   ru   rx   r8   r   r   )	r8   r   r\   rf   r>   r?   r]   
loss_valueconstant_terms	            r:   test_loss_of_perfect_predictionr	    s    "<=dii..C&&**C''55i'',,D''66zWW^$?N"">2 4>>*11%8 >>4>>2r
{

 68VVBZ1t~~112Av||AFLLOL%#  J
 11 2 M
 JUGr<   c                   ^ ^^
^^ Sn[        T USSUS9u  mmTS:X  a0  [        R                  " STR                  S   TR                  S   S9mT R	                  TTTS	9u  pEUR                  TR                  :X  d   eUR                  TR                  :X  d   eT R
                  (       dO  U UU4S
 jn[        UTSS9n[        XGSSS9  U UU4S jn[        UTSS9n	T R                  (       a  g[        XYSSS9  g[        T R                  5       Ht  m
U
U UUU4S jn[        UTSS2T
4   SS9n[        USS2T
4   USSS9  U
U UUU4S jn[        UTSS2T
4   SS9n	T R                  (       a  Ma  [        USS2T
4   U	SSS9  Mv     g)zTest gradients and hessians with numerical derivatives.

Gradient should equal the numerical derivatives of the loss function.
Hessians should equal the numerical derivatives of gradients.
r   r   r   r   r   r,   r   ro   r   c                 &   > TR                  TU TS9$ Nr   r8   re   r8   r   r]   s    r:   	loss_func6test_gradients_hessians_numerically.<locals>.loss_funcG  s"    99 +   r<   ư>)rf   h㈵>r   r   r  c                 &   > TR                  TU TS9$ r  r   r  s    r:   	grad_func6test_gradients_hessians_numerically.<locals>.grad_funcQ  s"    == + !  r<   c                 V   > TR                  5       nXS S 2T4'   TR                  TUTS9$ r  )copyr8   re   rawkr8   r\   r   r]   s     r:   r  r  f  s;    $))+AqD	yy!#&"/ !  r<   Nh㈵>c                 d   > TR                  5       nXS S 2T4'   TR                  TUTS9S S 2T4   $ r  )r  r   r  s     r:   r  r  r  sI    $))+AqD	}}!#&"/ %  Q$	 r<   )r^   rC   ru   rx   r   rF   rl   r   approx_hessianr   rH   )r8   r   r   rW   r   rg   r  	g_numericr  	h_numericr  r\   r]   s   ``        @@@r:   #test_gradients_hessians_numericallyr"  (  s    I9FN Av||AFLLOL  %# ! DA 77n*****77n*****	 )NM	4e<	 )NM	At%@ t~~&A  -Yq!t8LRVWIAadGYTF  -Yq!t8LRVWI""!Q$EJ9 'r<   zloss, x0, y_true)	)squared_errorg       r   )r#  g     @]@g?)r#  r   r   )binomial_lossr   r~   )r$  ir   )r$     r   )poisson_lossrb   r   )r&  r   r   )r&  g      6g      $@c           	        ^ ^ [         T    " SS9m [        R                  " T/[        R                  S9m[        R                  " U/[        R                  S9nS[        R                  S[        R                  4U U4S jjnS[        R                  S[        R                  4U U4S jjnS[        R                  S[        R                  4U U4S jjn[        UUUUS	S
S9nTR                  5       mUR                  5       n[        T R                  R                  U5      T5        [        U" U5      SSS9  [        T R                  TUS9SSS9  g)aO  Test that gradients are zero at the minimum of the loss.

We check this on a single value/sample using Halley's method with the
first and second order derivatives computed by the Loss instance.
Note that methods of Loss instances operate on arrays while the newton
root finder expects a scalar or a one-element array for this purpose.
Nr   r   re   returnc                 D   > TR                  TU S9TR                  TS9-   $ )zCompute loss plus constant term.

The constant term is such that the minimum function value is zero,
which is required by the Newton method.
rs   r]   )r8   r   re   r8   r]   s    r:   rd   test_derivatives.<locals>.func  s6     yy!  
)))89 	9r<   c                 $   > TR                  TU S9$ )Nrs   r  r,  s    r:   fprime test_derivatives.<locals>.fprime  s    }}F1}==r<   c                 *   > TR                  TU S9S   $ )Nrs   r,   )r   r,  s    r:   fprime2!test_derivatives.<locals>.fprime2  s    $$F1$EaHHr<   r   gHj>)x0r/  r2  maxitertolr   r  r  rs   gƠ>)r   rC   r   r   ndarrayr	   ravelr   rN   r  r   )r8   r4  r]   rd   r/  r2  optimums   ` `    r:   test_derivativesr;    s   4 4=t,DXXvhbjj1F	2$bjj	)B9

 9rzz 9 9>"** > > >I2:: I"** I I G \\^FmmoGDII%%g.7DM151DMMMH!RVWr<   c                   ^ ^^^ SmT R                   (       d0  T R                  R                  [        R                  " SSTS95      mOH[        R
                  " T5      R                  [        R                  5      T R                  -  mSTSSS2'   TS:X  a  [        R                  " S	S
TS9mT R                  TTS9nU UUU4S jnT R                   (       d  [        USSS0S9nT R                  T[        R                  " TU5      TS9nUR                  [        5       :X  d   eUR                  TR                  :X  d   e[!        U5        U[#        UR$                  SS9:H    UR'                  5       [#        SSS9:H    g[)        U[        R*                  " T R                  5      SSS0S[-        [        R.                  " ST R                  45      SS5      S9nT R                  T[        R0                  " UTS45      TS9nUR                  TR                  :X  d   e[!        U5        [3        X$R$                  SSS9  [3        UR'                  SS9SSS9  g)zrTest that fit_intercept_only returns the argmin of the loss.

Also test that the gradient is zero at the minimum.
2   r   ro   r   Nr   r   r~   r-   r   c                    > TR                   (       d  [        R                  " TU S9nO5[        R                  " [        R                  " U TTR
                  4S95      nT" TUTS9$ )N)rx   r`   rx   r   )rF   rC   r  ascontiguousarraybroadcast_torH   )re   r\   r8   rW   r   r]   s     r:   fun%test_loss_intercept_only.<locals>.fun  s[    !!WWI1EN11)T^^)DEN )'
 	
r<   gHz>r5  r   )r6  optionsr   r   -q=)r   gvIh%<=SLSQPr,   )r6  rE  methodconstraintsr  r  r   r7  )rF   rN   r  rC   ru   rK   rL   r   rH   r   r   r   rc   rx   tupler   r   r   re   r   r   zerosr   r   rt   r   )r8   r   arC  optgradrW   r]   s   ``    @@r:   test_loss_intercept_onlyrP    s     I""2;;r1)#DE9%,,RZZ84>>IssC	:v]KA
 
 cti5EF}}<<2'  

 ww%'!!!ww&,,&&&!	VCEEt$$
fQE** HHdnn&$(!T^^1D)Eq!L
 }}771y!n5'  

 ww&,,&&&!55t%8a(!%8r<   zloss, func, random_distr   c                 ,    [         R                  " U SS9$ )N   )q)rC   
percentile)re   s    r:   <lambda>rU    s    r}}Q"/Er<   poissonexponentialbinomialc                 b   [         R                  R                  U5      nUS:X  a  UR                  SSSS9nO[	        XB5      " SS9nU R                  US9n[        U5        U[        U R                  R                  U" U5      5      5      :X  d   eU R                  R                  U5      [        U" U5      5      :X  d   e[        U [        5      (       a%  [        U R                  R                  U5      U5        U R                  R                  (       a?  UR                  U R                  R                   5        U R                  US9n[        U5        U R                  R"                  (       a@  UR                  U R                  R$                  5        U R                  US9n[        U5        gg)zTest that fit_intercept_only returns the correct functional.

We test the functional for specific, meaningful distributions, e.g.
squared error estimates the expectation of a probability distribution.
rX  r,   rr   r   rA   r+  N)rC   rD   rE   rX  getattrr   r   r   rN   r  r2   r   r   rR   rU   fillr>   rV   r?   )r8   rd   random_distr   r[   y_trainbaseline_predictions          r:    test_specific_fit_intercept_onlyr_    se   $ ))

 2
3Cj ,,q#C,0#+5111A )*&W)F"GGGG9901VDM5JJJJ$%%		))*=>@ST ))T))--."55W5E-.**T))../"55W5E-. +r<   c            	         [         R                  R                  S5      n Sn[        US9nU R	                  SUS-   SS9R                  [         R                  5      nUR                  US9nUR                  U4:X  d   e[         R                  " XR                  S9n[        U5       H  nX6:H  R                  5       XV'   M     [        U[         R                  " U5      [         R                  " [         R                  " U5      5      -
  5        [        US	S	S	24   UR                  R                  US	S	S	24   5      5        [         R                  " S
S9[         R                   " S
S94 HX  nUR                  [         R                  5      nUR                  US9nUR                  UR                  :X  d   e[#        U5        MZ     g	)zATest that fit_intercept_only returns the mean functional for CCE.r   r   r   r,   r   rA   r+  r   Nrq   r@  )rC   rD   rE   r   randintrL   r   r   rx   rL  r   r   meanr   logrN   r   r   )r[   rH   r8   r]  r^  r   r  s          r:   (test_multinomial_loss_fit_intercept_onlyrd  1  se   
))


"CI3D kk!Y]k5<<RZZHG111A$$444
--0A9""$ 'RWWRVVAY5G)GH'a0$))..472LMHH2&b(9:..,"55W5E"((GMM999-.	 ;r<   c                     SnSn[        US9n[        UUU S9u  pE[        R                  " SSUS9nUR                  R                  UUUS9nUR                  UUUS9n[        Xx5        g	)
a  Test that Multinomial cy_gradient gives the same result as gradient.

CyHalfMultinomialLoss does not inherit from CyLossFunction and has a different API.
As a consequence, the functions like `loss` and `gradient` do not rely on `cy_loss`
and `cy_gradient`.
r   r   r   r8   rW   rZ   r~   r-   ro   r   N)r   r^   rC   ru   r1   _test_cy_gradientr   r   )	r   rW   rH   r8   r]   r\   r   r   r   s	            r:   test_multinomial_cy_gradientrh  H  s     II3D9F
 KKQI6MJJ((%# ) E
 MM%#  E
 E!r<   c                    [         R                  R                  U 5      nSn[        5       n[	        SS9nUR                  SSUS9R                  [         R                  5      nUR                  US9n[         R                  " US45      nSU-  USS2S4'   SU-  USS2S	4'   [        UR                  XVS
9UR                  XWS
95        g)zKTest that multinomial loss with n_classes = 2 is the same as binomial loss.r   r-   r   r   rA   g      Nrr   r,   rs   )rC   rD   rE   r   r   ra  rL   r   r   rG   r   r8   )r   r[   rW   binommultinomr]  r\   raw_multinoms           r:   "test_binomial_and_multinomial_lossrm  f  s    
))

 2
3CIE"Q/Hkk!QYk/66rzzBGZZYZ/N88YN+L.LA~-LA

'
AWBr<   r]   )r   r   r   )r   r,   r,   r{   )g      r   r   )r.   rB   rB   c                     S nS n[        5       nU R                  U5      n UR                  U5      nX4nU" U6 [        U" U6 5      :X  d   e[        UR                  " U6 U" U6 5        g)a  Test that both formulations of the binomial deviance agree.

Often, the binomial deviance or log loss is written in terms of a variable
z in {-1, +1}, but we use y in {0, 1}, hence z = 2 * y - 1.
ESL II Eq. (10.18):

    -loglike(z, f) = log(1 + exp(-2 * z * f))

Note:
    - ESL 2*f = raw_prediction, hence the factor 2 of ESL disappears.
    - Deviance = -2*loglike + .., but HalfBinomialLoss is half of the
      deviance, hence the factor of 2 cancels in the comparison.
c           	          SU -  S-
  n[         R                  " [         R                  " S[         R                  " U* U-  5      -   5      5      $ Nr-   r,   )rC   rb  rc  r  r   raw_predzs      r:   alt_loss:test_binomial_vs_alternative_formulation.<locals>.alt_loss  s;    EAIwwrvva"&&!h"77899r<   c                 P    SU -  S-
  nU* S[         R                  " X!-  5      -   -  $ rp  )rC   r  rq  s      r:   alt_gradient>test_binomial_vs_alternative_formulation.<locals>.alt_gradient  s,    EAIrQ--..r<   N)r   rL   r   r   r   )r]   r{   global_dtypert  rw  bin_lossdatums          r:   (test_binomial_vs_alternative_formulationr|  w  sq    ":/
  !H]]<(F]]<(FEUvh&67777H%%u-|U/CDr<   c                    Sn[        U USSUS9u  p4[        U S5      (       aQ  U R                  U5      nUR                  X R                  4:X  d   e[
        R                  " USS9[        SSS	9:X  d   e[        U S
5      (       a  SS[
        R                  " U5      4[
        R                  " U5      S4[
        R                  " U5      [
        R                  " U5      44 Hv  u  peU R                  UUSUUS9u  peUR                  X R                  4:X  d   e[
        R                  " USS9[        SSS	9:X  d   e[        UU R                  UUSSS95        Mx     gg)z<Test that predict_proba and gradient_proba work as expected.r   r   r   r   r   r,   r   r   rF  r   )NNNr   r   )r^   r5   r   rx   rH   rC   r   r   r   r   r   r   )r8   r   rW   r]   r\   r   rO  s          r:   test_predict_probar~    sh    I9F t_%%"">2{{y..9999vve!$qe(<<<<t%&&2==01]]>*D1]]>*BMM.,IJ	
KD ---"! . KD ;;9nn"====66%a(F1%,@@@@!#1"&!%	  
 'r<   r   order)CFc                    SnUS:X  a  [         R                  " U5      nU " US9n U R                  UUUS9u  pVU R                  (       a&  UR                  U4:X  d   eUR                  S:X  d   eOrU R
                  (       a;  UR                  X@R                  4:X  d   eUR                  X@R                  4:X  d   eO&UR                  U4:X  d   eUR                  U4:X  d   eUR                  U:X  d   eUR                  U:X  d   eUS:X  a;  UR                  R                  (       d   eUR                  R                  (       d   egUR                  R                  (       d   eUR                  R                  (       d   eg)zTest that init_gradient_and_hessian works as expected.

passing sample_weight to a loss correctly influences the constant_hessian
attribute, and consequently the shape of the hessian array.
r   r   r(  )rW   r   r  )r,   r  N)rC   r   init_gradient_and_hessianconstant_hessianrx   rF   rH   r   flagsc_contiguousf_contiguous)r8   r   r   r  rW   r   r   s          r:   test_init_gradient_and_hessiansr    sU    I	*m,D66 7 H
 ~~)---}}$$$			~~)^^!<<<<}}NN ;;;;}},,,}},,,>>U"""==E!!!|~~****}}))))~~****}}))))r<   zparams, err_msgz+Valid options for 'dtype' are .* Got dtype=z	 instead.c                     U " 5       n [         R                  " [        [        4US9   U R                  " SSS0UD6u  p4SSS5        g! , (       d  f       g= f)zDTest that init_gradient_and_hessian raises errors for invalid input.matchrW   r   N )pytestraises
ValueError	TypeErrorr  )r8   paramserr_msgr   r   s        r:   %test_init_gradient_and_hessian_raisesr    sE     6D	
I.g	> ::QQQ&Q 
?	>	>s   A
Azloss, params, err_type, err_msgr(   z4quantile must be an instance of float, not NoneType.zquantile == 0, must be > 0.zquantile == 1.1, must be < 1.c                 p    [         R                  " X#S9   U " S0 UD6  SSS5        g! , (       d  f       g= f)z/Test that loss raises errors for invalid input.r  Nr  )r  r  )r8   r  err_typer  s       r:   #test_loss_init_parameter_validationr     s&    B 
x	/v 
0	/	/s   	'
5c                     Sn[        U USSSS9u  p#[        R                  " U 5      n[        R                  " U5      nU " X#S9[	        U" X#S95      :X  d   eg)z Test that losses can be pickled.r   r   r   r   r   rs   N)r^   pickledumpsloadsr   )r8   rW   r]   r\   pickled_lossunpickled_losss         r:   test_loss_pickler  %  sj     I9F <<%L\\,/Nv=fDB   r<   r   )r)   r   r,   r   r-   rB   c                    [        U S9n[        U S9nSn[        XSS9u  pEUR                  R	                  U5      nUR                  XES9UR                  U5      -   nUR                  XFS9UR                  U5      -   n[        Xx5        UR                  XES9u  pUR                  XFS9u  p[        XU-  5        [        XU-  US-  U-  -   5        g)zCTest for identical losses when only the link function is different.r*   rq   r   rf  rs   r-   N)	r   r   r^   rN   r  r8   r   r   r   )r   half_tweedie_loghalf_tweedie_identityrW   r]   r\   r{   loss_logloss_identitygradient_loghessian_loggradient_identityhessian_identitys                r:   %test_tweedie_log_identity_consistencyr  7  s    'Q/3!<I9F ""**>:F  $$ % 11&9:H *.. / 66v>?M H, !1 A A !B !L +@*P*P +Q +' L+<"<=//&!)>N2NNr<   z array_api_loss_class, loss_class)r   r   r   method_name)__call__r   r8   r   use_sample_weightz"namespace, device_name, dtype_namec                 L   S n[        XEU5      u  p[        U5      n
US:X  a  SOSnSnSnU " XS9nU" 5       n[        UUSS	US
9u  nnUR                  U5      nUR                  U5      nUR	                  UU	S9nUR	                  UU	S9nU(       a`  [
        R                  R                  U5      nUR                  SSUS9R                  SS 5      R                  U5      nUR	                  UU	S9nOS nS n[        X5      n[        X5      nU" UUUS9n[        SS9   U" UUUS9nUS:X  a  [
        R                  " UU5      (       d   eOB[        U[        5      (       a"  [        UU5       H  u  nnU" UUUUUU
S9  M     OU" UUUUUU
S9  S S S 5        g ! , (       d  f       g = f)Nc                     [        [        U [        SS9XUS9  U R                  UR                  :X  d   e[	        U 5      [	        U5      :X  d   eg )Ncpuxpr$   r  )r   r!   rC   r   array_api_device	result_xp	result_npraw_prediction_xpr  r   r  s         r:   _assert_array_api_result5test_loss_array_api.<locals>._assert_array_api_result~  sR     	I"U3YPT	
 "3"9"9999	*.>?P.QQQQr<   float32r  r   r   r   r  r   )ir=  r   r#   r   r   rA   r   r   T)array_api_dispatchr  r  )r%   r    r^   rL   asarrayrC   rD   rE   rI   r  rZ  r   iscloser2   rK  zip)array_api_loss_class
loss_classr  r  	namespacedevice_name
dtype_namer  r  r$   r  r   random_seedrW   array_api_loss_instanceloss_instancer]   r\   	y_true_xpr  r[   sample_weight_npsample_weight_xprI  array_api_methodr  r  res_xpres_nps                                r:   test_loss_array_apir  d  s   4R &ijIJB*%D*4DKI2bHLM9FN ]]:&F#**:6N

6&
1I

>&
Aii##K0KKAIK.33At<CCJO 	 ::&6v:F]0F6DnDTI 
4	0$,*
	 :%::i3333)U++&))Y&?NFF,"("(*;!! '@ )''&7- 
1	0	0s   A0F
F#c           	         [         R                  " S5      nSUR                  l        [        R
                  " SSS5      n[        XU5      u  pVU H  nUS:X  a  UR                  XuR                  US9nOUR                  XuR                  US9n[        [        UUR                  U5      US95      n	[        UR                  S	UR                  U5      -   5      5      n
U
S
:  d   eU	[         R                  " U
US:X  a  SOSS
S9:X  a  M   e   g )Nmpmathr   i(   i,  r  )r   r$   )r\   raw_prediction_expr  r,   r   r  rG  r   )r  importorskipmpprecrC   ru   r%   r  r  r   rM   r   r  rc  r   )r  r  r  r  values_to_testr  r$   valuere   r  result_mpmaths              r:   test_log1pexpr    s    
   *FFIIN[[b#.N%ijIJB"

5

6
BA

5

6
BA #%66!9
	 fjjVZZ->)>?@q   FMM"i/U
 
 	
 
!  r<   c                  \   [         R                  " [        [         R                  5       V Vs/ s H  u  pU R	                  S5      (       d  M  UPM!     nn n[        U5      S:  d   S5       eU H5  nUR                  S:X  a  M   UR                   SUR                  < S35       e   gs  snn f )a  Check that Cython extension types in _loss have the correct __module__.

When _loss_cython_tree in meson.build is missing __init__.py files, Cython
can not detect the package hierarchy and set __module__ = '_loss' instead
of 'sklearn._loss._loss' on all Cy* extension types, e.g.
`CyHalfMultinomialLoss`. This breaks downstream tools like skops that rely
on __module__ for serialization.
Cyr   z+No Cy* classes found in sklearn._loss._losszsklearn._loss._lossz.__module__ == z , expected 'sklearn._loss._loss'N)inspect
getmembers_loss_moduleisclass
startswithlen
__module__r4   )r9   obj
cy_classesclss       r:   test_cy_loss_classes_moduler    s     !++L'//JJID??4  	J  
 z?QM MM~~!66 	
||nOCNN+= >- .	
6 s   B(B()r   r   r   )gr  r  numpyrC   r  numpy.testingr   r   r   scipy.optimizer   r   r   r	   scipy.specialr
   sklearnr   sklearn._lossr   r  sklearn._loss.linkr   r   sklearn._loss.lossr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   sklearn.utilsr   sklearn.utils._array_apir    r!   r"   r$   r  sklearn.utils._testingr%   r&   r'   listvalues
ALL_LOSSESLOSS_INSTANCESr;   r^   rl   markparametrizer|   infY_COMMON_PARAMSY_TRUE_PARAMSY_PRED_PARAMSr   r   rc  log1pr   r  r   r   r   r   r   r   r	  r"  r;  rP  rb  medianr_  rd  rh  rm  r   r|  r~  r  int64r  r  r  r  r  r  r  r  r  r  s   0r:   <module>r     s       =   $ " / @    $ , 
  '.."#
%/0ZT$&Z0 t$!!!#!$!$!$#&   CE""JU 5GH < I <L ,w.?@_)RVVGRVV+<=]'266'266):;[%'89c
bffWb$$?@_sCjBFF7Ba"@A2c
bffWbff,=>1SzRVVGRVV+<=3#srvvgr4-HI1SzRVVGRq"&&+IJ1SzRVVGRq"&&+IJ2&c
bffWbff4EF1%'>"&&"&&@QR3'#srvvgr45PQ1%SzRVVGRq"&&3QR1%SzRVVGRq"&&3QRBFF7B266*BCB"&&"c266 :;'2 R [1#r241ay"-3!b)2&<1%ay"53'!b1!Q$M2. QC 2M21r=13aS)2&M:1%}b93'aS1aV$S#JA/ 'm#7	7 'm#7	7 Js		S#q!Q/s	#sCd3s 
c	"Cad;s 
d	#S#~xN	s
 
d	#S#xEs 
Cq	)3_dDQs 
Cq	)3ZtLs 
	CABFF1I,=uaHs 
#rvvay"&&)e*;YNs 
q	!3q	3DdDQs 
!q	)3QRVVAY5FdSs 
!q	)3RVVAY5FdSs $!,)	
s, FF1IHHQK$**	
+sr 
	S%Aq1sst 
	S%r15usv 
	S$1a0wsx 
	S$R3ysz 
	S%r15{s| 
	S%Y8MN}s~ 
	S%F+@*AA*EFs@ 
	S%y:OPAsB 
	S%F+@*AA*EFCsD 
	S$I7LMEsF 
	S$E*?)@1)DEGsH 
	S$a1FGIsJ 
	S%3JKKsL 
	S$1a0MsN 
	S$a3OsR $#	
Qs` 
	S%Aq1asb 
	S#q!Q/csd 
	S#sAq1esh  !,o&,	
gsx  !,o&,	
wsH  !,o&,	
GsX  !,n%.	
Wsh 	m  wpBqwpB6 ,*UDM:bjj"**%=>rzz2::&>?4)4$+$+q!f-Y7 . , , 5 @ ? ; -Y7x 5GH4/:K$ ; IK$\ 5GH4/:L> ; IL>^ 5GH68*<=BF > IBFJ 5GH
 I
< 5GH4/:-H ; I-H` 5GH4/:VK ; IVKr " +X #$+X\ 5GH4/:>9 ; I>9B 		RWWh/	"))X.	d	#%ExP		BGGY/	"''=1		BGG]3		RWWj1//>/."<" BHH[$9288K;P#QRBHH^$<bhh{>S#TUE V SED 5GH( I(V ,4/:2::rzz":;*-!* . < ; -!*H , bhh9"((9M	
R -R % B		
 O)		
 
z3'5TUB		
 O)		
 
Z%z3RS5>?> 5GH I" 56) 7)X &	!#34	$&9:	 /2
 	G   D ,udm<(-/P	 ="Pf (-/
	
8
e- 1s   $x.