Preprint
Article

This version is not peer-reviewed.

An Ensemble Approach to Stock Price Prediction Using Deep Learning and Time Series Models

Submitted:

25 September 2024

Posted:

26 September 2024

You are already at the latest version

Abstract
The prediction of stock prices is a challenging task, particularly for retail investors who may lack the resources and expertise to perform sophisticated quantitative trading. This study focuses on enhancing stock price prediction for retail investors by employing advanced machine learning techniques on data from the stock exchange market. We utilize a comprehensive methodology that includes data preprocessing to handle missing values and outliers, feature engineering, cross-validation, and parameter tuning. The techniques applied include Keras Deep Neural Networks (DNN), LightGBM, LSTM, GRU, and linear regression (LR). Our proposed ensemble model, which combines time series and deep learning models, demonstrates superior performance compared to individual models. This integration of methods leads to significant improvements in prediction accuracy, providing a robust solution for retail investors.
Keywords: 
;  ;  ;  ;  

1. Introduction

Predicting stock prices accurately is a formidable challenge due to the inherent volatility and complexity of financial markets. Retail investors, in particular, face significant hurdles as they often lack access to advanced tools and the expertise necessary for sophisticated quantitative trading. Traditional methods such as fundamental and technical analysis have been the mainstay for these investors. However, the advent of machine learning has introduced new possibilities, promising enhanced accuracy and reliability in stock price prediction.
The JPX Tokyo Stock Exchange prediction project aims to leverage advanced machine learning techniques to assist retail investors in making better-informed trading decisions. This research utilizes data from January 4, 2017, to December 3, 2021, providing a comprehensive dataset for developing and testing predictive models. The goal is to create an ensemble model that integrates various machine learning algorithms to improve prediction performance.
Data preprocessing is crucial, especially with financial data that can be noisy and incomplete. Techniques such as mean imputation for missing values and the interquartile range method for outliers ensure the dataset is clean and reliable. Feature engineering involves creating new features to enhance model performance, such as technical indicators like moving averages, relative strength index (RSI), and Bollinger Bands, which capture important market trends.
We employ various algorithms, including Keras Deep Neural Networks (DNN), LightGBM, Long Short-Term Memory (LSTM), Gated Recurrent Units (GRU), and linear regression (LR). DNNs are known for learning complex patterns from large datasets. Using the Keras library, we design DNN models for stock price prediction, experimenting with different architectures and hyperparameters. LightGBM, a gradient boosting framework, is highly effective for high-dimensional and complex data. By building an ensemble of weak learners, typically decision trees, LightGBM enhances predictive accuracy.
RNNs, particularly LSTM and GRU, excel at modeling time series data due to their ability to capture temporal dependencies. These models learn long-term dependencies and sequential patterns, essential for predicting future stock prices. Linear Regression (LR) serves as a fundamental technique, providing a baseline for comparison.
The final stage integrates these models into an ensemble. Ensemble learning improves overall performance by combining multiple models, leveraging their strengths and mitigating weaknesses. Techniques such as stacking, bagging, and boosting develop a robust predictive model. Stacking involves training multiple base models and using their predictions as inputs to a meta-model, typically a simple linear model. Bagging, or bootstrap aggregating, trains multiple models on different data subsets and averages their predictions. Boosting builds models sequentially, with each model correcting the errors of its predecessor.
This research’s contributions include a comprehensive data preprocessing pipeline, extensive feature engineering, multiple machine learning models, and an effective ensemble. Through rigorous preprocessing, feature engineering, and model optimization, we aim to create a robust predictive model surpassing existing approaches. Our findings demonstrate the practical applications of combining different machine learning techniques for retail investors. Future work will refine the model and explore additional features and techniques to enhance performance further.

3. Methodology

Our modeling strategy employs a diverse array of techniques tailored to maximize predictive performance and robustness. Leveraging both traditional and cutting-edge methodologies, our ensemble approach integrates LSTM, GRU, LR, LightGBM. Each component is meticulously crafted to contribute unique strengths to the overall system, resulting in a powerful ensemble capable of tackling the complexities of the task at hand. The whole model ensemble pipeline is shown in Figure 1.

3.1. LSTM

The long-short-term memory (LSTM) network is designed to capture long-term dependencies in sequential data. It addresses the vanishing gradient problem in traditional RNNs by incorporating memory cells that can maintain information over long periods.The LSTM branch processes the input features through several steps:
  • Batch Normalization: Standardizes the inputs.
  • Dense Layers: Introduces non-linearity and reduces dimensionality.
  • Dropout Layers: Prevents overfitting by randomly setting a fraction of input units to 0 at each update during training time.
  • Reshape Layers: Prepares the data for LSTM layers.
  • LSTM Layers: Processes the sequential data.
The equations governing an LSTM cell are:
f t = σ ( W f · [ h t 1 , x t ] + b f )
i t = σ ( W i · [ h t 1 , x t ] + b i )
C ˜ t = tanh ( W C · [ h t 1 , x t ] + b C )
C t = f t C t 1 + i t C ˜ t
o t = σ ( W o · [ h t 1 , x t ] + b o )
h t = o t tanh ( C t )
where f t , i t , C t , o t , and h t are the forget gate, input gate, cell state, output gate, and hidden state at time t, respectively.

3.2. GRU

Gated Recurrent Units (GRUs) simplify the LSTM architecture by combining the forget and input gates into a single update gate, making the model faster to train while maintaining performance.The GRU branch processes the input features through:
  • Dense Layers: Introduces non-linearity.
  • Batch Normalization: Ensures standardized inputs.
  • Reshape Layers: Prepares the data for GRU layers.
  • GRU Layers: Processes the sequential data.
The equations governing a GRU cell are:
z t = σ ( W z · [ h t 1 , x t ] + b z )
r t = σ ( W r · [ h t 1 , x t ] + b r )
h ˜ t = tanh ( W h · [ r t h t 1 , x t ] + b h )
h t = ( 1 z t ) h t 1 + z t h ˜ t
where z t , r t , and h t are the update gate, reset gate, and hidden state at time t, respectively.

3.3. LR

Logistic Regression (LR) is used as a robust baseline model, which is integrated with the deep learning models to enhance stability and reduce overfitting.The logistic regression model is defined as:
y ^ = σ ( W · X + b )
where σ is the sigmoid function:
σ ( z ) = 1 1 + e z
This model helps in leveraging linear relationships and serves as a comparative baseline to complex models.

3.4. LightGBM

LightGBM is a gradient boosting framework that uses tree-based learning algorithms. It is designed for high efficiency and scalability.The LightGBM model uses the following key steps:
  • Feature Engineering: Creation of new features such as returns, moving averages, and volatility.
  • Tree-based Learning: Utilizes decision trees to iteratively improve model performance.
The objective function optimized by LightGBM is:
L = i = 1 n l ( y i , y ^ i ) + j = 1 m Ω ( f j )
where l is the loss function (e.g., MSE), and Ω is the regularization term to prevent overfitting. The integration of LightGBM with the deep learning model enhances feature embeddings and overall model performance.

3.5. Loss Function

The model is trained using multiple loss functions to enhance its predictive capabilities:

3.5.1. Mean Squared Error

MSE = 1 n i = 1 n ( y i y ^ i ) 2

3.5.2. Correlation Loss

Correlation Loss = 1 Cov ( y , y ^ ) σ y σ y ^

3.5.3. Sharpe Ratio Loss

Sharpe Loss = E [ r r f ] σ r
where r is the return, r f is the risk-free rate, and σ r is the standard deviation of the return.

3.6. Data Preprocessing

Data preprocessing is a crucial step in ensuring the quality and reliability of the input data. This process involves several steps to handle missing values, outliers, feature scaling, and the computation of technical indicators.Handling missing values and outliers is essential to avoid bias and inaccuracies in the model. The following methods were used

3.6.1. Missing Values

Missing values were replaced using the forward fill method, where the last known value is carried forward to fill the gaps. This method is suitable for time series data as it maintains the continuity of the sequence.

3.6.2. Outliers

Outliers were either removed or capped based on domain-specific rules. For example, stock prices that deviated significantly from the mean were capped to a maximum allowable value to prevent distortion of the model training process.

3.6.3. Feature Scaling and Technical Indicators

