B
    0d@                 @   sR  d dl Zd dlZd dl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
dddgejdd ddgdd Zejd
dddgdd Zejd
dddgejdd ddgejdddgdd Zejd
dddg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
dddgejdd dgd.d/ Zd0d1 Zejd2d3d4gd5d6 Z ejd7d d8dgejdd ddgd9d: Z!d;d< Z"ejdd ddgd=d> Z#dS )?    N)	bootstrap)assert_allcloseassert_equal)stats   )
_bootstrap)rng_integersc           	   C   s  d} t jt| d tdtj W d Q R X d} t jt| d tt tj W d Q R X d} t jt| d tdddgdgftj W d Q R X d} t jt| d( tdddgdddd	gftjd
d W d Q R X d} t jt| d tdtjdd W d Q R X d} t jt| d tdddgftjdd W d Q R X d} t jt| d tdddgftjdd W d Q R X d} t jt| d tdddgftjdd W d Q R X d} t jt| d tdddgftjdd W d Q R X d} t jt| d tdddgftjdd W d Q R X d} t jt| d tdddgftjdd W d Q R X d} t jt| d tdddgftjdd W d Q R X d} dd }t jt| d$ td d!d"gd d!d"gf|d#d W d Q R X d$} t jt| d tdddgftjd%d& W d Q R X 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      z=When `paired is True`, all samples must have the same length    T)pairedz'`vectorized` must be `True` or `False`.Z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   R/var/www/html/venv/lib/python3.7/site-packages/scipy/stats/tests/test_bootstrap.py	statistic>   s    z$test_bootstrap_iv.<locals>.statisticg?g?g333333?BCaz"'herring' cannot be used to seed aZherring)random_state)pytestZraises
ValueErrorr   r   r   tuple)messager   r   r   r   test_bootstrap_iv
   sV    ","""""""(r$   r   basic
percentiler   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    r4   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   r*   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 )Nr   )ir   abres)r6   r   r   r   r   my_paired_statisticg   s    
z2test_bootstrap_paired.<locals>.my_paired_statistic)r   T)r   r   )r5   )r5   )
r   r+   r,   r-   Zarangelenr   r   r.   r1   )r   nr;   r7   r2   r3   r   )r6   r   r   r   test_bootstrap_paired\   s    
r>   r   TFc          	   C   sT  |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
}	dddg}
||
|< 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   r5   c             S   s$   | j |d|j |d |j |d S )N)r   )r   )r   r   zr   r   r   r   r6      s    z/test_bootstrap_vectorized.<locals>.my_statistic)r'   r(   r)   r*   )r   r   r   r   r   r
   )r5   )r    Zxfailr   r+   r,   r-   r   Zbroadcast_toreshaper   r.   r/   r0   r1   listpopr   shape)r   r   r   r6   rD   Z	n_samplesr   r   r@   r2   rA   r3   Zresult_shaper   r   r   test_bootstrap_vectorizedv   s8    




rE   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
   )ZdfrG   rH   )alpha)r   r   r   r   gMb@?)rtolga2U0*3?)Zatol)r   normrvstr<   r   r   ZsemintervalZstdr   r   r.   r1   )r   datarJ   distZexpected_intervalZexpected_ser:   r   r   r   test_bootstrap_against_theory   s    $rR   )gQ7@gHzS@)g\(<@g=
ףpU@)gHz'@@gQV@)r%   r&   r   zmethod, expectedc             C   sT   t dddddddddd	d
dddddg}t|ft jd| dd}t|j|dd d S )Nr'   r)   g      )@g+@            "   2   Q   Y   y         i@B r   )r   r   r   g{Gzt?)rK   )r   arrayr   r   r   r.   )r   expectedr   r:   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st
t|||j}|dkst
d S )Nr   r*   i  g?rF   r
   )rG   rH   i  )rI   rX   r5   )r   r   r   r   r   r   g?)r   r+   r,   r   rL   r   rM   r   r.   sumAssertionError	binomtestpvalue)r   r_   r=   r   r   rQ   	stat_truen_replicationsrP   r:   cici_contains_truerd   r   r   r   #test_bootstrap_against_itself_1samp   s*    ri   i|  iz  )r%   r&   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st
t||
|j}|dkst
d S )Nr   r*   x   i  g?r5   c             S   s$   t j| |d}t j||d}|| S )N)r   )r   r   )data1data2r   r   r   r   r   r   my_stat  s    z4test_bootstrap_against_itself_2samp.<locals>.my_statr
   )rG   rH   g?i  )rI   rX   )r   r   r   r   r   r   )r5   )r   r+   r,   r   rL   r   rM   r   r.   ra   rb   rc   rd   )r   r_   Zn1Zn2r   r   rm   Zdist1Zdist2re   rf   rk   rl   r:   rg   rh   rd   r   r   r   #test_bootstrap_against_itself_2samp  s2    
rn   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                s   t  fdd|D S )Nc             3   s   | ]}|  V  qd S )N)r   ).0sample)r   r   r   	<genexpr>7  s    zEtest_bootstrap_vectorized_3samp.<locals>.statistic.<locals>.<genexpr>)ra   )r   rP   r   )r   r   r   5  s    z2test_bootstrap_vectorized_3samp.<locals>.statisticc                 s*   x| D ]}|j dkstqW  | ddiS )Nr
   r   r   )ndimrb   )rP   rp   )r   r   r   statistic_1d9  s    
z5test_bootstrap_vectorized_3samp.<locals>.statistic_1dr   rF   Tr*   )r   r   r   r   r   F)r   r+   r,   r-   r   r   r.   r1   )r   r   rs   r   r   r@   r2   r3   r   )r   r   test_bootstrap_vectorized_3samp2  s    rt   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 )N)r   )r   )r   r   r   r   r   r   O  s    z2test_bootstrap_vectorized_1samp.<locals>.statisticc                s   | j dkst | ddS )Nr
   r   )r   )rr   rb   )r   )r   r   r   rs   S  s    z5test_bootstrap_vectorized_1samp.<locals>.statistic_1dr   rF   Tr*   )r   r   r   r   r   r   Fr'   )r   )r   r+   r,   r-   r   r   r.   r1   )r   r   rs   r   r2   r3   r   )r   r   test_bootstrap_vectorized_1sampK  s    
ru   c              C   s   d} t jd t jj|  }tt|}xFt| d D ]6}|d|d d f }t j||dd}t 	||s8t
q8W t jttj|dddd}t 	||st
d S )	N)r   r   rF      r   r5   .)r   r   )r   )r   r+   r,   r-   nextr   Z_jackknife_resamplerangedeletearray_equalrb   ZconcatenaterB   )rD   r   r   r7   slcr_   y2r   r   r   test_jackknife_resampled  s    r~   rng_nameZRandomStateZdefault_rngc             C   s   t tj| d }|d kr&t|  d |d}|d}d}d}tjd tjj| }tj|||d}xTt	|D ]H}|d|d d f }	t
|d|d |d }
|d|
f }t|	|sptqpW d S )Nz not available.r   r'   )r   r   rF   rv   )r   .r5   )getattrr   r+   r    skipr,   r-   r   Z_bootstrap_resamplery   r   r{   rb   )r   rngZrng1Zrng2r   rD   r   r   r7   r|   jsr_   r   r   r   test_bootstrap_resamplew  s    r   scoreg      ?c             C   sV   d}t jd t jj| }tj|| dd}dd }||| ddd }t||d d S )	N)r'         r   r5   )r   c             S   s   t tj|| |S )N)r   Zapply_along_axisr   Zpercentileofscore)r8   r   r   r   r   r   vectorized_pos  s    z0test_percentile_of_score.<locals>.vectorized_posr*   gV瞯<)r   r+   r,   r-   r   Z_percentile_of_scorer   )r   r   rD   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||}x@t| d D ]0}|| }t j|| || dd}t||d qNW d S )N)r'   r   r   r5   r*   )r   gV瞯<)	r   r+   r,   r-   r   Z_percentile_along_axisry   r&   r   )rD   r   qr   r7   r:   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                s   t  fdd|D S )Nc             3   s   | ]}|  V  qd S )N)r   )ro   rp   )r   r   r   rq     s    z>test_vectorize_statistic.<locals>.statistic.<locals>.<genexpr>)ra   )r   rP   r   )r   r   r     s    z+test_vectorize_statistic.<locals>.statisticc                 s*   x| D ]}|j dkstqW  | ddiS )Nr
   r   r   )rr   rb   )rP   rp   )r   r   r   rs     s    
z.test_vectorize_statistic.<locals>.statistic_1dr   r   rF   rv   r
   )r   )r   Z_vectorize_statisticr   r+   r,   r-   r   )r   rs   Z
statistic2r   r   r@   r2   r3   r   )r   r   test_vectorize_statistic  s    
r   )$numpyr   r    Zscipy.statsr   Znumpy.testingr   r   Zscipyr    r   Zscipy._lib._utilr   r$   markZparametrizer4   r>   rE   rR   Ztests_Ritemsr`   Ztests_against_itself_1sampri   Ztests_against_itself_2samprn   rt   Zxfail_on_32bitru   r~   r   r   r   r   r   r   r   r   <module>   sL   A-&/
