Welcome to mirror list, hosted at ThFree Co, Russian Federation.

calc.c « zbxeval « libs « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ec652f165e8f22ca896adb68e97f689edabe2a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
** Zabbix
** Copyright (C) 2001-2021 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.
**/

#include "common.h"
#include "zbxalgo.h"
#include "zbxeval.h"

static int	zbx_is_normal_double(double dbl)
{
	if (FP_ZERO != fpclassify(dbl) && FP_NORMAL != fpclassify(dbl))
		return FAIL;

	return SUCCEED;
}

/******************************************************************************
 *                                                                            *
 * Function: calc_arithmetic_mean                                             *
 *                                                                            *
 * 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;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_calc_kurtosis                                           *
 *                                                                            *
 * 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;
		fourth_moment += diff * diff * diff * diff;
	}

	second_moment /= values->values_num;
	fourth_moment /= values->values_num;

	/* step 3: calculate kurtosis */

	second_moment2 = second_moment * second_moment;

	if (FP_NORMAL != fpclassify(second_moment2) || SUCCEED != zbx_is_normal_double(fourth_moment))
		goto err;

	res = fourth_moment / second_moment2;

	if (SUCCEED != zbx_is_normal_double(res))
		goto err;

	*result = res;

	return SUCCEED;
err:
	*error = zbx_strdup(*error, "cannot calculate kurtosis() value");

	return FAIL;
}

static int	zbx_vector_dbl_compare(const void *d1, const void *d2)
{
	const double	*p1 = (const double *)d1;
	const double	*p2 = (const double *)d2;

	ZBX_RETURN_IF_NOT_EQUAL(*p1, *p2);

	return 0;
}

/******************************************************************************
 *                                                                            *
 * Function: find_median                                                      *
 *                                                                            *
 * Purpose: find median (helper function)                                     *
 *                                                                            *
 * Parameters: v - [IN/OUT] non-empty vector with input data.                 *
 *                 NOTE: it will be modified (sorted in place).               *
 *                                                                            *
 * Return value: median                                                       *
 *                                                                            *
 ******************************************************************************/
