U
    yc                     @   s   d dl Z d dlZdd ZdeeeedddZdeeeeddd	ZdeeeeedddZeZdddZdddZ	dddZ
d ddZd!ddZd"ddZdS )#    Nc                 C   s&   ||  }t |d|  |  }||fS )z4finds mu and sigma corresponding to a Binomial(n, p)   )mathsqrt)npmusigma r	   B/home/cheung/ML/DS-from-scratch/07-hypo-testing/lib/HypoTesting.py normal_approximation_to_binomial	   s    r   r   )xr   r   returnc                 C   s(   t | | d  d |d  t|  S )N   )r   expZSQRT_TWO_PIr   r   r   r	   r	   r
   
normal_pdf   s    r   c                 C   s$   dt | | t d |  d S )Nr   r   )r   erfr   r   r	   r	   r
   
normal_cdf   s    r   h㈵>)r   r   r   	tolerancer   c                 C   sd   |dks|dkr$||t | |d  S d}d}|| |kr`|| d }t|}|| k rZ|}q,|}q,|S )z,Find approximate inverse using binary searchr   r   )r   g      $g      $@r   )inverse_normal_cdfr   )r   r   r   r   Zlow_zZhi_zZmid_zZmid_pr	   r	   r
   r   $   s    r   c                 C   s   dt | || S Nr   r   )lor   r   r	   r	   r
   normal_probability_aboveD   s    r   c                 C   s   t |||t | || S )Nr   r   hir   r   r	   r	   r
   normal_probability_betweenJ   s    r   c                 C   s   dt | ||| S r   )r   r   r	   r	   r
   normal_probability_outsideP   s    r   c                 C   s   t | ||S )z9
    returns the z for which P(Z <= z) = probability
    r   probabilityr   r   r	   r	   r
   normal_upper_boundX   s    r"   c                 C   s   t d|  ||S )z9
    returns the z for which P(Z >= z) = probability
    r   r   r    r	   r	   r
   normal_lower_bounda   s    r#   c                 C   s,   d|  d }t |||}t|||}||fS )zb
    returns the symmetric (about the mean) bounds
    that contain the specified probability
    r   r   )r#   r"   )r!   r   r   Ztail_probabilityZupper_boundZlower_boundr	   r	   r
   normal_two_sided_boundsl   s    r$   )r   r   )r   r   )r   r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   randomr   floatr   r   r   Znormal_probability_belowr   r   r   r"   r#   r$   r	   r	   r	   r
   <module>   s&   	    



	
