From 937357e4bc55610f116f66d15a8e0fc1e260c02c Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Tue, 12 Oct 2021 13:08:16 +0200 Subject: Implemented ndpi_ses_fitting() and ndpi_des_fitting() for comuting the best alpha/beta values for exponential smoothing --- src/include/ndpi_api.h.in | 6 ++- src/lib/ndpi_analyze.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/include/ndpi_api.h.in b/src/include/ndpi_api.h.in index ff276fe3e..a9c3cdb7b 100644 --- a/src/include/ndpi_api.h.in +++ b/src/include/ndpi_api.h.in @@ -1528,12 +1528,14 @@ extern "C" { int ndpi_ses_init(struct ndpi_ses_struct *ses, double alpha, float significance); int ndpi_ses_add_value(struct ndpi_ses_struct *ses, const u_int64_t _value, double *forecast, double *confidence_band); - + void ndpi_ses_fitting(double *values, u_int32_t num_values, float *ret_alpha); + /* ******************************* */ int ndpi_des_init(struct ndpi_des_struct *des, double alpha, double beta, float significance); int ndpi_des_add_value(struct ndpi_des_struct *des, const u_int64_t _value, double *forecast, double *confidence_band); - + void ndpi_des_fitting(double *values, u_int32_t num_values, float *ret_alpha, float *ret_beta); + /* ******************************* */ int ndpi_jitter_init(struct ndpi_jitter_struct *hw, u_int16_t num_periods); diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c index b8e009c6f..4879954ab 100644 --- a/src/lib/ndpi_analyze.c +++ b/src/lib/ndpi_analyze.c @@ -1232,6 +1232,61 @@ int ndpi_ses_add_value(struct ndpi_ses_struct *ses, const u_int64_t _value, doub return(rc); } +/* *********************************************************** */ + +/* + Computes the best alpha value using the specified values used for training +*/ +void ndpi_ses_fitting(double *values, u_int32_t num_values, float *ret_alpha) { + u_int i; + float alpha, best_alpha; + double sse, lowest_sse; + int trace = 0; + + lowest_sse = 0, best_alpha = 0; + + for(alpha=0.1; alpha<0.99; alpha += 0.05) { + struct ndpi_ses_struct ses; + + ndpi_ses_init(&ses, alpha, 0.05); + + if(trace) + printf("\nDouble Exponential Smoothing [alpha: %.2f]\n", alpha); + + sse = 0; + + for(i=0; i