static double	find_median(zbx_vector_dbl_t *v)
{
	zbx_vector_dbl_sort(v, zbx_vector_dbl_compare);

	if (0 == v->values_num % 2)	/* number of elements is even */
		return (v->values[v->values_num / 2 - 1] + v->values[v->values_num / 2]) / 2.0;
	else
		return v->values[v->values_num / 2];
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_calc_mad                                                *
 *                                                                            *
 * Purpose: calculate 'median absolute deviation'                             *
 *                                                                            *
 * Parameters: values - [IN] non-empty vector with input data.                *
 *                            NOTE: its elements will be modified and should  *
 *                            not be used in the caller!                      *
 *             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_mad(zbx_vector_dbl_t *values, double *result, char **error)
{
	double	median;
	int	i;

	/* step 1: find median of input data */
	median = find_median(values);

	if (SUCCEED != zbx_is_normal_double(median))
		goto err;

	/* step 2: find absolute differences of input data and median. Reuse input data vector. */

	for (i = 0; i < values->values_num; i++)
		values->values[i] = fabs(values->values[i] - median);

	/* step 3: find median of the differences */
	median = find_median(values);

	if (SUCCEED != zbx_is_normal_double(median))
		goto err;

	*result = median;

	return SUCCEED;
err:
	*error = zbx_strdup(*error, "cannot calculate mad() value");

	return FAIL;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_calc_skewness                                           *
 *                                                                            *
 * Purpose: evaluate 'skewness' function                                      *
 *                                                                            *
 * 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_skewness(zbx_vector_dbl_t *values, double *result, char **error)
{
	double	mean, std_dev = 0, sum_diff3 = 0, divisor;
	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 standard deviation and sum_diff3 */

	for (i = 0; i < values->values_num; i++)
	{
		double	diff = values->values[i] - mean;

		std_dev += diff * diff;
		sum_diff3 += diff * diff * diff;
	}

	std_dev = sqrt(std_dev / values->values_num);

	/* step 3: calculate skewness */

	divisor = values->values_num * std_dev * std_dev * std_dev;

	if (FP_NORMAL != fpclassify(divisor) || SUCCEED != zbx_is_normal_double(sum_diff3))
		goto err;

	*result = sum_diff3 / divisor;

	return SUCCEED;
err:
	*error = zbx_strdup(*error, "cannot calculate skewness() value");

	return FAIL;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_calc_stddevpop                                          *
 *                                                                            *
 * Purpose: evaluate function 'stdevpop' (population standard deviation)      *
 *                                                                            *
 * 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')             *
 *                                                                            *
 * Comments: the algorithm was taken from "Population standard deviation of   *
 *           grades of eight students" in                                     *
 *           https://en.wikipedia.org/wiki/Standard_deviation                 *
 *                                                                            *
 ******************************************************************************/
int	zbx_eval_calc_stddevpop(zbx_vector_dbl_t *values, double *result, char **error)
{
	double	mean, std_dev = 0;
	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 standard deviation */

	for (i = 0; i < values->values_num; i++)
	{
		double	diff = values->values[i] - mean;

		std_dev += diff * diff;
	}

	std_dev = sqrt(std_dev / values->values_num);

	if (SUCCEED != zbx_is_normal_double(std_dev))
		goto err;

	*result = std_dev;

	return SUCCEED;
err:
	*error = zbx_strdup(*error, "cannot calculate stddevpop() value");

	return FAIL;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_calc_stddevsamp                                         *
 *                                                                            *
 * Purpose: evaluate function 'stddevsamp' (sample standard deviation)        *
 *                                                                            *
 * Parameters: values - [IN] vector with input data with at least 2 elements  *
 *             result - [OUT] calculated value                                *
 *             error  - [OUT] dynamically allocated error message             *
 *                                                                            *
 * Return value: SUCCEED - evaluated successfully                             *
 *               FAIL - failed to evaluate function (see 'error')             *
 *                                                                            *
 * Comments: the algorithm was taken from "Population standard deviation of   *
 *           grades of eight students" in                                     *
 *           https://en.wikipedia.org/wiki/Standard_deviation                 *
 *                                                                            *
 ******************************************************************************/
int	zbx_eval_calc_stddevsamp(zbx_vector_dbl_t *values, double *result, char **error)
{
	double	mean, std_dev = 0;
	int	i;

	if (2 > values->values_num)	/* stddevsamp requires at least 2 data values */
	{
		*error = zbx_strdup(*error, "not enough data");
		return FAIL;
	}

	/* step 1: calculate arithmetic mean */
	mean = calc_arithmetic_mean(values);

	if (SUCCEED != zbx_is_normal_double(mean))
		goto err;

	/* step 2: calculate the standard deviation */

	for (i = 0; i < values->values_num; i++)
	{
		double	diff = values->values[i] - mean;

		std_dev += diff * diff;
	}

	std_dev = sqrt(std_dev / (values->values_num - 1));	/* divided by 'n - 1' because */
								/* sample standard deviation */
	if (SUCCEED != zbx_is_normal_double(std_dev))
		goto err;

	*result = std_dev;

	return SUCCEED;
err:
	*error = zbx_strdup(*error, "cannot calculate stddevsamp() value");

	return FAIL;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_calc_sumofsquares                                       *
 *                                                                            *
 * Purpose: calculate sum of squares                                          *
 *                                                                            *
 * 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_sumofsquares(zbx_vector_dbl_t *values, double *result, char **error)
{
	double	sum = 0;
	int	i;

	for (i = 0; i < values->values_num; i++)
		sum += values->values[i] * values->values[i];

	if (SUCCEED != zbx_is_normal_double(sum))
	{
		*error = zbx_strdup(*error, "cannot calculate sumofsquares() value");
		return FAIL;
	}

	*result = sum;

	return SUCCEED;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_calc_varpop                                             *
 *                                                                            *
 * Purpose: evaluate function 'varpop' (population variance)                  *
 *                                                                            *
 * 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')             *
 *                                                                            *
 * Comments: the algorithm was taken from "Population variance" in            *
 *           https://en.wikipedia.org/wiki/Variance#Population_variance       *
 *                                                                            *
 ******************************************************************************/
int	zbx_eval_calc_varpop(zbx_vector_dbl_t *values, double *result, char **error)
{
	double	mean, res = 0;
	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 population variance */

	for (i = 0; i < values->values_num; i++)
	{
		double	diff = values->values[i] - mean;

		res += diff * diff;
	}

	res /= values->values_num;	/* divide by 'number of values' for population variance */

	if (SUCCEED != zbx_is_normal_double(res))
		goto err;

	*result = res;

	return SUCCEED;
err:
	*error = zbx_strdup(*error, "cannot calculate varpop() value");

	return FAIL;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_calc_varsamp                                            *
 *                                                                            *
 * Purpose: evaluate function 'varsamp' (sample variance)                     *
 *                                                                            *
 * 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')             *
 *                                                                            *
 * Comments: the algorithm was taken from "Sample variance" in                *
 *           https://en.wikipedia.org/wiki/Variance#Population_variance       *
 *                                                                            *
 ******************************************************************************/
int	zbx_eval_calc_varsamp(zbx_vector_dbl_t *values, double *result, char **error)
{
	double	mean, res = 0;
	int	i;

	if (2 > values->values_num)	/* varsamp requires at least 2 data values */
	{
		*error = zbx_strdup(*error, "not enough data");
		return FAIL;
	}

	/* step 1: calculate arithmetic mean */
	mean = calc_arithmetic_mean(values);

	if (SUCCEED != zbx_is_normal_double(mean))
		goto err;

	/* step 2: calculate the sample variance */

	for (i = 0; i < values->values_num; i++)
	{
		double	diff = values->values[i] - mean;

		res += diff * diff;
	}

	res /= values->values_num - 1;	/* divide by 'number of values' - 1 for unbiased sample variance */

	if (SUCCEED != zbx_is_normal_double(res))
		goto err;

	*result = res;

	return SUCCEED;
err:
	*error = zbx_strdup(*error, "cannot calculate varsamp() value");

	return FAIL;
}