MySQL 26.10.0
Source Code Documentation
fx_holt_winters_forecaster.h
Go to the documentation of this file.
1/* Copyright (c) 2025, 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#pragma once
25
27#include "fx_point_forecast.h"
28#include "fx_sample.h"
29
30/**
31 @class Holt_Winters_Forecaster
32 @brief Implements the Holt-Winters forecasting algorithm for time series data.
33
34 The Holt-Winters method predicts future values in a time series based on past
35 observations. This class provides an implementation of the Holt-Winters
36 algorithm, allowing users to:
37 - Initialize the model using historical data (@ref initialize_model)
38 - Advance the state based on new data (@ref advance)
39 - Generate forecasts for future time steps (@ref forecast_at)
40
41 @note This class is designed to work with time series data represented as a
42 sequence of @ref Sample objects.
43*/
45 public:
46 /**
47 @brief Initialize Holt-Winters model if sufficient history exists.
48
49 Requires at least two full seasonal cycles. Computes:
50 - Initial trend as the average per-step difference between corresponding
51 points in the first two seasons.
52 - Initial level as the mean of the first season.
53 - Initial seasonal components as (y_t - (level + t * trend)).
54 @param batch Time series (must contain at least 2*m samples).
55 @note This must be called before any advance() if the model is not
56 initialized.
57 @note Assumes a write lock on the forecast model is active.
58 */
59 void initialize_model(const Series &batch);
60
61 /**
62 @brief Advance the Holt-Winters model with new observations.
63
64 Process every observation in batch using ingest_one().
65 @param batch Time series of samples, this batch is the unprocessed tail
66 @return Advance_Result Number of observations processed.
67 @note Assumes a write lock on the forecast model is active.
68 */
69 Advance_Result advance(const Series &batch);
70
71 /**
72 @brief Generate an h-minute-ahead forecast from current state.
73
74 Forecast = level + (k_minutes * trend) + seasonal[(T - m + h) mod m].
75 Adds 95% confidence interval using residual standard deviation.
76
77 @param k_minutes Horizon in minutes (e.g. 60, 1440, 10080).
78 @return Point_Forecast Predicted value, CI bounds, and target timestamp.
79 @note Seasonal index is aligned one full cycle back: (T - m + h) mod m.
80 */
81 Point_Forecast forecast_at(int k_minutes);
82
83 /**
84 @brief Return the Holt-Winters state for updating.
85 */
87
88 /**
89 @brief Reset the Holt-Winters model to its default/uninitialized values.
90
91 Clears all fitted parameters and statistics, requiring re-initialization or
92 retraining before the forecaster can be used again.
93 */
94 void reset_model();
95
96 private:
98 /**
99 @brief Process a single observation into the Holt-Winters state.
100 Steps:
101 - Read the seasonal component from one full cycle (day) back.
102 - Compute one-step-ahead fitted value and residual for this data point (yt).
103 - Update level, trend, and seasonal components using smoothing equations.
104 - Record residual into Welford stats for confidence intervals.
105 @param ts Timestamp (unix seconds) of this observation
106 @param yt Observed value at time ts
107 @param ar Advance result
108
109 @note The seasonal slot for this phase (minute of day) is overwritten with
110 the new seasonal estimate after use. This guarantees 'seasonal
111 alignment.' The term 'global index' simply means the index of the
112 entire set of disk metrics, of which the current time series batch is
113 a subset.
114 @note Assumes a write lock on the forecast model is active.
115 */
116 void ingest_one(int64_t ts, double yt, Advance_Result &ar);
117};
Implements the Holt-Winters forecasting algorithm for time series data.
Definition: fx_holt_winters_forecaster.h:44
Point_Forecast forecast_at(int k_minutes)
Generate an h-minute-ahead forecast from current state.
Definition: fx_holt_winters_forecaster.cc:70
Advance_Result advance(const Series &batch)
Advance the Holt-Winters model with new observations.
Definition: fx_holt_winters_forecaster.cc:58
Holt_Winters_State m_state
Definition: fx_holt_winters_forecaster.h:97
void initialize_model(const Series &batch)
Initialize Holt-Winters model if sufficient history exists.
Definition: fx_holt_winters_forecaster.cc:30
void ingest_one(int64_t ts, double yt, Advance_Result &ar)
Process a single observation into the Holt-Winters state.
Definition: fx_holt_winters_forecaster.cc:102
Holt_Winters_State & state()
Return the Holt-Winters state for updating.
Definition: fx_holt_winters_forecaster.h:86
void reset_model()
Reset the Holt-Winters model to its default/uninitialized values.
Definition: fx_holt_winters_forecaster.cc:96
std::vector< Sample > Series
Definition: fx_sample.h:36
Definition: fx_point_forecast.h:39
Holt-Winters parameters, fitted state and residual statistics.
Definition: fx_holt_winters_state.h:44
Raw forecast record.
Definition: fx_point_forecast.h:31