o
    i6                     @   s   d dl Z d dlZd dlm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mZmZ d dlmZmZmZ G dd	 d	Zeejd
G dd dZdS )    N)raises)stats)normexpon)array_api_compatible)array_namespaceis_array_api_strictis_jax)xp_assert_closexp_assert_equalxp_assert_lessc                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd ZdS ) TestEntropyc                 C   sZ   | g d}| g d}t||}t||}t|| d t| | d d S )N)      ?皙?333333?)皙?g      ?g?        )asarrayr   entropyr   r   )selfxppkqkZeselfZedouble r   X/home/kim/smarthome/.venv/lib/python3.10/site-packages/scipy/stats/tests/test_entropy.pytest_entropy_positive   s   z!TestEntropy.test_entropy_positivec                 C   s   | d}tj|dd}t||d |d | d}||ddk |d|}t||}tj||dd}t||| t	d |d d S )N          @baseg      @gh㈵>   )
onesr   r   r   absr   whereZarangemathlog)r   r   r   Sr   ZS2r   r   r   test_entropy_base   s   

(zTestEntropy.test_entropy_basec                 C   s(   | g d}tt|| d d S )N)r         ?r   g R^?r   r
   r   r   )r   r   xr   r   r   test_entropy_zero%   s   
zTestEntropy.test_entropy_zeroc                 C   sX   | ddgddgddgg}| ddgddgddgg}tt||| ddg d S )Nr   r   333333?r   r   gN!0?2?r)   r   r   r   r   r   r   r   test_entropy_2d,   s
   zTestEntropy.test_entropy_2dc                 C   s   | ddgddgddgg}| ddgddgddgg}tt||| |jdg | ddgddgddgg}tt||| ddg d S )	Nr   r   r,   r   r   r   r-   g^fTF?)r   r
   r   r   infr.   r   r   r   test_entropy_2d_zero3   s   z TestEntropy.test_entropy_2d_zeroc                 C   s>   | ddgddgddgg}ttj|dd| g d d S )	Nr   r   r,   r   r      axis)R^?r5   gD%+?r)   r   r   r   r   r   r   $test_entropy_base_2d_nondefault_axis>   s   z0TestEntropy.test_entropy_base_2d_nondefault_axisc                 C   s\   | ddgddgddgg}| ddgddgddgg}ttj||dd| g d d S )	Nr   r   r,   r   r   r2   r3   ){[<?r8   gC8X?r)   r.   r   r   r   test_entropy_2d_nondefault_axisD   s
   z+TestEntropy.test_entropy_2d_nondefault_axisc                 C   sv   | ddgddgddgg}| ddgddgg}d}tjt|d t|| W d    d S 1 s4w   Y  d S )Nr   r   r,   r   r   /Array shapes are incompatible for broadcasting.match)r   pytestr   
ValueErrorr   r   )r   r   r   r   messager   r   r   test_entropy_raises_value_errorK   s   "z+TestEntropy.test_entropy_raises_value_errorc                 C   s:   | ddgddgddgg}ttj|ddt| d S Nr   r   r,   r   r   r   r3   r)   r6   r   r   r   1test_base_entropy_with_axis_0_is_equal_to_defaultS   s   z=TestEntropy.test_base_entropy_with_axis_0_is_equal_to_defaultc                 C   sZ   | ddgddgddgg}| ddgddgddgg}ttj||ddt|| d S rA   r)   r.   r   r   r   ,test_entropy_with_axis_0_is_equal_to_defaultY   s
   
