Class ExponentialSmoothingModel<T>
- Namespace
- AiDotNet.TimeSeries
- Assembly
- AiDotNet.dll
Represents a model that implements exponential smoothing for time series forecasting.
public class ExponentialSmoothingModel<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 type used for calculations, typically float or double.
- Inheritance
-
ExponentialSmoothingModel<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
Exponential smoothing is a time series forecasting method that assigns exponentially decreasing weights to past observations, giving more importance to recent data while still considering older observations. This model supports simple, double (with trend), and triple (with trend and seasonality) exponential smoothing.
For Beginners: Exponential smoothing helps predict future values based on past data.
Think of it like predicting tomorrow's weather:
- Recent weather (yesterday, today) is more important than weather from weeks ago
- You can identify trends (getting warmer over time)
- You can account for seasons (summer is usually warmer than winter)
For example, if you're forecasting daily sales:
- Simple smoothing: Uses a weighted average of past values, giving more weight to recent sales
- Double smoothing: Also captures if sales are trending up or down
- Triple smoothing: Adds seasonal patterns (e.g., higher sales on weekends)
Exponential smoothing is called "exponential" because the weight given to older data decreases exponentially as the data gets older.
Constructors
ExponentialSmoothingModel(ExponentialSmoothingOptions<T>)
Initializes a new instance of the ExponentialSmoothingModel<T> class with the specified options.
public ExponentialSmoothingModel(ExponentialSmoothingOptions<T> options)
Parameters
optionsExponentialSmoothingOptions<T>The configuration options for the exponential smoothing model.
Remarks
This constructor initializes the exponential smoothing model with the provided configuration options. The options specify parameters such as smoothing factors, whether to include trend and seasonality components, and the seasonal period if applicable.
For Beginners: This sets up your forecasting model with your chosen settings.
When creating the model, you can specify:
- Alpha: How much weight to give to recent data vs. older data (0-1)
- Beta: How much to consider trends in your data (0-1)
- Gamma: How much to consider seasonal patterns (0-1)
- Whether to include trend analysis
- Whether to include seasonal analysis
Higher values (closer to 1) make the model respond quickly to changes. Lower values (closer to 0) make the model more stable and less reactive to short-term fluctuations.
Methods
CreateInstance()
Creates a new instance of the exponential smoothing 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 exponential smoothing model.
Remarks
This method creates a new instance of the exponential smoothing model with the same configuration options as the current instance. This is useful for creating copies or clones of the model.
For Beginners: This creates a new copy of the model with the same settings.
Creating a new instance:
- Makes a fresh copy of the model with the same configuration
- The new copy hasn't been trained yet
- You can train and use the copy independently from the original
This is helpful when you want to experiment with different training data while preserving your original model.
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
This method reads the model's essential parameters from a binary stream, allowing a previously saved model to be loaded from a file or database. The deserialized parameters include the smoothing factors (alpha, beta, gamma) and the initial values for level, trend, and seasonal components.
For Beginners: This loads a previously saved model.
The method:
- Reads the saved model data from a file or database
- Converts this data back into the model's parameters
- Reconstructs the model exactly as it was when saved
This is like opening a document you previously saved, allowing you to continue using the model without having to train it again.
EvaluateModel(Matrix<T>, Vector<T>)
Evaluates the model's performance on test data.
public override Dictionary<string, T> EvaluateModel(Matrix<T> xTest, Vector<T> yTest)
Parameters
xTestMatrix<T>The test input features matrix.
yTestVector<T>The test target values vector.
Returns
- Dictionary<string, T>
A dictionary containing various evaluation metrics.
Remarks
This method evaluates the model's performance on test data by generating predictions and calculating various error metrics. The returned metrics include Mean Squared Error (MSE), Root Mean Squared Error (RMSE), and Mean Absolute Percentage Error (MAPE).
For Beginners: This measures how accurate the model's predictions are.
The evaluation:
- Makes predictions for data the model hasn't seen before
- Compares these predictions to the actual values
- Calculates different types of error measurements:
- MSE (Mean Squared Error): Average of squared differences
- RMSE (Root Mean Squared Error): Square root of MSE, in the same units as your data
- MAPE (Mean Absolute Percentage Error): Average percentage difference
Lower values indicate better performance. For example, a MAPE of 5% means predictions are off by 5% on average.
GetModelMetadata()
Returns 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
This method returns detailed metadata about the exponential smoothing model, including its type, current parameters (alpha, beta, gamma, initial values), and configuration options. This metadata can be used for model selection, comparison, and documentation purposes.
For Beginners: This provides information about your model's settings and state.
The metadata includes:
- The type of model (Exponential Smoothing)
- Current smoothing parameters (alpha, beta, gamma)
- Initial values for level, trend, and seasonal components
- Configuration settings from when you created the model
- A serialized version of the entire model
This information is useful for:
- Keeping track of different models you've created
- Comparing model configurations
- Documenting which settings worked best
- Sharing model information with others
Predict(Matrix<T>)
Generates predictions for the given input data.
public override Vector<T> Predict(Matrix<T> input)
Parameters
inputMatrix<T>The input features matrix.
Returns
- Vector<T>
A vector containing the predicted values.
Remarks
This method generates forecasts for future time periods based on the trained model parameters. It applies the exponential smoothing algorithm using the optimized smoothing parameters (alpha, beta, gamma) and the initial values for level, trend, and seasonal components.
For Beginners: This makes predictions for future time periods.
After the model is trained:
- It uses the best alpha, beta, and gamma values it found
- It starts with the established level, trend, and seasonal patterns
- It calculates predictions for each requested future time period
- It updates its components after each prediction to stay accurate
This is like a weather forecast that uses current conditions and patterns to predict what will happen in the coming days.
PredictSingle(Vector<T>)
Predicts a single value based on the input features vector.
public override T PredictSingle(Vector<T> input)
Parameters
inputVector<T>The input features vector.
Returns
- T
The predicted value.
Remarks
This method generates a single forecast value based on the provided input features. It uses the trained model parameters (alpha, beta, gamma) and the established level, trend, and seasonal components to make the prediction.
For Beginners: This is a shortcut for getting just one prediction.
Instead of making multiple predictions at once, this method:
- Takes a single set of input features
- Uses the model's learned patterns
- Returns a single predicted value
It's like asking for tomorrow's forecast specifically, rather than getting the forecast for the whole week ahead.
Reset()
Resets the model to its initial state.
public override void Reset()
Remarks
This method resets the exponential smoothing model to its initial state, clearing any learned parameters and returning to the initial smoothing factors provided in the options.
For Beginners: This resets the model to start fresh.
Resetting the model:
- Clears any learning that has happened
- Returns to the initial settings you provided
- Allows you to train the model again from scratch
This is useful if you want to re-train the model with different data or if you want to compare different training approaches.
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
This method writes the model's essential parameters to a binary stream, allowing the model to be saved to a file or database. The serialized parameters include the smoothing factors (alpha, beta, gamma) and the initial values for level, trend, and seasonal components.
For Beginners: This saves the model so you can use it later.
The method:
- Converts the model's parameters to a format that can be saved
- Writes these values to a file or database
- Includes all the information needed to recreate the model exactly
This is like saving a document so you can open it again later without having to start from scratch.
TrainCore(Matrix<T>, Vector<T>)
Implements the core training logic for the exponential smoothing model.
protected override void TrainCore(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input features matrix (typically time indicators or related variables).
yVector<T>The target values vector (the time series data to forecast).
Remarks
This method contains the implementation details of the training process for the exponential smoothing model. It estimates the optimal smoothing parameters and initial values using the provided time series data.
For Beginners: This is the engine that powers the training process.
While the public Train method handles input validation and high-level logic, this method does the actual work of:
- Finding the best alpha, beta, and gamma values through grid search
- Calculating the initial level, trend, and seasonal components
It's like the detailed work that happens in a car engine when you press the accelerator pedal.