Skip to content

Instantly share code, notes, and snippets.

@abelsonlive
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abelsonlive/10997041 to your computer and use it in GitHub Desktop.
Save abelsonlive/10997041 to your computer and use it in GitHub Desktop.
given a histogram and the parameters used to construct it, return the percentile rank of of a given value
import math
def calc_per_rank(val, hist, min_, max_, n_bins):
# determine how large each bin is
bin_interval = ( max_ - min_ ) / float( n_bins)
# identify which bin the value falls in
bin_index = int( math.floor( ( float( val ) - min_ ) / bin_interval ) )
# divide the number of observations up to and including the value's bin
# by the number of all observations
ratio = float( sum( hist[:bin_index] ) ) / float( sum( hist ) )
# return the percentile rank of the value
# multiply by 100 for interpretability
return ratio * 100.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment