2020-05-18 08:53:34 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: Correlation.h
|
2021-01-29 06:31:58 -05:00
|
|
|
// AUTHOR: Rob Tillaart
|
2023-01-22 09:55:51 -05:00
|
|
|
// VERSION: 0.3.0
|
2020-05-18 08:53:34 -04:00
|
|
|
// PURPOSE: Calculate Correlation from a small dataset.
|
2023-01-22 09:55:51 -05:00
|
|
|
// URL: https://github.com/RobTillaart/Correlation
|
2020-05-18 08:53:34 -04:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
|
|
|
2023-01-22 09:55:51 -05:00
|
|
|
#define CORRELATION_LIB_VERSION (F("0.3.0"))
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-05-18 08:53:34 -04:00
|
|
|
|
|
|
|
class Correlation
|
|
|
|
{
|
|
|
|
public:
|
2022-10-30 13:12:10 -04:00
|
|
|
Correlation(uint8_t size = 20); // WARNING calculate memory usage !!
|
2021-01-29 06:31:58 -05:00
|
|
|
~Correlation();
|
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// returns true if the pair of values is added to internal array.
|
|
|
|
// returns false when internal array is full.
|
2020-05-18 08:53:34 -04:00
|
|
|
bool add(float x, float y);
|
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// administrative functions
|
2020-05-18 08:53:34 -04:00
|
|
|
uint8_t count() { return _count; };
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t size() { return _size; };
|
2020-05-18 08:53:34 -04:00
|
|
|
void clear();
|
|
|
|
|
2021-08-27 10:16:35 -04:00
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// in running mode, adding new pair of values will replace old ones
|
|
|
|
// this constantly adapts the regression parameters A and B (iff calculate is called)
|
2020-05-18 08:53:34 -04:00
|
|
|
void setRunningCorrelation(bool rc) { _runningMode = rc; };
|
2021-01-29 06:31:58 -05:00
|
|
|
bool getRunningCorrelation() { return _runningMode; };
|
2020-05-18 08:53:34 -04:00
|
|
|
|
2021-08-27 10:16:35 -04:00
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// worker, to calculate the correlation parameters.
|
|
|
|
// MUST be called before retrieving the parameters
|
2021-12-14 10:39:48 -05:00
|
|
|
// A, B, R, Rsquared, Esquared, avgX and avgY
|
2021-08-27 10:16:35 -04:00
|
|
|
//
|
2022-10-30 13:12:10 -04:00
|
|
|
// parameter forced overrules the _needRecalculate flag.
|
|
|
|
// forced is default false to maintain backwards compatibility
|
2021-08-27 10:16:35 -04:00
|
|
|
//
|
2022-10-30 13:12:10 -04:00
|
|
|
// returns false if contains no elements ==> count() == 0
|
2021-08-27 10:16:35 -04:00
|
|
|
bool calculate(bool forced = false);
|
2022-10-30 13:12:10 -04:00
|
|
|
// enables / disables R, Rsquared and Esquared calculation
|
|
|
|
// This can be used to speed up the calculate function if
|
|
|
|
// these values are not used in your project.
|
2021-08-27 10:16:35 -04:00
|
|
|
void setR2Calculation(bool doR2) { _doR2 = doR2; };
|
|
|
|
bool getR2Calculation() { return _doR2; };
|
|
|
|
void setE2Calculation(bool doE2) { _doE2 = doE2; };
|
|
|
|
bool getE2Calculation() { return _doE2; };
|
|
|
|
|
2020-05-18 08:53:34 -04:00
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// Y = A + B * X
|
|
|
|
// note if no elements are added or calculate is not called
|
2021-08-27 10:16:35 -04:00
|
|
|
// the values for A and B are 0
|
2020-05-18 08:53:34 -04:00
|
|
|
float getA() { return _a; };
|
|
|
|
float getB() { return _b; };
|
|
|
|
|
2021-08-27 10:16:35 -04:00
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// getR() returns correlation coefficient (0.2.0 fixed sign)
|
2021-08-27 10:16:35 -04:00
|
|
|
float getR() { return _r; };
|
|
|
|
float getRsquare() { return _r * _r; };
|
|
|
|
|
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// returns sum of the errors squared == indication of 'spread'
|
|
|
|
// the smaller this value the more the points are on/near one line.
|
2020-05-18 08:53:34 -04:00
|
|
|
float getEsquare() { return _sumErrorSquare; };
|
|
|
|
|
2021-08-27 10:16:35 -04:00
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// get the average values of the datasets (if count > 0)
|
2023-01-22 09:55:51 -05:00
|
|
|
float getAverageX() { return _avgX; }; // will replace getAvgX() in time
|
|
|
|
float getAverageY() { return _avgY; }; // will replace getAvgY() in time
|
|
|
|
// float getAvgX() { return _avgX; }; // obsolete in 0.3.0
|
|
|
|
// float getAvgY() { return _avgY; }; // obsolete in 0.3.0
|
2021-08-27 10:16:35 -04:00
|
|
|
|
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// based on the dataset get the estimated values for X and Y
|
|
|
|
// it uses the last calculated A and B
|
|
|
|
// library does not return a confidence interval for these values.
|
2020-05-18 08:53:34 -04:00
|
|
|
float getEstimateY(float x);
|
|
|
|
float getEstimateX(float y);
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// STATISTICAL
|
2023-01-22 09:55:51 -05:00
|
|
|
// to get bounding box of all x,y pairs.
|
|
|
|
float getMinX(); // idem
|
|
|
|
float getMaxX(); // idem
|
|
|
|
float getMinY(); // idem
|
|
|
|
float getMaxY(); // idem
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2022-10-30 13:12:10 -04:00
|
|
|
// DEBUGGING - access to internal arrays.
|
|
|
|
bool setXY(uint8_t index, float x, float y); // returns true if succeeded
|
|
|
|
bool setX(uint8_t index, float x); // returns true if succeeded
|
2023-01-22 09:55:51 -05:00
|
|
|
bool setY(uint8_t index, float y); // returns true if succeeded
|
2022-10-30 13:12:10 -04:00
|
|
|
float getX(uint8_t index); // idem
|
|
|
|
float getY(uint8_t index); // idem
|
2021-08-27 10:16:35 -04:00
|
|
|
|
2023-01-22 09:55:51 -05:00
|
|
|
float getSumXY(); // replaces getSumXiYi()
|
|
|
|
float getSumX2(); // replaces getSumXi2()
|
|
|
|
float getSumY2(); // replaces getSumYi2()
|
|
|
|
// float getSumXiYi() { return _sumXiYi; }; // obsolete in version 0.3.0
|
|
|
|
// float getSumXi2() { return _sumXi2; }; // obsolete in version 0.3.0
|
|
|
|
// float getSumYi2() { return _sumYi2; }; // obsolete in version 0.3.0
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2020-05-18 08:53:34 -04:00
|
|
|
private:
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t _size = 0;
|
2021-12-14 10:39:48 -05:00
|
|
|
uint8_t _index = 0;
|
2020-05-18 08:53:34 -04:00
|
|
|
uint8_t _count = 0;
|
|
|
|
bool _runningMode = false;
|
|
|
|
bool _needRecalculate = true;
|
2021-08-27 10:16:35 -04:00
|
|
|
bool _doE2 = true;
|
|
|
|
bool _doR2 = true;
|
2020-05-18 08:53:34 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
float * _x;
|
|
|
|
float * _y;
|
2020-05-18 08:53:34 -04:00
|
|
|
|
|
|
|
float _avgX;
|
|
|
|
float _avgY;
|
|
|
|
float _a;
|
|
|
|
float _b;
|
2022-06-21 02:22:05 -04:00
|
|
|
float _div_b;
|
2021-08-27 10:16:35 -04:00
|
|
|
float _r;
|
2020-05-18 08:53:34 -04:00
|
|
|
float _sumErrorSquare;
|
2021-01-29 06:31:58 -05:00
|
|
|
float _sumXiYi;
|
|
|
|
float _sumXi2;
|
|
|
|
float _sumYi2;
|
2020-05-18 08:53:34 -04:00
|
|
|
};
|
|
|
|
|
2021-12-14 10:39:48 -05:00
|
|
|
|
2023-01-22 09:55:51 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-14 10:39:48 -05:00
|
|
|
|