Skip to content

dominicmazvimavi/AutoregressiveLinearModelPrediction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Tensorflow 2.0: Autoregressive Linear Model for Time Series Prediction

Description

Autoregressive Linear Model for Time Series Predictions

This script demonstrates the implementation of an Autoregressive Linear Model for time series predictions using TensorFlow. The purpose of the script is to compare one-step time forecasting against multiple steps forward forecasting, showcasing the challenges and potential solutions for accurate predictions. Additionally, synthetic data is generated by adding noise to a sine wave to simulate real-world scenarios.

The script begins by generating synthetic data by combining a sine wave with random Gaussian noise using the np.random.randn function. The resulting series is then plotted to visualize the data.

Next, the dataset is constructed by splitting the series into input-output pairs with a sliding window approach. For each time step, T, a sequence of T previous values is used as the input (X), and the next value is taken as the output (Y). The resulting X and Y arrays are reshaped accordingly. The length of X is stored in N for further processing.

The Autoregressive Linear Model is then defined using the Keras functional API in TensorFlow. It consists of a single dense layer with one output unit. The model is compiled using mean squared error (MSE) as the loss function and the Adam optimizer with a learning rate of 0.1.

The model is trained using the fit method, with the training data (X[:-N//2] and Y[:-N//2]) and validated with the remaining data (X[-N//2:] and Y[-N//2:]). The loss history is plotted using matplotlib to observe the model's training progress.

To compare the one-step time forecasting approach with multiple steps forward forecasting, two different forecasting techniques are demonstrated. Firstly, the wrong forecast using true targets is showcased, where the model predicts one step ahead at each iteration and updates the predictions list accordingly. This approach illustrates the challenges faced in accurately forecasting future values using true targets.

To address these challenges, the recommended technique is demonstrated under the comment # Forecast future values (use only self-predictions for making future predictions). In this approach, the model predicts one step ahead based on its previous prediction. The last input, last_x, is initially set as the last input from the validation set. As predictions are made, last_x is updated by shifting the values one step to the left and replacing the last value with the predicted value. This approach relies solely on the model's self-predictions for forecasting future values.

Both forecasting approaches are plotted against the target values to visualize the predictions. The resulting plots provide insights into the model's performance and the effectiveness of the recommended technique for time series forecasting.

By exploring this script, developers and practitioners can gain a deeper understanding of autoregressive modeling for time series predictions. The script highlights the challenges of one-step time forecasting and demonstrates an alternative technique that relies on self-predictions for more accurate forecasting.


Languages and Utilities Used

  • Python
  • Tensorflow 2.0 Keras

Environments Used

  • Google Colab

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages