
    Џkh2                     ,   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mZmZ ej                  j                   Zej                  j!                  dd       G d	 d
             Zej                  j!                  dd       G d d             Zy)    N)raises)stats)normexpon)xp_assert_closexp_assert_equalxp_assert_lessz
dask.arrayzboolean index assignmentreasonc                   f    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zy)TestEntropyc                    |j                  g d      }|j                  g d      }t        j                  ||      }t        j                  ||      }t        ||j                  d             t	        | |j                  d             y )N)      ?皙?333333?)皙?g      ?g?        )asarrayr   entropyr   r	   )selfxppkqkeselfedoubles         Z/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/stats/tests/test_entropy.pytest_entropy_positivez!TestEntropy.test_entropy_positive   se    ZZ(ZZ)*b"%--B'rzz"~.xB0    c                    |j                  d      }t        j                  |d      }t        |j	                  |dz
        |j                  d             |j                  d      }|j                  |j                  d      dk  d|      }t        j                  ||      }t        j                  ||d      }t        |j	                  ||z  t        j                  d      z
        |j                  d             y )N          @baseg      @gh㈵>   )
onesr   r   r	   absr   wherearangemathlog)r   r   r   Sr   S2s         r   test_entropy_basezTestEntropy.test_entropy_base   s    WWR[MM"2&rvva"f~rzz%'89WWR[XXbiima'R0MM"b!]]2r+rvvadTXXb\12BJJu4EFr   c                     |j                  g d      }t        t        j                  |      |j                  d             y )N)r         ?r!   g R^?r   r   r   r   )r   r   xs      r   test_entropy_zerozTestEntropy.test_entropy_zero$   s0    JJ|$a(

#67	9r   c                     |j                  ddgddgddgg      }|j                  ddgddgddgg      }t        t        j                  ||      |j                  ddg             y )Nr   r   333333?r   r   gN!0?2?r0   r   r   r   r   s       r   test_entropy_2dzTestEntropy.test_entropy_2d*   sj    ZZ#sc3Z#s<=ZZ#sc3Z#s<=b"-

Iz#:;	=r   c                 x   |j                  ddgddgddgg      }|j                  ddgddgddgg      }t        t        j                  ||      |j                  |j                  dg             |j                  ddgddgddgg      }t        t        j                  ||      |j                  ddg             y )	Nr   r   r4   r   r   r   r5   g^fTF?)r   r   r   r   infr6   s       r   test_entropy_2d_zeroz TestEntropy.test_entropy_2d_zero0   s    ZZ#sc3Z#s<=ZZ#sc3Z#s<=b"-

BFFJ#78	: ZZ#sc3Z#s<=b"-

J
#;<	>r   c                     |j                  ddgddgddgg      }t        t        j                  |d      |j                  g d             y )	Nr   r   r4   r   r      axis)R^?r?   gD%+?r0   r   r   r   s      r   $test_entropy_base_2d_nondefault_axisz0TestEntropy.test_entropy_base_2d_nondefault_axis:   sF    ZZ#sc3Z#s<=bq1

#GH	Jr   c                     |j                  ddgddgddgg      }|j                  ddgddgddgg      }t        t        j                  ||d      |j                  g d             y )	Nr   r   r4   r   r   r<   r=   ){[<?rC   gC8X?r0   r6   s       r   test_entropy_2d_nondefault_axisz+TestEntropy.test_entropy_2d_nondefault_axis?   si    ZZ#sc3Z#s<=ZZ#sc3Z#s<=b"15

#GH	Jr   c                     |j                  ddgddgddgg      }|j                  ddgddgg      }d}t        j                  t        |      5  t	        j
                  ||       d d d        y # 1 sw Y   y xY w)Nr   r   r4   r   r   /Array shapes are incompatible for broadcasting.match)r   pytestr   
ValueErrorr   r   )r   r   r   r   messages        r   test_entropy_raises_value_errorz+TestEntropy.test_entropy_raises_value_errorE   st    ZZ#sc3Z#s<=ZZ#sc3Z01C]]:W5 	"MM"b!	" 	" 	"s   A//A8c                     |j                  ddgddgddgg      }t        t        j                  |d      t        j                  |             y Nr   r   r4   r   r   r   r=   r0   r@   s      r   1test_base_entropy_with_axis_0_is_equal_to_defaultz=TestEntropy.test_base_entropy_with_axis_0_is_equal_to_defaultL   sD    ZZ#sc3Z#s<=bq1b)	+r   c                     |j                  ddgddgddgg      }|j                  ddgddgddgg      }t        t        j                  ||d      t        j                  ||             y rN   r0   r6   s       r   ,test_entropy_with_axis_0_is_equal_to_defaultz8TestEntropy.test_entropy_with_axis_0_is_equal_to_defaultQ   si    ZZ#sc3Z#s<=ZZ#sc3Z#s<=b"15b"-	/r   c                     |j                  ddgddgddgg      }t        t        j                  |j                        t        j                  |d             y Nr   r   r4   r   r   r<   r=   r   r   r   r   Tr@   s      r   test_base_entropy_transposedz(TestEntropy.test_base_entropy_transposedW   sH    ZZ#sc3Z#s<=bdd+bq1	3r   c                     |j                  ddgddgddgg      }|j                  ddgddgddgg      }t        t        j                  |j                  |j                        t        j                  ||d             y rS   rT   r6   s       r   test_entropy_transposedz#TestEntropy.test_entropy_transposed\   sq    ZZ#sc3Z#s<=ZZ#sc3Z#s<=bddBDD1b"15	7r   c                    t         j                  j                  d      }|j                  |j                  d            }|j                  |j                  d            }t	        j
                  ||d      }t        |d   t	        j
                  ||d                t        |d   t	        j
                  ||d	                y )
