Skip to content

Instantly share code, notes, and snippets.

@javiermon
Created June 3, 2013 18:15
Show Gist options
  • Save javiermon/5700120 to your computer and use it in GitHub Desktop.
Save javiermon/5700120 to your computer and use it in GitHub Desktop.
percentile_cont in js
// http://stackoverflow.com/questions/14300004/postgresql-equivalent-of-oracles-percentile-cont-function
var percentile_cont = function (data, percentile) {
var row = (percentile*(data.length -1));
var crn = Math.ceil(row);
var frn = Math.floor(row);
var result = 0.0;
data.sort(function(a,b){return a-b});
if ((crn == frn) && (frn == row)) {
result = data[row];
} else {
result = (crn - row) * data[frn] + (row - frn) * data[crn];
}
return result;
};
var example = [5.1345, 195.1345, 1095.1345,
5995.1345, 15.1345, 25.1345, 495.1345,
35.1345, 695.1345, 595.1345, 35.1345,
30195.1345, 165.1345, 65.1345, 955.1345,
135.1345, 19195.1345,
145.1345, 85.1345, 455.1345];
percentile_cont(example, 0.25);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment