A Random Walk down the Stock Market

 


For the first post of this blog, I think it is apt to start with a random walk down the stock market.

People in the market should be of no stranger to the Random Walk Hypothesis (RWH). The concept of random walk could be traced back to Jules Regnault, a French broker in the 1800s. The term was later popularised by the book, A Random Walk Down Wall Street, written by Burton Malkiel. It is a financial theory that suggests stock prices evolve randomly and thus cannot be predicted by past trend.

In this post, I put the RWH to test by constructing a Long Short-Term Memory (LSTM) model to predict stock price movement (up or down) one day ahead.

The data used in this analysis was obtained from Kaggle. It contains stock market information of securities listed on the two US stock exchanges, NASDAQ and NYSE. The Date, Open, Low, High, Close, Adjusted Close and Volume of each trading day was provided for each stock.

For this analysis, ten of the largest companies listed on the US stock market was shortlisted for model development and prediction:

1. AAPL
2. ADBE
3. AMZN
4. COST
5. CSCO
6. GOOG
7. MSFT
8. NVDA
8. PEPS
10. TSLA

Together, they accounted for about a quarter of the S&P500 index.

For each of the ten shortlisted stocks, a target variable was created by assigning a label value of TRUE if the adjusted close price for the day is higher than the adjusted close price of the previous trading day, and FALSE otherwise.

A suite of technical indicators that measure price trends and momentum such as SMA, MACD, RSI, Bollinger Bands, MFI and PROC were also generated and will be used as input features for the LSTM model. The input features were scaled between 0 and 1 prior to model training, as LSTM might mistook features with higher scale as more important.

This analysis used historical data between 2013 and 2018 to train the LSTM model and data from 2019 to evaluate model performance. Figure 1 shows the price trend of the ten stocks between 2013 and 2019.

Figure 1. Adjusted close price of the ten selected stocks between 2013 and 2019.

LSTM is a special class of Recurrent Neural Network (RNN) designed for modelling long-term dependencies in sequential data (e.g., time series, text data). It does so by using a gating mechanism in its neural network to control what information to forget and what new information to remember.

Figure 2 shows the structure of an LSTM network. Similar to RNN, it has a sequential structure connecting repeating cells that allow information from one time step to be passed to the next. Within each cell, there is a forget gate that controls how much past information to forget; an input gate that determines what new information to add; and an output gate the controls what information to be pass to the next time step. This was done by the sigmoid layer in each of the three gates to control the information flow. For more detail explanation of LSTM please visit the wonderful piece posted in colah's blog: https://colah.github.io/posts/2015-08-Understanding-LSTMs/


 Figure 2. Structure of the LSTM network

The ability of LSTM in learning long term dependencies could be the reason for its frequent out-performance relative to traditional time series forecasting technique such as ARIMA [1][2][3]. 

A separate LSTM model was built for each of the ten stocks. This is to account for the possible difference in trend behaviour among the stocks. The model was trained using a sliding window of technical indicators and price movement (up or down) from the past 60 trading days to predict the price movement one day ahead. Figure 3 illustrates the first three training instances of a sliding window. The initial window consists of the feature input vectors for the first 60 trading days and the target variable on the 61st trading day. The window slide by one time step forward to create the next training instance which covered the feature inputs between the 2nd and 61st day, as well as the target variable on the 62nd day. This process is repeated until all the training data were exhausted.

Figure 3. Illustration of a sliding window

The model performance is evaluated primarily based on precision of the predicting an upward price movement. This performance measure captures how likely an upward price movement is captured by the LSTM model.

Table 1 shows the LSTM classification performance for the ten stocks. Naïve major class refers to the fraction of the more common stock price movement. In my case, the major class of all ten stocks is UP.

Table 1. Classification performance of the LSTM model

As shown, apart from TSLA, all other stocks have the same precision and accuracy as their respective naïve major class. In fact, LSTM deemed the best approach is to predict stock prices of these nine stocks to go UP for every instance in the test set. This is likely because the model could not learn anything from the trend and momentum indicators. The classification performance for TSLA was even worse as its precision and accuracy was lower than the naïve major class. The model for TSLA could have over-fitted and learned patterns that were spurious.

The result suggests stock price movement cannot be predicted by past trends as any information from it that could affect stock prices were already reflected in the current prices. Thus, stocks prices only changes in response to new and unpredictable information, and therefore move unpredictably.  This is the essence of the weak form of the Efficient Market Hypothesis (EMH) which asserts that stock prices already reflect all information that can be derived from historical trading data.

By extension, any publicly available information, in addition to historical prices, such as earning forecasts and market outlook should also be reflected in the current stock price. This is known as the semi-strong form of the EMH. However, not all is doomed to fail yet. Analysing and quantifying the impact of firm prospects and market conditions is much more difficult and costly than identifying price patterns. As such, predictive models that could pick up profitable signals buried in a large mess of information might be rewarded.

In my next update, I will be sharing the first of a three-part series that explores the feasibility of text mining news articles to predict stock price movement.



The python code used for this analysis is available at my Github here.

How would you have constructed the predictive model differently to predict stock price movement? What other finance-related analyses you would like to see? Leave a comment below to share your thoughts!

 

[1] A.A. Adebiyi, A.O. Adewumi, C.K. Ayo (2014). Comparison of ARIMA and Artificial Neural Networks Models for Stock Price Prediction. Journal of Applied Mathematics, Volume 2014.

[2] S. Siami-Namini, N. Tavakoli, A.S. Namin (2018). A Comparison of ARIMA and LSTM in Forecasting Time Series, 2018 17th IEEE International Conference on Machine Learning and Applications (ICMLA), IEEE, pp. 1394–1401. DOI: https://doi.org/10.1109/ICMLA.2018.00227

[3] W.J. Lu, J. Z. Li, Y. F. Li, A. J. Sun, J. Y. Wang (2020). A CNN-LSTM-Based Model to Forecast Stock Prices, Introduction, pp. 11. DOI: https://doi.org/10.1155/2020/6622927


Comments

Popular posts from this blog

Reflecting on 2024

Networth Update 3Q 2024

Starting Early and Staying Focused: How I Reached $500,000 at 32