Nl   @UC<   )   r<   r=   r   )r   .r<   )r<   .)nprandomdefault_rngr   r   r   r   )r   r   rngr1   yress         r   test_entropy_broadcastingz%TestEntropy.test_entropy_broadcastingb   s    ii##$56JJszz!}%JJszz&)*mmAqr*Aa6 ;<Aa6 ;<r   c                     |j                  d      }|j                  d      }d}t        j                  t        |      5  t	        j
                  ||       d d d        y # 1 sw Y   y xY w)N)
   r<      )   r[   rF   rG   r%   rI   r   rJ   r   r   )r   r   r1   ra   rK   s        r   test_entropy_shape_mismatchz'TestEntropy.test_entropy_shape_mismatchj   sT    GGK GGGC]]:W5 	 MM!Q	  	  	 s    A  A)c                     |j                  d      }d}t        j                  t        |      5  t	        j
                  |d       d d d        y # 1 sw Y   y xY w)Nre   z!`base` must be a positive number.rG   r"   rh   r   r   r1   rK   s       r   test_input_validationz!TestEntropy.test_input_validationq   sE    GGBK5]]:W5 	&MM!"%	& 	& 	&s   AAN)__name__
__module____qualname__r   r-   r2   r7   r:   rA   rD   rL   rO   rQ   rV   rX   rc   ri   rm    r   r   r   r      sP    1	G9=>J
J"+
/3
7= &r   r   c                      e Zd ZdZd Zd Zd Zd Zd Zd Z	e
j                  j                  dd	d
d e
j                  d edd            g      d        ZdddddZdddddZeeeeiZe
j                  j                  dd	d
d e
j                  d edd            g      e
j                  j                  deeg      d               Ze
j                  j                  dg d      d        Ze
j                  j                  dd	d
 e
j                  d edd            dg      e
j                  j                  d g d!      d"               Zy#)$TestDifferentialEntropyz
    Vasicek results are compared with the R package vsgoftest.

    # library(vsgoftest)
    #
    # samp <- c(<values>)
    # entropy.estimate(x = samp, window = <window_length>)

    c                    t         j                  j                  d      }|j                  d      }|j	                  |j                               }t        j                  |d      }t        ||j	                  d             t        j                  |dd      }t        ||j	                  d             t        j                  |d	d      }t        ||j	                  d
             y )Nr   d   vasicekmethod- {?r<   )window_lengthrx    ?r$   =&?	r]   r^   RandomStatestandard_normalr   tolistr   differential_entropyr   )r   r   random_statevaluesr   s        r   !test_differential_entropy_vasicekz9TestDifferentialEntropy.test_differential_entropy_vasicek   s    yy,,Q/--c2FMMO,,,VIF,=!>?,,V14=?,=!>?,,V14=?,=!>?r   c                    t         j                  j                  d      }|j                  d      }|j	                  |j                               }t        j                  |dd      }|j	                  g d      }t        ||       t        j                  |ddd      }|j	                  g d      }t        ||       t        j                  |dd	d      }|j	                  g d
      }t        ||       y )Nr   rZ   ru   r<   rv   )r>   rx   )ry   gUZdpx?gE7L?)r>   rz   rx   )r{   gzG?gt?r$   )r|   gl+ɍj?go4d?r}   )r   r   r   r   r   refs         r   4test_differential_entropy_vasicek_2d_nondefault_axiszLTestDifferentialEntropy.test_differential_entropy_vasicek_2d_nondefault_axis   s    yy,,Q/--h7FMMO,,,V!INjjRS%,,V!14=?jjQR%,,V!14=?jjRS%r   c                    t         j                  j                  d      }|j                  d      }|j	                  |j                               }d}|j                  d   }dd|dz  |hD ]G  }|j                  ||      }t        t        |      5  t        j                  ||d	       d d d        I y # 1 sw Y   TxY w)
Nr   r   zhWindow length \({window_length}\) must be positive and less than half the sample size \({sample_size}\).r<   r\   r[   )rz   sample_sizerG   )rz   r>   )r]   r^   r~   r   r   r   shapeformatassert_raisesrJ   r   r   )r   r   r   r   	error_strr   rz   formatted_error_strs           r   ,test_differential_entropy_raises_value_errorzDTestDifferentialEntropy.test_differential_entropy_raises_value_error   s    yy,,Q/--h7FMMO,< 	
 ll1o ![!^[A 	M"+"2"2+' #3 #
 z1DE **"/ 	 s   B55B>	c                    t         j                  j                  d      }|j                  d      }|j	                  |j                               }t        j                  |d      }t        j                  |      }t        ||       y )Nr   )ru   rZ   r=   r}   )r   r   r   r   r   default_entropys         r   >test_base_differential_entropy_with_axis_0_is_equal_to_defaultzVTestDifferentialEntropy.test_base_differential_entropy_with_axis_0_is_equal_to_default   sf    yy,,Q/--h7FMMO,,,V!<44V<1r   c                    t         j                  j                  d      }|j                  d      }|j	                  |j                               }t        t        j                  |j                        t        j                  |d             y )Nr   r   r<   r=   )
r]   r^   r~   r   r   r   r   r   r   rU   )r   r   r   r   s       r   )test_base_differential_entropy_transposedzATestDifferentialEntropy.test_base_differential_entropy_transposed   sd    yy,,Q/--h7FMMO,&&vxx0&&vA6	
r   c                    t         j                  j                  d      }|j                  |j	                               }d}t        j                  t        |      5  t        j                  |d       d d d        d}t        j                  t        |      5  t        j                  |d       d d d        y # 1 sw Y   GxY w# 1 sw Y   y xY w)	Nre   z+`base` must be a positive number or `None`.rG   rk   r"   z`method` must be one of...z	ekki-ekkirw   )
r]   r^   randr   r   rI   r   rJ   r   r   rl   s       r   rm   z-TestDifferentialEntropy.test_input_validation   s    IINN2JJqxxz"?]]:W5 	3&&qr2	3 /]]:W5 	>&&q=	> 	>		3 	3	> 	>s   B:C:CCrx   rv   van esebrahimicorreaarray_api_strictzNeeds fancy indexing.r
   )marksc                 L   |dk(  rdnd}t         j                  j                  |d      }|j                  |j	                               }|j                  t        t         j                  j                                     }t        j                  ||      }t        ||d       y )	Nr   '  i@B r   sizer   rw   {Gzt?)rtol)	r   r   rvsr   r   floatr   r   r   )r   rx   r   nr   expectedrb   s          r   test_consistencyz(TestDifferentialEntropy.test_consistency   sz     x'EWjjnn!!n4jj&::eEJJ$6$6$89:((V<XE2r   )gMbX?灕Cl?)gA`"?g)\(?)gHzG?gy&1?)gMb?r   )rv   r   r   r   )gE?l?)gx?gOn?)gףp=
?g~jt?)gS?r   distc                    d\  }}}| j                   |   |   }|j                  |d         |j                  |d         }	}|j                  ||fd      }
|j                  |
j                               }
|j                  t	        |j                                     }t        j                  |
||d      }t        |j                  |j                  ||z
  dz              |d	       t        |j                  |d
      |	d	       y )N)r   2      r   r<   r   r\   )rz   rx   r>   r[   r   )atol)
correctiongMb`?)rmse_std_casesr   r   r   r   r   r   r   r   sqrtmeanstd)r   rx   r   r   repsr   mr   rmse_expectedstd_expectedr   true_entropyrb   s                r   test_rmse_stdz%TestDifferentialEntropy.test_rmse_std  s     "
a&&t,V4&(jj!&=rzz(ST+?V|hhT1IAh6jj&zz%"78((A06RA|);a(? @A%E	3sq1<eLr   z	n, method))r$   r   )rf   r   )i  rv   c                     t         j                  j                  |fd      }|j                  |j	                               }t        j
                  |      }t        j
                  ||      }t        ||       y )Nr   r   rw   )r   r   r   r   r   r   r   )r   r   rx   r   r   res1res2s          r   test_method_autoz(TestDifferentialEntropy.test_method_auto#  s\     jjnn1$Qn7jj&))#.))#f=d#r   dtype)Nfloat32float64c                    g d}t        |t        |      d       }t        |t        |      |j                  d      j                        }t	        j
                  |j                  ||      |      }t	        j
                  |j                  ||j                        |      }t        ||j                  ||      d          y )N)r<   r<   r[   rZ   rZ         r      r   r$   	   re   rg   r/   )r   rw   rq   )getattrstrr   r   r   r   r   r   )	r   r   rx   r   r1   dtype_in	dtype_outrb   r   s	            r   test_dtypes_gh21192z+TestDifferentialEntropy.test_dtypes_gh21192/  s     92s5z40BE
BJJrN,@,@A	((AX)FvV((ARZZ)HQWXRZZ9Z=bABr   N)rn   ro   rp   __doc__r   r   r   r   r   rm   rI   markparametrizeparamskip_xp_backendsr   norm_rmse_std_casesexpon_rmse_std_casesr   r   r   r   r   r   rq   r   r   rs   rs   x   s   @"&(42

> [[X"#5*AC	
		( 	3	3 '5%3%3'5	 (6&4&4(6	 /13N [[X"#5*AC	
		( 	 [[VdE]3M 4	M  [[[ + 
$
$ [[X"#5>UV	
 	(  [[W&BC	C D	Cr   rs   )r)   rI   r   r   numpyr]   scipyr   scipy.statsr   r   scipy._lib._array_api_no_0dr   r   r	   r   r   r   rs   rq   r   r   <module>r      s      *   #9 9 ;;// l3MNf& f& Of&R l3MNIC IC OICr   