o
    ?Hh*                     @   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mm0m1	expected0	expected1m2	expected2 r/   b/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/stats/tests/test_contingency.pytest_margins   s2   





r1   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      )r3   r3   r3   )      (@r4   g      0@)      2@r5   g      8@)r   r   r"   r#   	ones_liker   )observedecorrectr/   r/   r0   test_expected_freq,   s   4r:   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        r2   )r   r   r   r   )r"   r#   r   r   r   selfobschi2pdofexpectedr/   r/   r0   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   /   rJ   )rF   r      )rN         )rL   !      g{GY@   )significantg"j,5#=r      r   rJ   r   r   rG         r   gjt!@gQI?)r"   r#   r   r   r   r>   r/   r/   r0   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_r=   r   Tg      g      ?)r   r   r   )r   r   r   )rZ   )r"   r#   r   r   r	   sum)r?   cgrB   rC   r8   c_corrr/   r/   r0   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)r?   r@   r/   r/   r0   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=rtol)r"   r#   r   r   )r?   r7   rB   r/   r/   r0   #test_chi2_contingency_yates_gh13875   s   z7TestChi2Contingency.test_chi2_contingency_yates_gh13875r=   FTc                 C   s@   t ddgddgg}t||d}t|j|j|j|jf| d S )Nr   r   r<   )r"   r#   r   r   	statisticpvaluerC   r   )r?   r=   r@   resr/   r/   r0   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   Fr<   iP  )n_resamplesr=   methodgV瞯<re   )r"   r%   r&   r   rh   r   fisher_exactri   PermutationMethodr   r   )r?   tableref_statistic
ref_pvaluern   rj   r/   r/   r0   test_exact_permutation   s   z*TestChi2Contingency.test_exact_permutationrn   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  ru   i  )seedF)rngrm   r<   g{Gzt?)atol)r"   randomdefault_rngr   random_tablervsr   r   rh   r   ri   rC   nanr   )r?   rn   rx   rowscolsrq   rj   refr/   r/   r0   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.)matchFrm   r   z+`correction=True` is not compatible with...)rn   z%`lambda_=2` is not compatible with...)r=   rZ   rn   herringz;`method='herring'` not recognized; if provided, `method`...)r}   z4If the `method` argument of `chi2_contingency` is...)r"   r%   r&   r   rp   pytestr   rb   r   MonteCarloMethodnormr}   )r?   rq   rn   messager/   r/   r0   test_resampling_invalid_args   s6   "z0TestChi2Contingency.test_resampling_invalid_argsN)__name__
__module____qualname__rE   rX   r_   rd   rg   r   markparametrizerk   slowrt   r   rp   r   r   r   r/   r/   r/   r0   r;   9   s     ^

	r;   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cramerr`   r   dd)dtype)ra   rb   r   r"   r#   objectr/   r/   r/   r0   test_bad_association_args  s   r   zstat, expected))r   g?)	tschuprowg:ǰ`ڳ?)pearsong!2q܍?c                 C   s6   t g dg dg dg}t|| d}t|| d S )N)r   rO   r   r   r   )rU   r   rN   rP   rJ   )rL   r   r   r   rJ   )r7   rn   )r"   r#   r   r   )statrD   obs1r'   r/   r/   r0   
test_assoc  s   
r   )numpyr"   numpy.testingr   r   r   r   r   r   r   ra   scipyr   scipy.specialr	   scipy.stats.contingencyr
   r   r   r   r1   r:   r;   r   r   r   r   r/   r/   r/   r0   <module>   s      X