Skip to content

Commit 8270df9

Browse files
committed
0.1.0 Gauss
1 parent 26e3dff commit 8270df9

File tree

19 files changed

+965
-0
lines changed

19 files changed

+965
-0
lines changed

libraries/Gauss/.arduino-ci.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
platforms:
2+
rpipico:
3+
board: rp2040:rp2040:rpipico
4+
package: rp2040:rp2040
5+
gcc:
6+
features:
7+
defines:
8+
- ARDUINO_ARCH_RP2040
9+
warnings:
10+
flags:
11+
12+
packages:
13+
rp2040:rp2040:
14+
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
15+
16+
compile:
17+
# Choosing to run compilation tests on 2 different Arduino platforms
18+
platforms:
19+
- uno
20+
# - due
21+
# - zero
22+
# - leonardo
23+
- m4
24+
- esp32
25+
- esp8266
26+
# - mega2560
27+
- rpipico
28+
libraries:
29+
- MultiMap
30+
31+
unittest:
32+
# These dependent libraries will be installed
33+
libraries:
34+
- MultiMap

libraries/Gauss/.github/FUNDING.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: RobTillaart
4+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
name: Arduino-lint
3+
4+
on: [push, pull_request]
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
runTest:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: json-syntax-check
15+
uses: limitusus/json-syntax-check@v1
16+
with:
17+
pattern: "\\.json$"
18+

libraries/Gauss/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Change Log Gauss
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
9+
## [0.1.0] - 2023-07-06
10+
- initial version
11+

libraries/Gauss/Gauss.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// FILE: Gauss.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: Library for the Gauss probability math.
6+
// DATE: 2023-07-06
7+
8+
9+
10+
// -- END OF FILE --
11+

libraries/Gauss/Gauss.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#pragma once
2+
//
3+
// FILE: Gauss.h
4+
// AUTHOR: Rob Tillaart
5+
// VERSION: 0.1.0
6+
// PURPOSE: Library for the Gauss probability math.
7+
// DATE: 2023-07-06
8+
9+
10+
#include "Arduino.h"
11+
#include "MultiMap.h"
12+
13+
#define GAUSS_LIB_VERSION (F("0.1.0"))
14+
15+
16+
class Gauss
17+
{
18+
public:
19+
Gauss()
20+
{
21+
_mean = 0;
22+
_stddev = 1;
23+
}
24+
25+
bool begin(float mean, float stddev)
26+
{
27+
_mean = mean;
28+
_stddev = abs(stddev);
29+
return true;
30+
}
31+
32+
33+
float P_smaller(float value)
34+
{
35+
if (_stddev == 0) return NAN;
36+
return _P_smaller((value - _mean) / _stddev);
37+
}
38+
39+
40+
float P_larger(float value)
41+
{
42+
return 1.0 - P_smaller(value);
43+
// if (_stddev == 0) return NAN;
44+
// optimize math division?
45+
// return _P_larger((value - _mean) / _stddev);
46+
}
47+
48+
49+
float P_between(float p, float q)
50+
{
51+
if (p >= q) return 0;
52+
return P_smaller(q) - P_smaller(p);
53+
}
54+
55+
56+
float P_equal(float value)
57+
{
58+
if (_stddev == 0) return NAN;
59+
float n = (value - _mean)/_stddev;
60+
float c = 1.0 / (_stddev * sqrt(TWO_PI));
61+
return c * exp(-0.5 * n * n);
62+
}
63+
64+
65+
float stddevs(float value)
66+
{
67+
return normalize(value);
68+
}
69+
70+
71+
float normalize(float value)
72+
{
73+
return (value - _mean)/_stddev;
74+
}
75+
76+
77+
float bellCurve(float value)
78+
{
79+
return P_equal(value);
80+
}
81+
82+
83+
84+
private:
85+
86+
float _P_smaller(float x)
87+
{
88+
// TODO improve accuracy or reduce points.
89+
float __gauss[] = {
90+
0.5000, 0.5398, 0.5793, 0.6179, 0.6554, 0.6915, 0.7257, 0.7580,
91+
0.7881, 0.8159, 0.8413, 0.8643, 0.8849, 0.9032, 0.9192, 0.9332,
92+
0.9452, 0.9554, 0.9641, 0.9713, 0.9772, 0.9821, 0.9861, 0.9893,
93+
0.9918, 0.9938, 0.9953, 0.9965, 0.9974, 0.9981, 0.9987, 1.0000
94+
};
95+
float __z[] = {
96+
0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,
97+
0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5,
98+
1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3,
99+
2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 10.0
100+
};
101+
102+
if (x < 0) return 1.0 - multiMap<float>(-x, __z, __gauss, 32);
103+
return multiMap<float>(x, __z, __gauss, 32);
104+
}
105+
106+
float _mean = 0;
107+
float _stddev = 1;
108+
};
109+
110+
111+
// -- END OF FILE --

