CFLib.org – Common Function Library Project

nthPercentile(population, percentile)

Last updated July 8, 2013

author

rodell basalo

Version: 1 | Requires: CF9 | Library: CFMLLib

Description:
Reference: http://en.wikipedia.org/wiki/95th_percentile

Return Values:
Returns a numeric value that is the value from the population that is the specified percentile

Example:

<!---get the 90th %tile--->
<cfset pop = [1,2,6,7,3,5,6,8,0,2,3,4,5,9,12,23,78]>

<cfset nthPercentile = getNthPercentile(pop,90)>

<cfoutput>#nthPercentile#</cfoutput>

Parameters:

Name Description Required
population An array of population values Yes
percentile The percentile to return value for Yes

Full UDF Source:

/**
 * Gets the nth percentile of the given population
 * v0.9 by rodell basalo
 * v1.0 by Adam Cameron (improved argument/variable naming, simplified logic slightly)
 * 
 * @param population 	 An array of population values (Required)
 * @param percentile 	 The percentile to return value for (Required)
 * @return Returns a numeric value that is the value from the population that is the specified percentile 
 * @author rodell basalo (delroekid@gmail.com) 
 * @version 1.0, July 8, 2013 
 */
public numeric function getNthPercentile(required array population, required numeric percentile){
	if (percentile < 0 || percentile > 100){
		throw(type="ArgumentOutOfRangeException", message="percentile argument value out of range", detail="The percentile argument must be in the range 1-100")
	}
	arraySort(population, "numeric");

	var populationSize = arrayLen(population);
	var nthPercentIndex = round((percentile/100) * populationSize + 0.5);

	return population[min(nthPercentIndex, populationSize)];
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson