B
    u9aP                 @   s4  d ddddddddd	d
dgZ ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZ G dd  d eZd4ddZdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd5d$d%Zd&d	 Zd'd Zd(d Zd)d Zd*d Zd6d,dZd-d
 Zd7d.d/Zd8d0dZ d9d1dZ!d:d2dZ"d;d3dZ#dS )<StatisticsErrorpstdev	pvariancestdevvariancemedian
median_lowmedian_highmedian_groupedmeanmodeharmonic_mean    N)Fraction)Decimal)groupby)bisect_leftbisect_rightc               @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r    /usr/lib/python3.7/statistics.pyr   b   s   c             C   s   d}t |\}}||i}|j}ttt|}xRt| tD ]D\}}	t||}x0tt |	D ]"\}}|d7 }||d| ||< qVW q8W d |kr|d  }
ntdd t|	 D }
||
|fS )Nr      c             s   s   | ]\}}t ||V  qd S )N)r   ).0dnr   r   r   	<genexpr>   s    z_sum.<locals>.<genexpr>)
_exact_ratioget_coerceinttyper   mapsumsorteditems)datastartcountr   r   ZpartialsZpartials_getTtypvaluestotalr   r   r   _sumh   s    $
r-   c             C   s(   y|   S  tk
r"   t| S X d S )N)Z	is_finiteAttributeErrormathZisfinite)xr   r   r   	_isfinite   s    r1   c             C   s   | |kr| S |t ks|tkr | S | t kr,|S t|| r:|S t| |rH| S t| t rV|S t|t rd| S t| tr|t|tr||S t| trt|tr| S d}t|| j|jf d S )Nz"don't know how to coerce %s and %s)r    bool
issubclassr   float	TypeErrorr   )r)   Smsgr   r   r   r      s(       
 
 
 
 r   c             C   s   ydt | tkst | tkr"|  S y| j| jfS  tk
r`   y|  S  tk
rZ   Y nX Y nX W n ttfk
r   | d fS X d}t	|
t | jd S )Nz0can't convert type '{}' to numerator/denominator)r!   r4   r   as_integer_ratio	numeratordenominatorr.   OverflowError
ValueErrorr5   formatr   )r0   r7   r   r   r   r      s    
r   c             C   sj   t | |kr| S t|tr(| jdkr(t}y|| S  tk
rd   t|tr^|| j|| j S  Y nX d S )Nr   )r!   r3   r    r:   r4   r5   r   r9   )valuer)   r   r   r   _convert   s    
r?   c             C   s`   t t|  }|s|S |d d }x4tdt|D ]"}|| d |kr6|d | }P q6W |S )Nr   r   )collectionsCounteritermost_commonrangelen)r&   tableZmaxfreqir   r   r   _counts   s    rH   c             C   s.   t | |}|t| kr&| | |kr&|S td S )N)r   rE   r<   )ar0   rG   r   r   r   
_find_lteq  s    
rJ   c             C   s>   t | ||d}|t| d kr6| |d  |kr6|d S td S )N)lor   )r   rE   r<   )rI   lr0   rG   r   r   r   
_find_rteq  s     rM   negative valuec             c   s(   x"| D ]}|dk rt ||V  qW d S )Nr   )r   )r+   errmsgr0   r   r   r   	_fail_neg  s    
rP   c             C   sH   t | | krt| } t| }|dk r,tdt| \}}}t|| |S )Nr   z%mean requires at least one data point)rB   listrE   r   r-   r?   )r&   r   r)   r,   r(   r   r   r   r
   "  s    c             C   s   t | | krt| } d}t| }|dk r2tdn<|dkrn| d }t|tjtfrf|dk rbt||S tdy"t	dd t
| |D \}}}W n tk
r   dS X t|| |S )Nz.harmonic mean does not support negative valuesr   z.harmonic_mean requires at least one data pointr   zunsupported typec             s   s   | ]}d | V  qdS )r   Nr   )r   r0   r   r   r   r   c  s    z harmonic_mean.<locals>.<genexpr>)rB   rQ   rE   r   
isinstancenumbersZRealr   r5   r-   rP   ZeroDivisionErrorr?   )r&   rO   r   r0   r)   r,   r(   r   r   r   r   <  s$    
"c             C   s\   t | } t| }|dkr td|d dkr8| |d  S |d }| |d  | |  d S d S )Nr   zno median for empty data   r   )r$   rE   r   )r&   r   rG   r   r   r   r   k  s    c             C   sL   t | } t| }|dkr td|d dkr8| |d  S | |d d  S d S )Nr   zno median for empty datarU   r   )r$   rE   r   )r&   r   r   r   r   r     s    c             C   s,   t | } t| }|dkr td| |d  S )Nr   zno median for empty datarU   )r$   rE   r   )r&   r   r   r   r   r     s
    r   c       
      C   s   t | } t| }|dkr"tdn|dkr2| d S | |d  }x*||fD ]}t|ttfrHtd| qHW y||d  }W n( tk
r   t|t|d  }Y nX t| |}t	| ||}|}|| d }	|||d |  |	  S )Nr   zno median for empty datar   rU   zexpected number but got %r)
r$   rE   r   rR   strbytesr5   r4   rJ   rM   )
r&   intervalr   r0   objLl1l2Zcffr   r   r   r	     s&    

c             C   sB   t | }t|dkr |d d S |r6tdt| ntdd S )Nr   r   z.no unique mode; found %d equally common valueszno mode for empty data)rH   rE   r   )r&   rF   r   r   r   r     s    c                sd    d krt |  t fdd| D \}}}t fdd| D \}}}||d t|  8 }||fS )Nc             3   s   | ]}|  d  V  qdS )rU   Nr   )r   r0   )cr   r   r     s    z_ss.<locals>.<genexpr>c             3   s   | ]}|  V  qd S )Nr   )r   r0   )r^   r   r   r     s    rU   )r
   r-   rE   )r&   r^   r)   r,   r(   UZtotal2Zcount2r   )r^   r   _ss  s    r`   c             C   sL   t | | krt| } t| }|dk r,tdt| |\}}t||d  |S )NrU   z*variance requires at least two data pointsr   )rB   rQ   rE   r   r`   r?   )r&   xbarr   r)   ssr   r   r   r   !  s    &c             C   sH   t | | krt| } t| }|dk r,tdt| |\}}t|| |S )Nr   z*pvariance requires at least one data point)rB   rQ   rE   r   r`   r?   )r&   mur   r)   rb   r   r   r   r   P  s    'c             C   s2   t | |}y| S  tk
r,   t|S X d S )N)r   sqrtr.   r/   )r&   ra   varr   r   r   r     s
    	
c             C   s2   t | |}y| S  tk
r,   t|S X d S )N)r   rd   r.   r/   )r&   rc   re   r   r   r   r     s
    	
)r   )rN   )r   )N)N)N)N)N)$__all__r@   r/   rS   Z	fractionsr   Zdecimalr   	itertoolsr   Zbisectr   r   r<   r   r-   r1   r   r   r?   rH   rJ   rM   rP   r
   r   r   r   r   r	   r   r`   r   r   r   r   r   r   r   r   <module>O   s@   

: 

/
7*

/
0
