COMPARISON BETWEEN CLSSICAL MODELS (VAR& VECM) & A.I(ANN & RNN INCLUDING LTSM & GRU) MODELS.

Published: 10 September 2025| Version 1 | DOI: 10.17632/d2tpkh2ysk.1
Contributor:
Eldirdiri Fadol Ibrahim Ibrahim

Description

This Python script performs a comprehensive comparison of classical time series models and AI-based models on synthetic financial datasets. It generates synthetic gold price data (Open, Close, High, Low) and banking data (Interest Rate, Deposits, Loans, Exchange Rate) for 200 daily observations. The script then: Applies classical models – VAR (Vector Autoregression) and VECM (Vector Error Correction Model) – to analyze relationships between gold prices and banking indicators and to forecast future prices. Implements AI models – ANN (Artificial Neural Network), Simple RNN, LSTM, and GRU – to predict gold prices based on bank data sequences. Evaluates model performance using RMSE and R² metrics. Plots the actual gold prices, AI predictions, and VAR/VECM forecasts in a single figure for direct visual comparison. The script is written in a single-page format, making it easy to run and modify. It demonstrates the effectiveness of classical econometric models versus AI sequence models in financial forecasting. Author: Prof. Eldirdiri Fadol Ibrahim

Files

Steps to reproduce

Install Python 3.x and the required libraries including pandas, numpy, matplotlib, scikit-learn, statsmodels, and tensorflow/keras. Use a virtual environment to manage dependencies and set np.random.seed(42) for reproducibility. Begin by generating synthetic gold price data with the columns Date, Open, Close, High, and Low. Simulate the Close price as a cumulative sum of small random increments with added Gaussian noise to create realistic fluctuations. Derive the Open, High, and Low prices consistently with Close, ensuring the High is always above the maximum of Open and Close and the Low is always below the minimum. Next, generate synthetic bank data including Date, InterestRate, Deposits, Loans, and ExchangeRate. Simulate trends using cumulative sums combined with random variations to imitate real-world financial behavior. Merge the gold and bank datasets on Date and set Date as the index for time series analysis. Perform exploratory data analysis by plotting the time series of gold and banking variables, inspecting trends, outliers, and anomalies, and computing descriptive statistics such as mean, median, and standard deviation to understand the underlying distributions. For classical econometric modeling, apply differencing to ensure stationarity. Prepare the data for a VAR model by combining Close with InterestRate, Deposits, Loans, and ExchangeRate. Fit a VAR on the differenced dataset, determining the optimal lag using criteria such as AIC or BIC. Generate multi-step forecasts to assess model performance. Conduct a Johansen cointegration test to examine long-term equilibrium relationships and fit a VECM with an appropriate rank to model both long-term and short-term dynamics between gold prices and bank variables. For AI modeling, normalize the data using MinMaxScaler. Create sequences of a specified length (e.g., 5) for RNN, LSTM, and GRU models, where the input consists of past banking features and the target is the next-day gold Close. Flatten sequences for ANN input. Split the sequences into training (80%) and testing (20%) sets. Construct and train an ANN with input flattened, hidden ReLU layers, and a linear output layer. Build RNN, LSTM, and GRU models with respective recurrent layers followed by Dense output layers. Compile all AI models with the Adam optimizer and MSE loss, training each for 50 epochs with a batch size of 8. Generate predictions for the test set and inverse-transform scaled outputs back to the original gold price scale. Evaluate all models using RMSE to measure prediction error and R² to assess variance explained. Compare ANN, RNN, LSTM, and GRU predictions with VAR and VECM forecasts. Visualize the results by plotting actual versus predicted gold prices for all AI models, optionally overlaying VAR and VECM forecasts for comparison.

Institutions

  • Moorfields Private

Categories

Data Analysis, Consumer Economics of Economic System, Public Economics of Economic System

Licence