o
    i*                     @   s   d dl Zd dlmZmZmZmZmZ d dlZd dlm	Z
 d dlmZ d dlmZ d dlmZmZmZmZ dd Zd	d
 ZG dd dZdd Zejdg ddd ZdS )    N)assert_equalassert_array_equalassert_array_almost_equalassert_approx_equalassert_allclose)raises)stats)xlogy)marginsexpected_freqchi2_contingencyassociationc                  C   s^  t dg} t| }tt|d |d }t|t dg t dgg} t| \}}t dgg}t dgg}t|| t|| t ddd} t| \}}t dgdgg}t g dg}t|| t|| t d	dd
d} t| \}}}t dggdggg}t dgdgdggg}t g dgg}t|| t|| t|| d S )N   r               3   )r      
   r                  B      <   \   |   )r   r   H   N   )nparrayr
   r   lenr   arangereshape)amZm0m1Z	expected0Z	expected1m2Z	expected2 r+   \/home/kim/smarthome/.venv/lib/python3.10/site-packages/scipy/stats/tests/test_contingency.pytest_margins   s2   





r-   c                  C   s   t tdgtdg tddgddggddgddggddgddggg} t| }t |t|  tg dg dg} t| }tg dg dg}t|| d S )	Nr         ?r   r   )r   r      )r/   r/   r/   )      (@r0   g      0@)      2@r1   g      8@)r   r   r"   r#   Z	ones_liker   )observedeZcorrectr+   r+   r,   test_expected_freq,   s   4r4   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zej	
dddgdd Zej	jdd Zej	jej	
dejejfdd Zdd ZdS )TestChi2Contingencyc                 C   s   t ddgddgg}t|dd\}}}}t|d t|d t|d t|| t g d}t|dd\}}}}t|d t|d t|d t|| d S )	Nr   r   F
correctiong        r.   )r   r   r   r   )r"   r#   r   r   r   selfobsZchi2pdofexpectedr+   r+   r,   test_chi2_contingency_trivial:   s   






z1TestChi2Contingency.test_chi2_contingency_trivialc                 C   s   t g dg dg dg dgg dg dg dg dgg}t|\}}}}t|d	d
d t|ddd t|d t ddgddggddgddgggddgddggddgddgggg}t|\}}}}t|ddd t|ddd t|d d S )N)r   "      )#         )r       	   )r   r   r   )r   /   rC   )r?   r      )rG         )rE   !      g{GY@   )Zsignificantg"j,5#=r      r   rC   r   r   r@         r   gjt!@gQI?)r"   r#   r   r   r   r8   r+   r+   r,   test_chi2_contingency_RM   sL   	
&	z+TestChi2Contingency.test_chi2_contingency_Rc                 C   s   t ddgddgg}t|ddd\}}}}t|dt|||    t|ddd\}}}}|t d	d
gd
d	gg }t|dt|||    t g dg dg}t|dd\}}}}t|dt|||    d S )Nr   r   Z   zlog-likelihoodF)lambda_r7   r   Tg      g      ?)r   r   r   )r   r   r   )rR   )r"   r#   r   r   r	   sum)r9   cgr;   r<   r3   Zc_corrr+   r+   r,   test_chi2_contingency_g   s    z+TestChi2Contingency.test_chi2_contingency_gc                 C   s^   t ddgddgg}ttt| t ddgddgg}ttt| t d}ttt| d S )Nr   r   r   r   )r   r   )r"   r#   assert_raises
ValueErrorr   empty)r9   r:   r+   r+   r,   test_chi2_contingency_bad_args   s   
z2TestChi2Contingency.test_chi2_contingency_bad_argsc                 C   s4   t ddgddgg}t|d }t|ddd d S )Ni%  r   r   r   r   g-q=Zrtol)r"   r#   r   r   )r9   r2   r;   r+   r+   r,   #test_chi2_contingency_yates_gh13875   s   z7TestChi2Contingency.test_chi2_contingency_yates_gh13875r7   FTc                 C   s@   t ddgddgg}t||d}t|j|j|j|jf| d S )Nr   r   r6   )r"   r#   r   r   	statisticpvaluer<   r   )r9   r7   r:   resr+   r+   r,   test_result   s   zTestChi2Contingency.test_resultc                 C   sf   t ddd}t|ddj}t|j}tjdd}t|d|d}t	|j| t
|j|dd	 d S )
Nr   r   Fr6   iP  )Zn_resamplesr7   methodgV瞯<r\   )r"   r%   r&   r   r^   r   Zfisher_exactr_   PermutationMethodr   r   )r9   tableZref_statisticZ
ref_pvaluerc   r`   r+   r+   r,   test_exact_permutation   s   z*TestChi2Contingency.test_exact_permutationrc   c                 C   s   t jd}g d}g d}tj|||d }t|d||dd}t|dd}t|j|j t	|j
|j
d	d
 t|jt j t|j|j d S )Nl   {5 )i,  i     )   i  rg   i  )seedF)rngrb   r6   g{Gzt?)Zatol)r"   randomZdefault_rngr   Zrandom_tablervsr   r   r^   r   r_   r<   nanr   )r9   rc   rj   rowscolsre   r`   refr+   r+   r,   test_resampling_randomized   s   z.TestChi2Contingency.test_resampling_randomizedc                 C   s  t dddd}t }d}tjt|d t|d|d W d    n1 s)w   Y  t ddd}t }d}tjt|d t||d	 W d    n1 sUw   Y  t	 }d
}tjt|d t|dd|d W d    n1 szw   Y  d}d}tjt|d t|d|d W d    n1 sw   Y  tj	tj
jd}d}tjt|d t|d|d W d    d S 1 sw   Y  d S )Nr   r   z7Use of `method` is only compatible with two-way tables.)matchFrb   r   z+`correction=True` is not compatible with...)rc   z%`lambda_=2` is not compatible with...)r7   rR   rc   Zherringz;`method='herring'` not recognized; if provided, `method`...)rl   z4If the `method` argument of `chi2_contingency` is...)r"   r%   r&   r   rd   pytestr   rY   r   MonteCarloMethodZnormrl   )r9   re   rc   messager+   r+   r,   test_resampling_invalid_args   s6   "z0TestChi2Contingency.test_resampling_invalid_argsN)__name__
__module____qualname__r>   rP   rV   r[   r]   rs   markparametrizera   Zslowrf   r   rd   rt   rq   rv   r+   r+   r+   r,   r5   9   s     ^

	r5   c                   C   sz   t ttddgddggd t ttddggddgggd t ttddgddggd t tttjddgd	dggtd
d d S )Nr   r   r   r   XcramerrW   r   dd)Zdtype)rX   rY   r   r"   r#   objectr+   r+   r+   r,   test_bad_association_args  s   r   zstat, expected))r}   g?)Z	tschuprowg:ǰ`ڳ?)Zpearsong!2q܍?c                 C   s6   t g dg dg dg}t|| d}t|| d S )N)r   rH   r   r   r   )rM   r   rG   rI   rC   )rE   r   r   r   rC   )r2   rc   )r"   r#   r   r   )statr=   Zobs1r'   r+   r+   r,   
test_assoc  s   
r   )numpyr"   Znumpy.testingr   r   r   r   r   rs   r   rX   Zscipyr   Zscipy.specialr	   Zscipy.stats.contingencyr
   r   r   r   r-   r4   r5   r   rz   r{   r   r+   r+   r+   r,   <module>   s      X