Time Series Modeling of Bitcoin Prices | by Paul Tune | Towards Data Science

While the world was (and still is) in the grip of a pandemic in 2020, Bitcoin has had a stellar run. From its spectacular crash in 2018 and a relatively uneventful year in 2019, it started 2020 with a price of USD$8,000 to an all new record high of USD$50,000 as of the writing of this article. That’s roughly more than a 600% return! It is one of the most successful investments of 2020.
Yet Bitcoin is, to say the least, controversial.
For one, academics cannot decide if it is a currency that can be used for transactions, a store of value like gold or a mere collectible. Some have used it as an example to fundamentally question the nature of money itself.
Bitcoin’s supporters believe it and other cryptocurrencies are the future of money for good reason. We have seen the ease of the printing of currencies through stimulus programs demonstrated by world governments after the COVID-19 outbreak last year. With this in mind, Bitcoin supporters see the built-in scarcity of Bitcoin through the increasing difficulty in mining for new Bitcoins as a way to overcome its devaluation. Bitcoin’s deflationary nature makes it attractive as a store of value. It can be used for transactions in a completely decentralized manner, making it attractive for circumventing the middle man, often considered to be the banks.
Companies are split about its potential. Square was one of the first major companies to buy $50 million USD worth of Bitcoins for transaction purposes, followed by Paypal. Tesla has recently announced that the company would be heavily invested in Bitcoin, buying about USD$1.5 billion worth of Bitcoin.
Well-known investors have differing opinions if Bitcoin is an investment, or not. Warren Buffett said
Cryptocurrencies basically have no value and they don’t produce anything. They don’t reproduce, they can’t mail you a check, they can’t do anything, and what you hope is that somebody else comes along and pays you more money for them later on, but then that person’s got the problem. In terms of value: zero.
Some in the public dismiss Bitcoin as a currency for pirates and criminals. Some are increasingly worried about its energy consumption, which is higher than all of Argentina’s electricity consumption. Many still do not understand what it is and how its underlying technology, the blockchain works. And some have bought cryptocurrency just through the sheer fear of missing out!
Whatever your stand is on the matter, Bitcoin has certainly produced no shortage of opinions. What does matter now is that we can improve our data science skills by learning some time series modeling.
In this article, we learn about price forecasting and time series modelling using Facebook’s highly useful forecasting tool: Prophet. We will also learn how to factor in multiple data sources to improve our price estimates. All code shown in this article can be found here.
Before we begin, a disclaimer. This article is by no means a way to accurately determine the price of Bitcoin, and is purely for educational purposes. Please understand the financial risks before buying Bitcoin or any other financial asset.
Bitcoin has naturally attracted the attention of the public due to the potential of massive gains. It is then not surprising that there have been efforts to develop price forecasting models for Bitcoin and various cryptocurrencies. We’ll provide an overview of two models here.
One model relies on Metcalfe’s law, simply stated as
The value of a network is proportional to the square of the number of users in the network
The proposed model by Timothy Peterson and the follow-up paper applied Metcalfe’s law to Bitcoin. Since the value of Bitcoin is driven by the number of users, we would have to model the growth of users of Bitcoin. The model uses a Gompertz function to model the growth instead of a logistic function. For reference, the growth of the number of users at time t is given by
This function is fitted on the number of active Bitcoin addresses, as shown in the figure below. The forecast then serves as the foundation for the valuation of Bitcoin.
The stock-to-flow model for Bitcoin (S2F) by the pseudonymous PlanB. It is considered a controversial model for its assumption: that Bitcoin is scarce, and therefore, the value is determined by how many coins there are in circulation (stock) and how many coins have been produced (flow).
A follow-up article by PlanB proposed a new stock-to-flow model with cross asset pricing. The model is still fundamentally similar to the initial S2F model. However, this model formulates Bitcoin as going through distinct phases like water. The four distinct phases are
the proof-of-concept phase
the payments phase
the E-gold phase
the financial asset phase
These clusters were found using a genetic search algorithm, with the price predictions coming from the aforementioned S2F model.
There have been arguments against the S2F model (see here). The model has been accused of being a “chameleon”, a model that has no basis in reality, but sounds plausible. Regardless, the model’s price predictions has been on point throughout the whole of 2020 to the present
As we have seen, like any new technology, Bitcoin and cryptocurrencies in general are hard to value because no one can know exactly what’s in store for the future. We expect a lot of uncertainty around pricing, and the large variance around pricing forecasts around Bitcoin reflects this uncertainty.
Prophet is a tool developed by Sean J. Taylor and Benjamin Letham at Facebook for large scale time series forecasting. It was developed to account for time series data encountered in businesses. The underlying model accounts for a long term trend, seasonality (daily, monthly and yearly), and holidays. As we shall see, this allows us to visualize the decomposition of the modelled time series once a fit is performed.
For simplicity, we use Bitcoin prices at the open of the market. We set our starting date to be the 1st of January 2016. The end date of the training dataset is set to the 31st of December 2020. The test set is set from the 1st of January 2021 to the 12th of February 2021.
Our goal is to test how well Prophet fits on Bitcoin’s prices with and without additional side information.
Our initial model only uses Bitcoin’s past price action to determine its future price. We will be using the Prophet’s Python API. Prophet is easy to use but it requires that your Pandas DataFrame is formatted with a Date and y column. In this case the y column is Bitcoin’s price in USD.
We denote our model by init_model . I explicitly set the daily seasonality to False because our data is in days and we don’t have a finer timescale to model fluctuations in a day.
Another important setting is to have seasonality_mode="multiplicative" . The reason for this is that Bitcoin’s seasonality fluctuations are increasing year-on-year, which fits a multiplicative model rather than an additive one.
init_model = Prophet(    daily_seasonality=False,     seasonality_mode="multiplicative",)init_model.fit(input_df)
No optimization of Prophet’s parameters were attempted. This forecast is the not the same as the forecast made here, since the forecast made there used an additive seasonality mode. I have also placed a hard constraint that price is at least $0. Another option to explore is to fit on the logarithm of Bitcoin’s price instead, which would ensure the constraint on the price is met.
Constraints on the forecasts are still work in progress in Prophet, so we will have to make do for now.
The fit is quite good on the training set. The R2 score for this model on the training set is 0.96. The root mean squared error (RMSE) is 946. However, the model unfortunately was not able to predict the recent run up in prices, but the trend is still going up.
Prophet provides a prediction yhat with lower and upper predictions (given by yhat_lower and yhat_upper respectively) . The predictions vastly underpredicts prices at the end of January compared to the current price of Bitcoin (which exceeded USD$50,000 as of writing).
We can do better.
The Bitcoin network itself possesses a wealth of information. These include the number of unique addresses, transaction volumes, total Bitcoins currently in circulation, and the gradual difficulty in mining through the hash rate. These information is up-to-date and publicly available from Quandl.
Moreover, macroeconomic factors do play a part in determining price. The overall market capitalization can be captured through the Standard and Poor’s (S&P) 500’s index. The US Treasury Bill rates captures the current interest rates, thereby providing an indicator of an appetite by investors for riskier assets. We can get these information from Yahoo! Finance.
Finally, given the buzz about Bitcoin, we can add search trend information from Google Trends.
We can see the correlation between the unique addresses with Bitcoin price over rolling 30-day windows. We can see that it is generally positive, so that would provide additional information to our price prediction.
We can add regressors easily by calling the add_regressor method, as shown here:
extended_model = Prophet(    daily_seasonality=False,     seasonality_mode="multiplicative",)  for col in input_df.columns:         if col not in ["ds", "y"]:         extended_model.add_regressor(col, mode="additive")  extended_model.fit(input_df)
I have chosen the regressors to be additive. I have tested with multiplicative regressors, but the fit is far worse.
The fit is marginally better than the previous model, with an R2 score of 0.97 with an RMSE of 822.
The price prediction given by this model still vastly under predicts the actual price.
The departure from the from actual price could mean that we lack additional information, which the model cannot account for. For instance, we have not added sentiment data from social media, or news outlets. A good example would be a jump in Bitcoin price when Elon Musk announced that Tesla has bought $1.5 billion dollars in Bitcoin, as Tesla will now accept payments in Bitcoin.
It is important to note that a regressor is only useful if a good forecast of the regressor can be obtained. For example, we can assume the number of unique addresses to grow like predictions from a Gompertz curve, like in Peterson’s Metcalfe law-based model. So in practice, it would be important to find regressors that are stable and fairly easy to predict.
Recall that Prophet’s model assumes that there is a long term trend, seasonality, and holidays effects. We now visualize these components.
We can look at the decomposition of the forecast by a simple method call
fig = extended_model.plot_components(forecast)
which produces
We can see that the price is only trending up in the long term according to the model. Interestingly, prices are lower on the weekends than on weekdays. Moreover, prices also rise higher towards the end of the year to the start of the following year than other periods. We can also see the seasonality in the extra regressors we have added.
We can also apply Prophet to Ethereum, a blockchain network with smart contract functionality. Here, I use the number of active addresses in the network and Google’s search trend sentiment.
More details can be found in the supplied code.
The famous physicist, Niels Bohr, is purported to have said
It is difficult to make predictions, especially about the future
Forecasting Bitcoin prices is no different. Who would have thought that Bitcoin would have its roots in an obscure whitepaper written by a anonymous author, to almost a trillion USD worth in market capitalization today? As with all things though, there are a few lessons we can gather from this exercise.
The first is that every forecasting model has a set of assumptions. These assumptions can be implicit or explicit, but they are there.
Peterson’s model assumes that Bitcoin is a network that follows Metcalfe’s law. PlanB’s model assumes that scarcity will ultimately be the deciding factor of Bitcoin’s value. In Prophet, the underlying model has an explicit structure: it has trend, seasonal and spurious (holiday) components. Deep learning models have other assumptions but they can be implicit due to their non-parameteric nature.
The strength of models with explicit structure is that it is easier to perform sensitivity analysis to test the robustness of the model under various scenarios. This is especially important in financial models, as this can spell disaster if an overoptimistic forecast is made.
The second is that forecasting by itself is only half the task. Often forecasting goes hand-in-hand with a decision. In this case, it would be a decision to buy, hold or sell Bitcoin. It would be wise to have models that produce a range and uncertainty bounds. In this way, when uncertainty is very high, allocation is lower with more diversification, so as to reduce the overall risk of a portfolio. Some methods such as reinforcement learning learns both the forecast and decision together, but that is a topic for another day.
Prophet is a very useful tool for time series forecasting, but like with every other model, the wielder of the tool has to check if her data matches the assumptions of the model. I hope that this article shows how additional information can be added to Prophet to make it more powerful.
The thoughts and views expressed in this article are mine alone and do not reflect the view of Towards Data Science. This article is intended to be educational in nature and should not be construed as individual investment advice nor as a recommendation to buy, sell, or hold any security or to adopt any investment strategy.
Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.