Sign in Join
Wiki copytemporary

Functions for descriptive statistics: what changed

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

OldNewLine
1-![](/wiki-images/8/84/Fpc_source_logo.png)
1+[![fpc source logo.png](/wiki-images/e/e1/fpc_source_logo.png)](</File:fpc_source_logo.png>)
22
3-Descriptive statistics aim at characterising empirical data by summative parameters (and also by tables and plots).
3+│ **English (en)** │ **[[Functions for descriptive statistics/fr|français (fr)]]** │
44
5+Descriptive statistics aim at characterising empirical data by summative parameters (and also by tables and plots).
6+
57## Standard functions defined in math unit
68
7-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.
9+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.
810
9-- [`mean`](https://www.freepascal.org/docs-html/rtl/math/mean.html): Returns the mean value of an array.
10-- [`stdDev`](https://www.freepascal.org/docs-html/rtl/math/stddev.html): Returns the (sample) standard deviation of an array.
11-- [`popNStdDev`](https://www.freepascal.org/docs-html/rtl/math/popnstddev.html): Returns the (population) standard deviation of an array.
12-- [`meanAndStdDev`](https://www.freepascal.org/docs-html/rtl/math/meanandstddev.html): Returns mean and standard deviation of an array.
13-- [`momentSkewKurtosis`](https://www.freepascal.org/docs-html/rtl/math/momentskewkurtosis.html): Returns the first four moments of an array.
14-- [`variance`](https://www.freepascal.org/docs-html/rtl/math/variance.html): Returns the (sample) variance of an array.
15-- [`popnVariance`](https://www.freepascal.org/docs-html/rtl/math/popnvariance.html): Returns the (population) variance of an array.
16-- [`totalVariance`](https://www.freepascal.org/docs-html/rtl/math/totalvariance.html): Returns the total variance of an array.
17-- [`sumOfSquares`](https://www.freepascal.org/docs-html/rtl/math/sumofsquares.html): Returns the sum of squares of an array.
18-- [`sum`](https://www.freepascal.org/docs-html/rtl/math/sum.html): Returns the sum of values of an array.
19-- [`sumsAndSquares`](https://www.freepascal.org/docs-html/rtl/math/sumsandsquares.html): Returns sum and sum of squares of the values in an array.
20-- [`MinValue`](https://www.freepascal.org/docs-html/rtl/math/minvalue.html): Returns the smallest of the values in an array.
21-- [`MaxValue`](https://www.freepascal.org/docs-html/rtl/math/maxvalue.html): Returns the largest of the values in an array.
11+ * [`mean`](<https://www.freepascal.org/docs-html/rtl/math/mean.html>): Returns the mean value of an array.
12+ * [`stdDev`](<https://www.freepascal.org/docs-html/rtl/math/stddev.html>): Returns the (sample) standard deviation of an array.
13+ * [`popNStdDev`](<https://www.freepascal.org/docs-html/rtl/math/popnstddev.html>): Returns the (population) standard deviation of an array.
14+ * [`meanAndStdDev`](<https://www.freepascal.org/docs-html/rtl/math/meanandstddev.html>): Returns mean and standard deviation of an array.
15+ * [`momentSkewKurtosis`](<https://www.freepascal.org/docs-html/rtl/math/momentskewkurtosis.html>): Returns the first four moments of an array.
16+ * [`variance`](<https://www.freepascal.org/docs-html/rtl/math/variance.html>): Returns the (sample) variance of an array.
17+ * [`popnVariance`](<https://www.freepascal.org/docs-html/rtl/math/popnvariance.html>): Returns the (population) variance of an array.
18+ * [`totalVariance`](<https://www.freepascal.org/docs-html/rtl/math/totalvariance.html>): Returns the total variance of an array.
19+ * [`sumOfSquares`](<https://www.freepascal.org/docs-html/rtl/math/sumofsquares.html>): Returns the sum of squares of an array.
20+ * [`sum`](<https://www.freepascal.org/docs-html/rtl/math/sum.html>): Returns the sum of values of an array.
21+ * [`sumsAndSquares`](<https://www.freepascal.org/docs-html/rtl/math/sumsandsquares.html>): Returns sum and sum of squares of the values in an array.
22+ * [`MinValue`](<https://www.freepascal.org/docs-html/rtl/math/minvalue.html>): Returns the smallest of the values in an array.
23+ * [`MaxValue`](<https://www.freepascal.org/docs-html/rtl/math/maxvalue.html>): Returns the largest of the values in an array.
2224
23-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.
25+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.
2426
2527## Standard functions defined in other units
2628
27-- [`length`](https://www.freepascal.org/docs-html/rtl/system/length.html): Delivers the length (n) of an array.
29+ * [`length`](<https://www.freepascal.org/docs-html/rtl/system/length.html>): Delivers the length (n) of an array.
2830
2931## Custom functions
3032
31-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.
33+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.
3234
33-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.
35+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.
3436
3537### Median
3638
37-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:
38-
39-``` pascal
39+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:
40+```
4041type
41- TExtArray = array of Extended;
42-
43-// Based on Shell Sort - avoiding recursion allows for sorting
44-// of very large arrays, too
45-function sortExtArray(const data: TExtArray): TExtArray;
46-var
47- data2: TExtArray;
48- arrayLength, i, j, k: longint;
49- h: extended;
50-begin
51- arrayLength := high(data);
52- data2 := copy(data, 0, arrayLength + 1);
53- k := arrayLength div 2;
54- while k > 0 do
55- begin
56- for i := 0 to arrayLength - k do
42+ TExtArray = array of Extended;
43+
44+ // Based on Shell Sort - avoiding recursion allows for sorting
45+ // of very large arrays, too
46+ function sortExtArray(const data: TExtArray): TExtArray;
47+ var
48+ data2: TExtArray;
49+ arrayLength, i, j, k: longint;
50+ h: extended;
5751 begin
58- j := i;
59- while (j >= 0) and (data2[j] > data2[j + k]) do
52+ arrayLength := high(data);
53+ data2 := copy(data, 0, arrayLength + 1);
54+ k := arrayLength div 2;
55+ while k > 0 do
6056 begin
61- h := data2[j];
62- data2[j] := data2[j + k];
63- data2[j + k] := h;
64- if j > k then
65- dec(j, k)
66- else
67- j := 0;
57+ for i := 0 to arrayLength - k do
58+ begin
59+ j := i;
60+ while (j >= 0) and (data2[j] > data2[j + k]) do
61+ begin
62+ h := data2[j];
63+ data2[j] := data2[j + k];
64+ data2[j + k] := h;
65+ if j > k then
66+ dec(j, k)
67+ else
68+ j := 0;
69+ end;
70+ end;
71+ k := k div 2
6872 end;
73+ result := data2;
6974 end;
70- k := k div 2
71- end;
72- result := data2;
73-end;
74-
75-function median(const data: TExtArray): extended;
76-var
77- centralElement: integer;
78- sortedData: TExtArray;
79-begin
80- sortedData := sortExtArray(data);
81- centralElement := length(sortedData) div 2;
82- if odd(length(sortedData)) then
83- result := sortedData[centralElement]
84- else
85- result := (sortedData[centralElement - 1] + sortedData[centralElement]) / 2;
86-end;
75+
76+ function median(const data: TExtArray): extended;
77+ var
78+ centralElement: integer;
79+ sortedData: TExtArray;
80+ begin
81+ sortedData := sortExtArray(data);
82+ centralElement := length(sortedData) div 2;
83+ if odd(length(sortedData)) then
84+ result := sortedData[centralElement]
85+ else
86+ result := (sortedData[centralElement - 1] + sortedData[centralElement]) / 2;
87+ end;
8788```
8889
89-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).
90+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).
9091
9192### Standard error of the mean
9293
93-The standard error of the mean (SEM) is a measure that estimates how precisely the true mean of the population can be known.
94-
95-Calculation of SEM is simple:
94+The standard error of the mean (SEM) is a measure that estimates how precisely the true mean of the population can be known.
9695
97-``` pascal
96+Calculation of SEM is simple:
97+```
9898function sem(const data: array of Extended): extended;
99-begin
100- sem := stddev(data) / sqrt(length(data));
101-end;
99+ begin
100+ sem := stddev(data) / sqrt(length(data));
101+ end;
102102```
103103
104104### Coefficient of variation
105105
106-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.
107-
108-It can be calculated with:
106+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.
109107
110-``` pascal
108+It can be calculated with:
109+```
111110{**
112- calculates the coefficient of variation (CV or CoV) of a vector of extended
113-}
114-function cv(const data: array of Extended): extended;
115-begin
116- result := stddev(data) / mean(data);
117-end;
111+ calculates the coefficient of variation (CV or CoV) of a vector of extended
112+ }
113+ function cv(const data: array of Extended): extended;
114+ begin
115+ result := stddev(data) / mean(data);
116+ end;
118117```
119118
119+### RMS
120+
121+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.
122+
123+It is delivered by the following function:
124+```
125+function rms(const data: array of Extended): extended;
126+ { delivers the root mean square or second moment of data }
127+ begin
128+ result := sqrt(SumOfSquares(data) / length(data));
129+ end;
130+```
131+
120132---
121-*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).*
133+*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](/wiki-contact).*

Back to history