Sujal Bajracharya sujalbajracharya1@gmail.com Kathmandu University,
Yajjyu Tuladhar tuladharyajjyu.2004@gmail.com Kathmandu University
Nishan Dahal dahalnishan07@gmail.com Kathmandu University
For the longest of time LSTMs and transformers have been the industry standard for time series analysis and anything that involves data as a series. This project is trying to expand the horizon for time series analysis by employing graph theory for stock forecasting.
The stock market is a crucial component of the financial system, but predicting the movement of stock prices is challenging due to the dynamic and intricate relations arising from various aspects. Traditional methods often fail to capture the interdependencies between stocks, prompting exploration of Graph Neural Networks (GNNs) for improved accuracy. To make use of the property of long memory of stock in prediction, our framework applies a visibility method to turn stock features into graph structure, and then applies GNN architecture such as: GCN and GAT to forecast the future stock trend. The results demonstrated promising values with GCN able to capture graph structure more than GAT. These findings highlighted the GNN based approach to model complex financial relationships, providing valuable insights for investors and contributing to predictive tools in the field of Nepalese Stock Market.
Keywords: Graph Neural Network, LSTM, transformers, GCN, GAT
On behalf of the entire project team, we would like to express our heartfelt gratitude to Dr. Yagya Raj Pandeya, Head of the Department of Artificial Intelligence and the supervisor of this project, for his invaluable guidance, continuous support, and encouragement throughout the course of our research, "Exploring Graph Methods for Stock Forecasting." His expertise and mentorship have been instrumental in shaping the success of this project.
We are also deeply thankful to Mr. Harish Chandra Bhandari for his collaboration and significant contributions to this work. His insights and support have greatly enriched the quality and scope of our research. Lastly, we extend our gratitude to our families, friends, and peers for their unwavering support and encouragement, which have been a constant source of motivation for our team throughout this journey.
βExploring Graph Methods for Stock Forecastingβ is a project that focuses on developing a scalable and predictive model to forecast the stock market in the Nepalese Stock Market using Graph Neural Network.
In todayβs dynamic financial markets, making informed and strategic decisions regarding long-term stock investments is a formidable challenge. In Nepal, where the stock market is gradually evolving, the need for advanced predictive tools is becoming increasingly apparent. This project explores the application of Graph Neural Networks (GNNs) to predict stock market trends in Nepal.
Graph Neural Networks, a form of deep learning, excel at capturing complex relationships and patterns within data structured as graphs. By leveraging GNNs, we aim to model the intricate dependencies among different stocks and market factors, potentially offering more accurate predictions compared to traditional methods.
This project will focus on building a GNN model tailored to the Nepalese stock market, analyzing its performance against historical data, and evaluating its practical applications for investors in Nepal. The results could provide valuable contributions to the local financial ecosystem, creating a deeper understanding of stock market dynamics and improving investment strategies.
Traditional stock prediction methods, which often rely on linear models and time-series analysis, struggle to capture the intricate relationships and dependencies between different stocks. GNNs with their ability to model and learn from graph-structured data, present a promising solution.
The objective of this project is to develop a Graph Neural Network (GNN)-based model that enhances stock market predictions by leveraging the relationships between different stocks. The model aims to capture complex, non-linear interactions between various financial instruments, allowing for a more nuanced understanding of market dynamics. By working with graph-structured data, the approach will enable automatic feature learning, reducing the need for manual feature engineering and improving the overall predictive performance.
The Nepalese stock market is complex and volatile, making accurate predictions challenging with traditional models. This project is motivated by the need for advanced tools that can better capture the intricate relationships between stocks and market dynamics. Graph Neural Networks (GNNs) are used in stock price prediction because they explicitly model complex relationships between stocks, unlike LSTMs and RNNs, which focus only on sequential data. GNNs capture inter-stock dependencies, adapt to evolving financial networks with temporal extensions, and handle diverse data types like prices, news, and indicators, where LSTMs and RNNs struggle. Additionally, GNNs learn both local and global patterns, making them superior for relation-driven tasks, while LSTMs and RNNs are better suited for pure time-series analysis.
Hao Qian, Hongting Zhou, Qian Zhao, Hao Chen, 2024
Predicting stock prices is complex due to dynamic relationships influenced by economic indicators, financial reports, global news, and investor sentiment. Traditional models struggle to capture these multifaceted and temporal influences. To overcome these challenges, the Multi-relational Dynamic Graph Neural Network (MDGNN) framework is introduced. This framework uses a dynamic graph to capture the evolving relationships among stocks and related entities over time, while leveraging the Transformer's ability to encode temporal changes. The MDGNN framework provides a comprehensive view of these relationships and outperforms state-of-the-art methods in predicting stock movements on public datasets.
Timothe Watteau, 2024
This project explores the application of Graph Neural Networks (GNNs) to analyze the S&P100 index, comprising 100 leading U.S. stocks, to uncover intricate relationships and patterns within financial markets. By leveraging GNNs, the project provides a comprehensive framework for tasks such as stock price forecasting, clustering, trend classification, and portfolio optimization. Temporal Graph Convolutional Networks, such as T-GCN and A3T-GCN were implemented. It mainly focused on Spatio-Temporal Graph Neural Networks (STGCNNs) for forecasting future stock prices, enabling predictive analysis. Additionally, a novel Temporal Convolutional Graph Autoencoder is employed for deep graph clustering, enabling the identification of stock groupings based on historical price data and graph structure.
This project discusses the application of deep learning models, particularly Graph Convolutional Networks (GCN) and transformers, to improve stock price prediction by considering the relationships between different stocks. Traditional methods often focus on a stock's historical data in isolation, but this approach integrates multiple graph features to capture the interdependencies among stocks. The research explores various graph construction methods and applies these in conjunction with GCN and transformers to predict stock prices using data from the New York Stock Exchange. The findings indicate that while the combination of multiple graphs enhances prediction accuracy, it does not surpass the performance of a general GCN model due to the quality of the constructed graphs. Among the graph types tested, the correlation graph proved most effective, and both the multi-graph GCN and transformer with graph masking outperformed the traditional LSTM model.
The dataset, collected from the NEPSE Alpha online portal up to January, encompasses data for 133 companies across 11 sectors, including commercial and development banks, finance, and tourism. It includes financial indicators like opening price, closing price, daily high, daily low, quantity traded, and turnover.
Data cleaning involved handling missing values by using imputation techniques such as mean or median replacement or removing rows with excessive gaps. Duplicates were identified and removed to ensure data consistency, and outliers were detected using statistical methods like z-scores and treated appropriately. Data types and formats were standardized, ensuring numerical and date fields were parsed correctly. Inconsistent entries, such as errors in sector classifications or numeric formatting, were corrected. Additionally, derived features such as daily price range, percentage changes, and moving averages were added to enrich the dataset. These steps ensured the data's accuracy, consistency, and readiness for detailed analysis and modeling.
Graph is constructed using PyTorch Geometric, the GNN is also done in PyTorch Geometric. After creation the graph is visualized using NetworkX.
Graphs will be created using the fundamental data which has features [open, close, high, low, per change, traded amount, traded quantity, published date]
For the graph every node will represent a unique day in the time series data. Every node will have 7 features namely [open, close, high, low, per change, traded amount, traded quantity] now there is one feature remaining that is the published date this does not play any active role in the stock price prediction but this will also be included in every node as a non-training feature. Non-training features in the context of GNNs are features that can be used to uniquely identify a node but will not be used in any way during the training process. The reason for including published date in every node is that later on it will be used when creating inter stock edges.
Stock graphs are graphs created using only a single stock's data. In this graph we treat each stock as an independent entity and that is not affected by the other stocks whatsoever. The edges in this graph are created according to the visibility algorithm and convert a single variable time series data into a graph. The main principle behind the visibility algorithm is that for two arbitrary data values (
Now, selecting the feature to be used in the visibility algorithm. We have 7 features available among which open, close, high and low are highly correlated so choosing anyone among them will yield identical results. Traded quantity and amount are not that strong predictor of price so we wonβt use them and per changeβs value oscillates very heavy which results in a very dense graph which consequently results in over-smoothing of information during training as our model aggregates information up to the neighbors of its neighbors. Therefore, the most optimal feature for the visibility algorithm is βcloseβ as that is the feature we will be focusing on predicting on the sector-wise model.
For each graph we will consider of window of 30 steps in the data and the graph will be created as illustrated below:
In this way graphs are created for every window in the dataset for all individual stocks.
Sector graphs are inter stock graphs created using the data of stocks belonging to the same sector. For inter stock edges we used the published date included in each node to connect nodes with matching dates as illustrated below:
fig: Graph combining visualization
This process will be done for all the stocks in a sector until all nodes of the same day are connected.
Edge weights are the degree of connection between the nodes and some GNNs use them for better information pooling from the neighbor nodes. In our graph the weight of the edge between node A and B is
We use two architectures Graph Convolution Network(GCN) and Graph Attention Network(GAT). These networks though similar have there own pros and cons.
fig: GCN Architecture
A Graph Convolutional Network(GCN) is a neural network architecture designed for semi-supervised learning on graph-structured data. In this context, a two layer GCN is applied for information pooling up to neighbors of a node's neighbors with a symmetric adjacency matrix A (binary or weighted). The model begins with the preprocessing step:
The forward model then takes the simple form:
In each graph convolution layer the information from neighbour nodes are aggregated and passed through a dense layer, applying learnable transformation to the combined feature vectors. The neural network weights (W) are trained using gradient descent. The updated graph representation from first GCN block is passed into the second GCN layer, further aggregating the information from node's neighbour and the neighbourβs neighbour.
A Graph Attention Network (GAT) is a neural network architecture designed for processing graph-structured data by leveraging attention mechanisms to compute dynamic, node-specific representations by attending to features of neighbouring nodes. This enables the model to focus on most relevant neighbors during feature aggregation.
In graph creation, edge weights are computed as absolute difference between the values of connected nodes which is used as additional information to compute attention score.
Each node computes attention coefficients (
Once, the attention coefficients are computed, the features of neighboring nodes are aggregated using a weighted sum:
By leveraging two GNN architectures: GCN and GAT - we achieved significant insights into the potential of graph-based models for financial time-series predictions.
The key achievements were such as follow:
fig: NABIL bank(close)
fig: ALICL bank(close)
fig: GMFIL Finance(close)
fig: ICFC Finance(close)
The project successfully demonstrated the potential of Graph Neural Networks for stock price forecasting in NEPSE. The relevant features of a stock data was transformed and extracted using visibility graphs. The sector-wise prediction also showcased the relation between stocks in a sector. By applying both GCN and GAT architectures, we achieved predictions for both stock and sector graphs.
Hao Qian, Hongting Zhou, Qian Zhao, Hao Chen, Hongxiang Yao, Jingwei Wang, Ziqi Liu, Fei Yu, Zhiqiang Zhang, Jun Zhou; 2024; MDGNN: Multi-Relational Dynamic Graph Neural Network for Comprehensive and Dynamic Stock Investment Prediction in The Thirty-Eighth AAAI Conference on Artificial Intelligence (AAAI-24)
Zhiluo Chen, Zeyu Huang, Yukang Zhou; 2023; Predicting Stock Trend Using GNN in CMLAI 2023
Timothe Watteau; 2024; S&P100 Analysis With GNNs
Lucas Lacasa , Bartolo Luque, Fernando Ballesteros, Jordi Luque, and Juan Carlos Nun; 2007; From time series to complex networks: The visibility graph
Thomas N. Kipf, Max Welling; 2017; Semi - Supervised Classification with Graph Convolutional Networks
Petar Velickovic, Guillem Cucurull, Arantxa Casanova, Adriana Romero; 2018; Graph Attention Networks