CFLib.org – Common Function Library Project

Percentile(number, aPopulation [, insertNumber])

Last updated July 30, 2001

Version: 1 | Requires: ColdFusion 5 | Library: MathLib

 
Rated 1 time(s). Average Rating: 5.0

Description:
This function is used to get the percentile rank of a given number from a population of numbers. The percentile refers to the percent of numbers in the set that are below the number you pass in.

Return Values:
Returns a numeric percent value

Example:

view plain print about
<cfset population="33,29,29,31,32,33,33,35,37,25,28">
<cfset array = listToArray(population)>
<table>
    <tr>
        <td>Number to Check</td>
        <td>Percentile</td>
    </tr>
<cfoutput>
<cfloop from="1" to="#arraylen(array)#" index="ii">
    <cfset thisNumber = array[ii]>
    <tr>
        <td>#thisNumber#</td>
        <td>#percentile(thisNumber,array)#</td>
    </tr>
</cfloop>
</cfoutput>
</table>

Parameters:

Name Description Required
number The number you want to get the percentile rank of Yes
aPopulation An array of numbers which is the population to find the percentile in Yes
insertNumber Boolean value to decide if the number should be inserted into the population before calculation. By default this is false. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Function for calculating the percentile of a given number from a population of numbers.
 * 
 * @param number      The number you want to get the percentile rank of 
 * @param aPopulation      An array of numbers which is the population to find the percentile in 
 * @param insertNumber      Boolean value to decide if the number should be inserted into the population before calculation.  By default this is false. 
 * @return Returns a numeric percent value 
 * @author Nathan Dintenfass (nathan@changemedia.com) 
 * @version 1, July 30, 2001 
 */

function percentile(numberToCheck,populationArray){
    // (Thanks to Jim Flannery for pointing me at the formula).
    var ii = 1;
    //set a counter for the number below this value
    var countBelow = 0;
    //set a counter for how many instances of thisNumber there are
    var countWithin = 0;
    // deal with the optional parameter as to whether the number to check is to be added to the population.
    //if there a third argument and it is a boolean and it is true, insert the number to check 
    if(arraylen(arguments) gt 2 AND isBoolean(arguments[3]) and arguments[3])
        arrayAppend(populationArray,numberToCheck);
    //now, let's sort the array to make it easier to find the values
    arraySort(populationArray,"numeric");
    //loop through the array, setting the counters appropriately
    for(ii = 1; ii lte arraylen(populationArray); ii = ii + 1){
        //if this number is below numberToCheck, increment the countBelow
        if(populationArray[ii] lt numberToCheck){
            countBelow = countBelow + 1;
        }
        else{
            //if this number is equal to numberToCheck, increment the counterWithin
            if(populationArray[ii] eq numberToCheck){
                countWithin = countWithin + 1;
            }
            //if this number is above the numberToCheck break
            else{
                break;
            }
        }
    }
    //run the percentile formula and return
    return ((countBelow + 0.5 * countWithin)/arraylen(populationArray))*100;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
15 day(s) ago

James Sleeman James Sleeman added
quickSort
25 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
28 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson