
    /iiG                     
   d dl Zd dlZd dlmZ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 d dlmZ d dlmZmZ d d	lmZ ej                            d
ddg          ej                            dddg          d                         Zej                            d
ddg          ej                            ddddddgg          ej                            dddg          d                                     Zej                            dddg          d             Zd Zd Zej                            d
ddg          ej                            dg d          d                         Zej                            d
ddg          ej                            dg d           d!                         Zej                            d"dd#g          ej                            d
ddg          ej                            dddddg d g          d$                                     Zej                            ddg d%g          ej                            d
ddg          d&                         Z ej                            d' e                      ej                            d( ej!        d)           ej"        d*          dfd+  ej#        d          $                    ej"                  dfd, d- d.fd/ d0 d1d.gf ej%        g d2           ej%        g d3          d f ej%        ej&        ej&        d d4d5dg           ej%        g d6          d f ej%        g d2           ej%        g d6ej"        7          d1d.gfg          d8                         Z'ej                            d
ddg          ej                            d9d*d:g          d;                         Z(ej                            d<d=ej&        dgfdd=gej&        ej&        gd>d?ggfg          d@             Z)ej        *                    e edA          k     dBC          ej                            dDg dE          ej                            d
ddg          ej                            dFddg          dG                                                 Z+ej        *                    e edA          k     dHC          ej                            dDg dE          ej                            d
ddg          ej                            dFddg          dI                                                 Z,dS )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                    t          j        |           }t          j        |          }t          ||d|          }| dz  dk    r |du r|t          j        |          k    sJ dS t          |          t          j        |          k    sJ dS )au  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        d/var/www/html/bet.cuttalo.com/ml/venv/lib/python3.11/site-packages/sklearn/utils/tests/test_stats.py'test_weighted_percentile_matches_medianr!      s     		$ALOOM M2wGGGE ax1}}E))	!$$$$$$e}}	!,,,,,,    percentile_rank   #   =      /   c                 "   t           j                            |           }|                    d|          }t          j        |          }t          ||||          }|rd}nd}t          |          t          j        |||          k    sJ dS )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_numpyr5   (   s    " )

 2
3
3CBT""A	aB BIIIE  (%==BM!_VLLLLLLLLLr"   r   d   c                     t          j        ddgddgg          }t          j        ddgddgg          }t          ||| d          }t          d          D ]}||         t	          d	          k    sJ d
S )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   r4   r   idxs        r    *test_weighted_percentile_plus_one_clip_maxr=   G   s      	1a&1a&!""A	C:1v&	'	'B BFFFEQxx ) )SzVC[[((((() )r"   c                      t          j        dt           j                  } t          j        dt           j                  }t	          | |d          }t          |          dk    sJ dS )zJCheck `weighted_percentile` with unit weights and all 0 values in `array`.f   dtyper   r   N)r   zerosfloat64onesr   r   )r   r4   r   s      r    test_weighted_percentile_equalrE   ^   s\    
BJ'''A	BJ	'	'	'B B++E%==Ar"   c                      t          j        d          } t          j        d          }t          | |d          }t	          |          dk    sJ dS )zKCheck `weighted_percentile` with all weights equal to 0 returns last index.r   r   g      "@N)r   r   rB   r   r   )r   r4   values      r    )test_weighted_percentile_all_zero_weightsrH   h   sJ    
	"A	"B B++E%==Cr"   zpercentile_rank, expected_value))r   r   )r   r9   )r6   r'   c                 B   t          j        g d          }t          j        g d          }t          t          j        ||f          j        t          j        ||f          j        ||           }t          d          D ]}t          ||                   |k    sJ dS )a  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   r8   r   r9      r'      )r   r   r8   r8   r   r8   r   r   r   N)r   r:   r   vstackTr;   r   )r   r#   expected_valuer   r4   rG   r<   s          r    ,test_weighted_percentile_ignores_zero_weightrO   p   s     	&&&''A	'''	(	(B 
	1a&RYBx002OW  E Qxx 4 4eCj!!^333334 4r"   )r$   r%   r   r&   c                    t           j                            |           }|                    dd          }|                    dd          }t          j        ||          }t          ||||          }t          |t          j        |          ||          }|t          |          k    sJ |dk    r)|r)|t          t          j	        |                    k    sJ dS dS dS )z?Check integer weights give the same result as repeating values.r$   r   r*   r'   r   r   N)
r   r.   r/   r0   choicerepeatr   r   r   r   )	r2   r#   r   r3   xweights
x_repeatedpercentile_weightspercentile_repeateds	            r    3test_weighted_percentile_frequency_weight_semanticsrX      s     )

 2
3
3CBR  Ajjj$$G1g&&J-	7OW   /BL,,ow   (;!<!<<<<<"!VBIj,A,A%B%BBBBB BBr"   constant   c                     t           j                            |           }|                    dd          }|                    dd          }||z  }t          ||||          }t          ||||          }	|t          |	          k    sJ dS )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/   r0   rQ   r   r   )
r2   r#   r   rY   r3   rS   rT   weights_multipliedr1   percentile_multipliers
             r    ,test_weighted_percentile_constant_multiplierr^      s     )

 2
