Joe Biden won the election and he is going to be the 46th President of the United States, there are rumours about a second covid-19 wave, the covid-19 vaccines are closer than ever. Reading all those headlines and as a stock market investor a question came to my mind: How does exogenous factors influence the stocks prices?
So as an activity of the Data Science Nanodegree I am taking at Udacity and to try to combine the two things that I love the most that is data analysis and finance I decide to analyse the stock market data, using Data Science techniques to see how factors such as politics and pandemics can influence the stock market.
As advice of the Udacity Nanodegree’s professors, I divided my analyses into questions to make it easier to understand and better organize the ideas.
The yahoo finance python library yfinance was used to get the stock data using the ticker ^GSPC which returns the daily price for the S&P 500 beginning at 1928.
The S&P 500 Index or the Standard & Poor’s 500 Index is a market-capitalization-weighted index of 500 of the largest publicly traded companies in the U.S.
The R package ggplot2 was used to get the Presidential data, from its presidential dataset and the pandemics start data was taken from the livescience.
I — Does the elected President party has some influence at stocks prices ?
For the sake of doing that analysis I got how was the performance of the stock market during each President term since 1953 until now.
As we can see the best performance since 1953 was during President Obama’s first term where the stocks had a growth of 85% and another interesting fact is that when the President was a Democratic the stocks prices always had a positive performance.
But not all terms had the same length, like the Kennedy term that had a duration of 1036 days instead of the usual 1461 days. So I took the price variation per term per year.
The first Obama term also had the best performance per year, with an average stock growth per year of more than 21% and the worst was the Nixon second term with a performance of -20% per year.
But looking only for this graph we can not know if historically a Democratic President was better for the investments.
So it was added the performance for each party since 1953 until now and divided per the time that each party was in power and adjusted for inflation of the period since 1953 until 2020 that was on average 3.44% per year.
Now we can notice that at the Democratic government the stock price had an average performance of 9.78% per year while at the Republican government the stock average stock performance was 4%.
Thus, at the Democratic government the stock price had a performance almost twice as better per year than the Republican government.
So disregarding all other factors that also influence the assets prices and considering only the President party it is more probable that the stock market will grow during the government of Joe Biden.
II — How the earlier pandemics affected the stock market ?
With the covid-19 outbreak the market was hit hard, falling almost 40% in one month. So living a crisis like that for the first time as an investor, a question arose in my mind: “Did the other pandemics have the same impact at the assets price?”
So using the same stock data from the previous analysis and getting the former pandemics starting data from the Live Science I used the data to answer this question.
The pandemics chosen for the analysis where Asian Flu, AIDS (Acquired Immunodeficiency Syndrome), Swine Flu, Ebola and Covid-19. At the plot below we can see when each pandemic started.
Looking only for this plot we can not have the response whether the pandemics had an impact at the S&P 500. But one interesting thing to point out is no matter what happens in the long run the S&P 500 tendency it is to go up.
Then I took another approach, I did a zoom in getting the stock market performance one year before and one year after each one of the pandemics.
Also It was plotted the 200-day and 50-day moving average, to see if the pandemic changed the trend of the prices.
A buy signal occurs when the 50-day crosses above the 200-day. A sell signal occurs when the 50-day drops below the 200-day according to Fidelity.
Starting with Asian Flu and AIDS, we can see that after one year of the first positive case the market was dropping.
At the AIDS plot, we could see that right after the first case there was a sell sign.
Looking for the covid-19 and Ebola graph, they look similar but with different magnitudes. In both cases there was first a drop at the prices, but after one year the stocks were on the all time high over the two years period.
There was a sell sign at the Covid-19 plot in April but in July there was a buy sign and the stocks began to rise.
And finally, looking at the plot of the Swine flu pandemic. After the pandemic there was a buy sign.
Thus, it seems like we can not know the effects on a pandemic on the short-term. But one thing that has shown valid, it is that the S&P 500 for the long run has a growth tendency.
For a next medium post I intend to expand this topic, analysing how each one of the pandemics affected key sectors like the pharmaceutical sector and the meat industry, also adding more trend indicators.
Since, looking only to the moving averages may lead to false signals.
III-Is it possible to generate a simple network to predict the stock market prices ?
So to end with a flourish, let’s try to respond to the one billion question: Can we predict the stock market prices?
My first idea was to use a simple multilayer perceptron network passing the columns Open, High, Low and Volume to predict the Close price.
But, all the features are strongly correlated with the target variable as we can see below and It is easy to predict the close price knowing the Low and the High prices.
So I chose to implement a LSTM neural network that is widely used on time series forecasting.
I chose a 10-day window to forecast the eleventh day. Dividing the data into 64% for training, 20% for testing and 16% for validation.
The LSTM was trained for 60 epochs and let’s see the results for the validation data.
For the validation data the RMSE (Root Mean Squared Error) was of 106 and as we can see at the plot the predictions were at most time close to the real values.
Now, let’s see the results for the test set.
So on average the model is wrong by $ 381 on forecasts and as we can see at the plot, the predictions are not close to the real price.
Thus, this model was not capable of realizing good predictions. An interesting exercise to the next post is to try the points below:
- Add more features to the model such as the President party and the companies profits.
- Add more complexity to the model.
- Getting the data of others stocks.
In this article, we dug into the S&P 500 historical data using Data Science techniques aiming to find how some factors influenced the assets prices during a time period.
- We analyse what was the stock market performance in each of the presidential terms since 1953 and we saw that generally the stock goes up more with Democratic Governments.
- Then, we tried to see if there was any other factor that also influenced asset prices. The factor chosen was the most important pandemics since 1957. The result was that we were unable to find a pattern for the trend of the market movement in each one of the 5 pandemics analyzed.
- Unfortunately we can not build a simple LSTM neural network to predict the assets prices. But, it is not impossible to use machine learning and statistics to find what asset to buy. The example is the Medallion Fund which uses mathematics and statistical analyses and has an impeccable record of 39 percent after fees over a 30-year span from 1988 to 2018.
The analyses above are only educational and not investment advice. All investments in the stock market have a risk.
Did you like this analysis ? Did you know that historically the stock market was better when the President was a Democratic ? What factors do you think that influence the stock market price ? Please comment down below those factors and I urge you to do an analysis to confirm your thoughts!