libraries/Gauss/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023-2023 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

libraries/Gauss/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
[![Arduino CI](https://github.com/RobTillaart/Gauss/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3+
[![Arduino-lint](https://github.com/RobTillaart/Gauss/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/Gauss/actions/workflows/arduino-lint.yml)
4+
[![JSON check](https://github.com/RobTillaart/Gauss/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/Gauss/actions/workflows/jsoncheck.yml)
5+
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/Gauss/blob/master/LICENSE)
6+
[![GitHub release](https://img.shields.io/github/release/RobTillaart/Gauss.svg?maxAge=3600)](https://github.com/RobTillaart/Gauss/releases)
7+
8+
9+
# Gauss
10+
11+
Library for the Gauss probability math.
12+
13+
14+
## Description
15+
16+
Gauss is an experimental Arduino library to approximate the probability that a value is
17+
smaller or larger than a given value.
18+
These under the premises of a Gaussian distribution with parameters **mean** and **stddev**
19+
(a.k.a. average / mu and standard deviation / sigma).
20+
If these parameters are not given, 0 and 1 are used by default (normalized Gaussian distribution).
21+
22+
The values are approximated with **MultiMap()** using a 32 points interpolated lookup.
23+
Therefore the **MultiMap** library need to be downloaded too (see related below).
24+
The number of lookup points might chance in the future.
25+
26+
Return values are given as floats, if one needs percentages, just multiply by 100.0.
27+
28+
29+
#### Accuracy
30+
31+
The lookup table used has 32 points with 4 significant digits.
32+
Do not expect a higher accuracy / precision.
33+
For many applications this accuracy is sufficient.
34+
35+
(links to a table with more significant digits is welcome).
36+
37+
38+
#### Applications
39+
40+
- use as a filter? do not allow > 3 sigma
41+
- compare historic data to current data
42+
- compare population data with individual
43+
44+
45+
#### Related
46+
47+
- https://en.wikipedia.org/wiki/Normal_distribution
48+
- https://github.com/RobTillaart/Multimap
49+
- https://github.com/RobTillaart/Statistic (more stat links there).
50+
51+
52+
## Interface
53+
54+
```cpp
55+
#include Gauss.h
56+
```
57+
58+
59+
#### Base
60+
61+
- **Gauss()** constructor. Uses mean = 0 and stddev = 1 by default.
62+
- **bool begin(float mean, float stddev)** set the mean and stddev.
63+
Returns true on success. If needed stddev is made positive.
64+
65+
#### Probability
66+
67+
- **float P_smaller(float f)** returns probability **P(x < f)**.
68+
Multiply by 100.0 to get the value as a percentage.
69+
- **float P_larger(float f)** returns probability **P(x > f)**.
70+
Multiply by 100.0 to get the value as a percentage.
71+
- **float P_between(float f, float g)** returns probability **P(f < x < g)**.
72+
Multiply by 100.0 to get the value as a percentage.
73+
- **float P_equal(float f)** returns probability **P(x == f)**.
74+
This is the bell curve formula.
75+
76+
#### Other
77+
78+
- **float normalize(float f)** normalize a value to normalized distribution.
79+
Is equal to number of **stddevs()**.
80+
- **float stddevs(float f)** returns the number of stddevs from the mean.
81+
E.g if mean == 50 and stddev == 14, then 71 ==> +1.5 sigma.
82+
- **float bellCurve(float f)** returns probability **P(x == f)**.
83+
84+
85+
## Future
86+
87+
#### Must
88+
89+
- documentation
90+
- mu + sigma character
91+
- unit tests
92+
93+
#### Should
94+
95+
- optimize performance
96+
- remove division by stddev
97+
- optimize accuracy
98+
- revisit lookup of MultiMap
99+
- (-10 .. 0) might be more accurate (significant digits)?
100+
- double instead of floats? (good table?)
101+
102+
103+
104+
#### Could
105+
106+
- **void setMean(float f)**
107+
- **float getMean()**
108+
- **void setStddev(float f)**
109+
- **float getStddev()**
110+
- default values for **begin(0,1)**
111+
- add examples
112+
- e.g. temperature (DS18B20 or DHT22)
113+
- e.g. loadcell (HX711)
114+
- does the stddev needs to be positive,
115+
- what happens if negative values are allowed?
116+
- equality test Gauss objects
117+
- move code to .cpp file? (rather small lib).
118+
- embed MultiMap hardcoded instead of library dependency
119+
- **bellCurve()** => **Z()**?
120+
121+
122+
#### Won't (unless requested)
123+
124+

0 commit comments

Comments
 (0)