3
3CBR  Ajjj$$G 8+%a/7SSSJ0	    566666666r"   )r$   r%   r   c                 `  
 t           j                            |           }|                    dd          }|                    dd          |                    dd          }t          j        ||f          j        t                    }t          t                    rg }D ]>
|
                    
fdt          j        d                   D                        ?t          j        |d	          }|j        j        d         t                    fk    sJ nBfd
t          j        d                   D             }|j        j        d         fk    sJ t          ||           |                    dd          }	t          j        |	f          j        t                    }t          t                    rg }D ]>
|
                    
fdt          j        d                   D                        ?t          j        |d	          }|j        j        d         t                    fk    sJ nBfdt          j        d                   D             }|j        j        d         fk    sJ t          ||           dS )zECheck `_weighted_percentile` behaviour is correct when `array` is 2D.r   r*   r'   r$   r#   r   c                 J    g | ]}t          d d |f                    S Nr`   r   ).0ir   prw1x_2ds     r    
<listcomp>z/test_weighted_percentile_2d.<locals>.<listcomp>   sO         )QQQT
BG    r"   r8   )axisc                 J    g | ]}t          d d |f                    S rb   r   )rc   rd   r   r#   rf   rg   s     r    rh   z/test_weighted_percentile_2d.<locals>.<listcomp>   sO     
 
 
  !QQQT
B  
 
 
r"   c                 ^    g | ])}t          d d |f         d d |f                   *S rb   r   )rc   rd   r   re   w_2drg   s     r    rh   z/test_weighted_percentile_2d.<locals>.<listcomp>   s[         )QQQT
DAJG    r"   c                 ^    g | ])}t          d d |f         d d |f                   *S rb   r   )rc   rd   r   r#   rm   rg   s     r    rh   z/test_weighted_percentile_2d.<locals>.<listcomp>   s\     
 
 
  !QQQT
DAJQX  
 
 
r"   N)r   r.   r/   r0   rQ   rL   rM   r   
isinstancelistappendr;   shapestacklenr   )r2   r#   r   r3   x1x2wpp_listp_axis_0w2re   rf   rm   rg   s    ``       @@@@r    test_weighted_percentile_2dr{      sZ   
 )

 2
3
3C	Rb	!	!B	AB		B	Rb	!	!B9b"X D	b/7
 
 
B /4(( ,! 	 	BMM       #4:a=11	      8F,,,xDJqM3+?+?@@@@@@
 
 
 
 
 
 
 4:a=))	
 
 
 xDJqM+++++B!!! 
AB		B9b"X D	dOW
 
 
B /4(( ,! 	 	BMM       #4:a=11	      8F,,,xDJqM3+?+?@@@@@@
 
 
 
 
 
 
 4:a=))	
 
 
 xDJqM+++++B!!!!!r"   z#array_namespace, device, dtype_namezdata, weights, percentile*   r8   c                 ,    |                      d          S Nr   randr3   s    r    <lambda>r   	  s    SXXb\\ r"   c                 .    |                      dd          S )Nr   r9   r   r   s    r    r   r     s    SXXb!__ r"   c                 f    |                      d                              t          j                  S r~   r   astyper   float32r   s    r    r   r     s     #((2,,2E2Ebj2Q2Q r"   K   c                 .    |                      dd          S Nr$   r9   r   r   s    r    r   r     s    Q r"   c                 h    |                      dd                              t          j                  S r   r   r   s    r    r   r     s"    Q..rz:: r"      )r   r8   r   r9   rJ   r'   )r   r   r8   r8   r8   r   r9   rJ   )r   r8   r8   r8   r8   r   r@   c                    t          ||          }|                    d|          }|                    d|          }	|dk    rD|                    |                    ||	          |k              rt          j        d|            t          j        	                    |           }
t          |          r ||
          n|}t          |          r ||
          n|}|                    |          }t          |||          }|                    ||          }|                    ||          }t          d          5  t          |||          }t          |          t          |          k    sJ t!          |          d         t!          |          d         k    sJ t#          ||          }ddd           n# 1 swxY w Y   |j        |j        k    sJ |j        |j        k    sJ t)          ||           |d	k    r'|j        |j        cxk    rt          j        k    sn J dS |j        t          j        k    sJ dS )
zECheck `_weighted_percentile` gives consistent results with array API.r8   r
   r   zxp.nextafter is broken on T)array_api_dispatch)xpNr   )r   rB   rD   all	nextafterpytestxfailr   r.   r/   callabler   r   asarrayr   array_devicer   r   rA   rr   r   r   rC   )r2   array_namespacer   
dtype_namedatarT   r1   r   zerooner3   X_np
weights_np	result_npX_xp
weights_xp	result_xpresult_xp_nps                     r    .test_weighted_percentile_array_api_consistencyr      s   D 
ov	6	6B
 88Af8%%D
''!F'
#
#CQ266",,tS"9"9T"ABB:&::;;;
)

 2
3
3C 044999DD!)'!2!2?J;;z""D$T:zBBI::d6:**DJv66J	4	0	0	0 ; ;(z:FF	I&&,t*<*<<<<<Y''*mD.A.A!.DDDDD(r:::	; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 00000000I|,,, Y!Y_BBBB
BBBBBBBB!RZ//////s   A3GG
Gsample_weight_ndimr   c                 R  	
 t           j                            |           }|                    dd          t           j         |j        j         dk     <   t          j                  	|dk    r|                    ddd          
n|                    ddd	          
t          
d
          }	fdt          j        d                   D             
j
        dk    rJt          j        
j        d                                       j        d         j        d                   
	
fdt          j        d                   D             t          j        fdt          j        d                   D                       }t          ||           dS )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.
    r6   r         ?r   r8   rK   )r6   r   r*   )r6      r   c                 :    g | ]}d d |f          |f         S N )rc   colarray_with_nansnan_masks     r    rh   z9test_weighted_percentile_nan_filtered.<locals>.<listcomp>c  sC        	!!!S&))3./  r"   r   c                 :    g | ]}d d |f          |f         S r   r   )rc   r   r   r   s     r    rh   z9test_weighted_percentile_nan_filtered.<locals>.<listcomp>k  s@       25x3'',-  r"   c                 N    g | ]!}t          |         |         d           "S )r   r   r   )rc   r   r   filtered_arrayfiltered_weightss     r    rh   z9test_weighted_percentile_nan_filtered.<locals>.<listcomp>p  sN     	
 	
 	
  !s#%5c%:B  	
 	
 	
r"   N)r   r.   r/   r   nanrr   isnanr0   r   r;   ndimrR   reshaper:   r   )r2   r   r   r3   resultsexpected_resultsr   r   r   r   r   s     `   @@@@@r    %test_weighted_percentile_nan_filteredr   H  s    )

 2
3
3ChhsB''O>@fOHCHo34s:;x((HQAqy99Aqv66 #?M2wWWWG    .q122  N Q	-1Fq1IJJRR!!$o&;A&>
 
    9>?TUV?W9X9X   x	
 	
 	
 	
 	
 	
 _21566		
 	
 	
  '11111r"   zpercentile_rank, expectedZ   g       @g      @c           	         t          j        t           j        dgt           j        dgt           j        t           j        gt           j        t           j        gt           j        dgt           j        t           j        gg          }t          j        |          }t	          |||           }t          j        ||d          sJ dS )zCCheck that nans are ignored in general, except for all NaN columns.r'   r8   r   T)	equal_nanN)r   r:   r   r   r   array_equal)r#   expectedr:   rT   valuess        r    'test_weighted_percentile_all_nan_columnr   {  s     HVQKVQKVRVVRVVQKVRV	
	 	E l5!!G!%/BBF
 >&(d;;;;;;;;r"   z2.0z2np.quantile only accepts weights since version 2.0)reasonr1   )B   r   r   uniform_weightc                    |r|st          j        d           t          j                            |          }|                    dd          }|r.t          j        |          |                    ddd          z  }n|                    ddd          }t          ||| |          }t          j	        || dz  |s|nd	|rd
ndd          }t          ||           d	S )zICheck `_weighted_percentile` is equivalent to `np.quantile` with weights.zHnp.quantile does not support weights with method='averaged_inverted_cdf'r   r6   r8   rK   r*   r   r6   r   Nr+   r,   r   rT   r-   rj   )r   skipr   r.   r/   r   r   r0   r   quantiler   )	r1   r   r   r2   r3   r:   r   percentile_weighted_percentilepercentile_numpy_quantiles	            r    ,test_weighted_percentile_like_numpy_quantiler     s     
~ 
V	
 	
 	
 )

 2
3
3CHHRE :U++ckk!QQk.G.GGAqy99%9}j'& & &" !#S%3=*1E&&~! ! ! 57PQQQQQr"   z5np.nanquantile only accepts weights since version 2.0c                    |r|st          j        d           t          j                            |          }|                    dd          }t          j        | |j        |j         dk     <   |r.t          j        |          |	                    ddd          z  }n|	                    ddd          }t          ||| |	          }t          j        || dz  |s|nd
|rdndd          }t          ||           d
S )zICheck `_weighted_percentile` equivalent to `np.nanquantile` with weights.zKnp.nanquantile does not support weights with method='averaged_inverted_cdf'r   r6   r   r8   rK   r*   r   r   Nr+   r,   r   r   )r   r   r   r.   r/   r   r   rr   r   r0   r   nanquantiler   )	r1   r   r   r2   r3   r   r   r   percentile_numpy_nanquantiles	            r    /test_weighted_percentile_like_numpy_nanquantiler     sC     
~ 
-	
 	
 	

 )

 2
3
3Chhr3''O>@fOHCHo34s:; :_55 9D 9
 9
 
 Aqy99%9
G& & &" $&>S%3=*1E&&~$ $ $  57STTTT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!   r5   r=   rE   rH   rO   rX   r^   r{   r   int32rD   r   r:   r   r   r   r   skipifr   r   r   r"   r    <module>r      sF        = = = = = = = =       * * * * * *         
 < ; ; ; ; ; ? ? ? ? ? ? 9 9 9 9 9 9 9 9 4 4 4 4 4 4 T5M22"b**- - +* 32-( T5M22*RR!R,ABB"b**M M +* CB 32M8 *RI66) ) 76),        T5M22:<W<W<WXX4 4 YX 324* T5M22*,<,<,<==C C >= 32C* aV,,T5M22*RR=M=M=M,NOO7 7 PO 32 -,7( *R,>??T5M22F" F" 32 @?F"R )+T+T+V+V   
B!b)	!	!7272;;#5#5bh#?#?D	$	$&Q&QSUV ('::H	
 
$$$	%	%xrx0B0B0B'C'CQG	26261aA.	/	/:L:L:L1M1MqQ BH'''((BH'''rx888H	
% 6'0 '07  <'0T T5M22-1v66.2 .2 76 32.2b 	bfa[
bRVRV$sCj12 < < <, u%%%?    |||44UDM22)E4=99R R :9 32 54	 R> u%%%B    |||44UDM22)E4=99"U "U :9 32 54	 "U "U "Ur"   