1 /*****************************************************************************
2 * rrd_hw_math.c Math functions for Holt-Winters computations
3 *****************************************************************************/
6 #include "rrd_hw_math.h"
8 /*****************************************************************************
9 * RRDtool supports both the additive and multiplicative Holt-Winters methods.
10 * The additive method makes predictions by adding seasonality to the baseline,
11 * whereas the multiplicative method multiplies the seasonality coefficient by
12 * the baseline to make a prediction. This file contains all the differences
13 * between the additive and multiplicative methods, as well as a few math
14 * functions common to them both.
15 ****************************************************************************/
17 /*****************************************************************************
18 * Functions for additive Holt-Winters
19 *****************************************************************************/
21 rrd_value_t hw_additive_calculate_prediction(
22 rrd_value_t intercept,
25 rrd_value_t seasonal_coef)
27 return intercept + slope * null_count + seasonal_coef;
30 rrd_value_t hw_additive_calculate_intercept(
33 rrd_value_t seasonal_coef,
36 return hw_alpha * (observed - seasonal_coef)
37 + (1 - hw_alpha) * (coefs[CDP_hw_intercept].u_val
39 (coefs[CDP_hw_slope].u_val) *
40 (coefs[CDP_null_count].u_cnt));
43 rrd_value_t hw_additive_calculate_seasonality(
46 rrd_value_t intercept,
47 rrd_value_t seasonal_coef)
49 return hw_gamma * (observed - intercept)
50 + (1 - hw_gamma) * seasonal_coef;
53 rrd_value_t hw_additive_init_seasonality(
54 rrd_value_t seasonal_coef,
55 rrd_value_t intercept)
57 return seasonal_coef - intercept;
60 /*****************************************************************************
61 * Functions for multiplicative Holt-Winters
62 *****************************************************************************/
64 rrd_value_t hw_multiplicative_calculate_prediction(
65 rrd_value_t intercept,
68 rrd_value_t seasonal_coef)
70 return (intercept + slope * null_count) * seasonal_coef;
73 rrd_value_t hw_multiplicative_calculate_intercept(
76 rrd_value_t seasonal_coef,
79 if (seasonal_coef <= 0) {
83 return hw_alpha * (observed / seasonal_coef)
84 + (1 - hw_alpha) * (coefs[CDP_hw_intercept].u_val
86 (coefs[CDP_hw_slope].u_val) *
87 (coefs[CDP_null_count].u_cnt));
90 rrd_value_t hw_multiplicative_calculate_seasonality(
93 rrd_value_t intercept,
94 rrd_value_t seasonal_coef)
100 return hw_gamma * (observed / intercept)
101 + (1 - hw_gamma) * seasonal_coef;
104 rrd_value_t hw_multiplicative_init_seasonality(
105 rrd_value_t seasonal_coef,
106 rrd_value_t intercept)
108 if (intercept <= 0) {
112 return seasonal_coef / intercept;
115 /*****************************************************************************
116 * Math functions common to additive and multiplicative Holt-Winters
117 *****************************************************************************/
119 rrd_value_t hw_calculate_slope(
123 return hw_beta * (coefs[CDP_hw_intercept].u_val -
124 coefs[CDP_hw_last_intercept].u_val)
125 + (1 - hw_beta) * coefs[CDP_hw_slope].u_val;
128 rrd_value_t hw_calculate_seasonal_deviation(
129 rrd_value_t hw_gamma,
130 rrd_value_t prediction,
131 rrd_value_t observed,
134 return hw_gamma * fabs(prediction - observed)
135 + (1 - hw_gamma) * last;
138 rrd_value_t hw_init_seasonal_deviation(
139 rrd_value_t prediction,
140 rrd_value_t observed)
142 return fabs(prediction - observed);