Last active
August 29, 2015 13:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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