## Accurate Bitcoin Price Forecasting with Python – Blockchain Applications of Data Science Part 2

### Recap

We discussed earlier in Part 1 of Blockchain Applications of Data Science on this blog how the world could be made to become much more profitable for not just a select set of the super-rich but also to the common man, to anyone who participates in creating a digitally trackable product. We discussed how large scale adoption of cryptocurrencies and blockchain technology worldwide could herald a change in the economic demography of the world that could last for generations to come. In this article, we discuss how AI and data science can be used to tackle one of the most pressing questions of the blockchain revolution – how to model the future price of the Bitcoin cryptocurrency for trading for massive profit.

#### A Detour

But first, we take a short detour to explore another aspect of cryptocurrency that is not commonly talked about. Looking at the state of the world right now, it should be discussed more and I feel compelled to share this information with you before we skip to the juicy part about cryptocurrency price forecasting.

#### The Environmental Impact of Cryptocurrency Mining

Now, two fundamental assumptions. I assume you’ve read Part 1, which contained a link to a visual guide of how cryptocurrencies work. In case you missed the latter, here’s a link for you to check again.

The following articles speak about the impact of cryptocurrency mining on the environment. Read at least one partially at the very least so that you will understand as we progress with this article:

So cryptocurrency mining involves a huge wastage of computational resources, energy, and enough electrical power to run an entire country. This is mainly due to the model of the Proof-of-Work PoW mining system used by Bitcoin. For more, see the following article..

https://www.investopedia.com/tech/whats-environmental-impact-cryptocurrency/

In PoW mining, miners compete against each other in a desperate race to see who can find the solution to a mathematical hashing problem the quickest. And in every race, only one miner is rewarded with the Bitcoin value.

In a significant step forward, Vitalin Buterik’s Ethereum cryptocurrency has shifted to Proof-of-Stake based (PoS) mining system. This makes the mining process significantly less energy intensive than PoW. Some claim the energy savings may be 99.9% more efficient than PoW. Whatever the statistics may be, a PoS based mining process is a big step forward and may completely change the way the environmentalists feel about cryptocurrencies.

So by shifting to PoS mining we can save a huge amount of energy. That is a caveat you need to remember and be aware about because Bitcoin uses PoW mining only. It would be a dream come true for an environmentalist if Bitcoin could shift to PoS mining. Let’s hope and pray that it happens.

Now back to our main topic.

#### Use AI and Data Science to Predict Future Prices of Cryptocurrency – Including the Burst of the Bitcoin Bubble

What is a blockchain? A distributed database that is decentralized and has no central point of control. As on Feb 2018, the Bitcoin blockchain on a full node was 160-odd GB in size. Now in April 2019, it is 210 GB in size. So this is the question I am going to pose to you. Would it be possible to use the data in the blockchain distributed database to identify patterns and statistical invariances to invest minimally with maximum possible profit? Can we forecast and build models to predict the prices of cryptocurrency in the future using AI and data science? The answer is a definite **yes.**

#### Practical Considerations

You may wonder if applying data science techniques and statistical analysis can actually produce information that can help in forecasting the future price of bitcoin. I came across a remarkable kernel on www.Kaggle.com (a website for data scientists to practice problems and compete with each other in competitions) by a user with the handle **wayward artisan **and the profile name **Tania J**. I thought it was worth sharing since this is a statistical analysis of the rise and the fall of the bitcoin bubble vividly illustrating how statistical methods helped this user to forecast the future price of bitcoin. The entire kernel is very large and interesting, please do visit it at the link given below. Just the start and the middle section of the kernel is given here because of space considerations and intellectual property considerations as well.

#### A Kaggle Kernel That Modelled the Bitcoin Bubble Burst Within Reasonable Error Limits

This following kernel uses cryptocurrency financial data scraped from www.coinmarketcap.com. It is a sobering example of how AI predictions actually predicted the collapse of the bitcoin bubble, prompting as many sellers to sell as they did. Coming across this kernel is one of the main motivations to write this article. I have omitted a lot of details, especially building the model and analyzing its accuracy. I just wanted to show that it was possible.

For more details, visit the kernel on Kaggle at the link:

https://www.kaggle.com/taniaj/cryptocurrency-price-forecasting (Please visit this page**, **all aspiring data scientists. And pay attention to every concept discussed and used. Use Google and Wikipedia and you will learn a lot.)

