o
    i e                     @   s`   d Z ddlZddlZddlmZ 	dddZdd Zdd	 Zd
d Z								dddZ
dS )z'Utilities for testing random variables.    N)special_mathc                 C   sT  g }g }g }t d|d D ]P}t| jdkr%| jdd|d | d  }n| dd|d | d df }|tj|| dd ||| ||d| ||d  t|  qg }	t d|d D ]@}||d  |t| j	j
  }
t| j	j}t|
dksJ t|
|k ||
}
|	t||d  ||d   t|
  qg|	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    r!   Y/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/kernel_tests/random/util.pytest_moment_matching   s6   "
r#   c                 C   sJ   t | } t| }t j| |dd\}}|t| }t t || | S )zPearson's Chi-squared test.)r   r   )binsr   )r   ravelr   	histogramfloatsumsquare)xr$   nr&   _expectedr!   r!   r"   chi_squaredK   s
   
r.   c                 C   s"   ddt tj| td   S )zDCumulative distribution function for a standard normal distribution.      ?r   )r   	vectorizematherfr   r*   r!   r!   r"   
normal_cdfT   s   "r4   c              	   C   sv   t t | } t| }t d||}t d| d t t|  d||  d t dt|    }| ||  S )z9Anderson-Darling test for a standard normal distribution.r   r   )r   sortr%   r   linspacer(   logr4   )r*   r+   r   zr!   r!   r"   anderson_darlingY   s    r9   Mb@?-C6J?MbP?c                 C   sv  dd }dd }dd }d}d}d	}d
}|dur|}|dur |}|dur&|}|dur,|}|| | }|| | }|||| }| ||k  | | ||k  | ||||| | |  }|t}t|}||||d |||||| d |  }t|}||||	d |d d||| |||  |  |||| | d   }t|}||||
d dS )z1Tests truncated normal distribution's statistics.c                 S   s   dt |  t d  S )Nr/   r   )r1   erfcr   r3   r!   r!   r"   _normal_cdfo   s   z*test_truncated_normal.<locals>._normal_cdfc                 S   s$   t | d  d t dt j  S )Nr          @)r1   expr   pir3   r!   r!   r"   
normal_pdfr   s   $z)test_truncated_normal.<locals>.normal_pdfc                 S   s
   t | S )N)r   ndtrir3   r!   r!   r"   probitu   s   
z%test_truncated_normal.<locals>.probitg       r?   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_rtolr>   rB   rD   abmusigmaalphabetar8   expected_meanactual_meanexpected_medianactual_medianexpected_varianceactual_variancer!   r!   r"   test_truncated_normalc   sV   




r`   )r   )NNNNr:   r;   r<   )__doc__r1   numpyr   #tensorflow.python.ops.distributionsr   r#   r.   r4   r9   r`   r!   r!   r!   r"   <module>   s"   
3	