forecast
Analyzes the trend of time series data to predict future values. For periodic data, uses STL decomposition (Seasonal and Trend decomposition using Loess) to separate the data into trend, seasonality, and residual components, then produces forecast values based on the trend component. Starting with the 4.0 series' 4.0.2609.0 release, it also assigns each input record an anomaly score based on its deviation from the expected forecast value.
Command properties
| Item | Description |
|---|---|
| Command type | Transforming |
| Required permission | None |
| License usage | N/A |
| Parallel execution | Not supported |
| Distributed execution | Runs on Data Node (mapper) |
Syntax
In the syntax above, predictor, method, and alpha are available starting with 4.0.2609.0.
Options
count=INT- Number of records to forecast. Must be a natural number of 1 or more. (default:
5) period=INT- Period length of the time series data. If specified, must be a natural number of 3 or more. If not specified, the period is automatically calculated using Spectral Density Estimation, and if the calculated period is less than 2, the data is treated as non-periodic and simple linear regression is applied.
predictor={ets|linear|loess|constant}- When periodic data is decomposed with
method=stl(default), the method used to forecast the trend component (default:ets). This option name, and theconstantvalue, are available starting with 4.0.2609.0. In the 5.0 series (through 5.0.2605.2), use thesmootheroption instead, which accepts onlyets,linear, andloess(noconstant).
ets: Exponential smoothinglinear: Linear regressionloess: LOESS (Locally Estimated Scatterplot Smoothing)constant: Uses the mean value of the trend component as a constant. Suitable for time series without a clear trend. Available starting with 4.0.2609.0.
The option name smoother, used in earlier versions, continues to be recognized as an alias for predictor in the 4.0 series. This option does not apply when method=linear, or when the data is treated as non-periodic.
normalize=BOOL- Whether to apply Box-Cox transformation. When set to
true, corrects for non-linearly increasing trends. May have a performance impact due to additional computation. (default:false) accumulate=BOOL- Whether to accumulate confidence intervals. When set to
true, the confidence interval widens as the forecast point moves further into the future. (default:true) confidence=DOUBLE- Confidence interval percentage used to calculate the upper/lower bounds and the anomaly score. Must be a real number of 0 or more and less than 100. (default:
95) ThisDOUBLEtype and range validation apply starting with 4.0.2609.0. In the 5.0 series (through 5.0.2605.2), this option accepts a whole-number percentage instead and calculates only the confidence interval, since the anomaly score is not available in that series. seed=INT- Random seed value. Specify a seed value to reproduce the same results for the same input.
time=STR- Name of the time field. Used to calculate the time interval of the time series data. (default:
_time) method={stl|linear}- Time series decomposition algorithm (default:
stl). For non-periodic data — where the calculated (or specified) period is less than 2 — simple linear regression is applied regardless of this option. Available starting with 4.0.2609.0.
stl: Uses STL decomposition to separate the data into trend, seasonality, and residual components, then forecasts the trend component according to thepredictoroption.linear: Forecasts by combining the per-period average (seasonality) with linear regression (trend). Thepredictoroption does not apply with this method.
alpha=DOUBLE- Decay coefficient used to calculate the anomaly score (
_score). Must be a real number of 0 or more and 1 or less. Each record's anomaly score is calculated cumulatively asprevious record's _score value * alpha + current record's probability-based score. (default:0.6) Available starting with 4.0.2609.0.
Target
FIELD- Field containing the time series data to forecast
[by FIELD, ...]- Group fields. When a
byclause is specified, forecasting is performed independently for records with the same group field value. Specify multiple fields separated by commas (,).
Input fields
| Field | Type | Required | Description |
|---|---|---|---|
| FIELD | numeric | Required | Time series value to forecast. If this field's value is null in the partition's first record, the forecast for that partition is skipped. |
| time field | timestamp | Required | Time value of the time series data. Forecast times are calculated based on the interval between the last two records. |
| by fields | Any | Optional | Group criteria fields |
Output fields
| Field | Type | Description |
|---|---|---|
| _trend | double | Trend component value of the input record. If normalize=true, this is the value after applying the inverse Box-Cox transformation. |
| _expected | double | Expected forecast value of the input record (reflecting trend and seasonality). The reference value used to calculate _upper, _lower, and _score on input records. Available starting with 4.0.2609.0. |
| _score | double | Anomaly score of the input record. Increases with the difference between the actual value and _expected, and accumulates the previous record's score according to the alpha option. Available starting with 4.0.2609.0. |
| _upper | double | Upper bound of the confidence interval for the predicted record, calculated from _future. In the 4.0 series starting with 4.0.2609.0, this field is also added to input records, calculated from _expected. |
| _lower | double | Lower bound of the confidence interval for the predicted record, calculated from _future. In the 4.0 series starting with 4.0.2609.0, this field is also added to input records, calculated from _expected. |
| _future | double | Forecast value of the predicted record |
| _forecast_error | string | The reason a forecast could not be produced for the input record. Records with this field do not have the other forecast-related fields, such as _trend, populated. Available starting with 4.0.2609.0. |
Error codes
Parse errors
| Error code | Message | Description |
|---|---|---|
| 40502 | empty-forecast-clause | No group field is specified after the by clause |
| 40503 | empty-forecast-fields | The target field to forecast is not specified |
| 40804 | check-ml-license | A machine learning license is required. The error code differs by series — the 5.0 series (through 5.0.2605.2) uses 40804 (as does the 4.0 series before 4.0.2609.0); the 4.0 series starting with 4.0.2609.0 uses 40504 instead, with the same message. |
| 40501 | invalid-forecast-period | The period value is less than 3, or is not a number. Available starting with 4.0.2609.0; not validated in the 5.0 series (through 5.0.2605.2). |
| 40505 | invalid-forecast-method | The method value is not stl or linear. Available starting with 4.0.2609.0. |
| 40506 | invalid-forecast-count | The count value is less than 1, or is not a number. Available starting with 4.0.2609.0; not validated in the 5.0 series (through 5.0.2605.2). |
| 40507 | invalid-forecast-predictor | The predictor (or smoother) value is not one of the four valid values. Available starting with 4.0.2609.0. |
| 40510 | invalid-forecast-confidence | The confidence value is less than 0, is 100 or more, or is not a number. Available starting with 4.0.2609.0; not validated in the 5.0 series (through 5.0.2605.2). |
| 40511 | invalid-forecast-seed | The seed value is not a number. Available starting with 4.0.2609.0; not validated in the 5.0 series (through 5.0.2605.2). |
| 40512 | invalid-forecast-alpha | The alpha value is less than 0, is greater than 1, or is not a number. Available starting with 4.0.2609.0. |
Runtime errors
| Error code | Message | Description |
|---|---|---|
| - | cannot sort forecast item for query [id] | Failed to process a temporary file used to sort groups while using a by clause. The query fails with an error. |
Description
The forecast command collects all input records before performing time series forecasting. Input records are output as-is with the trend component (_trend) added, and forecasted records are appended after the input records with the _future, _upper, and _lower fields, up to the number specified by the count option. Starting with the 4.0 series' 4.0.2609.0 release, input records also receive the expected forecast value (_expected), confidence interval (_upper, _lower), and anomaly score (_score).
When the period value is 2 or more, the trend and seasonality are separated. In the 4.0 series starting with 4.0.2609.0, this is controlled by the method option, using either STL decomposition (stl, default) or a method that combines the per-period average with linear regression (linear); the predictor option applies only when method=stl, and determines the method used to forecast the trend component. The 5.0 series (through 5.0.2605.2) always uses STL decomposition and does not have the method option; the equivalent smoother option determines the trend forecasting method. When the period is less than 2, the data is treated as non-periodic and simple linear regression is used regardless of these options.
The forecast record time values are calculated based on the interval between the last two records of the time field specified by the time option. It is recommended to prepare data with a consistent time interval using the timechart command before using this command.
Forecasting is performed successfully only when all of the following conditions are met for the input data; otherwise, the original record is output as-is. In the 4.0 series starting with 4.0.2609.0, a _forecast_error field is also added to such records.
- There must be at least 5 records per partition.
- The value of the forecast target field must not be null in the partition's first record.
- If the
periodoption is specified, the number of input records must be at least twice theperiodvalue. - The time field specified by the
timeoption must be present in the partition's last two records and must be of the timestamp type (because the interval between these two records is used to calculate the forecast time values).
When a by clause is used, forecasting is performed independently for records with the same group field value. Temporary files may be used internally for sorting by group.
Examples
-
Output forecast values for time series data
table duration=30d web_logs | timechart span=1d count | forecast countAggregates web logs from the past 30 days by day, then outputs 5 forecast values for the
countfield. In the 4.0 series starting with 4.0.2609.0, this also outputs the anomaly score (_score) of each input record. -
Specify period and forecast count
table duration=90d web_logs | timechart span=1d sum(bytes) as traffic | forecast period=7 count=14 trafficSets the period to 7 (weekly pattern) and outputs 14 forecast data points.
-
Specify the time field and seed value
table duration=30d web_logs | timechart span=1h sum(bytes) as traffic | forecast period=24 time=_time seed=1234 trafficSets the period to 24 (daily pattern) and fixes the seed value to 1234 to produce reproducible results for the same input.
-
Forecast by group
table duration=30d web_logs | timechart span=1d count by method | forecast count=10 count by methodIndependently forecasts 10 values for the
countfield per group with the samemethodfield value. -
Apply Box-Cox transformation
table duration=90d web_logs | timechart span=1d sum(bytes) as traffic | forecast period=7 normalize=true trafficApplies Box-Cox transformation to correct for non-linearly increasing trends before forecasting.
-
Using a constant predictor for a time series without a trend (4.0 series, 4.0.2609.0 and later)
table duration=30d web_logs | timechart span=1d count | forecast period=7 predictor=constant alpha=0.8 countUses a constant predictor (
predictor=constant) for a time series without a clear trend, and raises the anomaly score accumulation weight (alpha) to 0.8 to more heavily reflect the influence of the previous record's score. Thepredictorandalphaoptions are available starting with 4.0.2609.0.
Change history
| Target | Introduced in |
|---|---|
The forecast command | Before Sonar 4.0 |
The predictor, method, and alpha options | 4.0.2609.0 |
The _expected, _score, and _forecast_error output fields | 4.0.2609.0 |
Adding the _upper and _lower confidence interval to input records | 4.0.2609.0 (previously added to forecast records only) |
Validation of the period, count, confidence, seed, and alpha values (error codes 40501, 40505-40507, 40510-40512) | 4.0.2609.0 |
Machine learning license error code changed from 40804 to 40504 | 4.0.2609.0 (4.0.2511.3 and earlier, and the 5.0 series, use 40804) |