a
    BCCf,                     @   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   Z/var/www/html/django/DPS/env/lib/python3.9/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㈵>   )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^?   )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_axis6   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}?r1   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_errorA   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_defaultF   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 r6   r'   r(   r   r   r   ,test_entropy_with_axis_0_is_equal_to_defaultK   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   arrayr   r   r   Tr/   r   r   r   test_base_entropy_transposedQ   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 r9   r:   r(   r   r   r   test_entropy_transposedV   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   s`   t jddd}t jdd}d}tjt|d t|| W d    n1 sR0    Y  d S )N
   r    r"      r!   z/Array shapes are incompatible for broadcasting.matchr   rA   rB   pytestr   r4   r   r   )r   rC   rD   messager   r   r   test_entropy_shape_mismatchd   s
    z'TestEntropy.test_entropy_shape_mismatchc                 C   sP   t jd}d}tjt|d tj|dd W d    n1 sB0    Y  d S )NrG   z!`base` must be a positive number.rI   r   rK   r   rC   rM   r   r   r   test_input_validationk   s    z!TestEntropy.test_input_validationN)__name__
__module____qualname__r   r   r$   r)   r+   r0   r2   r5   r7   r8   r=   r>   rF   rN   rQ   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_lengthrY   h?r   À%?r   rA   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?   rV   r    rW   )r-   rY   )rZ   g.x?gOjM?r[   r\   )r-   r]   rY   )r^   g\?g:?r   )r_   gpj?g80Qd?r`   rd   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 ]N}|j||d}tt|d  tj	||dd	 W d    q41 sx0    Y  q4d S )
Nr   rh   zhWindow length \({window_length}\) must be positive and less than half the sample size \({sample_size}\).r    r@   r!   )r]   sample_sizerI   )r]   r-   )
r   rA   ra   rb   shapeformatr3   r4   r   rc   )r   re   rf   Z	error_strrj   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   )rV   r?   r,   r`   )r   re   rf   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   rh   r    r,   )r   rA   ra   rb   r   r   rc   r<   )r   re   rf   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 sB0    Y  d}tjt|d tj|dd W d    n1 s0    Y  d S )	NrG   z+`base` must be a positive number or `None`.rI   rO   r   z`method` must be one of...z	ekki-ekkirX   )r   rA   rB   rL   r   r4   r   rc   rP   r   r   r   rQ      s    ,z-TestDifferentialEntropy.test_input_validationrY   )rW   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 )	Nrr   '  i@B r   sizere   rX   {Gzt?r\   )r   normrvsr   rc   r   )r   rY   nrx   expectedrE   r   r   r   test_consistency   s
    
z(TestDifferentialEntropy.test_consistency)gMbX?灕Cl?)gA`"?g)\(?)gHzG?gy&1?)gMb?r|   )rW   rp   rr   rq   zmethod, expectedc                 C   s|   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)rs   2      r   rt   r@   )r]   rY   r-   r!   rv   )ZatolgMb`?)
r   rw   rx   r   rc   r   r   sqrtmeanstdr   rY   rz   Zrepsry   mZrmse_expectedZstd_expectedrx   Ztrue_entropyrE   r   r   r   test_norm_rmse_std   s    

z*TestDifferentialEntropy.test_norm_rmse_std)gE?l?)gx?gOn?)gףp=
?g~jt?)gS?r   c                 C   s|   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 r}   )
r   Zexponrx   r   rc   r   r   r   r   r   r   r   r   r   test_expon_rmse_std  s    

z+TestDifferentialEntropy.test_expon_rmse_stdz	n, method))r   rp   )r"   rq   )i  rW   c                 C   s:   t jj|fdd}t |}t j||d}||ks6J d S )Nr   rt   rX   )r   rw   rx   rc   )r   ry   rY   rx   Zres1Zres2r   r   r   test_method_auto  s    
z(TestDifferentialEntropy.test_method_autoN)rR   rS   rT   __doc__rg   ri   rm   rn   ro   rQ   rL   markZparametrizer{   Znorm_rmse_std_caseslistitemsr   Zexpon_rmse_std_casesr   r   r   r   r   r   rU   r   s:   
	





rU   )numpyr   Znumpy.testingr   r   r   r   r   rL   r   r3   Zscipy.statsr   r   rU   r   r   r   r   <module>   s   f