Class TimeSeriesRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Represents a time series regression model that incorporates temporal dependencies, trends, and seasonality.
public class TimeSeriesRegression<T> : RegressionBase<T>, IRegression<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 type used for calculations, typically float or double.
- Inheritance
-
TimeSeriesRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
The TimeSeriesRegression class extends basic regression by accounting for the temporal structure of the data. It can model autoregressive components (past values affecting future values), trend components (long-term directional movement), and seasonal components (recurring patterns at fixed intervals).
For Beginners: This class helps predict future values based on patterns in time-based data.
Think of it like weather forecasting:
- It looks at past weather patterns to predict future weather
- It can recognize long-term trends (like gradual warming)
- It can detect seasonal patterns (like winter being colder than summer)
- It accounts for how recent weather affects tomorrow's weather
This is useful for any data that changes over time, such as stock prices, website traffic, energy consumption, or sales figures.
Constructors
TimeSeriesRegression(TimeSeriesRegressionOptions<T>, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the TimeSeriesRegression class with specified options and optional regularization.
public TimeSeriesRegression(TimeSeriesRegressionOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsTimeSeriesRegressionOptions<T>Configuration options for the time series regression model.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Optional regularization method to prevent overfitting. If not provided, no regularization is applied.
Remarks
This constructor creates a new TimeSeriesRegression model with the specified options and regularization. It initializes the time series model according to the model type specified in the options.
For Beginners: This sets up your time series prediction model with your chosen settings.
When creating a time series model:
- The options parameter determines how the model will look for patterns in your data
- The regularization parameter (optional) helps prevent the model from becoming too specialized to your training data
Think of it like configuring a new prediction tool before you start using it. The settings you choose will affect how well it works for your specific type of data.
Methods
CreateNewInstance()
Creates a new instance of the time series regression model with the same configuration.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of TimeSeriesRegression<T> with the same configuration as the current instance.
Remarks
This method creates a new time series regression model that has the same configuration as the current instance. It's used for model persistence, cloning, and transferring the model's configuration to new instances.
For Beginners: This method makes a fresh copy of the current model with the same settings.
It's like creating a blueprint copy of your model that can be used to:
- Save your model's settings
- Create a new identical model
- Transfer your model's configuration to another system
This is useful when you want to:
- Create multiple similar models
- Save a model's configuration for later use
- Reset a model while keeping its settings
Deserialize(byte[])
Restores the model state from a byte array previously created by the Serialize method.
public override void Deserialize(byte[] modelData)
Parameters
modelDatabyte[]The byte array containing the serialized model.
Remarks
This method deserializes a time series regression model from a byte array, reconstructing the base class data, configuration options, and time series model. This allows a previously saved model to be restored without retraining.
For Beginners: This method loads a previously saved model.
Deserialization:
- Takes the bytes created by Serialize and converts them back into a working model
- Restores all the learned patterns and relationships
- Recreates the exact same model configuration
It's like restoring a snapshot of the model, allowing you to use a trained model without having to retrain it each time. This saves time and ensures consistent predictions.
GetModelType()
Returns the type of this regression model.
protected override ModelType GetModelType()
Returns
- ModelType
Always returns ModelType.TimeSeriesRegression.
Remarks
This method identifies the type of this regression model as a time series regression, which helps with model type checking and serialization.
For Beginners: This method simply identifies what kind of model this is.
It's like a name tag for the model that helps the system distinguish between different types of models (like time series regression vs. linear regression). This is useful when saving or loading models, or when deciding how to process them.
Predict(Matrix<T>)
Predicts target values for the given input features.
public override Vector<T> Predict(Matrix<T> input)
Parameters
inputMatrix<T>Input feature matrix for which predictions should be made.
Returns
- Vector<T>
A vector containing the predicted values.
Remarks
This method generates predictions for the given input features by first preparing the input data with lagged features, trend, and seasonal components, and then applying the trained model. The trend and seasonal components are added to the base predictions to produce the final result.
For Beginners: This method makes predictions based on new data.
When making predictions:
- The new data is prepared the same way as during training
- The model applies what it learned to generate base predictions
- Trend and seasonal effects are added to improve accuracy
- The final predictions account for all time-related patterns the model found
For example, predicting store sales might combine:
- Base prediction using features like promotion and price
- Plus the ongoing trend of 5% growth per month
- Plus the seasonal effect of higher weekend sales
Serialize()
Converts the model into a byte array that can be stored or transmitted.
public override byte[] Serialize()
Returns
- byte[]
A byte array representation of the model.
Remarks
This method serializes the time series regression model, including its base class data and specific configuration options, into a byte array. This allows the model to be saved to disk, transmitted over a network, or otherwise persisted.
For Beginners: This method saves your trained model to a format that can be stored or shared.
Serialization:
- Converts your trained model into simple bytes that can be saved
- Preserves all the patterns and relationships the model has learned
- Includes all settings and configuration options
It's like taking a snapshot of the model that can be saved to a file or database. Later, you can use Deserialize to recreate the exact same model without retraining.
Train(Matrix<T>, Vector<T>)
Trains the time series regression model on the provided data.
public override void Train(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>Input feature matrix where each row is an observation and each column is a feature.
yVector<T>Target vector containing the values to predict.
Remarks
This method prepares the input data by adding lagged features, trend, and seasonal components as specified in the options. It then trains the time series model on this prepared data and applies regularization and autocorrelation correction if configured.
For Beginners: This method teaches the model to recognize patterns in your data.
During training:
- The data is prepared by adding time-related features
- The model learns which patterns are most important for making predictions
- Regularization may be applied to prevent the model from memorizing noise
- Special corrections may be made for time-based patterns in the prediction errors
After training is complete, the model is ready to make predictions on new data.