
    BVh                     V    d Z ddlZddlZddlmZ 	 d	dZd Zd Zd Z		 	 	 	 	 	 	 d
dZ
y)z'Utilities for testing random variables.    N)special_mathc           	         g }g }g }t        d|dz         D ]  }t        | j                        dk(  r| j                  dd|dz
  |z  dz      }n| dd|dz
  |z  dz   df   }|j	                  t        j                  ||z  d             |j	                  |j                  |             |j	                  |j                  d|z        |j                  |      dz  z
  t        |      z          g }	t        d|dz         D ]  }||dz
     |t        j                  | j                        j                  z  z   }
t        j                  | j                        j                  }t        j                  |
dkD        sJ t        j                  |
|k  ||
      }
|	j	                  t        ||dz
     ||dz
     z
  t        j                  |
      z                |	S )a\  Return z-test scores for sample moments to match analytic moments.

  Given `samples`, check that the first sample `number_moments` match
  the given  `dist` moments by doing a z-test.

  Args:
    samples: Samples from target distribution.
    number_moments: Python `int` describing how many sample moments to check.
    dist: SciPy distribution object that provides analytic moments.
    stride: Distance between samples to check for statistical properties.
      A stride of 0 means to use all samples, while other strides test for
      spatial correlation.
  Returns:
    Array of z_test scores.
        N.r   )axis)rangelenshapeflatappendnpmeanmomentfinfodtypeepstinyallwhereabssqrt)samplesnumber_momentsdiststridesample_momentsexpected_momentsvariance_sample_momentsistrided_rangez_test_scorestotal_variancer   s               Z/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/kernel_tests/random/util.pytest_moment_matchingr$      s   * .NQ&' Ia
7==Qll#9a!ev%5%9#9:m4Q& 01 44c9:m"''-"2;<DKKN+""	QU	dkk!n1	1S5GGII -NQ&' a 	 A&	BHHW]]#'''	(  88GMM"''D66.1$%%%XXnt3T>JN	A	!1!a%!8	8BGG= 	  
    c                     t        j                  |       } t        |       }t        j                  | |d      \  }}|t	        |      z  }t        j
                  t        j                  ||z
        |z        S )zPearson's Chi-squared test.)r   r   )binsr   )r   ravelr	   	histogramfloatsumsquare)xr'   nr)   _expecteds         r#   chi_squaredr1   K   s]    hhqk!	!f!ad&9,)Qt_(			)h./(:	;;r%   c                     dd t        j                  t        j                        | t        j                  d      z        z  z   S )zDCumulative distribution function for a standard normal distribution.      ?r   )r   	vectorizematherfr   r-   s    r#   
normal_cdfr8   T   s4    	s+R\\$((+A		!,<==	==r%   c           
      x   t        j                  t        j                  |             } t        |       }t        j                  d||      }t        j
                  d|z  dz
  t        j                  t        |             z  d||z
  z  dz   t        j                  dt        |       z
        z  z         }| ||z  z
  S )z9Anderson-Darling test for a standard normal distribution.r   r   )r   sortr(   r	   linspacer+   logr8   )r-   r.   r   zs       r#   anderson_darlingr>   Y   s    ggbhhqk!	!f!kk!Q!ffa!eai266*Q-001q5kAoJqM(9!::; <!
a!er%   c                    d }d }d }d}d}d}d}||}||}||}||}||z
  |z  }||z
  |z  } ||       ||      z
  } | ||k\  j                         |        | ||k  j                         |       | ||       ||      z
  |z  |z  z   }|j                  t              }t        j                  |      } ||||	       | | ||       ||      z   dz        |z  z   }t        j
                  |      } ||||		       |d
z  d| ||      z  | ||      z  z
  |z  z    ||       ||      z
  |z  d
z  z
  z  }t        j                  |      } ||||
       y)z1Tests truncated normal distribution's statistics.c                 `    dt        j                  |  t        j                  d      z        z  S )Nr3   r   )r5   erfcr   r7   s    r#   _normal_cdfz*test_truncated_normal.<locals>._normal_cdfo   s%    		1"tyy|+,,,r%   c                     t        j                  | dz   dz        t        j                  dt         j                  z        z  S )Nr          @)r5   expr   pir7   s    r#   
normal_pdfz)test_truncated_normal.<locals>.normal_pdfr   s1    88adGbL!DIIa$''k$:::r%   c                 ,    t        j                  |       S )N)r   ndtrir7   s    r#   probitz%test_truncated_normal.<locals>.probitu   s    a  r%   g       rD   g        g      ?N)atolr   r   )rtol)r+   astyper*   r   r   medianvar)assert_equalassert_all_closer.   ymeansstddevsminvalsmaxvals	mean_atolmedian_atolvariance_rtolrB   rG   rJ   abmusigmaalphabetar=   expected_meanactual_meanexpected_medianactual_medianexpected_varianceactual_variances                              r#   test_truncated_normalrf   c   s   -;! 
!!	"
%AA
	BEr6U
%
b&E	$$+e,,!Q||~q!Q||~q!
 
5)Jt,<<AEII-hhuo!
+;I>5K--357<= =/))A,-=/DQh!z%  4*T*:#::a?#Aez$//1
4qD9#9 : FF1I/r%   )r   )NNNNgMb@?g-C6J?gMbP?)__doc__r5   numpyr   #tensorflow.python.ops.distributionsr   r$   r1   r8   r>   rf    r%   r#   <module>rk      sJ    .   < 	0f<>
 !%"&"&"&$(&*(,Ar%   