o
    `^h6                     @   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qkeselfedouble r   \/home/air/shanriGPT/back/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   wherearangemathlog)r   r   r   Sr   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   r0   r   r   r   r1   g^fTF?)r   r
   r   r   infr2   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   r0   r   r      axis)R^?r9   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   r0   r   r   r6   r7   ){[<?r<   gC8X?r-   r2   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   r0   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   r0   r   r   r   r7   r-   r:   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 rE   r-   r2   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   r0   r   r   r6   r7   r   r
   r   r   Tr:   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 rH   rI   r2   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<   )   r6   r7   r   )r   .r6   )r6   .)nprandom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)
   r6      )   rN   r>   r?   r#   rA   r   rB   r   r   )r   r   r.   rT   rC   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 )NrW   z!`base` must be a positive number.r?   r    rZ   r   r   r.   rC   r   r   r   test_input_validation~   s
   
"z!TestEntropy.test_input_validationN)__name__
__module____qualname__r   r   r+   r/   r3   r5   r;   r=   rD   rF   rG   rK   rL   rV   r[   r^   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- {?r6   )window_lengthrg    ?r"   =&?	rP   rQ   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   rM   rd   r6   re   )r8   rg   )rh   gUZdpx?gE7L?)r8   ri   rg   )rj   gzG?gt?r"   )rk   gl+ɍj?go4d?rl   )r   r   rq   rr   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   rt   zhWindow length \({window_length}\) must be positive and less than half the sample size \({sample_size}\).r6   rO   rN   )ri   sample_sizer?   )ri   r8   )rP   rQ   rm   rn   r   ro   shapeformatassert_raisesrB   r   rp   )r   r   rq   rr   	error_strrw   ri   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   )rd   rM   r7   rl   )r   r   rq   rr   r   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   rt   r6   r7   )
rP   rQ   rm   rn   r   ro   r
   r   rp   rJ   )r   r   rq   rr   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 )	NrW   z+`base` must be a positive number or `None`.r?   r\   r    z`method` must be one of...z	ekki-ekkirf   )
rP   rQ   randr   ro   rA   r   rB   r   rp   r]   r   r   r   r^      s   "z-TestDifferentialEntropy.test_input_validationrg   )re   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   sizerq   rf   {Gzt?)rtol)r	   rA   xfailr   r   r   rvsr   ro   floatr   rp   r
   )r   rg   r   nr   expectedrU   r   r   r   test_consistency   s   
z(TestDifferentialEntropy.test_consistency)gMbX?灕Cl?)gA`"?g)\(?)gHzG?gy&1?)gMb?r   )re   r   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   r6   r   rO   )ri   rg   r8   rN   r   )atol)
correctiongMb`?)r	   rA   r   r   rmse_std_casesr   r   ro   r   r   r   rp   r
   sqrtmeanr   std)r   rg   r   r   repsr   mr   rmse_expectedstd_expectedr   true_entropyrU   xp_testr   r   r   test_rmse_std  s$   

z%TestDifferentialEntropy.test_rmse_stdz	n, method))r"   r   )rX   r   )i  re   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   rf   )
r	   rA   r   r   r   r   r   ro   rp   r   )r   r   rg   r   r   res1res2r   r   r   test_method_auto/  s   

z(TestDifferentialEntropy.test_method_autodtype)N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   )r6   r6   rN   rM   rM         r      r   r"   	   rW   rY   r,   )r   rf   r   )r   rA   r   getattrstrr   r   r   rp   r   r
   )	r   r   rg   r   r.   dtype_in	dtype_outrU   ru   r   r   r   test_dtypes_gh21192;  s   
z+TestDifferentialEntropy.test_dtypes_gh21192N)r_   r`   ra   __doc__rs   rv   r}   rA   markrb   r   r   r^   parametrizer   norm_rmse_std_casesexpon_rmse_std_casesr   r   r   r   r   r   r   r   r   r   rc      sP    

	


	
	rc   )r'   rA   r   rz   numpyrP   scipyr   scipy.statsr   r   scipy.conftestr   scipy._lib._array_apir   r   r	   scipy._lib._array_api_no_0dr
   r   r   r   r   usefixturesrc   r   r   r   r   <module>   s    x
