Skip to content

Percentiles

image Source: adopted from here

Introduction

Quote from Wiki

A percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations falls. For example, the 20^{th} percentile is the value (or score) below which 20% of the observations may be found. Equivalently, 80% of the observations are found above the 20^{th} percentile.

Question

Write a q function calcPercentile[p;v] to calculate the percentiles of a list of numbers. This function has two arguments:

  • p: an atom or a list of float numbers for percentiles, which is between 0 and 1.
  • v: a list of numerical numbers, either float or integers.

Answer

The suggested answer:

calcPercentile:{[p;v]v iasc[v] 0|-1+ceiling p*count v};

Example 1:

nums:100000?100f;
calcPercentile[0.1 0.25 0.5 0.6 0.76;nums]

Example 2:

nums:til 100;
calcPercentile[0.0 0.5 1;nums]

By definition, we have calcPercentile[0.0;nums]=min nums and calcPercentile[1;nums]=max nums.