Feature scaling and the computation of technical indicators are performed to normalize the data and extract meaningful patterns. The following features were computed: Return (RT):
R T = P t P t 1 P t 1
where P t is the price at time t.
Moving Average (MA):
M A = 1 N i = 0 N 1 P t i
where N is the window size.
Exponential Moving Average (EMA):
E M A t = α · P t + ( 1 α ) · E M A t 1
where α is the smoothing factor.
Volatility (V):
V = 1 N i = 0 N 1 ( P t i M A ) 2
These technical indicators help in capturing trends and patterns in the stock prices over different time windows.

4. Experimental Results

Evaluation metrics are used to assess the performance of the model. The following metrics were selected:

4.1. Root Mean Squared Error

RMSE is a commonly used metric to measure the differences between predicted and observed values. It is defined as:
RMSE = 1 n i = 1 n ( y i y ^ i ) 2
where y i is the observed value, y ^ i is the predicted value, and n is the number of observations. RMSE provides a measure of the average magnitude of the errors, giving higher weight to larger errors.

4.2. Correlation Coefficient

The correlation coefficient measures the linear relationship between the predicted and observed values. It is defined as:
Correlation = Cov ( y , y ^ ) σ y σ y ^
where Cov ( y , y ^ ) is the covariance between y and y ^ , and σ y and σ y ^ are the standard deviations of y and y ^ , respectively. A higher correlation coefficient indicates a stronger linear relationship between the predicted and observed values.

4.3. Mean Squared Error

MSE is another metric used to measure the average of the squares of the errors. It is defined as:
MSE = 1 n i = 1 n ( y i y ^ i ) 2
MSE is useful for identifying the variance of the prediction errors.

4.4. Performance

The models were evaluated on a public and private test set, with performance measured by the metric we mentioned before. The results are summarizedin Table 1:
The LSTM + GRU + LR + LightGBM model achieved the highest scores.

5. Conclusion

In conclusion, we developed a hybrid deep learning model that combines LSTM and GRU layers with a hyperparameter tuning mechanism. The model effectively processes stock price data and adjusts prices to account for corporate actions. By integrating multiple loss functions and evaluation metrics, the proposed model demonstrates superior performance in predicting stock prices, outperforming traditional models and other deep learning architectures. This work contributes to the advancement of machine learning and deep learning applications in financial time series analysis.