A subset of the code is given below (the first section):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import pandas as pd from pandas import DataFrame import numpy as np import matplotlib.pyplot as plt import seaborn as sns from datetime import datetime, timedelta from statsmodels.tsa.arima_model import ARIMA from statsmodels.tsa.statespace.sarimax import SARIMAX from statsmodels.graphics.tsaplots import plot_acf, plot_pacf from statsmodels.tsa.stattools import adfuller from statsmodels.tsa.seasonal import seasonal_decompose from scipy import stats import statsmodels.api as sm from itertools import product from math import sqrt from sklearn.metrics import mean_squared_error import warnings warnings.filterwarnings('ignore') %matplotlib inline colors = ["windows blue", "amber", "faded green", "dusty purple"] sns.set(rc={"figure.figsize": (20,10), "axes.titlesize" : 18, "axes.labelsize" : 12, "xtick.labelsize" : 14, "ytick.labelsize" : 14 }) |

*<subsequent code not shown for brevity>*

The dataset is available at the following link as a csv file in Microsoft Excel:

We focus on one of the middle sections with the first ARIMA model with SARIMAX (do look up Wikipedia and Google Search to learn about **ARIMA **and **SARIMAX**) which does the actual prediction at the time that the bitcoin bubble burst (only a subset of the code is shown). Visit the Kaggle kernel page on the link below this extract to get the entire code:

*<data analysis and model analysis code section not shown here for brevity>*

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def invboxcox(y,lmbda): if lmbda == 0: return(np.exp(y)) else: return(np.exp(np.log(lmbda*y+1)/lmbda)) # Prediction btc_month_pred = btc_month[['Close']] date_list = [datetime(2018, 6, 30), datetime(2018, 7, 31), datetime(2018, 8, 31)] future = pd.DataFrame(index=date_list, columns= btc_month.columns) btc_month_pred = pd.concat([btc_month_pred, future]) #btc_month_pred['forecast'] = invboxcox(best_model.predict(start=0, end=75), lmbda) btc_month_pred['forecast'] = invboxcox(best_model.predict(start=datetime(2014, 1, 31), end=datetime(2018, 8, 31)), lmbda) btc_month_pred.Close.plot(linewidth=3) btc_month_pred.forecast.plot(color='r', ls='--', label='Predicted Close', linewidth=3) plt.legend() plt.grid() plt.title('Bitcoin monthly forecast') plt.ylabel('USD') |

*<more code, not shown>*

From

Kaggle Code

This code and the code earlier in the kernel (not shown for the sake of brevity) that built the model for accuracy gave the following predictions as output:

What do we learn? Surprisingly, the model captures the Bitcoin bubble burst with a remarkably accurate prediction (error levels ~ 10%)!

### Conclusion

So, does AI and data science have anything to do with blockchain technology and cryptocurrency? The answer is a resounding, **yes**. Expect data science, statistical analysis, neural networks, and probability model distributions to play a heavy part when you want to forecast cryptocurrency prices.

For all the data science students out there, I am going to include one more screen from the same kernel on Kaggle (link):

The reason I want to show you this screen is that the terms and statistical lingo like **kurtosis** and **heteroskedasticity** are statistics concepts that you need to master in order to conduct forecasts like this, the main reason being to analyze the accuracy of the model you have constructed. The output window is given below:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
parameters aic 20 (1, 0, 1, 1) -198.417805 18 (1, 0, 0, 1) -198.151123 6 (0, 1, 0, 1) -197.632770 8 (0, 1, 1, 1) -197.415548 22 (1, 0, 2, 1) -197.335392 Statespace Model Results ========================================================================================= Dep. Variable: close_box No. Observations: 63 Model: SARIMAX(1, 1, 0)x(1, 1, 1, 4) Log Likelihood 103.209 Date: Sun, 03 Jun 2018 AIC -198.418 Time: 19:11:23 BIC -189.845 Sample: 04-30-2013 HQIC -195.046 - 06-30-2018 Covariance Type: opg ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ ar.L1 0.3760 0.121 3.119 0.002 0.140 0.612 ar.S.L4 -0.2801 0.085 -3.312 0.001 -0.446 -0.114 ma.S.L4 -0.7974 0.192 -4.163 0.000 -1.173 -0.422 sigma2 0.0015 0.000 5.331 0.000 0.001 0.002 =================================================================================== Ljung-Box (Q): 29.83 Jarque-Bera (JB): 94.63 Prob(Q): 0.88 Prob(JB): 0.00 Heteroskedasticity (H): 0.21 Skew: 1.21 Prob(H) (two-sided): 0.00 Kurtosis: 8.77 =================================================================================== Warnings: [1] Covariance matrix calculated using the outer product of gradients (complex-step). |

So yes, blockchain technology and cryptocurrencies have a lot of overlap with applications. But also remember, data science can be applied to any field where finance is a factor.

For more on blockchain and data science, see:

**Enjoy data science!**

**Follow this link**, if you are looking to **learn data science online****!**

You can **follow this link for our ****Big Data course**, which is a step further into advanced data analysis and processing**!**

Additionally, if you are having an interest in **learning Data Science, click here to start the ****Online Data Science Course**

Furthermore, if you want to read more about data science, read our **Data Science Blogs**