Implemented ndpi_is_outlier() for detecting outliers using z-score

This commit is contained in:
Luca
2023-12-28 19:59:54 +01:00
parent 1366518bff
commit 2f657cb8f9
3 changed files with 90 additions and 26 deletions
+15
View File
@@ -5500,6 +5500,20 @@ void pearsonUnitTest() {
/* *********************************************** */ /* *********************************************** */
void outlierUnitTest() {
u_int32_t data[] = {1, 2, 3, 4, 5};
u_int16_t num = sizeof(data) / sizeof(u_int32_t);
u_int16_t value_to_check = 8;
float threshold = 1.5, lower, upper;
float is_outlier = ndpi_is_outlier(data, num, value_to_check,
threshold, &lower, &upper);
/* printf("%.2f < %u < %.2f : %s\n", lower, value_to_check, upper, is_outlier ? "OUTLIER" : "OK"); */
assert(is_outlier == true);
}
/* *********************************************** */
void domainSearchUnitTest() { void domainSearchUnitTest() {
ndpi_domain_classify *sc = ndpi_domain_classify_alloc(); ndpi_domain_classify *sc = ndpi_domain_classify_alloc();
char *domain = "ntop.org"; char *domain = "ntop.org";
@@ -5588,6 +5602,7 @@ int main(int argc, char **argv) {
exit(0); exit(0);
#endif #endif
outlierUnitTest();
pearsonUnitTest(); pearsonUnitTest();
binaryBitmapUnitTest(); binaryBitmapUnitTest();
domainSearchUnitTest(); domainSearchUnitTest();
+23
View File
@@ -1949,6 +1949,29 @@ extern "C" {
/* ******************************* */ /* ******************************* */
/*
* Checks if a specified value is an outlier with respect to past values
* using the Z-score.
*
* @par past_valuea = List of observed past values (past knowledge)
* @par num_past_values = Number of observed past values
* @par value_to_check = The value to be checked with respect to past values
* @par threshold = Threshold on z-score:. Typical values:
* t = 1 - The value to check should not exceed the past values
* t > 1 - The value to check has to be within (t * stddev) boundaries
* @par lower - [out] Lower threshold
* @par upper - [out] Upper threshold
*
* @return true if the specified value is an outlier, false otherwise
*
*/
bool ndpi_is_outlier(u_int32_t *past_values, u_int32_t num_past_values,
u_int32_t value_to_check, float threshold,
float *lower, float *upper);
/* ******************************* */
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);
/* ******************************* */ /* ******************************* */
+26
View File
@@ -1648,6 +1648,31 @@ u_int ndpi_find_outliers(u_int32_t *values, bool *outliers, u_int32_t num_values
return(ret); return(ret);
} }
/* *********************************************************** */
/* Check if the specified value is an outlier with respect to the past values */
bool ndpi_is_outlier(u_int32_t *past_values, u_int32_t num_past_values,
u_int32_t value_to_check, float threshold,
float *lower, float *upper) {
struct ndpi_analyze_struct *data = ndpi_alloc_data_analysis_from_series(past_values, num_past_values);
float mean, stddev, v;
if(!data) return(false);
mean = ndpi_data_mean(data);
stddev = ndpi_data_stddev(data);
/* The mimimum threshold is 1 (i.e. the value of the stddev) */
if(threshold < 1.) threshold = 1.;
v = threshold * stddev;
*lower = mean - v, *upper = mean + v;
ndpi_free_data_analysis(data, 1 /* free memory */);
return(((value_to_check < *lower) || (value_to_check > *upper)) ? true : false);
}
/* ********************************************************************************* */ /* ********************************************************************************* */
/* /*
@@ -1707,6 +1732,7 @@ double ndpi_pearson_correlation(u_int32_t *values_a, u_int32_t *values_b, u_int1
return(covariance / sqrt(variance_a * variance_b)); return(covariance / sqrt(variance_a * variance_b));
} }
/* ********************************************************************************* */
/* ********************************************************************************* */ /* ********************************************************************************* */
static const u_int16_t crc16_ccitt_table[256] = { static const u_int16_t crc16_ccitt_table[256] = {