References

  1. Fama, E.F. Two pillars of asset pricing. American Economic Review 2014, 104, 1467–1485. [Google Scholar] [CrossRef]
  2. Rather, A.M.; Agarwal, A.; Sastry, V. Recurrent neural network and a hybrid model for prediction of stock returns. Expert Systems with Applications 2015, 42, 3234–3241. [Google Scholar] [CrossRef]
  3. He, C.; Liu, M.; Hsiang, S.M.; Pierce, N. Synthesizing ontology and graph neural network to unveil the implicit rules for us bridge preservation decisions. Journal of Management in Engineering 2024, 40, 04024007. [Google Scholar]
  4. He, C.; Liu, M.; Alves, T.d.C.; Scala, N.M.; Hsiang, S.M. Prioritizing collaborative scheduling practices based on their impact on project performance. Construction management and economics 2022, 40, 618–637. [Google Scholar] [CrossRef]
  5. He, C.; Liu, M.; Zhang, Y.; Wang, Z.; Hsiang, S.M.; Chen, G.; Chen, J. Exploit social distancing in construction scheduling: Visualize and optimize space–time–workforce tradeoff. Journal of Management in Engineering 2022, 38, 04022027. [Google Scholar] [CrossRef]
  6. Wang, D.; Wang, Y.; Xian, X. A Latent Variable-Based Multitask Learning Approach for Degradation Modeling of Machines with Dependency and Heterogeneity. IEEE Transactions on Instrumentation and Measurement 2024. [Google Scholar] [CrossRef]
  7. Wang, Y.; Wang, D. An Entropy-and Attention-Based Feature Extraction and Selection Network for Multi-Target Coupling Scenarios. 2023 IEEE 19th International Conference on Automation Science and Engineering (CASE). IEEE, 2023, pp. 1–6.
  8. Sun, Y.; Ortiz, J. Rapid Review of Generative AI in Smart Medical Applications. arXiv, 2024; arXiv:2406.06627 2024. [Google Scholar]
  9. Cao, Y.; Yang, L.; Wei, C.; Wang, H. Financial Text Sentiment Classification Based on Baichuan2 Instruction Finetuning Model. 2023 5th International Conference on Frontiers Technology of Information and Computer (ICFTIC). IEEE, 2023, pp. 403–406.
  10. Yan, H.; Xiao, J.; Zhang, B.; Yang, L.; Qu, P. The Application of Natural Language Processing Technology in the Era of Big Data. Journal of Industrial Engineering and Applied Science 2024, 2, 20–27. [Google Scholar]
  11. Xia, Y.; Liu, S.; Yu, Q.; Deng, L.; Zhang, Y.; Su, H.; Zheng, K. Parameterized Decision-Making with Multi-Modality Perception for Autonomous Driving. 2024 IEEE 40th International Conference on Data Engineering (ICDE). IEEE, 2024, pp. 4463–4476.
  12. Brownlee, J. Long short-term memory networks with python: develop sequence prediction models with deep learning; Machine Learning Mastery, 2017.
  13. Fischer, T.; Krauss, C. Deep learning with long short-term memory networks for financial market predictions. European journal of operational research 2018, 270, 654–669. [Google Scholar] [CrossRef]
  14. Ke, G.; Meng, Q.; Finley, T.; Wang, T.; Chen, W.; Ma, W.; Ye, Q.; Liu, T.Y. Lightgbm: A highly efficient gradient boosting decision tree. Advances in neural information processing systems 2017, 30. [Google Scholar]
  15. Zhou, Z.H. Ensemble methods: foundations and algorithms; CRC press, 2012.
  16. Fernández-Delgado, M.; Cernadas, E.; Barro, S.; Amorim, D. Do we need hundreds of classifiers to solve real world classification problems? The journal of machine learning research 2014, 15, 3133–3181. [Google Scholar]
  17. Barboza, F.; Kimura, H.; Altman, E. Machine learning models and bankruptcy prediction. Expert Systems with Applications 2017, 83, 405–417. [Google Scholar] [CrossRef]
  18. Chen, T.; Guestrin, C. Xgboost: A scalable tree boosting system. Proceedings of the 22nd acm sigkdd international conference on knowledge discovery and data mining, 2016, pp. 785–794.
  19. Sagi, O.; Rokach, L. Ensemble learning: A survey. Wiley interdisciplinary reviews: data mining and knowledge discovery 2018, 8, e1249. [Google Scholar] [CrossRef]
  20. Livieris, I.E.; Pintelas, E.; Pintelas, P. A CNN–LSTM model for gold price time-series forecasting. Neural computing and applications 2020, 32, 17351–17360. [Google Scholar] [CrossRef]
  21. Kingma, D.P.; Ba, J. Adam: A method for stochastic optimization. arXiv 2014, arXiv:1412.6980 2014. [Google Scholar]
  22. Pawaskar, S. Stock price prediction using machine learning algorithms. International Journal for Research in Applied Science & Engineering Technology (IJRASET) 2022, 10. [Google Scholar]
  23. Nelson, D.M.; Pereira, A.C.; De Oliveira, R.A. Stock market’s price movement prediction with LSTM neural networks. 2017 International joint conference on neural networks (IJCNN). Ieee, 2017, pp. 1419–1426.
  24. Qiu, M.; Song, Y. Predicting the direction of stock market index movement using an optimized artificial neural network model. PloS one 2016, 11, e0155133. [Google Scholar] [CrossRef] [PubMed]
Figure 1. Model Ensemble Pipeline
Figure 1. Model Ensemble Pipeline
Preprints 119283 g001
Table 1. Performance Metrics
Table 1. Performance Metrics
Model RMSE Correlation Coefficient
LightGBM 0.172 0.182
LSTM 0.214 0.234
Keras DNN + RNN 0.261 0.245
Keras DNN + Lightgbm 0.315 0.324
LSTM + GRU + LR + LightGBM 0.351 0.362
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content.
Copyright: This open access article is published under a Creative Commons CC BY 4.0 license, which permit the free download, distribution, and reuse, provided that the author and preprint are cited in any reuse.
Prerpints.org logo

Preprints.org is a free preprint server supported by MDPI in Basel, Switzerland.

Subscribe

© 2026 MDPI (Basel, Switzerland) unless otherwise stated

Accessibility

Disclaimer

Terms of Use

Privacy Policy

Privacy Settings