Using ensemble to forecast timeseries (machine learning)
Mostrar comentarios más antiguos
Hi everyone. Can someone help fix this? I have a simple timeseries. I am struggling to get a good forecast. I just a get a flat prediction and forecast. The timeseries is attached. Here is my code:
clc
clear all
load("y.mat");
timeSeries =y';
% Normalize the time series
meanTS = mean(timeSeries);
stdTS = std(timeSeries);
timeSeriesNorm = (timeSeries - meanTS) / stdTS;
% Split the normalized data into training (70%) and validation (30%) sets
trainRatio = 0.7;
trainSize = floor(trainRatio * length(timeSeriesNorm));
trainData = timeSeriesNorm(1:trainSize);
validationData = timeSeriesNorm(trainSize+1:end);
% Create lagged features for training data
numLags = 5; % Number of lagged features to include
X_train = zeros(length(trainData) - numLags, numLags); % No trend feature
Y_train = trainData(numLags+1:end);
for i = 1:numLags
X_train(:, i) = trainData(i:end - numLags + i - 1);
end
% Create lagged features for validation data
X_validation = zeros(length(validationData), numLags); % No trend feature
Y_validation = validationData;
for i = 1:numLags
X_validation(:, i) = timeSeriesNorm(trainSize - numLags + i:trainSize - numLags + i + length(validationData) - 1);
end
% Automatically tune hyperparameters using Bayesian optimization
ensembleModel = fitrensemble(X_train, Y_train, ...
'Method', 'LSBoost', ... % Use LSBoost for regression
'OptimizeHyperparameters', 'auto', ... % Automatically tune hyperparameters
'HyperparameterOptimizationOptions', struct('AcquisitionFunctionName', 'expected-improvement-plus', ...
'MaxObjectiveEvaluations', 30, ... % Number of iterations for optimization
'ShowPlots', false)); % Set to true to see optimization progress
% Display the best hyperparameters
bestHyperparameters = ensembleModel.HyperparameterOptimizationResults.XAtMinObjective;
disp('Best Hyperparameters:');
disp(bestHyperparameters);
% Predict on the validation set
Y_validation_pred_norm = predict(ensembleModel, X_validation);
% Denormalize the validation predictions
Y_validation_pred = Y_validation_pred_norm * stdTS + meanTS;
% Calculate the error on the validation set
validationError = mean((Y_validation * stdTS + meanTS - Y_validation_pred).^2);
fprintf('Validation Error: %.4f\n', validationError);
% Forecast beyond the available data to a horizon of 100
forecastHorizon = 100;
Y_forecast_norm = zeros(forecastHorizon, 1);
X_forecast = X_validation(end, :); % Start with the last validation input
for i = 1:forecastHorizon
% Predict the next value
Y_forecast_norm(i) = predict(ensembleModel, X_forecast);
% Update the input features for the next prediction
X_forecast = [X_forecast(2:end), Y_forecast_norm(i)]; % Update lagged features
end
% Denormalize the forecast
Y_forecast = Y_forecast_norm * stdTS + meanTS;
% Plot the original data, validation predictions, and forecast
figure;
plot(1:length(timeSeries), timeSeries, 'b', 'LineWidth', 1.5); % Original data
hold on;
plot(trainSize+1:trainSize+length(validationData), Y_validation_pred, 'r', 'LineWidth', 1.5); % Validation predictions
plot(trainSize+length(validationData)+1:trainSize+length(validationData)+forecastHorizon, Y_forecast, 'g', 'LineWidth', 1.5); % Forecast
legend('Original Data', 'Validation Predictions', 'Forecast');
xlabel('Time');
ylabel('Value');
title('Time Series Forecasting with Normalization and Automatic Hyperparameter Tuning');
grid on;
hold off;
Respuestas (1)
Nithin
el 25 de Feb. de 2025
1 voto
The model outputs a flat line because the data lacks strong seasonality, making it difficult for the model to predict future values. As a result, the model tends to predict values close to the average of the previous data.

The output indicates that the model is struggling to identify a pattern, thus defaulting to the mean value. I tried to further verify the code by repeating the input data multiple times by stacking it and then predicting the output using the same model. The image below demonstrates that the model functions as expected and accurately forecasts the data.

Therefore, adding more data to the input helps resolve the issue by enabling the model to detect a meaningful pattern and make accurate predictions.
I hope this helps address your query.
1 comentario
yamid
el 26 de Feb. de 2025
Categorías
Más información sobre Linear Predictive Coding en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
