Source
xxxxxxxxxx
int zbx_eval_calc_histogram_quantile(const double q, const zbx_vector_dbl_t *values, const char *err_fn,
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
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: calculate 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: evaluate 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;