# Bitcoin Time Series (long-time) forecasting using Machine Learning

# Here’s how to use basic Machine Learning to forecast Bitcoin Stock Price

Piero Paialunga

Note! I’m **not **a Bitcoin expert. This use case is just meant to show how to apply Machine Learning in a simple way and try to solve a real world problem.

When I started Machine Learning, I got fascinated by the idea of being able to interpret data and extract information out of them even if** I had no prior domain knowledge.**

In this case, as I’m a Physicist and a PhD Student in Aerospace Engineering, I have no idea or opinion about Bitcoin. Nonetheless, this topic is extremely discussed and it is interesting to understand how and when (and if) the Bitcoins will stabilize themselves in the near future. Thus, I want to stress it before we go:

The aim of this blogpost is to show how to predict the Bitcoin Stock price using basic Machine Learning.

So I just downloaded this dataset, and I used Python to analize it.

This is what it looks like:

And this is the plot of the “**Open**” column, that will be our **target**.

In order to get things clear, **this is what it looks like in terms of time**:

As it is possible to see, the data become more and more unstable as the time goes by. In fact, it is the differenced dataset:

In order to predict what is going to be next, the idea is to take out some part of the data, known as **Test Set.** The test set will be used to test the performance of the algorithm.

The **Training Set** will be used to make the machine **learn** its task. After the usage of the training set we will end up with multiple trained and ready-to-use models. The **validation set **will be used to understand the best model possible between the trained ones.

The Training set is approximatively **80%**, while the validation and the test set are **10%** each.

It looks like this:

Ok, so let’s start using Machine Learning. The idea is extremely simple and it is known as **Polynomial Regression.**

The regression process aims to find a real value (or a float number, if you will) out of an input value or vector.

Let’s make it simple. Imagine you have a bunch of points in a 2 dimensional space that occupies a narrow area of that space. The **linear regression task** aims to find the line that best fits the points in your 2d space. Once you have the line, given a certain new point you are able to predict the target value by referring to the fitted line.

The exact same idea can be applied in our case. If we don’t want to be limited by the linear case, we can transform the point in a K dimensional space (3,4,5,…), fit the best parameters of our K dimensional polynomial, and then go back to the original space. This is what’s called **Polynomial Regression.**

**This is the situation when you use all the degrees from 2 to 9.**

The thing about polynomial regression is that you need to be careful about K. If K is too low you may get low performance as your model is not powerful enough. At the same way your model can be “too powerful” and thus still not able to get you good performances (a fun explanation of this phenomenon known as overfitting here).

**This being said, the plain is to use the validation set to get the best K value and then use the K degree polynomial to get our prediction.**

As it is possible to see, the Degree = 3 polynomial is the one that best fit the validation data.

Finally, this is the prediction that has been made compared with the test set:

It is important to notice that the polynomial will keep rising its values while the test set has some up and downs that are difficult to interpret. For this reason, if we want to be more precise about what is happening in a shorter time span, we need to** play a different game**, and analyze the fluctuations of the points. But, as I said, it is another challenge.

Again, I’m not a Bitcoin expert. I just wanted to point out how easy it can be to get some insightful information and prediction out of a certain phenomenon with Data Analysis and Machine Learning, with 0 previous domain knowledge.

Upvote

Piero Paialunga

Related Articles