You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: libraries/Correlation/README.md
+49-18Lines changed: 49 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,12 +17,15 @@ This library calculates the coefficients of the linear correlation
17
17
between two (relative small) datasets. The size of these datasets is
18
18
20 by default. The size can be set in the constructor.
19
19
20
-
The formula of the correlation is expressed as **Y = A + B \* X**,
21
-
22
20
Please note that the correlation uses about ~50 bytes per instance,
23
21
and 2 floats == 8 bytes per pair of elements.
24
22
So ~120 elements will use up 50% of the RAM of an UNO.
25
23
24
+
The formula of the correlation is expressed as **Y = A + B \* X**,
25
+
26
+
If all points are on a vertical line, the parameter B will be NAN,
27
+
This will happen if the **sumXi2** is zero or very small.
28
+
26
29
Use with care.
27
30
28
31
@@ -31,47 +34,75 @@ Use with care.
31
34
32
35
### Constructor
33
36
34
-
-**Correlation(uint8_t size = 20)** allocates the array needed and resets internal admin.
37
+
-**Correlation(uint8_t size = 20)** allocates the array needed and resets internal admin. Size should be between 1 and 255. Size = 0 will set the size to 20.
35
38
-**~Correlation()** frees the allocated arrays.
36
39
37
40
38
41
### Base functions
39
42
40
-
-**bool add(float x, float y)** adds a pair of **floats** to the internal storage.
43
+
-**bool add(float x, float y)** adds a pair of **floats** to the internal storage arrays's.
41
44
Returns true if the value is added, returns false when internal array is full.
42
-
When running correlation is set, it will replace the oldest element and return true.
43
-
-**uint8_t count()** returns the amount of items in the internal arrays.
45
+
When running correlation is set, **add()** will replace the oldest element and return true.
46
+
Warning: **add()** does not check if the floats are NAN or INFINITE.
47
+
-**uint8_t count()** returns the amount of items in the internal arrays.
48
+
This number is always between 0 ..**size()**
44
49
-**uint8_t size()** returns the size of the internal arrays.
45
-
-**void clear()** resets the datastructure to start condition (zero elements added)
50
+
-**void clear()** resets the data structures to the start condition (zero elements added)
46
51
-**bool calculate()** does the math to calculate the correlation parameters A, B and R.
47
52
This function will be called automatically when needed.
48
53
You can call it on a more convenient time.
49
54
Returns false if nothing to calculate **count == 0**
55
+
-**void setR2Calculation(bool)** enables / disables the calculation of Rsquared.
56
+
-**bool getR2Calculation()** returns the flag set.
57
+
-**void setE2Calculation(bool)** enables / disables the calculation of Esquared.
58
+
-**bool getE2Calculation()** returns the flag set.
59
+
60
+
After the calculation the following functions can be called to return the core values.
50
61
-**float getA()** returns the A parameter of formula **Y = A + B \* X**
51
62
-**float getB()** returns the B parameter of formula **Y = A + B \* X**
52
-
-**float getR()** returns the correlation coefficient R.
63
+
-**float getR()** returns the correlation coefficient R which is always between -1 .. 1
53
64
The closer to 0 the less correlation there is between X and Y.
54
65
Correlation can be positive or negative.
55
-
Most often the R squared **sqr(R)** is used.
56
-
-**float getRsquare()** returns the **sqr(R)** which is always between 0.. 1.
66
+
Most often the Rsquare **R x R** is used.
67
+
-**float getRsquare()** returns **R x R** which is always between 0.. 1.
57
68
-**float getEsquare()** returns the error squared to get an indication of the
58
-
quality of the relation.
69
+
quality of the correlation.
59
70
-**float getAvgX()** returns the average of all elements in the X dataset.
60
71
-**float getAvgY()** returns the average of all elements in the Y dataset.
61
72
-**float getEstimateX(float y)** use to calculate the estimated X for a given Y.
62
73
-**float getEstimateY(float x)** use to calculate the estimated Y for a given X.
63
74
64
75
76
+
#### Correlation Coefficient R
77
+
78
+
Indicative description of the correlation
79
+
80
+
| R | correlation |
81
+
|:-------------:|:--------------|
82
+
| +1.0 | Perfect |
83
+
| +0.8 to +1.0 | Very strong |
84
+
| +0.6 to +0.8 | Strong |
85
+
| +0.4 to +0.6 | Moderate |
86
+
| +0.2 to +0.4 | Weak |
87
+
| 0.0 to +0.2 | Very weak |
88
+
| 0.0 to -0.2 | Very weak |
89
+
| -0.2 to -0.4 | Weak |
90
+
| -0.4 to -0.6 | Moderate |
91
+
| -0.6 to -0.8 | Strong |
92
+
| -0.8 to -1.0 | Very strong |
93
+
| -1.0 | Perfect |
94
+
95
+
65
96
### Running correlation
66
97
67
98
-**void setRunningCorrelation(bool rc)** sets the internal variable
68
99
runningMode which allows **add()** to overwrite old elements in the
69
100
internal arrays.
70
-
-**bool getRunningCorrelation()** returns the runningMOde flag.
101
+
-**bool getRunningCorrelation()** returns the runningMode flag.
71
102
72
-
The running correlation will be calculated over the last **count** elements.
73
-
This allows for more adaptive formula finding e.g. find the relation between
74
-
temperature and humidity per hour.
103
+
The running correlation will be calculated over the last **count** elements. If the array is full, count will be size.
104
+
This running correlation allows for more adaptive formula finding e.g. find the relation between
105
+
temperature and humidity per hour, and how it changes over time.
75
106
76
107
77
108
### Statistical
@@ -89,10 +120,10 @@ It also depends on **R** of course. Idem for **getEstimateY()**
89
120
90
121
### Debugging / educational
91
122
92
-
Normally not used.
123
+
Normally not used. For all these functions idx should be < count!
93
124
94
125
-**bool setXY(uint8_t idx, float x, float y)** overwrites a pair of values.
95
-
Returns true if succeeded, idx should be < count!
126
+
Returns true if succeeded.
96
127
-**bool setX(uint8_t idx, float x)** overwrites single X.
97
128
-**bool setY(uint8_t idx, float y)** overwrites single Y.
98
129
-**float getX(uint8_t idx)** returns single value.
@@ -106,7 +137,7 @@ Returns true if succeeded, idx should be < count!
106
137
107
138
- Template version
108
139
The constructor should get a TYPE parameter, as this
109
-
allows smaller datatypes to be analyzed taking less memory.
140
+
allows smaller data types to be analysed taking less memory.
0 commit comments