def binned_binom_proportion(x, success, bins=10, range=None, conf=0.68269, interval=u'wilson'):
 x = np.ravel(x)
 success = np.ravel(success).astype(np.bool)
 if (x.shape != success.shape):
  raise ValueError(u'sizes of x and success must match')
 (n, bin_edges) = np.histogram(x, bins=bins, range=range)
 (k, bin_edges) = np.histogram(x[success], bins=bin_edges)
 bin_ctr = ((bin_edges[:(-1)] + bin_edges[1:]) / 2.0)
 bin_halfwidth = (bin_ctr - bin_edges[:(-1)])
 valid = (n > 0)
 bin_ctr = bin_ctr[valid]
 bin_halfwidth = bin_halfwidth[valid]
 n = n[valid]
 k = k[valid]
 p = (k / n)
 bounds = binom_conf_interval(k, n, conf=conf, interval=interval)
 perr = np.abs((bounds - p))
 return (bin_ctr, bin_halfwidth, p, perr)

def binned_binom_proportion ( x , success , bins = 10 , range = None , conf = 0.68269 , interval = u'wilson' ) : x = np . ravel ( x ) success = np . ravel ( success ) . astype ( np . bool ) if ( x . shape != success . shape ) : raise ValueError ( u'sizes of x and success must match' ) ( n , bin_edges ) = np . histogram ( x , bins = bins , range = range ) ( k , bin_edges ) = np . histogram ( x [ success ] , bins = bin_edges ) bin_ctr = ( ( bin_edges [ : ( - 1 ) ] + bin_edges [ 1 : ] ) / 2.0 ) bin_halfwidth = ( bin_ctr - bin_edges [ : ( - 1 ) ] ) valid = ( n > 0 ) bin_ctr = bin_ctr [ valid ] bin_halfwidth = bin_halfwidth [ valid ] n = n [ valid ] k = k [ valid ] p = ( k / n ) bounds = binom_conf_interval ( k , n , conf = conf , interval = interval ) perr = np . abs ( ( bounds - p ) ) return ( bin_ctr , bin_halfwidth , p , perr )
binomial proportion and confidence interval in bins of a continuous variable x .