STLDecomposition<T>
Models & Types · AiDotNet.TimeSeries
Implements Seasonal-Trend decomposition using LOESS (STL) for time series analysis.
Inheritance: TimeSeriesModelBase<T> → STLDecomposition<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
STL decomposition is like breaking down a song into its basic elements - the melody (trend), the repeating chorus (seasonal pattern), and the unique variations (residuals).
For example, if you analyze monthly sales data:
- The trend component shows the long-term increase or decrease in sales
- The seasonal component shows regular patterns that repeat (like higher sales during holidays)
- The residual component shows what’s left after removing trend and seasonality (like unexpected events)
This decomposition helps you understand what’s driving your time series and can improve forecasting. The model offers different algorithms (standard, robust, and fast) to handle various types of data.
How It Works
STL decomposition breaks down a time series into three components: trend, seasonal, and residual. It uses locally weighted regression (LOESS) to extract these components, making it robust to outliers and applicable to a wide range of time series data.
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.TimeSeries;
using AiDotNet.Tensors.LinearAlgebra;
double[] series =
{
120, 135, 148, 160, 155, 170, 180, 195, 210, 198, 220, 235,
140, 155, 165, 178, 172, 190, 200, 215, 230, 218, 245, 260
};
var x = new Matrix<double>(series.Length, 1);
for (int i = 0; i < series.Length; i++) x[i, 0] = i;
var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new STLDecomposition<double>())
.ConfigureDataLoader(DataLoaders.FromMatrixVector(x, new Vector<double>(series)))
.BuildAsync();
var forecast = result.Predict(new Matrix<double>(6, 1));
Console.WriteLine($"STLDecomposition: forecast {forecast.Length} steps.");
Constructors
| Constructor | Summary |
|---|---|
STLDecomposition(STLDecompositionOptions<T> options) |
Initializes a new instance of the STLDecomposition class with optional configuration options. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> EvaluateModel(Matrix<T> xTest, Vector<T> yTest) |
Evaluates the performance of the trained model on test data. |
ModelMetadata<T> GetModelMetadata() |
Gets metadata about the model, including its type, configuration, and information about the decomposed components. |
T PredictSingle(Vector<T> input) |
Predicts a single value based on the decomposed components. |
Vector<T> GetResidual() |
Gets the residual component of the time series. |
Vector<T> GetSeasonal() |
Gets the seasonal component of the time series. |
Vector<T> GetTrend() |
Gets the trend component of the time series. |
Vector<T> Predict(Matrix<T> input) |
Generates forecasts for future time periods based on the decomposed components. |
void Reset() |
Serializes the model’s core parameters to a binary writer. |