diff --git a/.gitignore b/.gitignore deleted file mode 100644 index e43b0f9..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.DS_Store diff --git a/README.md b/README.md index 0cbc22a..be58f54 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ![GitHub release (latest by date)](https://img.shields.io/github/v/release/akkoyun/Statistical) ![arduino-library-badge](https://www.ardu-badge.com/badge/Statistical.svg?) ![Visits Badge](https://badges.pufler.dev/visits/akkoyun/Statistical) ![GitHub stars](https://img.shields.io/github/stars/akkoyun/Statistical?style=flat&logo=github) ![Updated Badge](https://badges.pufler.dev/updated/akkoyun/Statistical) ![PlatformIO Registry](https://badges.registry.platformio.org/packages/akkoyun/library/Statistical.svg) [![Check Arduino](https://github.com/akkoyun/Statistical/actions/workflows/check-arduino.yml/badge.svg)](https://github.com/akkoyun/Statistical/actions/workflows/check-arduino.yml) [![Compile Examples](https://github.com/akkoyun/Statistical/actions/workflows/compile-examples.yml/badge.svg)](https://github.com/akkoyun/Statistical/actions/workflows/compile-examples.yml) [![Spell Check](https://github.com/akkoyun/Statistical/actions/workflows/spell-check.yml/badge.svg)](https://github.com/akkoyun/Statistical/actions/workflows/spell-check.yml) - Build - 01.04.15 + Build - 02.00.02 --- diff --git a/docs/index.md b/docs/index.md index 0cbc22a..cbb0549 100644 --- a/docs/index.md +++ b/docs/index.md @@ -3,7 +3,7 @@ ![GitHub release (latest by date)](https://img.shields.io/github/v/release/akkoyun/Statistical) ![arduino-library-badge](https://www.ardu-badge.com/badge/Statistical.svg?) ![Visits Badge](https://badges.pufler.dev/visits/akkoyun/Statistical) ![GitHub stars](https://img.shields.io/github/stars/akkoyun/Statistical?style=flat&logo=github) ![Updated Badge](https://badges.pufler.dev/updated/akkoyun/Statistical) ![PlatformIO Registry](https://badges.registry.platformio.org/packages/akkoyun/library/Statistical.svg) [![Check Arduino](https://github.com/akkoyun/Statistical/actions/workflows/check-arduino.yml/badge.svg)](https://github.com/akkoyun/Statistical/actions/workflows/check-arduino.yml) [![Compile Examples](https://github.com/akkoyun/Statistical/actions/workflows/compile-examples.yml/badge.svg)](https://github.com/akkoyun/Statistical/actions/workflows/compile-examples.yml) [![Spell Check](https://github.com/akkoyun/Statistical/actions/workflows/spell-check.yml/badge.svg)](https://github.com/akkoyun/Statistical/actions/workflows/spell-check.yml) - Build - 01.04.15 + Build - 02.00.01 --- diff --git a/examples/.DS_Store b/examples/.DS_Store index 87f0cd3..f312a78 100644 Binary files a/examples/.DS_Store and b/examples/.DS_Store differ diff --git a/examples/Array/Array.ino b/examples/Array/Array.ino index 60d169e..afe26ae 100644 --- a/examples/Array/Array.ino +++ b/examples/Array/Array.ino @@ -6,39 +6,47 @@ void setup() { // Start Serial Serial.begin(115200); + // Set Data + Serial.println(F("Learning Data...")); + } void loop() { // Set Data - Serial.println("Learning Data..."); - float Data_Array[] = {220.14, 221.36, 218.21, 217.6, 215.7, 225.8, 220.14 }; - uint8_t Data_Size = 7; +// float Data[] = {220.14, 221.36, 218.21, 217.6, 215.7, 225.8, 220.14}; +// double Data[] = {220.14, 221.36, 218.21, 217.6, 215.7, 225.8, 220.14}; + uint8_t Data[] = {14, 36, 21, 6, 7, 14}; - // Print Array - Serial.print("Data Array : "); - for (uint8_t i = 0; i < Data_Size; i++) {Serial.print("["); Serial.print(Data_Array[i]); Serial.print("] ");} Serial.println(""); + // Construct Object + Array_Stats Data_Array(Data, sizeof(Data) / sizeof(Data[0])); // Print Stats - Serial.print("Array Sum : "); Serial.println(Stats.Array_Sum(Data_Array, Data_Size)); - Serial.print("Array Square Sum : "); Serial.println(Stats.Array_Sq_Sum(Data_Array, Data_Size)); - Serial.print("Array Min : "); Serial.println(Stats.Array_Min(Data_Array, Data_Size)); - Serial.print("Array Max : "); Serial.println(Stats.Array_Max(Data_Array, Data_Size)); - Serial.print("Array Arithmetic Average : "); Serial.println(Stats.Array_Arithmetic_Average(Data_Array, Data_Size)); - Serial.print("Array Geometric Average : "); Serial.println(Stats.Array_Geometric_Average(Data_Array, Data_Size)); - Serial.print("Array RMS Average : "); Serial.println(Stats.Array_RMS_Average(Data_Array, Data_Size)); - Serial.print("Array Extended RMS Average : "); Serial.println(Stats.Array_Ext_RMS_Average(Data_Array, Data_Size)); - Serial.print("Array Median : "); Serial.println(Stats.Array_Median(Data_Array, Data_Size)); - Serial.print("Array Standard Deviation : "); Serial.println(Stats.Array_Standard_Deviation(Data_Array, Data_Size)); - Serial.print("Array Standard Deviation Error : "); Serial.println(Stats.Array_Standard_Deviation_Error(Data_Array, Data_Size)); - Serial.print("Array Coefficient Factor : "); Serial.println(Stats.Array_Coefficient_Factor(Data_Array, Data_Size)); - - Serial.print("Sorted Data Array : "); - for (uint8_t i = 0; i < Data_Size; i++) {Serial.print("["); Serial.print(Data_Array[i]); Serial.print("] ");} Serial.println(""); - - Serial.println("--------------------------------------------------"); - - delay(2000); + Serial.println(F("------------------------------------------------")); + Serial.print(F("Data Array : ")); Data_Array.Array(); + Serial.println(F("------------------------------------------------")); + Serial.print(F("Array Size : ")); Serial.println(Data_Array.Size()); + Serial.print(F("Array Sum : ")); Serial.println(Data_Array.Sum()); + Serial.print(F("Array Min : ")); Serial.println(Data_Array.Min()); + Serial.print(F("Array Max : ")); Serial.println(Data_Array.Max()); + Serial.print(F("Array Square Sum : ")); Serial.println(Data_Array.Sq_Sum()); + Serial.print(F("Array Arithmetic Average : ")); Serial.println(Data_Array.Average(Data_Array.Arithmetic_Avg)); + Serial.print(F("Array Geometric Average : ")); Serial.println(Data_Array.Average(Data_Array.Geometric_Avg)); + Serial.print(F("Array RMS Average : ")); Serial.println(Data_Array.Average(Data_Array.RMS_Avg)); + Serial.print(F("Array Extended RMS Average : ")); Serial.println(Data_Array.Average(Data_Array.Ext_RMS_Avg)); + Serial.print(F("Array Q1 : ")); Serial.println(Data_Array.Quartile(1)); + Serial.print(F("Array Q2 (Median) : ")); Serial.println(Data_Array.Quartile(2)); + Serial.print(F("Array Q3 : ")); Serial.println(Data_Array.Quartile(3)); + Serial.print(F("Array IQR : ")); Serial.println(Data_Array.IQR()); + Serial.print(F("Array Standard Deviation : ")); Serial.println(Data_Array.Standard_Deviation()); + Serial.print(F("Array Standard Deviation Error : ")); Serial.println(Data_Array.Standard_Deviation_Error()); + Serial.print(F("Array Coefficient Factor : ")); Serial.println(Data_Array.Coefficient_Factor()); + Serial.print(F("Array Variance : ")); Serial.println(Data_Array.Variance()); + Serial.println(F("------------------------------------------------")); + Serial.print(F("Sorted Data Array : ")); Data_Array.Array(); + + // Loop Delay + delay(5000); } diff --git a/examples/FILO_Array/FILO_Array.ino b/examples/FILO_Array/FILO_Array.ino new file mode 100644 index 0000000..ef53d5c --- /dev/null +++ b/examples/FILO_Array/FILO_Array.ino @@ -0,0 +1,62 @@ +// Include Statistal Library +#include + + +// Set Data +float Data[5]; + +// Construct Object +Array_Stats Data_Array(Data, sizeof(Data) / sizeof(Data[0])); + + +void setup() { + + // Start Serial + Serial.begin(115200); + + // Set Data + Serial.println(F("Learning Data...")); + + // Set FILO + Data_Array.Set_FILO_Size(4); + + // Add Data to FILO Array + Data_Array.FILO_Add_Data(220.12); + Data_Array.FILO_Add_Data(214.35); + Data_Array.FILO_Add_Data(200.14); + Data_Array.FILO_Add_Data(212.80); + Data_Array.FILO_Add_Data(242.33); + Data_Array.FILO_Add_Data(215.99); + Data_Array.FILO_Add_Data(220.12); + + // Print Stats + Serial.println(F("------------------------------------------------")); + Serial.print(F("Array Size : ")); Serial.println(Data_Array.Size()); + Serial.println(F("------------------------------------------------")); + Serial.print(F("Data Array : ")); Data_Array.Array(); + Serial.print(F("Array Sum : ")); Serial.println(Data_Array.Sum()); + Serial.print(F("Array Min : ")); Serial.println(Data_Array.Min()); + Serial.print(F("Array Max : ")); Serial.println(Data_Array.Max()); + Serial.print(F("Array Square Sum : ")); Serial.println(Data_Array.Sq_Sum()); + Serial.print(F("Array Arithmetic Average : ")); Serial.println(Data_Array.Average(Data_Array.Arithmetic_Avg)); + Serial.print(F("Array Geometric Average : ")); Serial.println(Data_Array.Average(Data_Array.Geometric_Avg)); + Serial.print(F("Array RMS Average : ")); Serial.println(Data_Array.Average(Data_Array.RMS_Avg)); + Serial.print(F("Array Extended RMS Average : ")); Serial.println(Data_Array.Average(Data_Array.Ext_RMS_Avg)); + Serial.print(F("Array Q1 : ")); Serial.println(Data_Array.Quartile(1)); + Serial.print(F("Array Q2 (Median) : ")); Serial.println(Data_Array.Quartile(2)); + Serial.print(F("Array Q3 : ")); Serial.println(Data_Array.Quartile(3)); + Serial.print(F("Array IQR : ")); Serial.println(Data_Array.IQR()); + Serial.print(F("Array Standard Deviation : ")); Serial.println(Data_Array.Standard_Deviation()); + Serial.print(F("Array Standard Deviation Error : ")); Serial.println(Data_Array.Standard_Deviation_Error()); + Serial.print(F("Array Coefficient Factor : ")); Serial.println(Data_Array.Coefficient_Factor()); + Serial.print(F("Array Variance : ")); Serial.println(Data_Array.Variance()); + Serial.println(F("------------------------------------------------")); + Serial.print(F("Sorted Data Array : ")); Data_Array.Array(); + +} + +void loop() { + + +} + diff --git a/examples/Linear_Regression/Linear_Regression.ino b/examples/Linear_Regression/Linear_Regression.ino index 992c9a7..3bd1798 100644 --- a/examples/Linear_Regression/Linear_Regression.ino +++ b/examples/Linear_Regression/Linear_Regression.ino @@ -1,53 +1,48 @@ #include -Statistical DataSet; - void setup() { // Start Serial Serial.begin(115200); - + + // Set Data + Serial.println("Learning Data..."); + } void loop() { // Set Data - Serial.println("Learning Data..."); + uint8_t Data_X[] = {1, 2, 3, 4, 5, 6, 7}; + float Data_Y[] = {2, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7}; + + // Declare Object + Linear_Regression Regression(Data_X, Data_Y, sizeof(Data_X) / sizeof(Data_X[0])); + + // Print Array X + Serial.print("Data X Array : "); + for (size_t i = 0; i < sizeof(Data_X) / sizeof(Data_X[0]); i++) {Serial.print(F("[")); Serial.print(Data_X[i]); Serial.print(F("] "));} + Serial.println(""); + + // Print Array X + Serial.print("Data Y Array : "); + for (size_t i = 0; i < sizeof(Data_X) / sizeof(Data_X[0]); i++) {Serial.print(F("[")); Serial.print(Data_Y[i]); Serial.print(F("] "));} + Serial.println(""); + + // Print Regression Slope + Serial.print("Slope : "); Serial.println(Regression.Slope(),5); + + // Print Regression Offset + Serial.print("Offset : "); Serial.println(Regression.Offset(),5); + + // Print Regression R2 + Serial.print("R2 : "); Serial.println(Regression.R2(),5); + + // Print Divider + Serial.println("--------------------------"); - // Set Precision - uint8_t Linear_Regression_Precision = 5; - - // Set Array - float Data[Linear_Regression_Precision][2]; - - // Learn Data - DataSet.Array_FILO(Data, Linear_Regression_Precision, 1, 220.14); - DataSet.Array_FILO(Data, Linear_Regression_Precision, 2, 221.36); - DataSet.Array_FILO(Data, Linear_Regression_Precision, 3, 218.21); - DataSet.Array_FILO(Data, Linear_Regression_Precision, 4, 217.06); - DataSet.Array_FILO(Data, Linear_Regression_Precision, 5, 220.14); - DataSet.Array_FILO(Data, Linear_Regression_Precision, 6, 222.14); - DataSet.Array_FILO(Data, Linear_Regression_Precision, 7, 225.14); - - // Print FILO Array - Serial.print("Regression Data Array : "); - for (uint8_t i = 0; i < Linear_Regression_Precision; i++) { - Serial.print("["); - Serial.print(Data[i][0]); - Serial.print(","); - Serial.print(Data[i][1]); - Serial.print("] "); - } - Serial.println(""); - - // Print Calculated Data - Serial.print("Regression Data Count : "); Serial.println(Linear_Regression_Precision); - Serial.print("Regression Slope : "); Serial.println(DataSet.Linear_Regression_Slope(Data, Linear_Regression_Precision), 4); - Serial.print("Regression Offset : "); Serial.println(DataSet.Linear_Regression_Offset(Data, Linear_Regression_Precision), 4); - Serial.print("Regression R2 : "); Serial.println(DataSet.Linear_Regression_R2(Data, Linear_Regression_Precision), 4); - Serial.println("------------------------"); - - delay(10000); + // Delay + delay(1000); } diff --git a/examples/Stream/Stream.ino b/examples/Stream/Stream.ino index 4f042bd..8cea48f 100644 --- a/examples/Stream/Stream.ino +++ b/examples/Stream/Stream.ino @@ -1,7 +1,5 @@ #include -Statistical DataSet; - void setup() { // Start Serial @@ -11,23 +9,28 @@ void setup() { void loop() { - // Set Data + // Construct Object + Stream_Stats _Stream; + + // Learn Print Serial.println("Learning Data..."); - DataSet.Stream_Statistic(125.2); - DataSet.Stream_Statistic(122.2); - DataSet.Stream_Statistic(121.2); - DataSet.Stream_Statistic(128.2); - DataSet.Stream_Statistic(132.2); + + // Set Data + _Stream.Add(125.2); + _Stream.Add(122.2); + _Stream.Add(121.2); + _Stream.Add(128.2); + _Stream.Add(132.2); // Print Calculations - Serial.print("Stream Data Count : "); Serial.println(DataSet.Stream_Data_Count); - Serial.print("Stream Avg Value : "); Serial.println(DataSet.Stream_Average); - Serial.print("Stream Min Value : "); Serial.println(DataSet.Stream_Minimum); - Serial.print("Stream Max Value : "); Serial.println(DataSet.Stream_Maximum); + Serial.print("Stream Data Count : "); Serial.println(_Stream.Get_Data_Count()); + Serial.print("Stream Avg Value : "); Serial.println(_Stream.Get_Average()); + Serial.print("Stream Min Value : "); Serial.println(_Stream.Get_Min()); + Serial.print("Stream Max Value : "); Serial.println(_Stream.Get_Max()); Serial.println("----------------"); // Reset - DataSet.Stream_Data_Clear(); + _Stream.Clear(); delay(1000); diff --git a/library.json b/library.json index dba859c..bc00b47 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Statistical", - "version": "1.4.15", + "version": "2.0.2", "keywords": "Statistical, Max, Min, Average, Regression, Data, Sensor, Slope, Offset", "description": "Function calculates statistical parameters of data stream and array", "authors": @@ -30,6 +30,13 @@ "Array.ino" ] }, + { + "name": "FILO_Array", + "base": "examples/FILO_Array/", + "files": [ + "FILO_Array.ino" + ] + }, { "name": "Linear Regression", "base": "examples/Linear_Regression/", diff --git a/library.properties b/library.properties index c6a915e..50c7c4c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Statistical -version=1.4.15 +version=2.0.2 author=Gunce Akkoyun maintainer=Gunce Akkoyun sentence=Statistic, Sum, Max, Min, Sq_Sum, Arithmetic Average, Geometric Average, RMS Average, Ext RMS Average, Bubble Sort, Median, Standard Deviation, Standard Deviation Error, Coefficient Factor, Average, Stream, Regression, Slope, Data, Analyse diff --git a/src/Statistical.cpp b/src/Statistical.cpp deleted file mode 100644 index e220c1b..0000000 --- a/src/Statistical.cpp +++ /dev/null @@ -1,488 +0,0 @@ -/* ******************************************************************************* - * Copyright (C) 2014-2022 Mehmet Gunce Akkoyun Can not be copied and/or - * distributed without the express permission of Mehmet Gunce Akkoyun. - * - * Library : Data Statistical Library - * Code Developer : Mehmet Gunce Akkoyun (akkoyun@me.com) - *********************************************************************************/ - -#include "Statistical.h" - -// Stream Statistics -void Statistical::Stream_Statistic(float _Data) { - - // Calculate Max Value - if (Stream_Data_Count == 0) Stream_Maximum = _Data; - if (Stream_Data_Count > 0 and _Data > Stream_Maximum) Stream_Maximum = _Data; - - // Calculate Min Value - if (Stream_Data_Count == 0) Stream_Minimum = _Data; - if (Stream_Data_Count > 0 and _Data < Stream_Minimum) Stream_Minimum = _Data; - - // Calculate Avg Value - if (Stream_Data_Count == 0) Stream_Average = _Data; - if (Stream_Data_Count > 0) Stream_Average = Stream_Average + ((_Data - Stream_Average) / Stream_Data_Count); - - // Set Data Count (+1) - Stream_Data_Count++; - -} -void Statistical::Stream_Data_Clear(void) { - - // Clear Data Count - Stream_Data_Count = 0; - - // Celar Stream Variables - Stream_Average = 0; - Stream_Maximum = 0; - Stream_Minimum = 0; - -} - -// Linear Regression Statistics -float Statistical::Linear_Regression_Offset(float _Data[][2], uint16_t _Data_Count) { - - // Declare Variables - float _X[_Data_Count]; - float _Y[_Data_Count]; - float _Sum_X = 0; - float _Sum_Y = 0; - float _Sum_X2 = 0; - float _Sum_XY = 0; - float _b0 = 0; - - // Set X and Y Array - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set X - _X[i] = _Data[i][0]; - - // Set Y - _Y[i] = _Data[i][1]; - - } - - // Calculate Sum X - _Sum_X = Array_Sum(_X, _Data_Count); - - // Calculate Sum Y - _Sum_Y = Array_Sum(_Y, _Data_Count); - - // Calculate Sum xy - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set Sum xy - _Sum_XY += _X[i] * _Y[i]; - - } - - // Calculate Sum X2 - _Sum_X2 = Array_Sq_Sum(_X, _Data_Count); - - // Calculate Slope - _b0 = ((_Sum_X2 * _Sum_Y) - (_Sum_X * _Sum_XY)) / ((_Data_Count * _Sum_X2) - (_Sum_X * _Sum_X)); - - // End Function - return(_b0); - -} -float Statistical::Linear_Regression_Slope(float _Data[][2], uint16_t _Data_Count) { - - // Declare Variables - float _X[_Data_Count]; - float _Y[_Data_Count]; - float _Sum_X = 0; - float _Sum_Y = 0; - float _Sum_XY = 0; - float _Sum_X2 = 0; - float _b1 = 0; - - // Set X and Y Array - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set X - _X[i] = _Data[i][0]; - - // Set Y - _Y[i] = _Data[i][1]; - - } - - // Calculate Sum X - _Sum_X = Array_Sum(_X, _Data_Count); - - // Calculate Sum Y - _Sum_Y = Array_Sum(_Y, _Data_Count); - - // Calculate Sum xy - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set Sum xy - _Sum_XY += _X[i] * _Y[i]; - - } - - // Calculate Sum X2 - _Sum_X2 = Array_Sq_Sum(_X, _Data_Count); - - // Calculate Slope - _b1 = ((_Data_Count * _Sum_XY) - (_Sum_X * _Sum_Y)) / ((_Data_Count * _Sum_X2) - (_Sum_X * _Sum_X)); - - // End Function - return(_b1); - -} -float Statistical::Linear_Regression_R2(float _Data[][2], uint16_t _Data_Count) { - - // Declare Variables - float _X[_Data_Count]; - float _Y[_Data_Count]; - float _Mean_X; - float _Mean_Y; - float _x[_Data_Count]; // x = X - Xavg - float _y[_Data_Count]; // y = Y - Yavg - float _Sum_xy; - float _Sum_x2; - float _Sum_y2; - float _R2; - - // Set X and Y Array - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set X - _X[i] = _Data[i][0]; - - // Set Y - _Y[i] = _Data[i][1]; - - } - - // Calculate Mean X - _Mean_X = Array_Arithmetic_Average(_X, _Data_Count); - - // Calculate Mean Y - _Mean_Y = Array_Arithmetic_Average(_Y, _Data_Count); - - // Set x and y Array - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set x - _x[i] = _X[i] - _Mean_X; - - // Set y - _y[i] = _Y[i] - _Mean_Y; - - } - - // Calculate Sum xy - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set Sum xy - _Sum_xy += _x[i] * _y[i]; - - } - - // Calculate Sum x2 - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set Sum x2 - _Sum_x2 += _x[i] * _x[i]; - - } - - // Calculate Sum y2 - for (uint16_t i = 0; i < _Data_Count; i++) { - - // Set Sum y2 - _Sum_y2 += _y[i] * _y[i]; - - } - - // Calculate R2 - _R2 = (_Sum_xy * _Sum_xy) / (_Sum_x2 * _Sum_y2); - - // End Function - return(_R2); - -} - -// Array Statistics -float Statistical::Array_Sum(float _Data[], uint16_t _Data_Count) { - - // Declare Variable - float _Sum = 0; - - // Calculate Array Sum - for (uint16_t i=0; i < _Data_Count; i++) { - - _Sum += (float)_Data[i]; - - } - - // End Function - return(_Sum); - -} -float Statistical::Array_Max(float _Data[], uint16_t _Data_Count) { - - // Declare Variable - float _Max = (float)_Data[0]; - - // Calculate Array Max Value - for (uint16_t i=0; i < _Data_Count; i++) { - - if ((float)_Data[i] > _Max) _Max = (float)_Data[i]; - - } - - // End Function - return(_Max); - -} -float Statistical::Array_Min(float _Data[], uint16_t _Data_Count) { - - // Declare Variable - float _Min = (float)_Data[0]; - - // Calculate Array Min Value - for (uint16_t i=0; i < _Data_Count; i++) { - - if ((float)_Data[i] < _Min) _Min = (float)_Data[i]; - - } - - // End Function - return(_Min); - -} -float Statistical::Array_Sq_Sum(float _Data[], uint16_t _Data_Count) { - - // Declare Variable - float _Sq_Sum = 0; - - // Calculate Array Square Total - for (uint16_t i=0; i < _Data_Count; i++) { - - _Sq_Sum += sq((float)_Data[i]); - - } - - // End Function - return(_Sq_Sum); - -} -float Statistical::Array_Arithmetic_Average(float _Data[], uint16_t _Data_Count) { - - // Declare Variable - float _Sum = 0; - - // Calculate Array Sum - _Sum = Array_Sum(_Data, _Data_Count); - - // Calculate Arithmetic Average - float _Average = _Sum / _Data_Count; - - // End Function - return(_Average); - -} -float Statistical::Array_Geometric_Average(float _Data[], uint16_t _Data_Count) { - - // Declare Variable - float _GAvg_Sum = 0; - - // Calculate Geometric Average Value - for (uint16_t i=0; i < _Data_Count; i++) { - - _GAvg_Sum += (log(_Data[i]) - _GAvg_Sum) / (i + 1); - - } - - // Set Geometric Average Variable - float _GAvg = exp(_GAvg_Sum); - - // End Function - return(_GAvg); - -} -float Statistical::Array_RMS_Average(float _Data[], uint16_t _Data_Count) { - - // Calculate Array Sq Total - float _Sum = Array_Sq_Sum(_Data, _Data_Count); - - // Calculate RSM Average - float _RMS_Avg = (sqrt(_Sum / _Data_Count)); - - // End Function - return(_RMS_Avg); - -} -float Statistical::Array_Ext_RMS_Average(float _Data[], uint16_t _Data_Count) { - - // Calculate Array Sq Total - float _Sum = Array_Sq_Sum(_Data, _Data_Count); - - // Calculate Array Minimum - float _Min = Array_Min(_Data, _Data_Count); - - // Calculate Array Minimum - float _Max = Array_Max(_Data, _Data_Count); - - // Calculate Extended Sum - _Sum -= sq(_Min); - _Sum -= sq(_Max); - - // Calculate RSM Average - float _Ext_RMS_Avg = (sqrt(_Sum / _Data_Count)); - - // End Function - return(_Ext_RMS_Avg); - -} -float Statistical::Array_Median(float _Data[], uint16_t _Data_Count) { - - // Sort Array - Array_Bubble_Sort(_Data, _Data_Count); - - // Declare Variable - float _Median = 0; - - // Calculate Median - if (_Data_Count % 2 == 0) { - - _Median = _Data[_Data_Count / 2]; - - } else { - - _Median = ((_Data[_Data_Count / 2] - 1) + _Data[_Data_Count / 2]) / 2; - - } - - // End Function - return(_Median); - -} -float Statistical::Array_Standard_Deviation(float _Data[], uint16_t _Data_Count) { - - // Calculate Arithmetic Average - float _Avg = Array_Arithmetic_Average(_Data, _Data_Count); - - // Set Standard Deviation Variable - float _SDev = 0; - - // Calculate Array Standard Deviation - for (uint16_t i=0; i < _Data_Count; i++) _SDev += sq(_Data[i] - _Avg); - _SDev = sqrt(_SDev / (_Data_Count - 1)); - - // End Function - return(_SDev); - -} -float Statistical::Array_Standard_Deviation_Error(float _Data[], uint16_t _Data_Count) { - - // Calculate Standard Deviation - float _SDev = Array_Standard_Deviation(_Data, _Data_Count); - - // Calculate Standard Deviation Error - float _SDev_Err = (_SDev / sqrt((float)_Data_Count)); - - // End Function - return(_SDev_Err); - -} -float Statistical::Array_Coefficient_Factor(float _Data[], uint16_t _Data_Count) { - - // Calculate Arithmetic Average - float _Avg = Array_Arithmetic_Average(_Data, _Data_Count); - - // Calculate Standard Deviation - float _SDev = Array_Standard_Deviation(_Data, _Data_Count); - - // Calculate Coefficient Factor - float _Coef = (100 * _SDev / _Avg); - - // End Function - return(_Coef); - -} -float Statistical::Array_Average(float _Data[], int _Data_Count, int _AVG_Type) { - - // Select Average Type - switch (_AVG_Type) { - - case 1: - return(Array_Arithmetic_Average(_Data, _Data_Count)); - break; - - case 2: - return(Array_Geometric_Average(_Data, _Data_Count)); - break; - - case 3: - return(Array_Median(_Data, _Data_Count)); - break; - - case 4: - return(Array_RMS_Average(_Data, _Data_Count)); - break; - - case 5: - return(Array_Ext_RMS_Average(_Data, _Data_Count)); - break; - - default: - return(0); - break; - } - -} - -// Array Misc Functions -void Statistical::Array_Bubble_Sort(float _Data[], uint16_t _Data_Count) { - - // Declare Buffer Variables - uint16_t _New_n; - float _Temp = 0; - - // Sort Array - do { - - _New_n = 1; - - for (uint16_t i=1; i < _Data_Count; i++) { - - if(_Data[i - 1]>_Data[i]){ - - _Temp = _Data[i]; - - _Data[i]=_Data[i - 1]; - - _Data[i - 1] = _Temp; - - _New_n = i; - - } //end if - - } - - } while (_New_n > 1); - -} -void Statistical::Array_FILO(float _Array[][2], uint16_t _Data_Count, float _Data_X, float _Data_Y) { - - - // Redesign Array - for (uint16_t i = 0; i < _Data_Count-1; i++) { - - _Array[i][0] = _Array[i+1][0]; - _Array[i][1] = _Array[i+1][1]; - - } - - // Add New Data - _Array[_Data_Count-1][0] = _Data_X; - _Array[_Data_Count-1][1] = _Data_Y; - -} - -Statistical Stats; \ No newline at end of file diff --git a/src/Statistical.h b/src/Statistical.h index 4bd5724..a776efc 100644 --- a/src/Statistical.h +++ b/src/Statistical.h @@ -14,210 +14,812 @@ #include #endif -class Statistical { +// Array Statistics +template class Array_Stats { -public: + protected: + + // Declare Data Array + Data_Type * Data; - // ************************************************************ - // Public Variables - // ************************************************************ + // Declare Data Count + size_t Data_Count; - // Stream Calculation Variables - uint16_t Stream_Data_Count = 0; - float Stream_Average = 0; - float Stream_Minimum = 0; - float Stream_Maximum = 0; - - // ************************************************************ - // Public Functions - // ************************************************************ - - /** - * @brief Calculate Stream Statistic Parameters - * @version 01.00.00 - * @param _Data Stream Data - */ - void Stream_Statistic(float _Data); - - /** - * @brief Clear Stream Statistic Parameters - * @version 01.00.00 - */ - void Stream_Data_Clear(void); - - /** - * @brief Calculate Regression Offset - * - * @param _Data Data Array - * @param _Data_Count Data Count - * @return float Offset Value - */ - float Linear_Regression_Offset(float _Data[][2], uint16_t _Data_Count); - - /** - * @brief Calculate Regression Slope - * - * @param _Data Data Array - * @param _Data_Count Data Count - * @return float Slope Value - */ - float Linear_Regression_Slope(float _Data[][2], uint16_t _Data_Count); - - /** - * @brief Calculate Regression R2 - * - * @param _Data Data Array - * @param _Data_Count Data Count - * @return float R2 Value - */ - float Linear_Regression_R2(float _Data[][2], uint16_t _Data_Count); - - /** - * @brief Calculate the array sum. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Sum value of array. - */ - float Array_Sum(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array max value. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Max value of array. - */ - float Array_Max(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array min value. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Min value of array. - */ - float Array_Min(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array square sum. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Square total value of array. - */ - float Array_Sq_Sum(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array arithmetic average. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Arithmetic average value of array. - */ - float Array_Arithmetic_Average(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array geometric average. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Geometric average value of array. - */ - float Array_Geometric_Average(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array RMS average. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - RMS average value of array. - */ - float Array_RMS_Average(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array extended RMS average. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Extended RMS average value of array. - */ - float Array_Ext_RMS_Average(float _Data[], uint16_t _Data_Count); - - /** - * @brief Sort the array from min to max. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - */ - void Array_Bubble_Sort(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array median average. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Median average value of array. - */ - float Array_Median(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array standard deviation. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Standard deviation value of array. - */ - float Array_Standard_Deviation(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array standard deviation error. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Standard deviation error value of array. - */ - float Array_Standard_Deviation_Error(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array coefficient factor. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Coefficient factor value of array. - */ - float Array_Coefficient_Factor(float _Data[], uint16_t _Data_Count); - - /** - * @brief Calculate the array average batch. - * @version 01.00.00 - * @param _Data Data array - * @param _Data_Count Array size - * @return float - Average value of array. - */ - float Array_Average(float _Data[], int _Data_Count, int _AVG_Type); - - /** - * @brief Set Array FILO - * @version 01.00.00 - * @param _Array Data array - * @param _Data_Count Array size - * @param _Data New Array Data - */ - void Array_FILO(float _Array[][2], uint16_t _Data_Count, float _Data_X, float _Data_Y); - -private: - - // ************************************************************ - // Private Variables - // ************************************************************ + public: + + // Define Average Type + enum Average_Type { + Arithmetic_Avg = (uint8_t)1, + Geometric_Avg = (uint8_t)2, + RMS_Avg = (uint8_t)3, + Ext_RMS_Avg = (uint8_t)4 + }; + + /** + * @brief Function constructor + * @version 01.00.00 + * @param _Data Data array + * @param _Data_Count Array size + */ + Array_Stats(Data_Type * _Data, size_t _Data_Count) : Data(_Data), Data_Count(_Data_Count) { + + } + + /** + * @brief Calculate the array average batch. + * @version 01.00.00 + * @param _Average_Type Average Type + * @return float - Average value of array. + */ + float Average(uint8_t _Average_Type) { + + // Control for Type Limit + if (_Average_Type > 4) return(0); + + // Declare Variable + double _Result = 0; + + // Calculate Average + if (_Average_Type == 1) _Result = this->Arithmetic_Average(); + if (_Average_Type == 2) _Result = this->Geometric_Average(); + if (_Average_Type == 3) _Result = this->RMS_Average(); + if (_Average_Type == 4) _Result = this->Ext_RMS_Average(); + + // End Function + return(_Result); + + } + + /** + * @brief Get data array size + * @version 01.00.00 + */ + size_t Size(void) { + + // End Function + return(this->Data_Count); + + } + + /** + * @brief Calculate the array sum. + * @version 01.00.00 + * @return Sum value of array. + */ + float Sum(void) { + + // Declare Variable + double _Sum = 0; + + // Calculate Array Sum + for (size_t i = 0; i < this->Data_Count; i++) _Sum += (double)this->Data[i]; + + // Handle Calculation + if (isnan(_Sum)) _Sum = 0; + if (isinf(_Sum)) _Sum = 0; + + // End Function + return(_Sum); + + } + + /** + * @brief Calculate the array max value. + * @version 01.00.00 + * @return float - Max value of array. + */ + float Max(void) { + + // Declare Variable + double _Max = (double)this->Data[0]; + + // Calculate Array Max Value + for (uint16_t i=0; i < this->Data_Count; i++) if ((double)this->Data[i] > _Max) _Max = (double)this->Data[i]; + + // Handle Calculation + if (isnan(_Max)) _Max = 0; + if (isinf(_Max)) _Max = 0; + + // End Function + return(_Max); + + } + + /** + * @brief Calculate the array min value. + * @version 01.00.00 + * @return float - Min value of array. + */ + float Min(void) { + + // Declare Variable + double _Min = (double)this->Data[0]; + + // Calculate Array Min Value + for (uint16_t i=0; i < this->Data_Count; i++) if ((double)this->Data[i] < _Min) _Min = (double)this->Data[i]; + + // Handle Calculation + if (isnan(_Min)) _Min = 0; + if (isinf(_Min)) _Min = 0; + + // End Function + return(_Min); + + } + + /** + * @brief Calculate the array square sum. + * @version 01.00.00 + * @return float - Square total value of array. + */ + float Sq_Sum(void) { + + // Declare Variable + double _Sq_Sum = 0; + + // Calculate Array Square Total + for (uint16_t i = 0; i < this->Data_Count; i++) _Sq_Sum += sq((double)this->Data[i]); + + // Handle Calculation + if (isnan(_Sq_Sum)) _Sq_Sum = 0; + if (isinf(_Sq_Sum)) _Sq_Sum = 0; + + // End Function + return(_Sq_Sum); + + } + + /** + * @brief Calculate the array arithmetic average. + * @version 01.00.00 + * @return float - Arithmetic average value of array. + */ + float Arithmetic_Average(void) { + + // Calculate Arithmetic Average + double _Average = Sum() / this->Data_Count; + + // Handle Calculation + if (isnan(_Average)) _Average = 0; + if (isinf(_Average)) _Average = 0; + + // End Function + return(_Average); + + } + + /** + * @brief Calculate the array geometric average. + * @version 01.00.00 + * @return float - Geometric average value of array. + */ + float Geometric_Average(void) { + + // Declare Variable + double _GAvg_Sum = 0; + + // Calculate Geometric Average Value + for (size_t i = 0; i < this->Data_Count; i++) _GAvg_Sum += (log(this->Data[i]) - _GAvg_Sum) / (i + 1); + + // Set Geometric Average Variable + double _GAvg = exp(_GAvg_Sum); + + // Handle Calculation + if (isnan(_GAvg)) _GAvg = 0; + if (isinf(_GAvg)) _GAvg = 0; + + // End Function + return(_GAvg); + + } + + /** + * @brief Calculate the array RMS average. + * @version 01.00.00 + * @return float - RMS average value of array. + */ + float RMS_Average(void) { + + // Calculate Array Sq Total + double _Sum = Sq_Sum(); + + // Calculate RSM Average + double _RMS_Avg = (sqrt(_Sum / this->Data_Count)); + + // Handle Calculation + if (isnan(_RMS_Avg)) _RMS_Avg = 0; + if (isinf(_RMS_Avg)) _RMS_Avg = 0; + + // End Function + return(_RMS_Avg); + + } + + /** + * @brief Calculate the array extended RMS average. + * @version 01.00.00 + * @return float - Extended RMS average value of array. + */ + float Ext_RMS_Average(void) { + + // Calculate Array Sq Total + double _Sum = Sq_Sum(); + + // Calculate Extended Sum + _Sum -= sq(Min()); + _Sum -= sq(Max()); + + // Calculate RSM Average + double _Ext_RMS_Avg = (sqrt(_Sum / this->Data_Count)); + + // Handle Calculation + if (isnan(_Ext_RMS_Avg)) _Ext_RMS_Avg = 0; + if (isinf(_Ext_RMS_Avg)) _Ext_RMS_Avg = 0; + + // End Function + return(_Ext_RMS_Avg); + + } + + /** + * @brief Calculate the array quartile. + * @version 01.00.00 + * @return float - Quartile value of array. + */ + float Quartile(const uint8_t _Q) { + + // Sort Array + Bubble_Sort(); + + // Declare Variable + double _Quartile = 0; + double _Multiplier = 0; + + // Decide Q Multiplier + if (_Q == 1) _Multiplier = 0.25; + if (_Q == 2) _Multiplier = 0.50; + if (_Q == 3) _Multiplier = 0.75; + + // Calculate Quartile Position + double _Position = ((this->Data_Count + 1) * _Multiplier); + + // Calculate Quartile + _Quartile = this->Data[(int)_Position - 1] + ((_Position - (int)_Position) * (this->Data[(int)_Position] - this->Data[(int)_Position - 1])); + + // Handle Calculation + if (isnan(_Quartile)) _Quartile = 0; + if (isinf(_Quartile)) _Quartile = 0; + + // End Function + return(_Quartile); + + } + + /** + * @brief Calculate the array IQR. + * @version 01.00.00 + * @return float - IQR value of array. + */ + float IQR(void) { + + // Calculate Q1 + double _Q1 = Quartile(1); + + // Calculate Q3 + double _Q3 = Quartile(3); + + // Calculate IQR + double _IQR = _Q3 - _Q1; + + // Handle Calculation + if (isnan(_IQR)) _IQR = 0; + if (isinf(_IQR)) _IQR = 0; + + // End Function + return(_IQR); + + } + + /** + * @brief Calculate the array standard deviation. + * @version 01.00.00 + * @return float - Standard deviation value of array. + */ + float Standard_Deviation(void) { + + // Calculate Arithmetic Average + double _Avg = Arithmetic_Average(); + + // Set Standard Deviation Variable + double _SDev = 0; + + // Calculate Array Standard Deviation + for (uint16_t i = 0; i < this->Data_Count; i++) _SDev += sq(this->Data[i] - _Avg); + _SDev = sqrt(_SDev / (this->Data_Count - 1)); + + // Handle Calculation + if (isnan(_SDev)) _SDev = 0; + if (isinf(_SDev)) _SDev = 0; + + // End Function + return(_SDev); + + } + + /** + * @brief Calculate the array standard deviation error. + * @version 01.00.00 + * @return float - Standard deviation error value of array. + */ + float Standard_Deviation_Error(void) { + + // Calculate Standard Deviation + double _SDev = Standard_Deviation(); + + // Calculate Standard Deviation Error + double _SDev_Err = (_SDev / sqrt((double)this->Data_Count)); + + // Handle Calculation + if (isnan(_SDev_Err)) _SDev_Err = 0; + if (isinf(_SDev_Err)) _SDev_Err = 0; + + // End Function + return(_SDev_Err); + + } + + /** + * @brief Calculate the array coefficient factor. + * @version 01.00.00 + * @return float - Coefficient factor value of array. + */ + float Coefficient_Factor(void) { + + // Calculate Arithmetic Average + double _Avg = Arithmetic_Average(); + + // Calculate Standard Deviation + double _SDev = Standard_Deviation(); + + // Calculate Coefficient Factor + double _Coef = (100 * _SDev / _Avg); + + // Handle Calculation + if (isnan(_Coef)) _Coef = 0; + if (isinf(_Coef)) _Coef = 0; + + // End Function + return(_Coef); + + } + + /** + * @brief Calculate the array variance. + * @version 01.00.00 + * @return float - Variance value of array. + */ + float Variance(void) { + + // Declare Variable + double _Var = 0; + + // Calculate Arithmetic Average + double _Avg = Arithmetic_Average(); + + // Calculate Array Variance Value + for (size_t i = 0; i < this->Data_Count; i++) _Var += sq(this->Data[i] - _Avg); + + // Handle Calculation + if (isnan(_Var)) _Var = 0; + if (isinf(_Var)) _Var = 0; + + // End Function + return(_Var / (this->Data_Count - 1)); + + } + + /** + * @brief Sort the array from min to max. + * @version 01.00.00 + */ + void Bubble_Sort(void) { + + // Declare Buffer Variables + size_t _New_n; + Data_Type _Temp = 0; + + // Sort Array + do { + + _New_n = 1; + + for (size_t i = 1; i < this->Data_Count; i++) { + + if(this->Data[i - 1]>this->Data[i]){ + + _Temp = this->Data[i]; + + this->Data[i]=this->Data[i - 1]; + + this->Data[i - 1] = _Temp; + + _New_n = i; + + } //end if + + } + + } while (_New_n > 1); + + } + + /** + * @brief Set Array FILO + * @version 01.00.00 + * @param _Data_X Array size + * @param _Data_Y New Array Data + */ + void FILO(float _Array[][2], uint16_t _Data_Count, float _Data_X, float _Data_Y) { - // ************************************************************ - // Private Functions - // ************************************************************ + // Redesign Array + for (uint16_t i = 0; i < _Data_Count-1; i++) { + + _Array[i][0] = _Array[i+1][0]; + _Array[i][1] = _Array[i+1][1]; + + } + + // Add New Data + _Array[_Data_Count-1][0] = _Data_X; + _Array[_Data_Count-1][1] = _Data_Y; + + } + + /** + * @brief Print Full Array + * @version 01.00.00 + */ + void Array(void) { + + // Loop Data Count + for (uint8_t i = 0; i < this->Data_Count; i++) { + + // Print Array + Serial.print(F("[")); Serial.print(this->Data[i]); Serial.print(F("] ")); + + } + + // End Line + Serial.println(F("")); + + } + + /** + * @brief Set FILO array size + * @version 01.00.00 + * @param _FILO_Size Array size + */ + bool Set_FILO_Size(const size_t _FILO_Size) { + + // Control for Max Filo Size + if (_FILO_Size > 50) return(false); + + // Resize Array + realloc(this->Data, _FILO_Size); + + // Set Array Size Variable + this->Data_Count = _FILO_Size; + + // Reset Array + for (size_t i = 0; i < this->Data_Count; i++) this->Data[i] = 0; + + // End Function + return(true); + + } + + /** + * @brief FILO data add to array + * @version 01.00.00 + * @param _Data data + */ + void FILO_Add_Data(const Data_Type _Data) { + + // Redesign Array + for (size_t i = 0; i < this->Data_Count; i++) this->Data[i] = this->Data[i + 1]; + + // Add New Data + this->Data[this->Data_Count - 1] = _Data; + + } + +}; + +// Stream Statistics +template class Stream_Stats { + + private: + + // Declare Data Count + uint16_t Data_Count; + + // Declare Stat Variable + Data_Type Average; + Data_Type Min; + Data_Type Max; + + public: + + /** + * @brief Function constructor + * @version 01.00.00 + */ + Stream_Stats(void) : Data_Count(0), Average(0), Min(0), Max(0) { + + }; + + /** + * @brief Add Stream Data + * @version 01.00.00 + * @param _Data Stream Data + */ + void Add(Data_Type _Data) { + + // Calculate Max Value + if (this->Data_Count == 0) this->Max = _Data; + if (this->Data_Count > 0 and _Data > this->Max) this->Max = _Data; + + // Calculate Min Value + if (this->Data_Count == 0) this->Min = _Data; + if (this->Data_Count > 0 and _Data < this->Min) this->Min = _Data; + + // Calculate Avg Value + if (this->Data_Count == 0) this->Average = _Data; + if (this->Data_Count > 0) this->Average = this->Average + ((_Data - this->Average) / this->Data_Count); + + // Set Data Count (+1) + this->Data_Count++; + + }; + + /** + * @brief Clear Stream Statistic Parameters + * @version 01.00.00 + */ + void Clear(void) { + + // Clear Data Count + this->Data_Count = 0; + + // Celar Stream Variables + this->Max = 0; + this->Min = 0; + this->Average = 0; + + }; + + /** + * @brief Get stream data count + * @version 01.00.00 + */ + uint16_t Get_Data_Count(void) { + + // Return Data Count + return(this->Data_Count); + + }; + + /** + * @brief Get stream average + * @version 01.00.00 + */ + float Get_Average(void) { + + // Return Average + return(this->Average); + + }; + + /** + * @brief Get stream minimum value + * @version 01.00.00 + */ + float Get_Min(void) { + + // Return Min + return(this->Min); + + }; + + /** + * @brief Get stream maximum value + * @version 01.00.00 + */ + float Get_Max(void) { + + // Return Max + return(this->Max); + + }; }; -extern Statistical Stats; +// Linear Regression Statistics +template class Linear_Regression { + + protected: + + // Declare Data Array X + Data_Type_X * Data_X; + + // Declare Data Array Y + Data_Type_Y * Data_Y; + + // Declare Data Count + size_t Data_Count; + + public: + + /** + * @brief Function constructor + * @version 01.00.00 + */ + Linear_Regression(Data_Type_X * _Data_X, Data_Type_Y * _Data_Y, size_t _Data_Count) : Data_X(_Data_X), Data_Y(_Data_Y), Data_Count(_Data_Count) { + + } + + /** + * @brief Calculate Regression Offset + * @version 01.00.00 + * @return float Offset Value + */ + float Offset(void) { + + // Declare X Array + Array_Stats Data_Array_X(Data_X, Data_Count); + + // Declare Y Array + Array_Stats Data_Array_Y(Data_Y, Data_Count); + + // Declare Variables + float _Sum_X = 0; + float _Sum_Y = 0; + float _Sum_X2 = 0; + float _Sum_XY = 0; + float _b0 = 0; + + // Calculate Sum X + _Sum_X = Data_Array_X.Sum(); + + // Calculate Sum Y + _Sum_Y = Data_Array_Y.Sum(); + + // Calculate Sum X2 + _Sum_X2 = Data_Array_X.Sq_Sum(); + + // Calculate Sum XY + for (size_t i = 0; i < Data_Count; i++) _Sum_XY += Data_X[i] * Data_Y[i]; + + // Calculate Slope + _b0 = ((_Sum_X2 * _Sum_Y) - (_Sum_X * _Sum_XY)) / ((Data_Count * _Sum_X2) - (_Sum_X * _Sum_X)); + + // End Function + return(_b0); + + } + + /** + * @brief Calculate Regression Slope + * @version 01.00.00 + * @return float Slope Value + */ + float Slope(void) { + + // Declare X Array + Array_Stats Data_Array_X(Data_X, Data_Count); + + // Declare Y Array + Array_Stats Data_Array_Y(Data_Y, Data_Count); + + // Declare Variables + float _Sum_X = 0; + float _Sum_Y = 0; + float _Sum_XY = 0; + float _Sum_X2 = 0; + float _b1 = 0; + + // Calculate Sum X + _Sum_X = Data_Array_X.Sum(); + + // Calculate Sum Y + _Sum_Y = Data_Array_Y.Sum(); + + // Calculate Sum X2 + _Sum_X2 = Data_Array_X.Sq_Sum(); + + // Calculate Sum XY + for (size_t i = 0; i < Data_Count; i++) _Sum_XY += Data_X[i] * Data_Y[i]; + + // Calculate Slope + _b1 = ((Data_Count * _Sum_XY) - (_Sum_X * _Sum_Y)) / ((Data_Count * _Sum_X2) - (_Sum_X * _Sum_X)); + + // End Function + return(_b1); + + } + + /** + * @brief Calculate Regression R2 + * @version 01.00.00 + * @return float R2 Value + */ + float R2(void) { + + // Declare X Array + Array_Stats Data_Array_X(Data_X, Data_Count); + + // Declare Y Array + Array_Stats Data_Array_Y(Data_Y, Data_Count); + + // Declare Variables + float _Mean_X = 0; + float _Mean_Y = 0; + float _Sum_x2 = 0; + float _Sum_y2 = 0; + float _Sum_xy = 0; + float _x[Data_Count]; // x = X - Xavg + float _y[Data_Count]; // y = Y - Yavg + float _R2 = 0; + + // Calculate Mean X + _Mean_X = Data_Array_X.Arithmetic_Average(); + + // Calculate Mean Y + _Mean_Y = Data_Array_Y.Arithmetic_Average(); + + // Set x and y Array + for (size_t i = 0; i < Data_Count; i++) { + + // Set x + _x[i] = Data_X[i] - _Mean_X; + + // Set y + _y[i] = Data_Y[i] - _Mean_Y; + + } + + // Calculate Sum xy + for (size_t i = 0; i < Data_Count; i++) { + + // Set Sum xy + _Sum_xy += _x[i] * _y[i]; + + } + + // Calculate Sum x2 + for (size_t i = 0; i < Data_Count; i++) { + + // Set Sum x2 + _Sum_x2 += _x[i] * _x[i]; + + } + + // Calculate Sum y2 + for (size_t i = 0; i < Data_Count; i++) { + + // Set Sum y2 + _Sum_y2 += _y[i] * _y[i]; + + } + + // Calculate R2 + _R2 = (_Sum_xy * _Sum_xy) / (_Sum_x2 * _Sum_y2); + + // End Function + return(_R2); + + } + +}; #endif /* defined(__Statistical__) */