Source
int zbx_eval_calc_histogram_quantile(const double q, const zbx_vector_dbl_t *values, const char *err_fn,
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
typedef struct
{
double upper;
double count;
}
zbx_histogram_t;
ZBX_VECTOR_DECL(histogram, zbx_histogram_t)
ZBX_VECTOR_IMPL(histogram, zbx_histogram_t)
static int zbx_is_normal_double(double dbl)
{
if (FP_ZERO != fpclassify(dbl) && FP_NORMAL != fpclassify(dbl))
return FAIL;
return SUCCEED;
}
/******************************************************************************
* *
* Purpose: calculates arithmetic mean (i.e. average) *
* *
* Parameters: v - [IN] non-empty vector with input data *
* *
* Return value: arithmetic mean value *
* *
******************************************************************************/
static double calc_arithmetic_mean(const zbx_vector_dbl_t *v)
{
double sum = 0;
int i;
for (i = 0; i < v->values_num; i++)
sum += v->values[i];
return sum / v->values_num;
}
/******************************************************************************
* *
* Purpose: evaluates function 'kurtosis' *
* *
* Parameters: values - [IN] non-empty vector with input data *
* result - [OUT] calculated value *
* error - [OUT] dynamically allocated error message *
* *
* Return value: SUCCEED - evaluated successfully *
* FAIL - failed to evaluate function (see 'error') *
* *
******************************************************************************/
int zbx_eval_calc_kurtosis(zbx_vector_dbl_t *values, double *result, char **error)
{
double mean, second_moment = 0, fourth_moment = 0, second_moment2, res;
int i;
/* step 1: calculate arithmetic mean */
mean = calc_arithmetic_mean(values);
if (SUCCEED != zbx_is_normal_double(mean))
goto err;
/* step 2: calculate the second and the fourth moments */
for (i = 0; i < values->values_num; i++)
{
double diff = values->values[i] - mean;
second_moment += diff * diff;
fourth_moment += diff * diff * diff * diff;
}
second_moment /= values->values_num;
fourth_moment /= values->values_num;