Beginners Guide — Step by Step Analysis
Apple Stock and Bitcoin Price Predictions Using FB’s Prophet -Using Python
Time Series Analysis
This post is about using Facebook Prophet for forecasting apple stock and bit coin prices using python API.
- What is the Prophet?
- Apple Stock Price Prediction
- Bitcoin Price Prediction
- Basic Data Visualization using Matplotlib and Seaborn.
About Prophet:
Prophet is an open-source package (for both Python and R) for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well . Prophet is open source software released by Facebook’s core Data Science team. It is available for download on CRAN and PyPI.
Advantages of using Prophet
* Accurate and fast
*Fully automatic
*Tunable forecasts.
*Available in R or Python
Forecasting is a common data science task that helps organizations with capacity planning, goal setting, and anomaly detection. Despite its importance, there are serious challenges associated with producing reliable and high-quality forecasts — especially when there are a variety of time series and analysts with expertise in time series modeling are relatively rare. To address these challenges, we describe a practical approach to forecasting “at scale” that combines configurable models with analyst-in-the-loop performance analysis. We propose a modular regression model with interpretable parameters that can be intuitively adjusted by analysts with domain knowledge about the time series. We describe performance analyses to compare and evaluate forecasting procedures, and automatically flag forecasts for manual review and adjustment. Tools that help analysts to use their expertise most effectively enable reliable, practical forecasting of business time series.
Source:Taylor SJ, Letham B. 2017. Forecasting at scale. PeerJ Preprints 5:e3190v2https://doi.org/10.7287/peerj.preprints.3190v2
Prophet is optimized for the business forecast tasks we have encountered at Facebook, which typically have any of the following characteristics:
- hourly, daily, or weekly observations with at least a few months (preferably a year) of history
- strong multiple “human-scale” seasonalities: day of week and time of year
- important holidays that occur at irregular intervals that are known in advance (e.g. the Super Bowl)
- a reasonable number of missing observations or large outliers
- historical trend changes, for instance due to product launches or logging changes
- trends that are non-linear growth curves, where a trend hits a natural limit or saturates
The Prophet procedure is an additive regression model with four main components:
A piecewise linear or logistic growth curve trend. Prophet automatically detects changes in trends by selecting changepoints from the data.
A yearly seasonal component modeled using Fourier series.
A weekly seasonal component using dummy variables.
A user-provided list of important holidays.
Prophet Installation
- First, install Pystan
- Then install Fbprophet
1.Install Pystan
2.Install fbprophet
I had issues with installing in windows. Please follow the documentation and install pystan without errors and then install fbprophet.
Implementing prophet
I am using
- Python 3.6
- Jupyter Notebook
- Pandas
- Matplolib
- Seaborn
- Plotly
Steps:
- Install all the required packages including prophet and alphavantage.
- Get the API key from Alphavantage.
- Get the apple stock = AAPL data using the API.
- Get the Bit Coin = BTC data using the API.
- Save it to CSV file if needed.
- Data is available in the pandas data frame.
- Make sure to convert the data frame into the prophet required format — 2 columns ds and y.
- ds is the date and it should be a datetime data type.
- y is the closing price and it should be a float.
- Choose the forecast period like what time frame you want to do the forecast like 3 months or 1 year etc.
- Fit the model.
- Forecast the model for the forecast period like 3 months or 1 year etc.
- Forecast model — gives you the forecasted closing price, the upper range of the closing price, lower range of the closing price.
- Call the prophet plot method and do some plotting.
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
- 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: