o
    tBhF                     @   sv  d dl Zd dlZd dlmZmZ d dlmZmZ d dl	m
Z
 ddlmZ d dlmZ dd	 Zejd
g dejdg ddd Zejd
g ddd Zejd
g dejdg dejdddgdd Zejd
g ddd ZddddZejde dd Zdd d dZejde d!d" Zd#d$d%Zejde d&d' Zejd
d(d)gejdd d*gd+d, Zejd-ejd
g dejdd d*gd.d/ Zejd
g dd0d1 Z ejd
g dd2d3 Z!d4d5 Z"ejd6d7d8gd9d: Z#ejd;g d<ejdg dd=d> Z$d?d@ Z%ejdg ddAdB Z&dS )C    N)	bootstrap&BootstrapDegenerateDistributionWarning)assert_allcloseassert_equal)stats   )
_bootstrap)rng_integersc                  C   s  d} t jt| d tdtj W d    n1 sw   Y  d} t jt| d tt tj W d    n1 s:w   Y  d} t jt| d tg ddgftj W d    n1 s^w   Y  d} t jt| d tg dg dftjd	d
 W d    n1 sw   Y  d} t jt| d tdtjdd W d    n1 sw   Y  d} t jt| d tg dftjdd W d    n1 sw   Y  d} t jt| d tg dftjdd W d    n1 sw   Y  d} t jt| d tg dftjdd W d    n	1 sw   Y  d} t jt| d tg dftjdd W d    n	1 s8w   Y  d} t jt| d tg dftjdd W d    n	1 s]w   Y  d} t jt| d tg dftjdd W d    n	1 sw   Y  d} t jt| d tg dftjdd W d    n	1 sw   Y  d} dd }t jt| d tg dg df|d d W d    n	1 sw   Y  d!} t jt| d tg dftjd"d# W d    d S 1 sw   Y  d S )$Nz%`data` must be a sequence of samples.)match   z(`data` must contain at least one sample.z>each sample in `data` must contain two or more observations...)r   r      z=When `paired is True`, all samples must have the same length )r   r   r      T)pairedz'`vectorized` must be `True` or `False`.ekki)
vectorizedz`axis` must be an integer.g      ?axisz!could not convert string to floatni)confidence_levelz)`n_resamples` must be a positive integer.i)n_resamplesg     D@z+`batch` must be a positive integer or None.batchz`method` must be inmethodz;`method = 'BCa' is only available for one-sample statisticsc                 S   s    t | |}t ||}|| S Nnpmean)xyr   mean1mean2 r"   w/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/scipy/stats/tests/test_bootstrap.py	statistic>   s   z$test_bootstrap_iv.<locals>.statistic)皙?g?g333333?BCaz"'herring' cannot be used to seed aherringrandom_state)pytestraises
ValueErrorr   r   r   tuple)messager$   r"   r"   r#   test_bootstrap_iv
   sr   $r/   r   )basic
percentiler&   r   )r   r   r   c              	   C   s   t jd t jddd}t|ft jd | d|dd}t|ft jd| d|dd}t|jj|jj t|jj	|jj	 t|j
|j
 d S )Nr   
         d   )r   r   r)   r   r   )r   randomseedrandr   r   r   confidence_intervallowhighstandard_error)r   r   r   res1res2r"   r"   r#   test_bootstrap_batchK   s   r?   c                    s   t jd d}t j|t j|ddd d fdd	}t t}t|f|dd}tf d	dd
}t|j|j t|j	|j	 d S )Nr   r5   c                 S   s   | | d j |dS )Nr   r   r   )r   r   r   r"   r"   r#   my_statisticd   s   z+test_bootstrap_paired.<locals>.my_statisticc                    s   |  }|  } ||}|S r   r"   )ir   abresrB   r   r   r"   r#   my_paired_statisticg   s   
z2test_bootstrap_paired.<locals>.my_paired_statisticr(   T)r   r)   r@   )
r   r6   r7   r8   arangelenr   r   r9   r<   )r   nrH   rC   r=   r>   r"   rG   r#   test_bootstrap_paired\   s   
rM   r   TFc              	   C   sR  |s| dkrt jdd tjd ddd}d}|| }tj|}tj|}tj|}t|||f||| ddd	d
}	g d}
||
|< t||
|}t||
|}t||
|}t|||f||| d|d	d
}t	|j
j|	j
j t	|j
j|	j
j t	|j|	j t|}|| t|j
jj| t|j
jj| t|jj| d S )Nr&   z*BCa currently for 1-sample statistics only)reasonr   r@   c                 S   s$   | j |d|j |d |j |d S Nr   rA   )r   r   zr   r"   r"   r#   rB      s   $z/test_bootstrap_vectorized.<locals>.my_statistic)r2   r3   r4   r5   )r   r   r)   r   r   )r   r   r   rI   )r*   xfailr   r6   r7   r8   r   broadcast_toreshaper   r9   r:   r;   r<   listpopr   shape)r   r   r   rB   rV   	n_samplesr   r   rP   r=   rS   r>   result_shaper"   r"   r#   test_bootstrap_vectorizedv   s@   

rY   c                 C   s   t jjddddd}d}t jt|d t|t |d}|j|d	}|	 }t
|ftjd|| dd
}t|j|dd t|j|dd d S )N   r   i  r   )locscalesizer)   gffffff?r   )dfr[   r\   )alpha)r   r   r   r)   gMb@?rtolga2U0*3?)atol)r   normrvstrK   r   r   semintervalstdr   r   r9   r<   )r   datar_   distexpected_intervalexpected_serF   r"   r"   r#   test_bootstrap_against_theory   s   $rm   )gQ7@gHzS@)g\(<@g=
ףpU@)gHz'@@gQV@zmethod, expectedc                 C   s8   t g d}t|ft jd| dd}t|j|dd d S )N)r2   r4         )@rn   g+@            "   2   Q   Y   y         i@B r   )r   r   r)   g{Gzt?r`   )r   arrayr   r   r   r9   )r   expectedr   rF   r"   r"   r#   test_bootstrap_against_R   s
   r|   i  i  c              	   C   s   t jd d}d}d}tjddd}| }d}|j||fd	}t|ft j||d
| dd}	|	j}
t 	|
d |k ||
d k @ }||ksFJ t
|||j}|dksTJ d S )Nr   r5     ?rZ   r   r[   r\   i  r]   rt   r@   r$   r   r   r   r   r   r%   r   r6   r7   r   rc   r   rd   r   r9   sum	binomtestpvalue)r   r{   rL   r   r   rj   	stat_truen_replicationsri   rF   cici_contains_truer   r"   r"   r#   #test_bootstrap_against_itself_1samp   s2   r   i|  iz  )r0   r1   c              	   C   s   t jd d}d}d}d}ddd}tjdd	d
}tjdd	d
}| |  }	d}
|j|
|fd}|j|
|fd}t||f|||d| dd}|j}t 	|d |	k |	|d	 k @ }||ks`J t
||
|j}|dksnJ d S )Nr   r5   x   r}   r~   r@   c                 S   s$   t j| |d}t j||d}|| S rO   r   )data1data2r   r    r!   r"   r"   r#   my_stat  s   z4test_bootstrap_against_itself_2samp.<locals>.my_statr   r   r%   i  r   rt   r   rI   r   )r   r{   n1n2r   r   r   dist1dist2r   r   r   r   rF   r   r   r   r"   r"   r#   #test_bootstrap_against_itself_2samp  s:   
r   r0   r1   r   c              	      s   dddd  fdd}t jd t jdd}t jdd}t jdd}t|||f d	|d
| dd}t|||f|d|d
| dd}t|j|j t|j|j d S )Nr   r   c                       t  fdd|D S )Nc                 3       | ]}|  V  qd S r   rA   .0sampler   r"   r#   	<genexpr>7      zEtest_bootstrap_vectorized_3samp.<locals>.statistic.<locals>.<genexpr>r   r   ri   r"   r   r#   r$   5     z2test_bootstrap_vectorized_3samp.<locals>.statisticc                     &   | D ]	}|j dksJ q | ddiS Nr   r   r   ndimri   r   r$   r"   r#   statistic_1d9     z5test_bootstrap_vectorized_3samp.<locals>.statistic_1dr   rZ   Tr5   )r   r   r   r   r)   Fr   r6   r7   r8   r   r   r9   r<   )r   r   r   r   r   rP   r=   r>   r"   r   r#   test_bootstrap_vectorized_3samp2  s   r   z'Failure is not concerning; see gh-14107c              
      s   ddd  fdd}t jd t jdd}t|f d|d	d | dd
}t|f|d|d	d| dd
}t|j|j t|j|j d S )Nr   c                 S   s   | j |dS rO   rA   )r   r   r"   r"   r#   r$   O  s   z2test_bootstrap_vectorized_1samp.<locals>.statisticc                    s   | j dksJ  | ddS )Nr   r   r   r   )r   r   r"   r#   r   S  s   z5test_bootstrap_vectorized_1samp.<locals>.statistic_1dr   rZ   Tr5   )r   r   r   r   r   r)   Fr2   )r   r   )r   r   r   r   r=   r>   r"   r   r#   test_bootstrap_vectorized_1sampK  s   
r   c              	   C   s   ddg }| dkrHt jdd0 tt t|gt j| d}t|jt j	t j	f W d    n1 s3w   Y  W d    n1 sBw   Y  nt|gt j| d}t|jd t|j
d d S )	N#        @r&   ignore)invalidr   )r   r   r   )r   errstater*   warnsr   r   r   r   r9   nanr<   )r   ri   rF   r"   r"   r#   test_bootstrap_degenerated  s   
r   c                 C   s   t jd}tjddd}|jd|d}|f}t|tj| dt jdd}t|tj| dt jdd	d
}t|j	|j	 t|j
|j
 t|j
t jsKJ d S )Nl   E<1

 r   r   r   r5   )r]   r)   i[%  )r   r   r)   F)r   r   r)   r   )r   r6   default_rngr   rc   rd   r   skewr   r9   r<   
isinstancefloat64)r   rngrj   ri   rF   refr"   r"   r#   test_bootstrap_gh15678r  s   
r   c                  C   s   d} t jd t jj|  }tt|}t| d D ]}|d|d d f }t j||dd}t 	||s6J qt j
ttj|dddd}t 	||sMJ d S )	Nr   r   rZ      r   r@   .r   r   r   )r   r6   r7   r8   nextr   _jackknife_resamplerangedeletearray_equalconcatenaterT   )rV   r   r   rC   slcr{   y2r"   r"   r#   test_jackknife_resample  s   r   rng_nameRandomStater   c                 C   s   t tj| d }|d u rt|  d |d}|d}d}d}tjd tjj| }tj|||d}t	|D ]$}|d|d d f }	t
|d|d |d }
|d|
f }t|	|s[J q7d S )Nz not available.r   r2   r   r(   .r@   )getattrr   r6   r*   skipr7   r8   r   _bootstrap_resampler   r	   r   )r   r   rng1rng2r   rV   r   r   rC   r   jsr{   r"   r"   r#   test_bootstrap_resample  s    r   score)r   g      ?r   c                 C   sV   d}t jd t jj| }tj|| dd}dd }||| ddd }t||d d S )	N)r2         r   r@   r   c                 S   s   t tj|| |S r   )r   apply_along_axisr   percentileofscore)rD   r   r   r"   r"   r#   vectorized_pos  s   z0test_percentile_of_score.<locals>.vectorized_posr5   V瞯<)r   r6   r7   r8   r   _percentile_of_scorer   )r   r   rV   r   pr   p2r"   r"   r#   test_percentile_of_score  s   r   c                  C   s   d} t jd t jj|  }t jj| d d  d }t||}t| d D ]}|| }t j|| || dd}t||d q&d S )N)r2   r   r   r@   r5   r   r   )	r   r6   r7   r8   r   _percentile_along_axisr   r1   r   )rV   r   qr   rC   rF   r{   r"   r"   r#   test_percentile_along_axis  s   r   c                    s   dd   fdd}t |}tjd tjddd}tjdd	d}tjd	dd} |||| d
}||||| d
}t|| d S )Nc                    r   )Nc                 3   r   r   rA   r   r   r"   r#   r     r   z>test_vectorize_statistic.<locals>.statistic.<locals>.<genexpr>r   r   r"   r   r#   r$     r   z+test_vectorize_statistic.<locals>.statisticc                     r   r   r   r   r   r"   r#   r     r   z.test_vectorize_statistic.<locals>.statistic_1dr   r   rZ   r   r   r   )r   _vectorize_statisticr   r6   r7   r8   r   )r   r   
statistic2r   r   rP   r=   r>   r"   r   r#   test_vectorize_statistic  s   
r   )'numpyr   r*   scipy.statsr   r   numpy.testingr   r   scipyr    r   scipy._lib._utilr	   r/   markparametrizer?   rM   rY   rm   tests_Ritemsr|   tests_against_itself_1sampr   tests_against_itself_2sampr   r   xfail_on_32bitr   r   r   r   r   r   r   r   r"   r"   r"   r#   <module>   sx    A
,


%
.



