Class SARIMAModel<T>
- Namespace
- AiDotNet.TimeSeries
- Assembly
- AiDotNet.dll
Implements a Seasonal Autoregressive Integrated Moving Average (SARIMA) model for time series forecasting.
public class SARIMAModel<T> : TimeSeriesModelBase<T>, ITimeSeriesModel<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>
Type Parameters
TThe numeric data type used for calculations (e.g., float, double).
- Inheritance
-
SARIMAModel<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
The SARIMA model extends the ARIMA model by incorporating seasonal components, making it suitable for data with seasonal patterns. It combines autoregressive (AR), integrated (I), and moving average (MA) components for both seasonal and non-seasonal parts of the time series.
For Beginners: SARIMA is used to predict future values in a time series (data collected over time) that has seasonal patterns. Think of it like predicting ice cream sales throughout the year - there's a general trend (maybe increasing over years) and a seasonal pattern (higher in summer, lower in winter). SARIMA can capture both these patterns.
The model has several parameters:
- p: How many previous values influence the current value
- d: How many times we need to subtract consecutive values to make the data stable
- q: How many previous prediction errors influence the current prediction
- P, D, Q: The same as above, but for seasonal patterns
- m: The length of the seasonal cycle (e.g., 12 for monthly data with yearly patterns)
Constructors
SARIMAModel(SARIMAOptions<T>)
Initializes a new instance of the SARIMAModel class with the specified options.
public SARIMAModel(SARIMAOptions<T> options)
Parameters
optionsSARIMAOptions<T>The configuration options for the SARIMA model.
Methods
CreateInstance()
Creates a new instance of the SARIMA model with the same options.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of the SARIMA model.
Remarks
For Beginners: This method creates a fresh copy of the model with the same settings but no trained coefficients. It's useful when you want to: - Train the same model structure on different data - Create multiple versions of the same model for ensemble methods - Start fresh with the same configuration after experimenting
DeserializeCore(BinaryReader)
Deserializes the model's core parameters from a binary reader.
protected override void DeserializeCore(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read from.
Remarks
For Beginners: Deserialization is the process of loading a previously saved model from disk. This method reads the model's parameters from a file and reconstructs the model exactly as it was when it was saved.
EvaluateModel(Matrix<T>, Vector<T>)
Evaluates the performance of the trained model on test data.
public override Dictionary<string, T> EvaluateModel(Matrix<T> xTest, Vector<T> yTest)
Parameters
xTestMatrix<T>The input features matrix for testing.
yTestVector<T>The actual target values for testing.
Returns
- Dictionary<string, T>
A dictionary containing evaluation metrics (MSE, RMSE, MAE).
Remarks
For Beginners: This method tests how well our model performs by comparing its predictions to actual values. It calculates several error metrics: - MSE (Mean Squared Error): The average of the squared differences between predictions and actual values - RMSE (Root Mean Squared Error): The square root of MSE, which gives an error in the same units as the data - MAE (Mean Absolute Error): The average of the absolute differences between predictions and actual values
Lower values for these metrics indicate better model performance.
GetARParameters()
Gets the non-seasonal autoregressive (AR) parameters of the model.
public Vector<T> GetARParameters()
Returns
- Vector<T>
A vector containing the AR parameters.
Remarks
AR parameters represent how much the current value depends on its previous values.
For Beginners: Autoregressive (AR) parameters tell us how much each previous value affects the current value. For example, if we're predicting today's temperature, these parameters tell us how much yesterday's temperature, the day before, etc. influence today's temperature.
GetMAParameters()
Gets the non-seasonal moving average (MA) parameters of the model.
public Vector<T> GetMAParameters()
Returns
- Vector<T>
A vector containing the MA parameters.
Remarks
MA parameters represent how much the current value depends on previous prediction errors.
For Beginners: Moving Average (MA) parameters tell us how much each previous prediction error affects the current value. A prediction error is the difference between what we predicted and what actually happened. These parameters help the model learn from its mistakes.
GetModelMetadata()
Gets metadata about the model, including its type, parameters, and configuration.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetaData object containing information about the model.
Remarks
For Beginners: This method provides information about the model's configuration and parameters. It's useful for: - Comparing different models - Documenting the model's specifications - Tracking which parameters worked best for different datasets - Saving model metadata along with predictions
GetSeasonalARParameters()
Gets the seasonal autoregressive (SAR) parameters of the model.
public Vector<T> GetSeasonalARParameters()
Returns
- Vector<T>
A vector containing the seasonal AR parameters.
Remarks
Seasonal AR parameters represent how much the current value depends on values from previous seasons.
For Beginners: Seasonal Autoregressive parameters are similar to regular AR parameters, but they look at values from previous seasons rather than just previous time points. For example, if we're predicting July's ice cream sales, these parameters tell us how much last July's sales influence this July's sales.
GetSeasonalMAParameters()
Gets the seasonal moving average (SMA) parameters of the model.
public Vector<T> GetSeasonalMAParameters()
Returns
- Vector<T>
A vector containing the seasonal MA parameters.
Remarks
Seasonal MA parameters represent how much the current value depends on prediction errors from previous seasons.
For Beginners: Seasonal Moving Average parameters are similar to regular MA parameters, but they look at prediction errors from previous seasons. They help the model learn from seasonal patterns in its past mistakes.
GetSeasonalPeriod()
Gets the seasonal period used in the model.
public int GetSeasonalPeriod()
Returns
- int
The seasonal period (e.g., 12 for monthly data with yearly seasonality).
Remarks
For Beginners: The seasonal period is the number of time points after which the pattern repeats. For example, for monthly data with yearly patterns, the seasonal period is 12. For weekly data with yearly patterns, it would be 52.
Predict(Matrix<T>)
Generates predictions using the trained SARIMA model.
public override Vector<T> Predict(Matrix<T> input)
Parameters
inputMatrix<T>The input features matrix for which predictions are to be made.
Returns
- Vector<T>
A vector of predicted values.
Remarks
The prediction process combines: - Non-seasonal AR component - Seasonal AR component - Non-seasonal MA component - Seasonal MA component - The constant term
For Beginners: This method makes predictions for future values based on what the model has learned. It combines several components: - The effect of recent values (AR component) - The effect of values from the same season in previous years (seasonal AR) - The effect of recent prediction errors (MA component) - The effect of prediction errors from the same season in previous years (seasonal MA) - A baseline value (constant term)
The result is a forecast that accounts for both recent trends and seasonal patterns.
PredictSingle(Vector<T>)
Predicts a single value based on the input vector.
public override T PredictSingle(Vector<T> input)
Parameters
inputVector<T>The input vector containing historical values.
Returns
- T
The predicted value.
Remarks
For Beginners: This method predicts a single future value based on historical data provided in the input. The input should contain the recent history needed to make a prediction.
For a SARIMA model, the input should include:
- At least p values for the non-seasonal AR component
- At least P*m values for the seasonal AR component
- Recent errors for the MA components (though these are often set to zero if unknown)
SerializeCore(BinaryWriter)
Serializes the model's core parameters to a binary writer.
protected override void SerializeCore(BinaryWriter writer)
Parameters
writerBinaryWriterThe binary writer to write to.
Remarks
For Beginners: Serialization is the process of converting the model's state into a format that can be saved to disk. This allows you to save a trained model and load it later without having to retrain it.
TrainCore(Matrix<T>, Vector<T>)
Core implementation of the training logic for the SARIMA model.
protected override void TrainCore(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input features matrix.
yVector<T>The target values vector.
Remarks
For Beginners: This is the core method that performs the actual training of the SARIMA model. It applies differencing to make the data stationary, then estimates the various components of the model (AR, MA, seasonal AR, seasonal MA) to capture patterns in the data.