Skip to content

Commit 26e3dff

Browse files
committed
1.0.5 Statistic
1 parent 3c4f007 commit 26e3dff

File tree

8 files changed

+309
-14
lines changed

8 files changed

+309
-14
lines changed

libraries/Statistic/CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77

8+
## [1.0.5] - 2023-06-29
9+
- fix #18 add **range()** and **middle()**
10+
- fast first order functions, based on minimum() and maximum()
11+
- statistic value is (very) limited.
12+
- add example.
13+
- update readme.md
14+
15+
816
## [1.0.4] - 2023-05-09
917
- fix #16 => defined(__AVR__) to catch all ARCH_AVR
1018

11-
1219
## [1.0.3] - 2023-05-09
1320
- fix #13 **sqrtf()** missing
1421
- prep more correct NaN when \_cnt == 0;

libraries/Statistic/README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ The stability of the formulas is improved by the help of Gil Ross (Thanks!).
3838

3939
The template version (1.0.0) is created by Glen Cornell (Thanks!).
4040

41+
4142
#### Related
4243

4344
- https://github.com/RobTillaart/Correlation
4445
- https://github.com/RobTillaart/GST - Golden standard test metrics
46+
- https://github.com/RobTillaart/Histogram
4547
- https://github.com/RobTillaart/RunningAngle
4648
- https://github.com/RobTillaart/RunningAverage
4749
- https://github.com/RobTillaart/RunningMedian
@@ -64,6 +66,7 @@ You can override e.g. **statistic::Statistic<double, uint64_t, false>** for many
6466
(assumes double >> float).
6567
- **void clear()** resets all internal variables and counters.
6668

69+
6770
#### Core
6871

6972
- **typename T add(const typename T value)** returns value actually added to internal sum.
@@ -74,7 +77,9 @@ Alternatively one need to define the statistic object with a more precise data t
7477
- **typename T sum()** returns zero if count == zero.
7578
- **typename T minimum()** returns zero if count == zero.
7679
- **typename T maximum()** returns zero if count == zero.
77-
- **typename T average()** returns NAN if count == zero.
80+
- **typename T range()** returns maximum - minimum.
81+
- **typename T middle()** returns (minimum + maximum)/2. If T is an integer type rounding errors are possible.
82+
- **typename T average()** returns NAN if count == zero.
7883

7984
These three functions only work if **useStdDev == true** (in the template).
8085

@@ -84,7 +89,7 @@ pop_stdev = population standard deviation,
8489
- **typename T unbiased_stdev()** returns NAN if count == zero.
8590

8691

87-
#### Deprecated methods:
92+
#### Deprecated methods
8893

8994
- **Statistic(bool)** Constructor previously used to enable/disable the standard deviation functions.
9095
This argument now has no effect. It is recommended to migrate your code to the default constructor
@@ -93,6 +98,20 @@ This argument now has no effect. It is recommended to migrate your code to the
9398
It is recommended to migrate your code to `clear()` (with no arguments).
9499

95100

101+
#### Range() and middle()
102+
103+
**Range()** and **middle()** are fast functions with limited statistical value.
104+
Still they have their uses.
105+
106+
Given enough samples (e.g. 100+) and a normal distribution of the samples the **range()** is expected
107+
to be 3 to 4 times the **pop_stdev()**.
108+
If the range is larger than 4 standard deviations one might have added one or more outliers.
109+
110+
Given enough samples (e.g. 100+) and a normal distribution, the **middle()** and **average()** are
111+
expected to be close to each other.
112+
Note: outliers can disrupt the **middle()**, Several non-normal distributions do too.
113+
114+
96115
## Operational
97116

98117
See examples.
@@ -112,14 +131,8 @@ See https://github.com/RobTillaart/Statistic/blob/master/FAQ.md
112131

113132
#### Should
114133

115-
- return values of **sum(), minimum(), maximum()** when **count()** == zero
116-
- should these be NaN, which is technically more correct?
117-
- does it exist for all value types? => No!
118-
- for now user responsibility to check **count()** first.
119-
- refactor \_cnt to \_count
120134
- remove deprecated methods. (1.1.0)
121135

122-
123136
#### Could
124137

125138
- add **expected average EA** compensation trick
@@ -130,7 +143,14 @@ See https://github.com/RobTillaart/Statistic/blob/master/FAQ.md
130143
- do not forget to add **EA** times count for sum.
131144
- does not affect the **std_dev()**
132145
- all functions will become slightly slower.
133-
146+
- maybe in a derived class?
147+
- **lastTimeAdd()** convenience, user can track timestamp
148+
- **largestDelta()** largest difference between two consecutive additions.
149+
- need lastValue + delta so far.
134150

135151
#### Wont
136152

153+
- return values of **sum(), minimum(), maximum()** when **count()** == zero
154+
- should these be NaN, which is technically more correct?
155+
- does it exist for all value types? => No!
156+
- user responsibility to check **count()** first.

libraries/Statistic/Statistic.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// AUTHOR: Rob Tillaart
55
// modified at 0.3 by Gil Ross at physics dot org
66
// template version 1.0.0 by Glen Cornell
7-
// VERSION: 1.0.4
7+
// VERSION: 1.0.5
88
// PURPOSE: Recursive Statistical library for Arduino
99
// HISTORY: See CHANGELOG.md
1010
//
@@ -37,7 +37,7 @@
3737
// and HAVE_STDCXX_CSTDINT feature macros in your build environment.
3838

3939

40-
#define STATISTIC_LIB_VERSION (F("1.0.4"))
40+
#define STATISTIC_LIB_VERSION (F("1.0.5"))
4141

4242

4343
#if defined(__AVR__)
@@ -191,6 +191,13 @@ class Statistic
191191
value_type sum() const { return _sum; }; // zero if count == zero
192192
value_type minimum() const { return _min; }; // zero if count == zero
193193
value_type maximum() const { return _max; }; // zero if count == zero
194+
value_type range() const { return _max - _min; }; // zero if count == zero
195+
value_type middle() const
196+
{
197+
// prevent over- or underflow if value_type is an int type
198+
double mid = _max * 0.5 + _min * 0.5;
199+
return (value_type) mid;
200+
}; // zero if count == zero
194201

195202

196203
// NAN if count == zero
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
D:\Rob\WORK\Arduino\libraries\Statistic\examples\Average\Average.ino
2+
Demo Statistics lib 1.0.5
3+
Count: 10000
4+
Min: 1.0100
5+
Max: 100.9600
6+
Average: 50.9408
7+
variance: 835.0057
8+
pop stdev: 28.8965
9+
unbias stdev: 28.8979
10+
time(ms): 2536
11+
=====================================
12+
Count: 10000
13+
Min: 1.0000
14+
Max: 100.9800
15+
Average: 51.4161
16+
variance: 839.9565
17+
pop stdev: 28.9820
18+
unbias stdev: 28.9835
19+
time(ms): 2535
20+
=====================================
21+
Count: 10000
22+
Min: 1.0000
23+
Max: 100.9800
24+
Average: 50.9561
25+
variance: 833.2212
26+
pop stdev: 28.8656
27+
unbias stdev: 28.8670
28+
time(ms): 2536
29+
=====================================
30+
Count: 10000
31+
Min: 1.0000
32+
Max: 100.9700
33+
Average: 51.4648
34+
variance: 849.5839
35+
pop stdev: 29.1476
36+
unbias stdev: 29.1491
37+
time(ms): 2537
38+
=====================================
39+
Count: 10000
40+
Min: 1.0000
41+
Max: 100.9800
42+
Average: 50.8610
43+
variance: 842.0071
44+
pop stdev: 29.0174
45+
unbias stdev: 29.0188
46+
time(ms): 2537
47+
=====================================
48+
Count: 10000
49+
Min: 1.0000
50+
Max: 100.9800
51+
Average: 51.2327
52+
variance: 816.5170
53+
pop stdev: 28.5748
54+
unbias stdev: 28.5762
55+
time(ms): 2535
56+
=====================================
57+
Count: 10000
58+
Min: 1.0000
59+
Max: 100.9800
60+
Average: 50.8115
61+
variance: 835.1608
62+
pop stdev: 28.8991
63+
unbias stdev: 28.9006
64+
time(ms): 2536
65+
=====================================
66+
Count: 10000
67+
Min: 1.0000
68+
Max: 100.9800
69+
Average: 50.7948
70+
variance: 842.5279
71+
pop stdev: 29.0263
72+
unbias stdev: 29.0278
73+
time(ms): 2535
74+
=====================================
75+
Count: 10000
76+
Min: 1.0000
77+
Max: 100.9500
78+
Average: 51.0943
79+
variance: 831.8248
80+
pop stdev: 28.8414
81+
unbias stdev: 28.8428
82+
time(ms): 2536
83+
=====================================
84+
Count: 10000
85+
Min: 1.0000
86+
Max: 100.9700
87+
Average: 50.8915
88+
variance: 836.6669
89+
pop stdev: 28.9252
90+
unbias stdev: 28.9266
91+
time(ms): 2536
92+
=====================================
93+
Count: 10000
94+
Min: 1.0100
95+
Max: 100.9800
96+
Average: 50.8437
97+
variance: 825.5516
98+
pop stdev: 28.7324
99+
unbias stdev: 28.7339
100+
time(ms): 2536
101+
=====================================
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
D:\Rob\WORK\Arduino\libraries\Statiop stdev: 28.8965
2+
range/stddev: 3.4589
3+
=====================================
4+
D:\Rob\WORK\Arduino\libraries\Statistic\examples\statistic_range_middle\statistic_range_middle.ino
5+
STATISTIC_LIB_VERSION: 1.0.5
6+
Count: 10000
7+
Min: 1.0100
8+
Max: 100.9600
9+
Range: 99.9500
10+
Middle: 50.9850
11+
Average: 50.9408
12+
middle - avg: 0.0442
13+
variance: 835.0057
14+
pop stdev: 28.8965
15+
range/stddev: 3.4589
16+
=====================================
17+
Count: 10000
18+
Min: 1.0000
19+
Max: 100.9800
20+
Range: 99.9800
21+
Middle: 50.9900
22+
Average: 51.4161
23+
middle - avg: -0.4261
24+
variance: 839.9565
25+
pop stdev: 28.9820
26+
range/stddev: 3.4497
27+
=====================================
28+
Count: 10000
29+
Min: 1.0000
30+
Max: 100.9800
31+
Range: 99.9800
32+
Middle: 50.9900
33+
Average: 50.9561
34+
middle - avg: 0.0339
35+
variance: 833.2212
36+
pop stdev: 28.8656
37+
range/stddev: 3.4636
38+
=====================================
39+
Count: 10000
40+
Min: 1.0000
41+
Max: 100.9700
42+
Range: 99.9700
43+
Middle: 50.9850
44+
Average: 51.4648
45+
middle - avg: -0.4798
46+
variance: 849.5839
47+
pop stdev: 29.1476
48+
range/stddev: 3.4298
49+
=====================================
50+
Count: 10000
51+
Min: 1.0000
52+
Max: 100.9800
53+
Range: 99.9800
54+
Middle: 50.9900
55+
Average: 50.8610
56+
middle - avg: 0.1290
57+
variance: 842.0071
58+
pop stdev: 29.0174
59+
range/stddev: 3.4455
60+
=====================================
61+
Count: 10000
62+
Min: 1.0000
63+
Max: 100.9800
64+
Range: 99.9800
65+
Middle: 50.9900
66+
Average: 51.2327
67+
middle - avg: -0.2427
68+
variance: 816.5170
69+
pop stdev: 28.5748
70+
range/stddev: 3.4989
71+
=====================================
72+
Count: 10000
73+
Min: 1.0000
74+
Max: 100.9800
75+
Range: 99.9800
76+
Middle: 50.9900
77+
Average: 50.8115
78+
middle - avg: 0.1785
79+
variance: 835.1608
80+
pop stdev: 28.8991
81+
range/stddev: 3.4596
82+
=====================================
83+
Count: 10000
84+
Min: 1.0000
85+
Max: 100.9800
86+
Range: 99.9800
87+
Middle: 50.9900
88+
Average: 50.7948
89+
middle - avg: 0.1952
90+
variance: 842.5279
91+
pop stdev: 29.0263
92+
range/stddev: 3.4445
93+
=====================================
94+
Count: 10000
95+
Min: 1.0000
96+
Max: 100.9500
97+
Range: 99.9500
98+
Middle: 50.9750
99+
Average: 51.0943
100+
middle - avg: -0.1193
101+
variance: 831.8248
102+
pop stdev: 28.8414
103+
range/stddev: 3.4655
104+
=====================================

0 commit comments

Comments
 (0)