Analyze the Correlations Between Stock Market and the Keywords under News
Natural Language Processing is a technique used by a computer to understand and manipulate natural languages. Social Media can be an influence when delivering the right frame of speech, so I got very interested in this topic because I know Morgan Stanley and Stanford NLP group are using NLP techniques to evaluate past data pertaining to the stock market and world affairs of the corresponding time period, in order to make predictions in stock trends. They built models that will be able to buy and sell stock based on profitable prediction without any human interactions. The models use NLP to make smart “decisions” based on news, current affairs and some investor’s viewpoint. In my project, my motivation of this study is to scrape the titles and dates from relevant news and use them to analyze and try to find out the correlations between the stock market and the keywords under the news, so I scraped all the titles and date under the financial, economy and tech news sections from CNBC. The dataset I use was from Jan 06 2016 to Feb 06 2018. I downloaded the stock price from Jan 06 2016 to Feb 06 2016 from finance yahoo and join them by date. It was interesting to know about how to go from text data to vectors of numbers and applying Machine learning techniques that can help to influence the stock market.
I scraped the titles and data from 15 subcategories and the dataset was like this:
When I just got the data, the data was like this:
I cleaned the data and I also got to see if the stock market rise or fall compare to the previous day.
After I cleaned up the data, I tokenized the words, stemlize, removed punctuations and stop words such as ‘the’, ‘is’, ‘and’. I have also converted all the letters to lowercase to make a more even data set to play with. The data is manipulated by python(spacy package). Then I got the top 100 keywords and how many times that appeared on the news titles. The data was like this:
I used random forest run a model and got the variable importance to help me decide what are the keywords that had the most important relationship with the stock market.
I got 64 true negatives and 159 false positives, 76 false negatives and 217 true positives. This shows that my model predicts "rise" reach to 75% correct and predict "fall" 29% correct. This means that the model predict "rise" much better than "fall".
I chose 10 words from the variable importance of random forest, and got a histogram to see the a better understanding of their correlations with the stock fluctuation.
This project helped me understand the basics of Natural Language Processing and gain wider sense of the power of NLP in financial market. Even though I can’t create a perfect model or get more content and news from different websites because more features will help the model learn better, this work can be treated as a try of using Natural Language Processing and get a sense of machine learning.I will use TF-IDF(term frequency–inverse document frequency) to cut down more words. I will also try to get more news content, and add more features and observations.