
    KiG                     
   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  S SK	J
r
JrJr  S SK	Jr  S SKJr  S SKJrJr  S S	KJr  \R,                  R/                  S
SS/5      \R,                  R/                  SSS/5      S 5       5       r\R,                  R/                  S
SS/5      \R,                  R/                  SSSSSS//5      \R,                  R/                  SSS/5      S 5       5       5       r\R,                  R/                  SSS/5      S 5       rS rS r\R,                  R/                  S
SS/5      \R,                  R/                  S/ SQ5      S 5       5       r\R,                  R/                  S
SS/5      \R,                  R/                  S/ S Q5      S! 5       5       r\R,                  R/                  S"SS#/5      \R,                  R/                  S
SS/5      \R,                  R/                  SSSSS/ S Q/5      S$ 5       5       5       r\R,                  R/                  SS/ S%Q/5      \R,                  R/                  S
SS/5      S& 5       5       r \R,                  R/                  S'\" 5       5      \R,                  R/                  S(\RB                  " S)5      \RD                  " S*5      S4S+ \RF                  " S5      RI                  \RD                  5      S4S, S- S.4S/ S0 S1S./4\RJ                  " / S2Q5      \RJ                  " / S3Q5      S 4\RJ                  " \RL                  \RL                  S S4S5S/5      \RJ                  " / S6Q5      S 4\RJ                  " / S2Q5      \RJ                  " / S6Q\RD                  S79S1S./4/5      S8 5       5       r'\R,                  R/                  S
SS/5      \R,                  R/                  S9S*S:/5      S; 5       5       r(\R,                  R/                  S<S=\RL                  S/4SS=/\RL                  \RL                  /S>S?//4/5      S@ 5       r)\R,                  RU                  \\" SA5      :  SBSC9\R,                  R/                  SD/ SEQ5      \R,                  R/                  S
SS/5      \R,                  R/                  SFSS/5      SG 5       5       5       5       r+\R,                  RU                  \\" SA5      :  SHSC9\R,                  R/                  SD/ SEQ5      \R,                  R/                  S
SS/5      \R,                  R/                  SFSS/5      SI 5       5       5       5       r,g)J    N)assert_allcloseassert_array_equal)approx)config_context)_convert_to_numpyget_namespace)yield_namespace_device_dtype_combinationsdevice)_array_api_for_tests)
np_versionparse_version)_weighted_percentileaverageTFsize
      c                    [         R                  " U 5      n[         R                  " U5      n[        X#SUS9nU S-  S:X  a"  USL a  U[         R                  " U5      :w  d   eg[        U5      [         R                  " U5      :X  d   eg)aa  Ensure `_weighted_percentile` matches `median` when expected.

With unit `sample_weight`, `_weighted_percentile` should match the median except
when `average=False` and the number of samples is even.
For an even array and `average=False`, `percentile_rank=50` gives the lower
of the two 'middle' values, that are averaged when calculating the `median`.
2   r      r   FN)nparange	ones_liker   medianr   )r   r   ysample_weightscores        `/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/sklearn/utils/tests/test_stats.py'test_weighted_percentile_matches_medianr       sp     			$ALLOM 2wGE ax1}E)		!$$$e}		!,,,    percentile_rank   #   =      /   c                    [         R                  R                  U 5      nUR                  SUS9n[         R                  " U5      n[        XVX#S9nU(       a  SnOSn[        U5      [         R                  " XRUS9:X  d   eg)a  Check `_weighted_percentile` with unit weights is correct.

`average=True` results should be the same as `np.percentile`'s
'averaged_inverted_cdf'.
`average=False` results should be the same as `np.percentile`'s
'inverted_cdf'.
Note `np.percentile` is the same as `np.quantile` except `q` is in range [0, 100].

We parametrize through different `percentile_rank` and `size` to
ensure we get cases where `g=0` and `g>0` (see Hyndman and Fan 1996 for details).
r#   r   r   averaged_inverted_cdfinverted_cdf)methodN)r   randomRandomStaterandintr   r   r   
percentile)	global_random_seedr   r"   r   rngr   swr   r,   s	            r   &test_weighted_percentile_matches_numpyr4   (   sn    " ))

 2
3CBT"A	aB IE(%=BMM!VLLLLr!   r   d   c                     [         R                  " SS/SS//5      n[         R                  " SS/SS//5      n[        XU SS9n[        S5       H  nX4   [	        S	5      :X  a  M   e   g
)a  Check `j+1` index is clipped to max, when `average=True`.

`percentile_plus_one_indices` can exceed max index when `percentile_indices`
is already at max index.
Note that when `g` (Hyndman and Fan) / `fraction_above` is greater than 0,
`j+1` (Hyndman and Fan) / `percentile_plus_one_indices` is calculated but
never used, so it does not matter what this value is.
When percentile of percentile rank 100 falls exactly on the last value in the
`weighted_cdf`, `g=0` and `percentile_indices` is at max index. In this case
we set `percentile_plus_one_indices` to be max index as well, so the result is
the average of 2x the max index (i.e. last value of `weighted_cdf`).
r      g?g?r      Tr   g      ?N)r   arrayr   ranger   )r"   r   r3   r   idxs        r   *test_weighted_percentile_plus_one_clip_maxr<   G   sj      	1a&1a&!"A	C:1v&	'B FEQxzVC[((( r!   c                      [         R                  " S[         R                  S9n [         R                  " S[         R                  S9n[	        XS5      n[        U5      S:X  d   eg)zJCheck `weighted_percentile` with unit weights and all 0 values in `array`.f   dtyper   r   N)r   zerosfloat64onesr   r   )r   r3   r   s      r   test_weighted_percentile_equalrD   ^   sH    
BJJ'A	BJJ	'B +E%=Ar!   c                      [         R                  " S5      n [         R                  " S5      n[        XS5      n[	        U5      S:X  d   eg)zKCheck `weighted_percentile` with all weights equal to 0 returns last index.r   r   g      "@N)r   r   rA   r   r   )r   r3   values      r   )test_weighted_percentile_all_zero_weightsrG   h   s:    
		"A	"B +E%=Cr!   zpercentile_rank, expected_value))r   r   )r   r8   )r5   r&   c                 @   [         R                  " / SQ5      n[         R                  " / SQ5      n[        [         R                  " X345      R                  [         R                  " XD45      R                  XS9n[        S5       H  n[        XV   5      U:X  a  M   e   g)as  Check leading, trailing and middle 0 weights behave correctly.

Check that leading zero-weight observations are ignored when `percentile_rank=0`.
See #20528 for details.
Check that when `average=True` and the `j+1` ('plus one') index has sample weight
of 0, it is ignored. Also check that trailing zero weight observations are ignored
(e.g., when `percentile_rank=100`).
)r   r7   r   r8      r&      )r   r   r7   r7   r   r7   r   r   r   N)r   r9   r   vstackTr:   r   )r   r"   expected_valuer   r3   rF   r;   s          r   ,test_weighted_percentile_ignores_zero_weightrN   p   sz     	&'A	'	(B 
		1&RYYx022OE Qxej!^333 r!   )r#   r$   r   r%   c                    [         R                  R                  U 5      nUR                  SSS9nUR	                  SSS9n[         R
                  " XE5      n[        XEXS9n[        U[         R                  " U5      XS9nU[        U5      :X  d   eUS:X  a.  U(       a&  U[        [         R                  " U5      5      :X  d   eggg)z?Check integer weights give the same result as repeating values.r#   r   r)   r&   r   r   N)
r   r-   r.   r/   choicerepeatr   r   r   r   )	r1   r"   r   r2   xweights
x_repeatedpercentile_weightspercentile_repeateds	            r   3test_weighted_percentile_frequency_weight_semanticsrW      s     ))

 2
3CBR Ajjj$G1&J-	O /BLL,o (;!<<<<"!VBIIj,A%BBBB ")r!   constant   c                     [         R                  R                  U 5      nUR                  SSS9nUR	                  SSS9nXc-  n[        XVXS9n[        XWXS9n	U[        U	5      :X  d   eg)zCheck multiplying weights by a constant does not change the result.

Note scale invariance does not always hold when multiplying by a
float due to cumulative sum numerical error (which grows proportional to n).
r#   r)   r&   r   N)r   r-   r.   r/   rP   r   r   )
r1   r"   r   rX   r2   rR   rS   weights_multipliedr0   percentile_multipliers
             r   ,test_weighted_percentile_constant_multiplierr]      sx     ))

 2
3CBR Ajjj$G +%a/SJ0	  56666r!   )r#   r$   r   c                    [         R                  R                  U 5      nUR                  SSS9nUR	                  SSS9nUR                  SSS9n[         R
                  " XF45      R                  n[        XuXS9n[        U[        5      (       a  / n	U HK  n
U	R                  [        UR                  S   5       Vs/ s H  n[        USS2U4   XZUS9PM     sn5        MM     [         R                  " U	SS	9nUR                  UR                  S   [        U5      4:X  d   eOX[        UR                  S   5       Vs/ s H  n[        USS2U4   XQUS9PM     nnUR                  UR                  S   4:X  d   e[        X5        UR	                  SSS9n[         R
                  " X]45      R                  n[        X~XS9n[        U[        5      (       a  / n	U HR  n
U	R                  [        UR                  S   5       Vs/ s H  n[        USS2U4   USS2U4   XS9PM     sn5        MT     [         R                  " U	SS	9nUR                  UR                  S   [        U5      4:X  d   eO_[        UR                  S   5       Vs/ s H  n[        USS2U4   USS2U4   XS9PM     nnUR                  UR                  S   4:X  d   e[        X5        gs  snf s  snf s  snf s  snf )
zECheck `_weighted_percentile` behaviour is correct when `array` is 2D.r   r)   r&   r#   )r"   r   r7   N)axis)r   r-   r.   r/   rP   rK   rL   r   
isinstancelistappendr:   shapestacklenr   )r1   r"   r   r2   x1w1x2x_2dwpp_listprip_axis_0w2w_2ds                  r   test_weighted_percentile_2drr      s   
 ))

 2
3C	Rb	!B	AB	B	Rb	!B99bX  D	/
B /4((!BMM
 #4::a=1	 2 )QT
BG 2	 " 88F,xxDJJqM3+?@@@@ 4::a=)	
 * !QT
B *	 	 
 xxDJJqM++++B! 
AB	B99bX  D	O
B /4((!BMM
 #4::a=1	 2 )QT
DAJ 2	 " 88F,xxDJJqM3+?@@@@ 4::a=)	
 * !QT
DAJ *	 	 
 xxDJJqM++++B!i
,
s   <K
=K #K%

#K*z#array_namespace, device, dtype_namezdata, weights, percentile*   r7   c                 $    U R                  S5      $ Nr   randr2   s    r   <lambda>ry   	  s    SXXb\r!   c                 &    U R                  SS5      $ )Nr   r8   rv   rx   s    r   ry   ry     s    SXXb!_r!   c                 ^    U R                  S5      R                  [        R                  5      $ ru   rw   astyper   float32rx   s    r   ry   ry     s    #((2,2E2Ebjj2Qr!   K   c                 &    U R                  SS5      $ Nr#   r8   rv   rx   s    r   ry   ry     s    Qr!   c                 `    U R                  SS5      R                  [        R                  5      $ r   r|   rx   s    r   ry   ry     s    Q..rzz:r!      )r   r7   r   r8   rI   r&   )r   r   r7   r7   r7   r   r8   rI   )r   r7   r7   r7   r7   r   r?   c                 (   [        X5      nUR                  SUS9nUR                  SUS9n	US:X  aA  UR                  UR	                  X5      U:H  5      (       a  [
        R                  " SU 35        [        R                  R                  U 5      n
[        U5      (       a  U" U
5      OUn[        U5      (       a  U" U
5      OUnUR                  U5      n[        XU5      nUR                  XS9nUR                  XS9n[        SS9   [        XU5      n[        U5      [        U5      :X  d   e[!        U5      S   [!        U5      S   :X  d   e[#        UUS9nSSS5        WR$                  UR$                  :X  d   eUR&                  UR&                  :X  d   e[)        UU5        US	:X  a5  UR$                  UR$                  s=:X  a  [        R*                  :X  d   e   egUR$                  [        R,                  :X  d   eg! , (       d  f       N= f)
zECheck `_weighted_percentile` gives consistent results with array API.r7   r
   r   zxp.nextafter is broken on T)array_api_dispatch)xpNr~   )r   rA   rC   all	nextafterpytestxfailr   r-   r.   callabler}   r   asarrayr   array_devicer   r   r@   rd   r   r~   rB   )r1   array_namespacer   
dtype_namedatarS   r0   r   zerooner2   X_np
weights_np	result_npX_xp
weights_xp	result_xpresult_xp_nps                     r   .test_weighted_percentile_array_api_consistencyr      s   D 
o	6B
 88Af8%D
''!F'
#CQ266",,t"9T"ABB1&:;
))

 2
3C 49DD!)'!2!2J;;z"D$TzBI::d:*DJ6J	4	0(:F	I&,t*<<<<Y'*mD.A!.DDDD(r:	 
1 000000I|, Y!!Y__B

BBBBB!!RZZ/// 
1	0s   
AH
Hsample_weight_ndimr   c                    [         R                  R                  U 5      nUR                  SS5      n[         R                  XCR                  " UR
                  6 S:  '   [         R                  " U5      nUS:X  a  UR                  SSSS9nOUR                  SSS	S9n[        XFS
US9n[        UR
                  S   5       Vs/ s H  nXESS2U4   ) U4   PM     n	nUR                  S:X  aM  [         R                  " XdR
                  S   5      R                  UR
                  S   UR
                  S   5      n[        UR
                  S   5       Vs/ s H  oUSS2U4   ) U4   PM     n
n[         R                  " [        UR
                  S   5       Vs/ s H  n[        X   X   S
US9PM     sn5      n[        X5        gs  snf s  snf s  snf )a*  Test `_weighted_percentile` ignores NaNs.

Calling `_weighted_percentile` on an array with nan values returns the same
results as calling `_weighted_percentile` on a filtered version of the data.
We test both with sample_weight of the same shape as the data and with
one-dimensional sample_weight.
r5   r         ?r   r7   rJ   )r5   r   r)   )r5      r   Nr   )r   r-   r.   rw   nanrd   isnanr/   r   r:   ndimrQ   reshaper9   r   )r1   r   r   r2   array_with_nansnan_maskr   resultscolfiltered_arrayfiltered_weightsexpected_resultss               r   %test_weighted_percentile_nan_filteredr   H  s    ))

 2
3ChhsB'O>@ffOHHo334s:;xx(HQAqy9Aqv6 #?2wWG
 ..q122C 	!S&))3./2   Q		-1F1Fq1IJRR!!!$o&;&;A&>
 :??T?TUV?W9X9X#x3'',-9X   xx
 _22156		
 7 !#%5%:B 7		
 '1+
	
s   GG	Gzpercentile_rank, expectedZ   g       @g      @c           	         [         R                  " [         R                  S/[         R                  S/[         R                  [         R                  /[         R                  [         R                  /[         R                  S/[         R                  [         R                  //5      n[         R                  " U5      n[	        X#U 5      n[         R
                  " XASS9(       d   eg)zCCheck that nans are ignored in general, except for all NaN columns.r&   r7   r   T)	equal_nanN)r   r9   r   r   r   array_equal)r"   expectedr9   rS   valuess        r   'test_weighted_percentile_all_nan_columnr   {  s     HHVVQKVVQKVVRVVVVRVVVVQKVVRVV	
	E ll5!G!%/BF
 >>&d;;;r!   z2.0z2np.quantile only accepts weights since version 2.0)reasonr0   )B   r   r   uniform_weightc                    U(       a  U(       d  [         R                  " S5        [        R                  R	                  U5      nUR                  SS5      nU(       a)  [        R                  " U5      UR                  SSSS9-  nOUR                  SSSS9n[        XVXS9n[        R                  " UU S-  U(       d  UOS	U(       a  S
OSSS9n[        Xx5        g	)zICheck `_weighted_percentile` is equivalent to `np.quantile` with weights.zHnp.quantile does not support weights with method='averaged_inverted_cdf'r   r5   r7   rJ   r)   r   r5   r   Nr*   r+   r   rS   r,   r`   )r   skipr   r-   r.   rw   r   r/   r   quantiler   )	r0   r   r   r1   r2   r9   r   percentile_weighted_percentilepercentile_numpy_quantiles	            r   ,test_weighted_percentile_like_numpy_quantiler     s     ~V	
 ))

 2
3CHHREU+ckk!QQk.GGAqy9%9j&" !#S%3*1&~! 5Qr!   z5np.nanquantile only accepts weights since version 2.0c                    U(       a  U(       d  [         R                  " S5        [        R                  R	                  U5      nUR                  SS5      n[        R                  XTR
                  " UR                  6 S:  '   U(       a)  [        R                  " U5      UR                  SSSS9-  nOUR                  SSSS9n[        XVXS	9n[        R                  " UU S-  U(       d  UOS
U(       a  SOSSS9n[        Xx5        g
)zICheck `_weighted_percentile` equivalent to `np.nanquantile` with weights.zKnp.nanquantile does not support weights with method='averaged_inverted_cdf'r   r5   r   r7   rJ   r)   r   r   Nr*   r+   r   r   )r   r   r   r-   r.   rw   r   rd   r   r/   r   nanquantiler   )	r0   r   r   r1   r2   r   r   r   percentile_numpy_nanquantiles	            r   /test_weighted_percentile_like_numpy_nanquantiler     s     ~-	

 ))

 2
3Chhr3'O>@ffOHHo334s:;_5 9D 9
 
 Aqy9%9
&" $&>>S%3*1&~$  5Tr!   )-numpyr   r   numpy.testingr   r   r   sklearn._configr   sklearn.utils._array_apir   r   r	   r   r   sklearn.utils.estimator_checksr   sklearn.utils.fixesr   r   sklearn.utils.statsr   markparametrizer    r4   r<   rD   rG   rN   rW   r]   rr   r~   int32rC   r}   r9   r   r   r   r   skipifr   r    r!   r   <module>r      sJ     =  * 
 < ? 9 4 T5M2"b*- + 3-( T5M2*RR!R,AB"b*M + C 3M8 *RI6) 7),  T5M2:<WX4 Y 34* T5M2*,<=C > 3C* aV,T5M2*RR=M,NO7 P 3 -7( *R,>?T5M2F" 3 @F"R )+T+V  
B!b)	!2772;#5#5bhh#?D	$&QSUV (:H	
 
$	%rxx0B'CQG	2662661aA.	/:L1MqQ HH'(HH'rxx8H	
%6'07<'0T T5M2-1v6.2 7 3.2b 	bffa[
bRVVRVV$sCj12<<, u%%?   |4UDM2)E4=9R : 3 5	R> u%%B   |4UDM2)E4=9"U : 3 5	"Ur!   