<s> import math <EOL> class Histogram ( object ) : <EOL> def __init__ ( self , bin_scheme ) : <EOL> self . _hist = [ <NUM_LIT:0.0> ] * bin_scheme . bins <EOL> self . _count = <NUM_LIT:0.0> <EOL> self . _bin_scheme = bin_scheme <EOL> def record ( self , value ) : <EOL> self . _hist [ self . _bin_scheme . to_bin ( value ) ] += <NUM_LIT:1.0> <EOL> self . _count += <NUM_LIT:1.0> <EOL> def value ( self , quantile ) : <EOL> if self . _count == <NUM_LIT:0.0> : <EOL> return float ( '<STR_LIT>' ) <EOL> _sum = <NUM_LIT:0.0> <EOL> quant = float ( quantile ) <EOL> for i , value in enumerate ( self . _hist [ : - <NUM_LIT:1> ] ) : <EOL> _sum += value <EOL> if _sum / self . _count > quant : <EOL> return self . _bin_scheme . from_bin ( i ) <EOL> return float ( '<STR_LIT>' ) <EOL> @ property <EOL> def counts ( self ) : <EOL> return self . _hist <EOL> def clear ( self ) : <EOL> for i in range ( self . _hist ) : <EOL> self . _hist [ i ] = <NUM_LIT:0.0> <EOL> self . _count = <NUM_LIT:0> <EOL> def __str__ ( self ) : <EOL> values = [ '<STR_LIT>' % ( self . _bin_scheme . from_bin ( i ) , value ) for <EOL> i , value in enumerate ( self . _hist [ : - <NUM_LIT:1> ] ) ] <EOL> values . append ( '<STR_LIT>' % ( float ( '<STR_LIT>' ) , self . _hist [ - <NUM_LIT:1> ] ) ) <EOL> return '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( values ) <EOL> class ConstantBinScheme ( object ) : <EOL> def __init__ ( self , bins , min_val , max_val ) : <EOL> if bins < <NUM_LIT:2> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _min = float ( min_val ) <EOL> self . _max = float ( max_val ) <EOL> self . _bins = int ( bins ) <EOL> self . _bucket_width = ( max_val - min_val ) / ( bins - <NUM_LIT:2> ) <EOL> @ property <EOL> def bins ( self ) : <EOL> return self . _bins <EOL> def from_bin ( self , b ) : <EOL> if b == <NUM_LIT:0> : <EOL> return float ( '<STR_LIT>' ) <EOL> elif b == self . _bins - <NUM_LIT:1> : <EOL> return float ( '<STR_LIT>' ) <EOL> else : <EOL> return self . _min + ( b - <NUM_LIT:1> ) * self . _bucket_width <EOL> def to_bin ( self , x ) : <EOL> if x < self . _min : <EOL> return <NUM_LIT:0> <EOL> elif x > self . _max : <EOL> return self . _bins - <NUM_LIT:1> <EOL> else : <EOL> return int ( ( ( x - self . _min ) / self . _bucket_width ) + <NUM_LIT:1> ) <EOL> class LinearBinScheme ( object ) : <EOL> def __init__ ( self , num_bins , max_val ) : <EOL> self . _bins = num_bins <EOL> self . _max = max_val <EOL> self . _scale = max_val / ( num_bins * ( num_bins - <NUM_LIT:1> ) / <NUM_LIT:2> ) <EOL> @ property <EOL> def bins ( self ) : <EOL> return self . _bins <EOL> def from_bin ( self , b ) : <EOL> if b == self . _bins - <NUM_LIT:1> : <EOL> return float ( '<STR_LIT>' ) <EOL> else : <EOL> unscaled = ( b * ( b + <NUM_LIT:1.0> ) ) / <NUM_LIT> <EOL> return unscaled * self . _scale <EOL> def to_bin ( self , x ) : <EOL> if x < <NUM_LIT:0.0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> elif x > self . _max : <EOL> return self . _bins - <NUM_LIT:1> <EOL> else : <EOL> scaled = x / self . _scale <EOL> return int ( - <NUM_LIT:0.5> + math . sqrt ( <NUM_LIT> * scaled + <NUM_LIT> ) ) </s>
