Sign in Join
Wiki copytemporary

Revision 3 of 3, by WikiArchive on 11-11-2026. The page as it is now.

Functions for descriptive statistics

fpc source logo.png

│ English (en) │ français (fr) │

Descriptive statistics aim at characterising empirical data by summative parameters (and also by tables and plots).

Standard functions defined in math unit

The unit math of the run-time library provides a plethora of routines for descriptive statistics.

These functions expect an array of predefined length (e.g. array[1..100] of float) or a 0-based open array (e.g. array of extended) with subsequent call of the setLength procedure.

Standard functions defined in other units

Custom functions

Some functions aren't defined in the RTL. The subsequent section lists source code of some commonly used measures for centrality and dispersion. Where not otherwise specified the code is provided with a BSD license.

Together with other useful statistical code expanded and thoroughly tested versions of these functions are also available in the QUANTUM SALIS project.

Median

The term median denotes the 50% quantile of a sample, i.e. the value separating the higher half of a data vector from its lower half. It can be calculated with:

pascal
type
      TExtArray = array of Extended;
    
    // Based on Shell Sort - avoiding recursion allows for sorting
    // of very large arrays, too
    function sortExtArray(const data: TExtArray): TExtArray;
    var
      data2: TExtArray;
      arrayLength, i, j, k: longint;
      h: extended;
    begin
      arrayLength := high(data);
      data2 := copy(data, 0, arrayLength + 1);
      k := arrayLength div 2;
      while k > 0 do
      begin
        for i := 0 to arrayLength - k do
        begin
          j := i;
          while (j >= 0) and (data2[j] > data2[j + k]) do
          begin
            h := data2[j];
            data2[j] := data2[j + k];
            data2[j + k] := h;
            if j > k then
              dec(j, k)
            else
              j := 0;
          end;
        end;
        k := k div 2
      end;
      result := data2;
    end;
    
    function median(const data: TExtArray): extended;
    var
      centralElement: integer;
      sortedData: TExtArray;
    begin
      sortedData := sortExtArray(data);
      centralElement := length(sortedData) div 2;
      if odd(length(sortedData)) then
        result := sortedData[centralElement]
      else
        result := (sortedData[centralElement - 1] + sortedData[centralElement]) / 2;
    end;

Of course, the function sortExtArray may be replaced with another sorting algorithm, e.g. QuickSort. The Shell Sort algorithm presented here has the advantage that it is able to sort very large vectors even on machines with a very small amount of memory (albeit with the expense of slightly reduced speed compared to QuickSort).

Standard error of the mean

The standard error of the mean (SEM) is a measure that estimates how precisely the true mean of the population can be known.

Calculation of SEM is simple:

pascal
function sem(const data: array of Extended): extended;
    begin
      sem := stddev(data) / sqrt(length(data));
    end;

Coefficient of variation

The coefficient of variation (CoV or CV), also known as relative standard deviation (RSD), is a measure of dispersion that is standardised with respect to the data's mean.

It can be calculated with:

pascal
{**
      calculates the coefficient of variation (CV or CoV) of a vector of extended
    }
    function cv(const data: array of Extended): extended;
    begin
      result := stddev(data) / mean(data);
    end;

RMS

The root of mean square (RMS), also known as quadratic mean or second absolute moment is widely used e.g. in estimation theory and electrical engineering.

It is delivered by the following function:

pascal
function rms(const data: array of Extended): extended;
    { delivers the root mean square or second moment of data }
    begin
      result := sqrt(SumOfSquares(data) / length(data));
    end;

From the Free Pascal wiki, page "Functions for descriptive statistics", as the Internet Archive kept it on 2025-01-22, last changed on 2025-01-18. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.