U
    »mœd,  ã                   @   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   sH   dddg}dddg}t  ||¡}t  ||¡}td|kƒ t|dkƒ d S )Nç      à?çš™™™™™É?ç333333Ó?çš™™™™™¹?g      Ð?gÍÌÌÌÌÌä?ç        )ÚstatsÚentropyr   )ÚselfÚpkÚqkZeselfZedouble© r   úW/home/sam/Atlas/atlas_env/lib/python3.8/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      @gñhãˆµøä>é   )Ú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 dddg¡ddd� d S )Nr   é   é   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¯^fTðFÆ?)r   r   r   r   Úinfr(   r   r   r   Útest_entropy_2d_zero-   s    ÿÿz TestEntropy.test_entropy_2d_zeroc                 C   s4   ddgddgddgg}t tj|dd�ddd	gƒ d S )
Nr   r
   r%   r   r	   r    ©ÚaxisgæòöR^ä?gD%¬†+å?r'   ©r   r   r   r   r   Ú$test_entropy_base_2d_nondefault_axis7   s    ÿz0TestEntropy.test_entropy_base_2d_nondefault_axisc                 C   sL   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�ddd	gƒ d S )
Nr   r
   r%   r   r	   r    r,   g.c}“Í?g„aÀ’«XÀ?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 r4   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   Ú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 r7   r8   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   sL   t j ddd¡}t j dd¡}d}tjt|d�� t ||¡ W 5 Q R X d S )Né
   r    r"   é   r!   z+shape mismatch: objects cannot be broadcast©Úmatch©r   r?   r@   Úpytestr   r2   r   r   )r   rA   rB   Úmessager   r   r   Útest_entropy_shape_mismatche   s
    z'TestEntropy.test_entropy_shape_mismatchc              	   C   s<   t j d¡}d}tjt|d�� tj|dd� W 5 Q R X 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/   r0   r3   r5   r6   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ddddg¡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#d$d%d&g¡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©Úmethodç¿dãÁ{õ?ç�íµ ÷Æ°>©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|dddgd	d
� tj|dddd�}t|dddgd	d
� tj|dddd�}t|dddg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]   gpÏó§�jõ?g80¹Qd­ô?r^   rb   r   r   r   Ú4test_differential_entropy_vasicek_2d_nondefault_axisŽ   s0    
ý
ÿý
ÿýzLTestDifferentialEntropy.test_differential_entropy_vasicek_2d_nondefault_axisc              
   C   st   t j d¡}| d¡}d}|jd }dd|d |hD ]:}|j||d�}tt|d�� tj	||dd	� W 5 Q R X q4d 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Úformatr1   r2   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   sh   t j d¡}d}tjt|d�� tj|dd� W 5 Q R X d}tjt|d�� tj|dd� W 5 Q R X 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   r2   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   ç{®Gázt?rZ   )r   ÚnormÚrvsr   ra   r   )r   rW   Únrv   ÚexpectedrC   r   r   r   Útest_consistencyÞ   s
    
z(TestDifferentialEntropy.test_consistency)gòÒMbXÉ?ç�•C‹lç»?)g‰A`åÐ"Ë?g)\�Âõ(¼?)gHáz®GÁ?gyé&1¬¼?)gü©ñÒMbÀ?rz   )rU   rn   rp   ro   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)rq   é2   é   r   rr   r>   )r[   rW   r-   r!   rt   )Zatolgü©ñÒMb`?)
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ð   s    

 ÿ ÿz*TestDifferentialEntropy.test_norm_rmse_std)g¢E¶óýÔÈ?ç‹lçû©ñÂ?)g¶óýÔxéÆ?gßO�—nÃ?)g×£p=
×Ã?gÛù~j¼tÃ?)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exponrv   r   ra   r   r   r~   r   r€   r�   r   r   r   Útest_expon_rmse_std  s    

 ÿ ÿ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s6t‚d S )Nr   rr   rV   )r   ru   rv   ra   ÚAssertionError)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   ÚmarkZparametrizery   Znorm_rmse_std_casesÚlistÚitemsrƒ   Zexpon_rmse_std_casesr…   r‡   r   r   r   r   rS   s   sF   
	 ÿ

ü
ÿ
ü
ÿ

þrS   )Únumpyr   Znumpy.testingr   r   r   r   r   rJ   r   r1   Zscipy.statsr   r   rS   r   r   r   r   Ú<module>   s   f