z8TestEntropy.test_entropy_with_axis_0_is_equal_to_defaultc                 C   s<   | ddgddgddgg}tt|jtj|dd d S Nr   r   r,   r   r   r2   r3   r   r
   r   r   Tr6   r   r   r   test_base_entropy_transposed`   s   z(TestEntropy.test_base_entropy_transposedc                 C   s^   | ddgddgddgg}| ddgddgddgg}tt|j|jtj||dd d S rD   rE   r.   r   r   r   test_entropy_transposedf   s
   z#TestEntropy.test_entropy_transposedc                 C   st   t jd}||d}||d}tj||dd}t|d t||d  t|d t||d	  d S )
Nl   @UC<   )   r2   r3   r   )r   .r2   )r2   .)nprandomZdefault_rngr   r   r   r   )r   r   rngr*   yresr   r   r   test_entropy_broadcastingm   s   z%TestEntropy.test_entropy_broadcastingc                 C   sX   | d}| d}d}tjt|d t|| W d    d S 1 s%w   Y  d S )N)
   r2      )   rJ   r:   r;   r!   r=   r   r>   r   r   )r   r   r*   rO   r?   r   r   r   test_entropy_shape_mismatchv   s   

"z'TestEntropy.test_entropy_shape_mismatchc                 C   sP   | d}d}tjt|d tj|dd W d    d S 1 s!w   Y  d S )NrR   z!`base` must be a positive number.r;   r   rU   r   r   r*   r?   r   r   r   test_input_validation~   s
   
"z!TestEntropy.test_input_validationN)__name__
__module____qualname__r   r   r'   r+   r/   r1   r7   r9   r@   rB   rC   rG   rH   rQ   rV   rY   r   r   r   r   r      s>    
	













r   skip_xp_backendsc                   @   s$  e Zd ZdZdd Zdd Zdd Zejj	dd	d
dd Z
ejj	dd	d
dd Zdd Zejdg ddd ZdddddZdddddZeeeeiZejdg dejdeegdd  Zejd!g d"d#d$ Zejj	dd	d
ejdg dejd%g d&d'd( Zd)S )*TestDifferentialEntropyz
    Vasicek results are compared with the R package vsgoftest.

    # library(vsgoftest)
    #
    # samp <- c(<values>)
    # entropy.estimate(x = samp, window = <window_length>)

    c                 C   s   t jd}|d}|| }tj|dd}t||d tj|ddd}t||d tj|d	dd}t||d
 d S )Nr   d   vasicekmethod- {?r2   )window_lengthrb    ?r    =&?	rL   rM   RandomStatestandard_normalr   tolistr   differential_entropyr
   )r   r   random_statevaluesr   r   r   r   !test_differential_entropy_vasicek   s   
z9TestDifferentialEntropy.test_differential_entropy_vasicekc                 C   s   t jd}|d}|| }tj|ddd}|g d}t|| tj|dddd}|g d}t|| tj|dd	dd}|g d
}t|| d S )Nr   rI   r_   r2   r`   )r4   rb   )rc   gUZdpx?gE7L?)r4   rd   rb   )re   gzG?gt?r    )rf   gl+ɍj?go4d?rg   )r   r   rl   rm   r   refr   r   r   4test_differential_entropy_vasicek_2d_nondefault_axis   s    




zLTestDifferentialEntropy.test_differential_entropy_vasicek_2d_nondefault_axisc              	   C   s   t jd}|d}|| }d}|jd }dd|d |hD ]'}|j||d}tt	|d t
j||dd	 W d    n1 sCw   Y  q!d S )
Nr   ro   zhWindow length \({window_length}\) must be positive and less than half the sample size \({sample_size}\).r2   rK   rJ   )rd   sample_sizer;   )rd   r4   )rL   rM   rh   ri   r   rj   shapeformatassert_raisesr>   r   rk   )r   r   rl   rm   Z	error_strrr   rd   Zformatted_error_strr   r   r   ,test_differential_entropy_raises_value_error   s(   

zDTestDifferentialEntropy.test_differential_entropy_raises_value_errorz	jax.numpyz#JAX doesn't support item assignment)reasonc                 C   sJ   t jd}|d}|| }tj|dd}t|}t|| d S )Nr   )r_   rI   r3   rg   )r   r   rl   rm   r   Zdefault_entropyr   r   r   >test_base_differential_entropy_with_axis_0_is_equal_to_default   s   

zVTestDifferentialEntropy.test_base_differential_entropy_with_axis_0_is_equal_to_defaultc                 C   sD   t jd}|d}|| }tt|j	tj|dd d S )Nr   ro   r2   r3   )
rL   rM   rh   ri   r   rj   r
   r   rk   rF   )r   r   rl   rm   r   r   r   )test_base_differential_entropy_transposed   s   

