o
    Eb,                     @   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qkZeselfZedouble r   @/usr/lib/python3/dist-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㈵>   )npZonesfloatr   r   r   abslog)r   r   Sr   Z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^?   )Z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^?r-   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    r+   ).c}?r0   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   r+   r&   r.   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 r5   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    r+   r   Zarrayr   r   r   Tr.   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 r8   r9   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    r+   )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   r?   r@   pytestr   r3   r   r   )r   rA   rB   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 )NrE   z!`base` must be a positive number.rG   r   rI   r   rA   rK   r   r   r   test_input_validationl   s
   "z!TestEntropy.test_input_validationN)__name__
__module____qualname__r   r   r#   r(   r*   r/   r1   r4   r6   r7   r;   r<   rD   rL   rO   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{?ư>Zrtolr    )window_lengthrW   h?r   À%?r   r?   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   r=   rT   r    rU   )r,   rW   )rX   g.x?gOjM?rY   rZ   )r,   r[   rW   )r\   g\?g:?r   )r]   gpj?g80Qd?r^   rb   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   rf   zhWindow length \({window_length}\) must be positive and less than half the sample size \({sample_size}\).r    r>   r!   )r[   sample_sizerG   )r[   r,   )
r   r?   r_   r`   shapeformatr2   r3   r   ra   )r   rc   rd   Z	error_strrh   r[   Z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   )rT   r=   r+   r^   )r   rc   rd   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   s8   t jd}|d}tt|jjtj|dd d S )Nr   rf   r    r+   )r   r?   r_   r`   r   r   ra   r:   )r   rc   rd   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 )	NrE   z+`base` must be a positive number or `None`.rG   rM   r   z`method` must be one of...z	ekki-ekkirV   )r   r?   r@   rJ   r   r3   r   ra   rN   r   r   r   rO      s   "z-TestDifferentialEntropy.test_input_validationrW   )rU   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 )	Nrp   '  i@B r   sizerc   rV   {Gzt?rZ   )r   normrvsr   ra   r   )r   rW   nrv   expectedrC   r   r   r   test_consistency   s
   
z(TestDifferentialEntropy.test_consistency)gMbX?灕Cl?)gA`"?g)\(?)gHzG?gy&1?)gMb?rz   )rU   rn   rp   ro   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)rq   2      r   rr   r>   )r[   rW   r,   r!   rt   )ZatolgMb`?)
r   ru   rv   r   ra   r   r   sqrtmeanstdr   rW   rx   Zrepsrw   mZrmse_expectedZstd_expectedrv   Ztrue_entropyrC   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   Zexponrv   r   ra   r   r   r   r   r   r   r   r   r   test_expon_rmse_std  r   z+TestDifferentialEntropy.test_expon_rmse_stdz	n, method))r   rn   )r"   ro   )i  rU   c                 C   s:   t jj|fdd}t |}t j||d}||ksJ d S )Nr   rr   rV   )r   ru   rv   ra   )r   rw   rW   rv   Zres1Zres2r   r   r   test_method_auto  s   
z(TestDifferentialEntropy.test_method_autoN)rP   rQ   rR   __doc__re   rg   rk   rl   rm   rO   rJ   ZmarkZparametrizery   Znorm_rmse_std_caseslistitemsr   Zexpon_rmse_std_casesr   r   r   r   r   r   rS   s   s<    
	





rS   )Znumpyr   Znumpy.testingr   r   r   r   r   rJ   r   r2   Zscipy.statsr   r   rS   r   r   r   r   <module>   s   f