Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.