zATestDifferentialEntropy.test_base_differential_entropy_transposedc                 C   s   t jd}|| }d}tjt|d tj	|dd W d    n1 s(w   Y  d}tjt|d tj	|dd W d    d S 1 sIw   Y  d S )	NrR   z+`base` must be a positive number or `None`.r;   rW   r   z`method` must be one of...z	ekki-ekkira   )
rL   rM   Zrandr   rj   r=   r   r>   r   rk   rX   r   r   r   rY      s   "z-TestDifferentialEntropy.test_input_validationrb   )r`   van esebrahimicorreac                 C   s   t |r|dkrtd nt|r|dkrtd |dkr!dnd}tjj|dd}|| }|t	tj
 }tj||d	}t||d
d d S )Nr{   Needs array assignment.r|   Needs fancy indexing.'  i@B r   sizerl   ra   {Gzt?)Zrtol)r	   r=   xfailr   r   r   rvsr   rj   floatr   rk   r
   )r   rb   r   nr   expectedrP   r   r   r   test_consistency   s   
z(TestDifferentialEntropy.test_consistency)gMbX?灕Cl?)gA`"?g)\(?)gHzG?gy&1?)gMb?r   )r`   rz   r|   r{   )gE?l?)gx?gOn?)gףp=
?g~jt?)gS?r   distc                 C   s   t |r|dkrtd nt|r|dkrtd d\}}}| j| | }||d ||d }}	|j||fdd}
||
 }
|t|	 }t
j|
||d	d
}t|||| d |dd t|}t|j|dd|	dd d S )Nr{   r}   r|   r~   )r   2      r   r2   r   rK   )rd   rb   r4   rJ   r   )Zatol)Z
correctiongMb`?)r	   r=   r   r   rmse_std_casesr   r   rj   r   r   r   rk   r
   sqrtmeanr   Zstd)r   rb   r   r   Zrepsr   mr   Zrmse_expectedZstd_expectedr   Ztrue_entropyrP   Zxp_testr   r   r   test_rmse_std  s$   

z%TestDifferentialEntropy.test_rmse_stdz	n, method))r    rz   )rS   r{   )i  r`   c                 C   s`   t |r|dkrtd tjj|fdd}|| }t|}tj||d}t	|| d S )Nr{   r}   r   r   ra   )
r	   r=   r   r   r   r   r   rj   rk   r   )r   r   rb   r   r   Zres1Zres2r   r   r   test_method_auto/  s   

z(TestDifferentialEntropy.test_method_autodtype)NZfloat32float64c           	      C   s   t |r|dkrtd g d}t|t|d }t|t||dj}tj|j||d|d}tj|j||j	d|d}t
||j||dd  d S )Nr|   r~   )r2   r2   rJ   rI   rI         r      r   r    	   rR   rT   r(   )r   ra   r   )r   r=   r   getattrstrr   r   r   rk   r   r
   )	r   r   rb   r   r*   Zdtype_inZ	dtype_outrP   rp   r   r   r   test_dtypes_gh21192;  s   
z+TestDifferentialEntropy.test_dtypes_gh21192N)rZ   r[   r\   __doc__rn   rq   rv   r=   markr]   rx   ry   rY   Zparametrizer   Znorm_rmse_std_casesZexpon_rmse_std_casesr   r   r   r   r   r   r   r   r   r   r^      sP    

	


	
	r^   )r$   r=   r   ru   numpyrL   Zscipyr   Zscipy.statsr   r   Zscipy.conftestr   Zscipy._lib._array_apir   r   r	   Zscipy._lib._array_api_no_0dr
   r   r   r   r   Zusefixturesr^   r   r   r   r   <module>   s    x
