Sign in Join
Wiki copytemporary

Functions for descriptive statistics: what changed

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

Title: Functions for descriptive statistics

OldNewLine
1+<figure>
2+<img src="fpc_source_logo.png" title="fpc_source_logo.png" />
3+<figcaption>fpc_source_logo.png</figcaption>
4+</figure>
5+
6+Descriptive statistics aim at characterising empirical data by summative parameters (and also by tables and plots).
7+
8+## Standard functions defined in math unit
9+
10+The 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.
11+
12+- [`mean`](https://www.freepascal.org/docs-html/rtl/math/mean.html): Returns the mean value of an array.
13+- [`stdDev`](https://www.freepascal.org/docs-html/rtl/math/stddev.html): Returns the (sample) standard deviation of an array.
14+- [`popNStdDev`](https://www.freepascal.org/docs-html/rtl/math/popnstddev.html): Returns the (population) standard deviation of an array.
15+- [`meanAndStdDev`](https://www.freepascal.org/docs-html/rtl/math/meanandstddev.html): Returns mean and standard deviation of an array.
16+- [`momentSkewKurtosis`](https://www.freepascal.org/docs-html/rtl/math/momentskewkurtosis.html): Returns the first four moments of an array.
17+- [`variance`](https://www.freepascal.org/docs-html/rtl/math/variance.html): Returns the (sample) variance of an array.
18+- [`popnVariance`](https://www.freepascal.org/docs-html/rtl/math/popnvariance.html): Returns the (population) variance of an array.
19+- [`totalVariance`](https://www.freepascal.org/docs-html/rtl/math/totalvariance.html): Returns the total variance of an array.
20+- [`sumOfSquares`](https://www.freepascal.org/docs-html/rtl/math/sumofsquares.html): Returns the sum of squares of an array.
21+- [`sum`](https://www.freepascal.org/docs-html/rtl/math/sum.html): Returns the sum of values of an array.
22+- [`sumsAndSquares`](https://www.freepascal.org/docs-html/rtl/math/sumsandsquares.html): Returns sum and sum of squares of the values in an array.
23+- [`MinValue`](https://www.freepascal.org/docs-html/rtl/math/minvalue.html): Returns the smallest of the values in an array.
24+- [`MaxValue`](https://www.freepascal.org/docs-html/rtl/math/maxvalue.html): Returns the largest of the values in an array.
25+
26+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`](https://www.freepascal.org/docs-html/rtl/system/setlength.html) procedure.
27+
28+## Standard functions defined in other units
29+
30+- [`length`](https://www.freepascal.org/docs-html/rtl/system/length.html): Delivers the length (n) of an array.
31+
32+## Custom functions
33+
34+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.
35+
36+Together 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.
37+
38+### Median
39+
40+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:
41+
42+``` pascal
43+type
44+ TExtArray = array of Extended;
45+
46+// Based on Shell Sort - avoiding recursion allows for sorting
47+// of very large arrays, too
48+function sortExtArray(const data: TExtArray): TExtArray;
49+var
50+ data2: TExtArray;
51+ arrayLength, i, j, k: longint;
52+ h: extended;
53+begin
54+ arrayLength := high(data);
55+ data2 := copy(data, 0, arrayLength + 1);
56+ k := arrayLength div 2;
57+ while k > 0 do
58+ begin
59+ for i := 0 to arrayLength - k do
60+ begin
61+ j := i;
62+ while (j >= 0) and (data2[j] > data2[j + k]) do
63+ begin
64+ h := data2[j];
65+ data2[j] := data2[j + k];
66+ data2[j + k] := h;
67+ if j > k then
68+ dec(j, k)
69+ else
70+ j := 0;
71+ end;
72+ end;
73+ k := k div 2
74+ end;
75+ result := data2;
76+end;
77+
78+function median(const data: TExtArray): extended;
79+var
80+ centralElement: integer;
81+ sortedData: TExtArray;
82+begin
83+ sortedData := sortExtArray(data);
84+ centralElement := length(sortedData) div 2;
85+ if odd(length(sortedData)) then
86+ result := sortedData[centralElement]
87+ else
88+ result := (sortedData[centralElement - 1] + sortedData[centralElement]) / 2;
89+end;
90+```
91+
92+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).
93+
94+### Standard error of the mean
95+
96+The standard error of the mean (SEM) is a measure that estimates how precisely the true mean of the population can be known.
97+
98+Calculation of SEM is simple:
99+
100+``` pascal
101+function sem(const data: array of Extended): extended;
102+begin
103+ sem := stddev(data) / sqrt(length(data));
104+end;
105+```
106+
107+### Coefficient of variation
108+
109+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.
110+
111+It can be calculated with:
112+
113+``` pascal
114+{**
115+ calculates the coefficient of variation (CV or CoV) of a vector of extended
116+}
117+function cv(const data: array of Extended): extended;
118+begin
119+ result := stddev(data) / mean(data);
120+end;
121+```
122+
123+---
124+*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