<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import collections <EOL> import math <EOL> import operator <EOL> import functools <EOL> from fractions import Fraction <EOL> from decimal import Decimal <EOL> from . exceptions import StatisticsError <EOL> from . py3comp import xrange , iteritems <EOL> def isfinite ( n ) : <EOL> """<STR_LIT>""" <EOL> return not ( math . isinf ( n ) or math . isnan ( n ) ) <EOL> def sum ( data , start = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> n , d = exact_ratio ( start ) <EOL> T = type ( start ) <EOL> partials = { d : n } <EOL> coerce_types_ = coerce_types <EOL> exact_ratio_ = exact_ratio <EOL> partials_get = partials . get <EOL> for x in data : <EOL> T = coerce_types_ ( T , type ( x ) ) <EOL> n , d = exact_ratio_ ( x ) <EOL> partials [ d ] = partials_get ( d , <NUM_LIT:0> ) + n <EOL> if None in partials : <EOL> assert issubclass ( T , ( float , Decimal ) ) <EOL> assert not isfinite ( partials [ None ] ) <EOL> return T ( partials [ None ] ) <EOL> total = Fraction ( ) <EOL> for d , n in sorted ( partials . items ( ) ) : <EOL> total += Fraction ( n , d ) <EOL> if issubclass ( T , int ) : <EOL> assert total . denominator == <NUM_LIT:1> <EOL> return T ( total . numerator ) <EOL> if issubclass ( T , Decimal ) : <EOL> return T ( total . numerator ) / total . denominator <EOL> return T ( total ) <EOL> def exact_ratio ( x ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> return x . numerator , x . denominator <EOL> except AttributeError : <EOL> try : <EOL> return x . as_integer_ratio ( ) <EOL> except AttributeError : <EOL> try : <EOL> return decimal_to_ratio ( x ) <EOL> except AttributeError : <EOL> msg = "<STR_LIT>" <EOL> raise TypeError ( msg . format ( type ( x ) . __name__ ) ) <EOL> except ( OverflowError , ValueError ) : <EOL> return ( x , None ) <EOL> def decimal_to_ratio ( d ) : <EOL> """<STR_LIT>""" <EOL> sign , digits , exp = d . as_tuple ( ) <EOL> if exp in ( '<STR_LIT:F>' , '<STR_LIT:n>' , '<STR_LIT:N>' ) : <EOL> assert not d . is_finite ( ) <EOL> raise ValueError <EOL> num = <NUM_LIT:0> <EOL> for digit in digits : <EOL> num = num * <NUM_LIT:10> + digit <EOL> if sign : <EOL> num = - num <EOL> den = <NUM_LIT:10> ** - exp <EOL> return ( num , den ) <EOL> def coerce_types ( T1 , T2 ) : <EOL> """<STR_LIT>""" <EOL> if T1 is T2 : return T1 <EOL> if T1 is int : return T2 <EOL> if T2 is int : return T1 <EOL> if issubclass ( T2 , T1 ) : return T2 <EOL> if issubclass ( T1 , T2 ) : return T1 <EOL> if issubclass ( T2 , float ) : return T2 <EOL> if issubclass ( T1 , float ) : return T1 <EOL> if T1 . __base__ is T2 . __base__ : return T2 <EOL> raise TypeError ( '<STR_LIT>' % ( T1 , T2 ) ) <EOL> def counts ( data ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> table = collections . Counter ( data ) . most_common ( ) <EOL> if not table : <EOL> return table <EOL> maxfreq = table [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> for i in range ( <NUM_LIT:1> , len ( table ) ) : <EOL> if table [ i ] [ <NUM_LIT:1> ] != maxfreq : <EOL> table = table [ : i ] <EOL> break <EOL> return table <EOL> def mean ( data ) : <EOL> """<STR_LIT>""" <EOL> if iter ( data ) is data : <EOL> data = list ( data ) <EOL> n = len ( data ) <EOL> if n < <NUM_LIT:1> : <EOL> raise StatisticsError ( '<STR_LIT>' ) <EOL> return sum ( data ) / n <EOL> def median ( data ) : <EOL> """<STR_LIT>""" <EOL> data = sorted ( data ) <EOL> n = len ( data ) <EOL> if n == <NUM_LIT:0> : <EOL> raise StatisticsError ( "<STR_LIT>" ) <EOL> if n % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> return data [ n // <NUM_LIT:2> ] <EOL> else : <EOL> i = n // <NUM_LIT:2> <EOL> return ( data [ i - <NUM_LIT:1> ] + data [ i ] ) / <NUM_LIT:2> <EOL> def median_low ( data ) : <EOL> """<STR_LIT>""" <EOL> data = sorted ( data ) <EOL> n = len ( data ) <EOL> if n == <NUM_LIT:0> : <EOL> raise StatisticsError ( "<STR_LIT>" ) <EOL> if n % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> return data [ n // <NUM_LIT:2> ] <EOL> else : <EOL> return data [ n // <NUM_LIT:2> - <NUM_LIT:1> ] <EOL> def median_high ( data ) : <EOL> """<STR_LIT>""" <EOL> data = sorted ( data ) <EOL> n = len ( data ) <EOL> if n == <NUM_LIT:0> : <EOL> raise StatisticsError ( "<STR_LIT>" ) <EOL> return data [ n // <NUM_LIT:2> ] <EOL> def mode ( data ) : <EOL> """<STR_LIT>""" <EOL> table = counts ( data ) <EOL> if len ( table ) == <NUM_LIT:1> : <EOL> return table [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> elif table : <EOL> raise StatisticsError ( <EOL> '<STR_LIT>' % len ( table ) <EOL> ) <EOL> else : <EOL> raise StatisticsError ( '<STR_LIT>' ) <EOL> def _ss ( data , c = None ) : <EOL> """<STR_LIT>""" <EOL> if c is None : <EOL> c = mean ( data ) <EOL> ss = sum ( ( x - c ) ** <NUM_LIT:2> for x in data ) <EOL> ss -= sum ( ( x - c ) for x in data ) ** <NUM_LIT:2> / len ( data ) <EOL> assert not ss < <NUM_LIT:0> , '<STR_LIT>' % ss <EOL> return ss <EOL> def variance ( data , xbar = None ) : <EOL> """<STR_LIT>""" <EOL> if iter ( data ) is data : <EOL> data = list ( data ) <EOL> n = len ( data ) <EOL> if n < <NUM_LIT:2> : <EOL> raise StatisticsError ( '<STR_LIT>' ) <EOL> ss = _ss ( data , xbar ) <EOL> return ss / ( n - <NUM_LIT:1> ) <EOL> def pvariance ( data , mu = None ) : <EOL> """<STR_LIT>""" <EOL> if iter ( data ) is data : <EOL> data = list ( data ) <EOL> n = len ( data ) <EOL> if n < <NUM_LIT:1> : <EOL> raise StatisticsError ( '<STR_LIT>' ) <EOL> ss = _ss ( data , mu ) <EOL> return ss / n <EOL> def stdev ( data , xbar = None ) : <EOL> """<STR_LIT>""" <EOL> var = variance ( data , xbar ) <EOL> try : <EOL> return var . sqrt ( ) <EOL> except AttributeError : <EOL> return math . sqrt ( var ) <EOL> def pstdev ( data , mu = None ) : <EOL> """<STR_LIT>""" <EOL> var = pvariance ( data , mu ) <EOL> try : <EOL> return var . sqrt ( ) <EOL> except AttributeError : <EOL> return math . sqrt ( var ) <EOL> def geometric_mean ( data ) : <EOL> """<STR_LIT>""" <EOL> if not data : <EOL> raise StatisticsError ( '<STR_LIT>' ) <EOL> data = [ x if x > <NUM_LIT:0> else math . e if x == <NUM_LIT:0> else <NUM_LIT:1.0> for x in data ] <EOL> return math . pow ( math . fabs ( functools . reduce ( operator . mul , data ) ) , <NUM_LIT:1.0> / len ( data ) ) <EOL> def harmonic_mean ( data ) : <EOL> """<STR_LIT>""" <EOL> if not data : <EOL> raise StatisticsError ( '<STR_LIT>' ) <EOL> return len ( data ) / sum ( map ( lambda x : <NUM_LIT:1.0> / x if x else <NUM_LIT:0.0> , data ) ) <EOL> def skewness ( data ) : <EOL> """<STR_LIT>""" <EOL> if not data : <EOL> raise StatisticsError ( '<STR_LIT>' ) <EOL> size = len ( data ) <EOL> sd = stdev ( data ) ** <NUM_LIT:3> <EOL> if not sd : <EOL> return <NUM_LIT:0.0> <EOL> mn = mean ( data ) <EOL> return sum ( map ( lambda x : ( ( x - mn ) ** <NUM_LIT:3> / sd ) , data ) ) / size <EOL> def kurtosis ( data ) : <EOL> """<STR_LIT>""" <EOL> if not data : <EOL> raise StatisticsError ( '<STR_LIT>' ) <EOL> size = len ( data ) <EOL> sd = stdev ( data ) ** <NUM_LIT:4> <EOL> if not sd : <EOL> return <NUM_LIT:0.0> <EOL> mn = mean ( data ) <EOL> return sum ( map ( lambda x : ( ( x - mn ) ** <NUM_LIT:4> / sd ) , data ) ) / size - <NUM_LIT:3> <EOL> def percentile ( data , n ) : <EOL> """<STR_LIT>""" <EOL> size = len ( data ) <EOL> idx = ( n / <NUM_LIT> ) * size - <NUM_LIT:0.5> <EOL> if idx < <NUM_LIT:0> or idx > size : <EOL> raise StatisticsError ( "<STR_LIT>" . format ( size , n ) ) <EOL> return data [ int ( idx ) ] <EOL> def get_histogram ( data ) : <EOL> """<STR_LIT>""" <EOL> count = len ( data ) <EOL> if count < <NUM_LIT:2> : <EOL> raise StatisticsError ( '<STR_LIT>' . format ( count ) ) <EOL> min_ = data [ <NUM_LIT:0> ] <EOL> max_ = data [ - <NUM_LIT:1> ] <EOL> std = stdev ( data ) <EOL> bins = get_histogram_bins ( min_ , max_ , std , count ) <EOL> res = { x : <NUM_LIT:0> for x in bins } <EOL> for value in data : <EOL> for bin_ in bins : <EOL> if value <= bin_ : <EOL> res [ bin_ ] += <NUM_LIT:1> <EOL> break <EOL> return sorted ( iteritems ( res ) ) <EOL> def get_histogram_bins ( min_ , max_ , std , count ) : <EOL> """<STR_LIT>""" <EOL> width = _get_bin_width ( std , count ) <EOL> count = int ( round ( ( max_ - min_ ) / width ) + <NUM_LIT:1> ) <EOL> if count : <EOL> bins = [ i * width + min_ for i in xrange ( <NUM_LIT:1> , count + <NUM_LIT:1> ) ] <EOL> else : <EOL> bins = [ min_ ] <EOL> return bins <EOL> def _get_bin_width ( stdev , count ) : <EOL> """<STR_LIT>""" <EOL> w = int ( round ( ( <NUM_LIT> * stdev ) / ( count ** ( <NUM_LIT:1.0> / <NUM_LIT:3> ) ) ) ) <EOL> if w : <EOL> return w <EOL> else : <EOL> return <NUM_LIT:1> </s>
