o
    Ge,                     @   sl   d dl Zd dlmZmZ d dlmZmZmZ d dlZd dlm	Z
 d dlmZ G dd dZG dd dZdS )	    N)assert_equalassert_allclose)assert_assert_almost_equalassert_array_almost_equal)raisesc                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) TestEntropyc                 C   sD   g d}g d}t ||}t ||}td|k t|dk d S )N)      ?皙?333333?)皙?g      ?g?        )statsentropyr   )selfpkqkeselfedouble r   h/var/www/html/SPIRAL.web.tool/spiral_venv/lib/python3.10/site-packages/scipy/stats/tests/test_entropy.pytest_entropy_positive   s   z!TestEntropy.test_entropy_positivec                 C   s   t dt}tj|dd}tt|d dk  t dt}d|d d< t||}tj||dd}tt|| t d dk  d S )N   g       @baseg      @gh㈵>   )nponesfloatr   r   r   abslog)r   r   Sr   S2r   r   r   test_entropy_base   s   "zTestEntropy.test_entropy_basec                 C   s   t tg dddd d S )N)r         g R^?   )decimal)r   r   r   )r   r   r   r   test_entropy_zero"   s   
zTestEntropy.test_entropy_zeroc                 C   sF   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   r   test_entropy_2d'   s
   zTestEntropy.test_entropy_2dc                 C   sj   ddgddgddgg}ddgddgddgg}t t||tjdg d|d d< t t||d	dg d S )
Nr   r
   r)   r   r	   r   r*   r   g^fTF?)r   r   r   r   infr,   r   r   r   test_entropy_2d_zero-   s   z TestEntropy.test_entropy_2d_zeroc                 C   s2   ddgddgddgg}t tj|ddg d d S )	Nr   r
   r)   r   r	   r$   axis)R^?r2   gD%+?r+   r   r   r   r   r   $test_entropy_base_2d_nondefault_axis7   s   z0TestEntropy.test_entropy_base_2d_nondefault_axisc                 C   sJ   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	   r$   r0   ).c}?r5   gaX?r+   r,   r   r   r   test_entropy_2d_nondefault_axis<   s
   z+TestEntropy.test_entropy_2d_nondefault_axisc                 C   s:   ddgddgddgg}ddgddgg}t ttj|| d S )Nr   r
   r)   r   r	   )assert_raises
ValueErrorr   r   r,   r   r   r   test_entropy_raises_value_errorB   s   z+TestEntropy.test_entropy_raises_value_errorc                 C   s4   ddgddgddgg}t tj|ddt| d S Nr   r
   r)   r   r	   r   r0   r+   r3   r   r   r   1test_base_entropy_with_axis_0_is_equal_to_defaultG   s   z=TestEntropy.test_base_entropy_with_axis_0_is_equal_to_defaultc                 C   sN   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 r:   r+   r,   r   r   r   ,test_entropy_with_axis_0_is_equal_to_defaultL   s
   
z8TestEntropy.test_entropy_with_axis_0_is_equal_to_defaultc                 C   s>   t ddgddgddgg}tt|jjtj|dd d S Nr   r
   r)   r   r	   r$   r0   r   arrayr   r   r   Tr3   r   r   r   test_base_entropy_transposedR   s   z(TestEntropy.test_base_entropy_transposedc                 C   s`   t ddgddgddgg}t ddgddgddgg}tt|j|jjtj||dd d S r=   r>   r,   r   r   r   test_entropy_transposedW   s
   z#TestEntropy.test_entropy_transposedc                 C   sn   t jd t jd}t jdd}tj||dd}t|d t||d  t|d t||d  d S )Nr      r%   r$   r0   )r   randomrandr   r   r   )r   xyresr   r   r   test_entropy_broadcasting]   s   z%TestEntropy.test_entropy_broadcastingc                 C   sb   t jddd}t jdd}d}tjt|d t|| W d    d S 1 s*w   Y  d S )N
   r$   r&      r%   z+shape mismatch: objects cannot be broadcastmatchr   rE   rF   pytestr   r8   r   r   )r   rG   rH   messager   r   r   test_entropy_shape_mismatche   s   "z'TestEntropy.test_entropy_shape_mismatchc                 C   sR   t jd}d}tjt|d tj|dd W d    d S 1 s"w   Y  d S )NrK   z!`base` must be a positive number.rM   r   rO   r   rG   rQ   r   r   r   test_input_validationl   s
   "z!TestEntropy.test_input_validationN)__name__
__module____qualname__r   r#   r(   r-   r/   r4   r6   r9   r;   r<   rA   rB   rJ   rR   rU   r   r   r   r   r      s     	
r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
jdg ddd ZdddddZe
jdee dd ZdddddZe
jdee dd Z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   sr   t jd}|d}tj|dd}t|ddd tj|ddd	}t|d
dd tj|ddd	}t|ddd d S )Nr   d   vasicekmethodd{?ư>rtolr$   )window_lengthr]   h?r   À%?r   rE   RandomStatestandard_normalr   differential_entropyr   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}t|g ddd tj|dddd	}t|g d
dd tj|dddd	}t|g ddd d S )Nr   rC   rZ   r$   r[   )r1   r]   )r^   g.x?gOjM?r_   r`   )r1   rb   r]   )rc   g\?g:?r   )rd   gpj?g80Qd?re   ri   r   r   r   4test_differential_entropy_vasicek_2d_nondefault_axis   s0   



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 s<w   Y  qd S )
Nr   rm   zhWindow length \({window_length}\) must be positive and less than half the sample size \({sample_size}\).r$   rD   r%   )rb   sample_sizerM   )rb   r1   )
r   rE   rf   rg   shapeformatr7   r8   r   rh   )r   rj   rk   	error_strro   rb   formatted_error_strr   r   r   ,test_differential_entropy_raises_value_error   s&   

zDTestDifferentialEntropy.test_differential_entropy_raises_value_errorc                 C   s<   t jd}|d}tj|dd}t|}t|| d S )Nr   )rZ   rC   r0   re   )r   rj   rk   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   s8   t jd}|d}tt|jjtj|dd d S )Nr   rm   r$   r0   )r   rE   rf   rg   r   r   rh   r@   )r   rj   rk   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 sBw   Y  d S )	NrK   z+`base` must be a positive number or `None`.rM   rS   r   z`method` must be one of...z	ekki-ekkir\   )r   rE   rF   rP   r   r8   r   rh   rT   r   r   r   rU      s   "z-TestDifferentialEntropy.test_input_validationr]   )r[   van esebrahimicorreac                 C   sJ   |dkrdnd}t jj|dd}t j }t j||d}t||dd d S )	Nrz   '  i@B r   sizerj   r\   {Gzt?r`   )r   normrvsr   rh   r   )r   r]   nr   expectedrI   r   r   r   test_consistency   s
   
z(TestDifferentialEntropy.test_consistency)gMbX?灕Cl?)gA`"?g)\(?)gHzG?gy&1?)gMb?r   )r[   rx   rz   ry   zmethod, expectedc                 C   |   d\}}}|\}}t jj||fdd}t j }	t j|||dd}
ttt|
|	 d |dd tt	|
|d	d d S 
N)r{   2      r   r|   rD   )rb   r]   r1   r%   r~   )atolgMb`?)
r   r   r   r   rh   r   r   sqrtmeanstdr   r]   r   repsr   mrmse_expectedstd_expectedr   true_entropyrI   r   r   r   test_norm_rmse_std      

z*TestDifferentialEntropy.test_norm_rmse_std)gE?l?)gx?gOn?)gףp=
?g~jt?)gS?r   c                 C   r   r   )
r   exponr   r   rh   r   r   r   r   r   r   r   r   r   test_expon_rmse_std  r   z+TestDifferentialEntropy.test_expon_rmse_stdz	n, method))r   rx   )r&   ry   )i  r[   c                 C   s:   t jj|fdd}t |}t j||d}||ksJ d S )Nr   r|   r\   )r   r   r   rh   )r   r   r]   r   res1res2r   r   r   test_method_auto  s   
z(TestDifferentialEntropy.test_method_autoN)rV   rW   rX   __doc__rl   rn   rt   rv   rw   rU   rP   markparametrizer   norm_rmse_std_caseslistitemsr   expon_rmse_std_casesr   r   r   r   r   r   rY   s   s<    
	





rY   )numpyr   numpy.testingr   r   r   r   r   rP   r   r7   scipy.statsr   r   rY   r   r   r   r   <module>   s   f