Apple Stock and Bitcoin Price Predictions Using FB’s Prophet -Using Python
By: Senthil E
Tags: Stock Market, Bitcoin, Time Series Analysis, Python, Data Science

Apple Stock Price Prediction

First import all the libraries

2.Fetch the Apple Stock prices

We can use free API’s like

Alphavantage API Quandl API Pandas_datareader

I am using the Alphavantage API

Get the API Key

Alphavantage documentation

You can output the data to

CSV

Json

Pandas dataframe

Also, you can specify the output size

Compact — Returns last 100 days

— Returns last 100 days Full — returns last 20 years of data

The data frame looks like below

3. Write it to a file in case if you need it in the future.

4. Do some basic plotting

Closing Price Histogram

Technical Analysis :

SMA : Simple Moving Average

A simple moving average (SMA) is an arithmetic moving average calculated by adding recent closing prices and then dividing that by the number of time periods in the calculation average.

Relative Strength Index (RSI)

The Relative Strength Index (RSI), developed by J. Welles Wilder, is a momentum oscillator that measures the speed and change of price movements. The RSI oscillates between zero and 100. Traditionally the RSI is considered overbought when above 70 and oversold when below 30. Signals can be generated by looking for divergences and failure swings. RSI can also be used to identify the general trend.

5. Now convert the dataframe into fbprophet expected format. The input to Prophet is always a dataframe with two columns: ds and y . The ds (datestamp) column should be of a format expected by Pandas, ideally YYYY-MM-DD for a date or YYYY-MM-DD HH:MM:SS for a timestamp. The y column must be numeric, and represents the measurement we wish to forecast.

6. Prophet follows the sklearn model API. We create an instance of the Prophet class and then call its fit and predict methods. We fit the model by instantiating a new Prophet object. Any settings to the forecasting procedure are passed into the constructor. Then you call its fit method and pass in the historical dataframe. Fitting should take 1-5 seconds.

7. Forecast for next 1 year. Predictions are then made on a dataframe with a column ds containing the dates for which a prediction is to be made. You can get a suitable dataframe that extends into the future a specified number of days using the helper method Prophet.make_future_dataframe . By default it will also include the dates from the history, so we will see the model fit as well.

The forecast df will contain the next one year dates. The last 10 records look like below

8. The predict method will assign each row in future a predicted value which it names yhat . If you pass in historical dates, it will provide an in-sample fit. The forecast object here is a new dataframe that includes a column yhat with the forecast, as well as columns for components and uncertainty intervals.

9. You can plot the forecast by calling the Prophet.plot method and passing in your forecast dataframe.

10. If you want to see the forecast components, you can use the Prophet.plot_components method. By default you'll see the trend, yearly seasonality, and weekly seasonality of the time series. If you include holidays, you'll see those here, too.

11. An interactive figure of the forecast can be created with plotly. You will need to install plotly separately, as it will not by default be installed with fbprophet.

12.Create the bitcoin dataset

No of records

13.Some basic data visualization

14. Now fit the model and do the forecast.The code is

The plot is

Understanding the Plot:

First plot the date and price

The plot looks like

The forecast plot looks like

The plot function is

There is a blog that explains clearly on the plot. The link is below

Validation

This cross validation procedure can be done automatically for a range of historical cutoffs using the cross_validation function. We specify the forecast horizon ( horizon ), and then optionally the size of the initial training period ( initial ) and the spacing between cutoff dates ( period ). By default, the initial training period is set to three times the horizon, and cutoffs are made every half a horizon.

The output of cross_validation is a dataframe with the true values y and the out-of-sample forecast values yhat , at each simulated forecast date and for each cutoff date. In particular, a forecast is made for every observed point between cutoff and cutoff + horizon . This dataframe can then be used to compute error measures of yhat vs. y .

Here we do cross-validation to assess prediction performance on a horizon of 365 days, starting with 730 days of training data in the first cutoff and then making predictions every 180 days. On this 8 year time series, this corresponds to 11 total forecasts.

The df looks like

You can see both actual y and forecasted value yhat.

Performance Metrics

The performance_metrics utility can be used to compute some useful statistics of the prediction performance ( yhat , yhat_lower , and yhat_upper compared to y ), as a function of the distance from the cutoff (how far into the future the prediction was). The statistics computed are mean squared error (MSE), root mean squared error (RMSE), mean absolute error (MAE), mean absolute percent error (MAPE), and coverage of the yhat_lower and yhat_upper estimates.

RMSE (Root Mean Square Error)

MAE (Mean Absolute Error)

MAPE (Mean Absolute Percentage Error)

Conclusion:

The main objective of this post is to install prophet, extract historical stock price data and crypto currency data , do some basic visualization and use prophet to do the forecast. Please refer the fbprophet documentation

and you can play with it by changing the parameters

Modeling with holidays

Using custom seasonality

Using a logistic growth trend model, etc

References:

[1] Sean J. Taylor and Benjamin Letham Forecasting at Scale , Facebook, Menlo Park, California, United States

Links: