The median and percentile functions described in this section operate on sorted data. For convenience we use quantiles, measured on a scale of 0 to 1, instead of percentiles (which use a scale of 0 to 100).
gsl_sort
should
always be used first.
When the dataset has an odd number of elements the median is the value of element @math{(n-1)/2}. When the dataset has an even number of elements the median is the mean of the two nearest middle values, elements @math{(n-1)/2} and @math{n/2}. Since the algorithm for computing the median involves interpolation this function always returns a floating-point number, even for integer data types.
There are no checks to see whether the data are sorted, so the function
gsl_sort
should always be used first.
The quantile is found by interpolation, using the formula
where @math{i} is floor
(@math{(n - 1)f}) and @math{\delta} is
@math{(n-1)f - i}.
Thus the minimum value of the array (data[0*stride]
) is given by
f equal to zero, the maximum value (data[(n-1)*stride]
) is
given by f equal to one and the median value is given by f
equal to 0.5. Since the algorithm for computing quantiles involves
interpolation this function always returns a floating-point number, even
for integer data types.