Sign in Join
Wiki copytemporary

Functions for descriptive statistics: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1-<figure>
2-<img src="fpc_source_logo.png" title="fpc_source_logo.png" />
3-<figcaption>fpc_source_logo.png</figcaption>
4-</figure>
1+![](/wiki-images/8/84/Fpc_source_logo.png)
52
63Descriptive statistics aim at characterising empirical data by summative parameters (and also by tables and plots).
74
85## Standard functions defined in math unit
96
107The unit [`math`](https://www.freepascal.org/docs-html/rtl/math/statisticalroutines.html) of the [[RTL|run-time library]] provides a plethora of routines for descriptive statistics.
118
129- [`mean`](https://www.freepascal.org/docs-html/rtl/math/mean.html): Returns the mean value of an array.
1310- [`stdDev`](https://www.freepascal.org/docs-html/rtl/math/stddev.html): Returns the (sample) standard deviation of an array.
1411- [`popNStdDev`](https://www.freepascal.org/docs-html/rtl/math/popnstddev.html): Returns the (population) standard deviation of an array.
1512- [`meanAndStdDev`](https://www.freepascal.org/docs-html/rtl/math/meanandstddev.html): Returns mean and standard deviation of an array.
1613- [`momentSkewKurtosis`](https://www.freepascal.org/docs-html/rtl/math/momentskewkurtosis.html): Returns the first four moments of an array.
1714- [`variance`](https://www.freepascal.org/docs-html/rtl/math/variance.html): Returns the (sample) variance of an array.
1815- [`popnVariance`](https://www.freepascal.org/docs-html/rtl/math/popnvariance.html): Returns the (population) variance of an array.
1916- [`totalVariance`](https://www.freepascal.org/docs-html/rtl/math/totalvariance.html): Returns the total variance of an array.
2017- [`sumOfSquares`](https://www.freepascal.org/docs-html/rtl/math/sumofsquares.html): Returns the sum of squares of an array.
2118- [`sum`](https://www.freepascal.org/docs-html/rtl/math/sum.html): Returns the sum of values of an array.
2219- [`sumsAndSquares`](https://www.freepascal.org/docs-html/rtl/math/sumsandsquares.html): Returns sum and sum of squares of the values in an array.
2320- [`MinValue`](https://www.freepascal.org/docs-html/rtl/math/minvalue.html): Returns the smallest of the values in an array.
2421- [`MaxValue`](https://www.freepascal.org/docs-html/rtl/math/maxvalue.html): Returns the largest of the values in an array.
2522
2623These 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`](https://www.freepascal.org/docs-html/rtl/system/setlength.html) procedure.
2724
2825## Standard functions defined in other units
2926
3027- [`length`](https://www.freepascal.org/docs-html/rtl/system/length.html): Delivers the length (n) of an array.
3128
3229## Custom functions
3330
3431Some 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.
3532
3633Together with other useful statistical code expanded and thoroughly tested versions of these functions are also available in the [QUANTUM SALIS](http://quantum-salis.sf.net/) project.
3734
3835### Median
3936
4037The 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:
4138
4239``` pascal
4340type
4441 TExtArray = array of Extended;
4542
4643// Based on Shell Sort - avoiding recursion allows for sorting
4744// of very large arrays, too
4845function sortExtArray(const data: TExtArray): TExtArray;
4946var
5047 data2: TExtArray;
5148 arrayLength, i, j, k: longint;
5249 h: extended;
5350begin
5451 arrayLength := high(data);
5552 data2 := copy(data, 0, arrayLength + 1);
5653 k := arrayLength div 2;
5754 while k > 0 do
5855 begin
5956 for i := 0 to arrayLength - k do
6057 begin
6158 j := i;
6259 while (j >= 0) and (data2[j] > data2[j + k]) do
6360 begin
6461 h := data2[j];
6562 data2[j] := data2[j + k];
6663 data2[j + k] := h;
6764 if j > k then
6865 dec(j, k)
6966 else
7067 j := 0;
7168 end;
7269 end;
7370 k := k div 2
7471 end;
7572 result := data2;
7673end;
7774
7875function median(const data: TExtArray): extended;
7976var
8077 centralElement: integer;
8178 sortedData: TExtArray;
8279begin
8380 sortedData := sortExtArray(data);
8481 centralElement := length(sortedData) div 2;
8582 if odd(length(sortedData)) then
8683 result := sortedData[centralElement]
8784 else
8885 result := (sortedData[centralElement - 1] + sortedData[centralElement]) / 2;
8986end;
9087```
9188
9289Of 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).
9390
9491### Standard error of the mean
9592
9693The standard error of the mean (SEM) is a measure that estimates how precisely the true mean of the population can be known.
9794
9895Calculation of SEM is simple:
9996
10097``` pascal
10198function sem(const data: array of Extended): extended;
10299begin
103100 sem := stddev(data) / sqrt(length(data));
104101end;
105102```
106103
107104### Coefficient of variation
108105
109106The 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.
110107
111108It can be calculated with:
112109
113110``` pascal
114111{**
115112 calculates the coefficient of variation (CV or CoV) of a vector of extended
116113}
117114function cv(const data: array of Extended): extended;
118115begin
119116 result := stddev(data) / mean(data);
120117end;
121118```
122119
123120---
124121*From the Free Pascal wiki, page "Functions for descriptive statistics", as it stood on 2020-11-04 after a change by Jwdietrich, out of the wiki's dump of 2024-05-26. 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](/wiki-contact).*

Back to history