Implemented ndpi_predict_linear() for predicting a timeseries value overtime
This commit is contained in:
@@ -5150,6 +5150,20 @@ void zscoreUnitTest() {
|
|||||||
|
|
||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
|
|
||||||
|
void linearUnitTest() {
|
||||||
|
u_int32_t values[] = {15, 27, 38, 49, 68, 72, 90, 150, 175, 203};
|
||||||
|
u_int32_t prediction;
|
||||||
|
u_int32_t const num = NDPI_ARRAY_LENGTH(values);
|
||||||
|
bool do_trace = false;
|
||||||
|
int rc = ndpi_predict_linear(values, num, 2*num, &prediction);
|
||||||
|
|
||||||
|
if(do_trace) {
|
||||||
|
printf("[rc: %d][predicted value: %u]\n", rc, prediction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* *********************************************** */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@brief MAIN FUNCTION
|
@brief MAIN FUNCTION
|
||||||
**/
|
**/
|
||||||
@@ -5189,6 +5203,7 @@ int main(int argc, char **argv) {
|
|||||||
exit(0);
|
exit(0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
linearUnitTest();
|
||||||
zscoreUnitTest();
|
zscoreUnitTest();
|
||||||
sesUnitTest();
|
sesUnitTest();
|
||||||
desUnitTest();
|
desUnitTest();
|
||||||
|
|||||||
@@ -1833,6 +1833,22 @@ extern "C" {
|
|||||||
|
|
||||||
/* ******************************* */
|
/* ******************************* */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Predicts a value using simple linear regression
|
||||||
|
* Z-Score = (Value - Mean) / StdDev
|
||||||
|
*
|
||||||
|
* @par values = pointer to the individual values to be analyzed [in]
|
||||||
|
* @par num_values = number of 'values' [in]
|
||||||
|
* @par predict_periods = number of periods for which we want to make the prediction [in]
|
||||||
|
* @par prediction = predicted value after 'predict_periods' [out]
|
||||||
|
*
|
||||||
|
* @return The number of outliers found
|
||||||
|
*/
|
||||||
|
int ndpi_predict_linear(u_int32_t *values, u_int32_t num_values,
|
||||||
|
u_int32_t predict_periods, u_int32_t *prediction);
|
||||||
|
|
||||||
|
/* ******************************* */
|
||||||
|
|
||||||
u_int32_t ndpi_quick_16_byte_hash(u_int8_t *in_16_bytes_long);
|
u_int32_t ndpi_quick_16_byte_hash(u_int8_t *in_16_bytes_long);
|
||||||
|
|
||||||
/* ******************************* */
|
/* ******************************* */
|
||||||
|
|||||||
+42
-1
@@ -1619,12 +1619,47 @@ u_int ndpi_find_outliers(u_int32_t *values, bool *outliers, u_int32_t num_values
|
|||||||
return(ret);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************/
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Simple Linear regression [https://en.wikipedia.org/wiki/Simple_linear_regression]
|
||||||
|
https://www.tutorialspoint.com/c-program-to-compute-linear-regression
|
||||||
|
*/
|
||||||
|
int ndpi_predict_linear(u_int32_t *values, u_int32_t num_values,
|
||||||
|
u_int32_t predict_periods, u_int32_t *prediction) {
|
||||||
|
u_int i;
|
||||||
|
float m, c, d;
|
||||||
|
float sumx = 0, sumx_square = 0, sumy = 0, sumxy = 0;
|
||||||
|
|
||||||
|
for(i = 0; i < num_values; i++) {
|
||||||
|
float y = values[i];
|
||||||
|
float x = i + 1;
|
||||||
|
|
||||||
|
sumx = sumx+x;
|
||||||
|
sumx_square = sumx_square + (x * x);
|
||||||
|
sumy = sumy + y;
|
||||||
|
sumxy = sumxy + (x * y);
|
||||||
|
}
|
||||||
|
|
||||||
|
d = (num_values * sumx_square) - (sumx * sumx);
|
||||||
|
|
||||||
|
if(d == 0) return(-1);
|
||||||
|
|
||||||
|
m = ((num_values * sumxy) - (sumx * sumy)) / d; /* beta */
|
||||||
|
c = ((sumy * sumx_square) - (sumx * sumxy)) / d; /* alpha */
|
||||||
|
|
||||||
|
*prediction = c + (m * (predict_periods + num_values - 1));
|
||||||
|
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
/* ********************************************************** */
|
/* ********************************************************** */
|
||||||
/* http://home.thep.lu.se/~bjorn/crc/crc32_fast.c */
|
/* http://home.thep.lu.se/~bjorn/crc/crc32_fast.c */
|
||||||
/* ********************************************************** */
|
/* ********************************************************** */
|
||||||
|
|
||||||
|
|
||||||
static uint32_t crc32_for_byte(uint32_t r) {
|
static uint32_t crc32_for_byte(uint32_t r) {
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
@@ -1633,6 +1668,8 @@ static uint32_t crc32_for_byte(uint32_t r) {
|
|||||||
return r ^ (uint32_t)0xFF000000L;
|
return r ^ (uint32_t)0xFF000000L;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
/* Any unsigned integer type with at least 32 bits may be used as
|
/* Any unsigned integer type with at least 32 bits may be used as
|
||||||
* accumulator type for fast crc32-calulation, but unsigned long is
|
* accumulator type for fast crc32-calulation, but unsigned long is
|
||||||
* probably the optimal choice for most systems. */
|
* probably the optimal choice for most systems. */
|
||||||
@@ -1651,6 +1688,8 @@ static void init_tables(uint32_t* table, uint32_t* wtable) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
static void __crc32(const void* data, size_t n_bytes, uint32_t* crc) {
|
static void __crc32(const void* data, size_t n_bytes, uint32_t* crc) {
|
||||||
static uint32_t table[0x100], wtable[0x100*sizeof(accum_t)];
|
static uint32_t table[0x100], wtable[0x100*sizeof(accum_t)];
|
||||||
size_t n_accum = n_bytes/sizeof(accum_t);
|
size_t n_accum = n_bytes/sizeof(accum_t);
|
||||||
@@ -1667,6 +1706,8 @@ static void __crc32(const void* data, size_t n_bytes, uint32_t* crc) {
|
|||||||
*crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
|
*crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
u_int32_t ndpi_crc32(const void* data, size_t n_bytes) {
|
u_int32_t ndpi_crc32(const void* data, size_t n_bytes) {
|
||||||
u_int32_t crc = 0;
|
u_int32_t crc = 0;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user