o
    ?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   )Zaxis)rangelenshapeZflatappendnpmeanZmomentZfinfoZdtypeZepstinyallwhereabssqrt)ZsamplesZnumber_momentsdistZstrideZsample_momentsZexpected_momentsZvariance_sample_momentsiZstrided_rangeZz_test_scoresZtotal_variancer    r   k/home/www/facesmatcher.com/pyenv/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sumZ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	   Z	vectorizematherfr   r   r   r   r   
normal_cdfT   s   "r#   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   Zlinspacer   logr#   )r   r   r   zr   r   r   anderson_darlingY   s    r'   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   )r    erfcr   r"   r   r   r   _normal_cdfo   s   z*test_truncated_normal.<locals>._normal_cdfc                 S   s$   t | d  d t dt j  S )Nr          @)r    expr   pir"   r   r   r   
normal_pdfr   s   $z)test_truncated_normal.<locals>.normal_pdfc                 S   s
   t | S )N)r   Zndtrir"   r   r   r   probitu   s   
z%test_truncated_normal.<locals>.probitg       r-   g        g      ?N)Zatolr   r   )Zrtol)r   Zastyper   r	   r
   Zmedianvar)Zassert_equalZassert_all_closer   yZmeansZstddevsZminvalsZmaxvalsZ	mean_atolZmedian_atolZvariance_rtolr,   r0   r1   abmusigmaalphabetar&   Zexpected_meanZactual_meanZexpected_medianZactual_medianZexpected_varianceZactual_variancer   r   r   test_truncated_normalc   sV   




r:   )r   )NNNNr(   r)   r*   )__doc__r    numpyr	   Z#tensorflow.python.ops.distributionsr   r   r   r#   r'   r:   r   r   r   r   <module>   s"   
3	