Stock Price Prediction with the help of python and fbprophet(prophet) library_(Part 1/3)
By: Rakshit Ratan
Tags: Data Science

Predicting stock prices is one of the hottest topics in the Financial market. And the most interesting point of all these predictions and forecasting are none of them are accurate. Stock market and the prices of stocks change over a lot of attributes, like demand-supply, interest rates profit as well as on the sentiments of market & investors.

So when it comes to predicting the prices, all we can hope to be as accurate as possible. In this article, we won’t be working on being right or wrong but will be using statistical tools like Python and its libraries to analyze past data and trends to forecast future prices.

I have divided this project into three different articles and in each part, we will be covering different Python libraries to predict stock prices. In this article, we will be using the facebook predict library “fbpredict”.

Key Terms and Tools:

Python — It is a high-level programming language used in Data analysis.

— It is a high-level programming language used in Data analysis. Pandas — Data wrangling, manipulation and analysis library

— Data wrangling, manipulation and analysis library NumPy — Statistical computing library

— Statistical computing library Matplotlib — Visualisation and plotting library

— Visualisation and plotting library Jupyter Notebook — Notebook editor supporting python codes

— Notebook editor supporting python codes Yahoo Finance API — Provides stock data

— Provides stock data Stock Ticker — Symbol with which a stock is traded on the exchange.

— Symbol with which a stock is traded on the exchange. Stock Market Analysis — It is the process of analyzing the future stock prices based on current attributes and also involves predicting future prices based on historical data and prices. Is has two commonly known methods: 1) Fundamental analysis and 2) Technical Analysis(More Here)

— It is the process of analyzing the future stock prices based on current attributes and also involves predicting future prices based on historical data and prices. Is has two commonly known methods: 1) Fundamental analysis and 2) Technical Analysis(More Here) Time Series Forecasting — It is a method used in all types of fields to forecast or predict future events after analyzing the historical facts, figures & data through time sequencing.

So, Let’s get started:

Importing the libraries:

import pandas as pd

from pandas import Series,DataFrame

import numpy as np # For Visualization

import matplotlib.pyplot as plt %matplotlib inline # For reading stock data from yahoo API

import pandas_datareader as web

CODE:

# We will be using the Microsoft(Ticker - ‘MSFT’) stock for this analysis.

Since Prophet library only allows two-column entry and require date as one of its input for time series forecasting. We will be using the “Adj Close” columns value and create another data frame and then reset the index. So, the date index column becomes an entry column into Prophet model.

OR we can use the following code above: #To set DataFrame as the Stock Ticker

globals()[stock] = web.DataReader(stock,’yahoo’,start,end)[‘Adj Close’]

Forecasting:

Prophet:

Prophet is a python library developed by Facebook for time series forecasting with no data preprocessing requirements. The model in Prophet takes a dataframe with two columns, data(ds) and target(y) to capture the pattern and seasonality in historical data.

Pro: Prophet works with missing data, better relative to other python libraries used for forecasting.

Con: A limited number of columns are allowed.

# Importing the prophet library: import fbprophet

# After importing the prophet library we need to create an instance of a Prophet object.

# Instantiation − The creation of an instance of a class.

Method − A special kind of function that is defined in a class definition. Object − A unique instance of a data structure that’s defined by its class.

m = fbprophet.Prophet()

# Now we have to use the fit method on the prophet object and pass in our dataframe because we need to fit our model to our historical data.

m.fit(df)

# Now it is time to create future dates, using Prophet and its builtin helper function “make_future_dataframe”

future = m.make_future_dataframe(periods=365)

future.tail()

# After creating dates we can predict future stock prices using the predict method:

Comparing the initial data collected from the yahoo finance API and data obtained after using the Predict function:

Clearly, we can observe the data forecasted has been compared on many attributes.

Visualisation:

m.plot(forecast);

# visualising the individual forecast components can be done using Prophet’s built-in “plot_components” method:

m.plot_components(forecast);

Final Thoughts

This project was something of a test, in the next two parts of this article we will be working with a few more forecasting libraries as well as we will check the forecasted data for validation.

Jupiter notebook: Click Here

#Python #fbprophet #Prophet #stocks