Prophet Baseline Notebook

This notebook contains the code used to predict the price of bitcoin just using FB prophet.

You can think of this as a sort of baseline model!


In [1]:
from fbprophet import Prophet
from sklearn.metrics import r2_score
%run helper_functions.py
%autosave 120
%matplotlib inline
%run prophet_helper.py
%run prophet_baseline_btc.py
plt.style.use('fivethirtyeight')
plt.rcParams["figure.figsize"] = (15,10)
plt.rcParams["xtick.labelsize"] = 16
plt.rcParams["ytick.labelsize"] = 16
plt.rcParams["axes.labelsize"] = 20
plt.rcParams['legend.fontsize'] = 20
plt.style.use('fivethirtyeight')
pd.set_option('display.max_colwidth', -1)
import numpy as np
import math
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score


Autosaving every 120 seconds

Forecasting BTC Price with Fb Prophet


In [2]:
df = unpickle_object("blockchain_info_df.pkl")
df.head()


Out[2]:
gold_price eth_price pos_sent neg_sent tot_num_trans unique_addr hash_rate mempool_trans USD_trans_val mkt_price
date
2016-10-26 1273.9 11.455 0.143706 0.066297 225924.0 431781.0 1.752433e+06 69811.000000 1.768021e+08 672.221413
2016-10-27 1269.3 11.515 0.127429 0.064310 326388.0 457806.0 2.042404e+06 171358.250000 2.832341e+08 682.223962
2016-10-28 1265.9 11.280 0.128794 0.056393 250876.0 434269.0 1.916330e+06 131888.333333 1.946002e+08 687.688337
2016-10-29 1265.9 10.770 0.139114 0.063177 229689.0 416457.0 1.878507e+06 17765.750000 1.674801e+08 714.895450
2016-10-30 1265.9 10.805 0.131922 0.064369 209337.0 360327.0 2.130656e+06 20822.250000 1.797445e+08 698.003400

In [3]:
df_btc = pd.DataFrame(df['mkt_price'])

In [4]:
true, pred = prophet_baseline_BTC(df_btc, 30, "mkt_price")


Running model from 2016-10-26 to 2017-01-23 and predicting on 2017-01-24

True value of mkt_price on 2017-01-24 is 890.320225
The predicted value for mkt_price on 2017-01-24 is 894.3099830812673

Running model from 2016-10-27 to 2017-01-24 and predicting on 2017-01-25

True value of mkt_price on 2017-01-25 is 893.045625
The predicted value for mkt_price on 2017-01-25 is 892.3820039963203

Running model from 2016-10-28 to 2017-01-25 and predicting on 2017-01-26

True value of mkt_price on 2017-01-26 is 915.95625
The predicted value for mkt_price on 2017-01-26 is 886.5515155298705

Running model from 2016-10-29 to 2017-01-26 and predicting on 2017-01-27

True value of mkt_price on 2017-01-27 is 919.27975
The predicted value for mkt_price on 2017-01-27 is 884.3053284251986

Running model from 2016-10-30 to 2017-01-27 and predicting on 2017-01-28

True value of mkt_price on 2017-01-28 is 920.31225
The predicted value for mkt_price on 2017-01-28 is 897.3012525460083

Running model from 2016-10-31 to 2017-01-28 and predicting on 2017-01-29

True value of mkt_price on 2017-01-29 is 915.933
The predicted value for mkt_price on 2017-01-29 is 898.7287790890276

Running model from 2016-11-01 to 2017-01-29 and predicting on 2017-01-30

True value of mkt_price on 2017-01-30 is 921.179325
The predicted value for mkt_price on 2017-01-30 is 892.0607916109784

Running model from 2016-11-02 to 2017-01-30 and predicting on 2017-01-31

True value of mkt_price on 2017-01-31 is 964.706075
The predicted value for mkt_price on 2017-01-31 is 899.9606620352394

Running model from 2016-11-03 to 2017-01-31 and predicting on 2017-02-01

True value of mkt_price on 2017-02-01 is 979.703875
The predicted value for mkt_price on 2017-02-01 is 919.1005879987616

Running model from 2016-11-04 to 2017-02-01 and predicting on 2017-02-02

True value of mkt_price on 2017-02-02 is 1007.6137125
The predicted value for mkt_price on 2017-02-02 is 923.680568183997

Running model from 2016-11-05 to 2017-02-02 and predicting on 2017-02-03

True value of mkt_price on 2017-02-03 is 1013.0269999999999
The predicted value for mkt_price on 2017-02-03 is 928.5440462441122

Running model from 2016-11-06 to 2017-02-03 and predicting on 2017-02-04

True value of mkt_price on 2017-02-04 is 1030.9994125
The predicted value for mkt_price on 2017-02-04 is 955.0082028454975

Running model from 2016-11-07 to 2017-02-04 and predicting on 2017-02-05

True value of mkt_price on 2017-02-05 is 1014.837725
The predicted value for mkt_price on 2017-02-05 is 959.6399715198762

Running model from 2016-11-08 to 2017-02-05 and predicting on 2017-02-06

True value of mkt_price on 2017-02-06 is 1024.01375
The predicted value for mkt_price on 2017-02-06 is 968.6960096084451

Running model from 2016-11-09 to 2017-02-06 and predicting on 2017-02-07

True value of mkt_price on 2017-02-07 is 1050.11
The predicted value for mkt_price on 2017-02-07 is 984.0300635729837

Running model from 2016-11-10 to 2017-02-07 and predicting on 2017-02-08

True value of mkt_price on 2017-02-08 is 1052.3766125
The predicted value for mkt_price on 2017-02-08 is 993.9727286788716

Running model from 2016-11-11 to 2017-02-08 and predicting on 2017-02-09

True value of mkt_price on 2017-02-09 is 976.103
The predicted value for mkt_price on 2017-02-09 is 994.3404944985732

Running model from 2016-11-12 to 2017-02-09 and predicting on 2017-02-10

True value of mkt_price on 2017-02-10 is 999.1035
The predicted value for mkt_price on 2017-02-10 is 994.2564104155307

Running model from 2016-11-13 to 2017-02-10 and predicting on 2017-02-11

True value of mkt_price on 2017-02-11 is 1008.8466625
The predicted value for mkt_price on 2017-02-11 is 1000.5905917250267

Running model from 2016-11-14 to 2017-02-11 and predicting on 2017-02-12

True value of mkt_price on 2017-02-12 is 1000.604625
The predicted value for mkt_price on 2017-02-12 is 1000.1996693900584

Running model from 2016-11-15 to 2017-02-12 and predicting on 2017-02-13

True value of mkt_price on 2017-02-13 is 999.877375
The predicted value for mkt_price on 2017-02-13 is 1001.9539569384817

Running model from 2016-11-16 to 2017-02-13 and predicting on 2017-02-14

True value of mkt_price on 2017-02-14 is 1011.78025
The predicted value for mkt_price on 2017-02-14 is 1016.2382631903505

Running model from 2016-11-17 to 2017-02-14 and predicting on 2017-02-15

True value of mkt_price on 2017-02-15 is 1012.3259875
The predicted value for mkt_price on 2017-02-15 is 1022.5311499527854

Running model from 2016-11-18 to 2017-02-15 and predicting on 2017-02-16

True value of mkt_price on 2017-02-16 is 1035.208125
The predicted value for mkt_price on 2017-02-16 is 1014.4486936746613

Running model from 2016-11-19 to 2017-02-16 and predicting on 2017-02-17

True value of mkt_price on 2017-02-17 is 1055.53685
The predicted value for mkt_price on 2017-02-17 is 1018.6402661685622

Running model from 2016-11-20 to 2017-02-17 and predicting on 2017-02-18

True value of mkt_price on 2017-02-18 is 1056.6371375
The predicted value for mkt_price on 2017-02-18 is 1025.110929916259

Running model from 2016-11-21 to 2017-02-18 and predicting on 2017-02-19

True value of mkt_price on 2017-02-19 is 1052.7792857142856
The predicted value for mkt_price on 2017-02-19 is 1024.8305315637854

Running model from 2016-11-22 to 2017-02-19 and predicting on 2017-02-20

True value of mkt_price on 2017-02-20 is 1084.7550125
The predicted value for mkt_price on 2017-02-20 is 1029.033188702353

Running model from 2016-11-23 to 2017-02-20 and predicting on 2017-02-21

True value of mkt_price on 2017-02-21 is 1123.7884285714283
The predicted value for mkt_price on 2017-02-21 is 1045.6349895459348

Running model from 2016-11-24 to 2017-02-21 and predicting on 2017-02-22

True value of mkt_price on 2017-02-22 is 1123.2231875
The predicted value for mkt_price on 2017-02-22 is 1055.3536299666964

This TS model for BTC has an MSE score of 2114.093334320576

In [5]:
r2_score(true, pred) #we see that our baseline model just predicts 44% of the variation when predicting price


Out[5]:
0.44573985972451446

In [6]:
plt.plot(pred)
plt.plot(true)
plt.legend(["Prediction", 'Actual'], loc='upper left')
plt.xlabel("Prediction #")
plt.ylabel("Price")
plt.title("TS FB Prophet Baseline - Price Prediction");


Let's predict percentage change!


In [7]:
df_btc_pct = df_btc.pct_change()
df_btc_pct.rename(columns={"mkt_price": "percent_change"}, inplace=True)
df_btc_pct = df_btc_pct.iloc[1:, :]
print(df_btc_pct.shape)
df_btc_pct.head()


(119, 1)
Out[7]:
percent_change
date
2016-10-27 0.014880
2016-10-28 0.008010
2016-10-29 0.039563
2016-10-30 -0.023629
2016-10-31 0.005728

In [8]:
true_pct, pred_pct = prophet_baseline_BTC(df_btc_pct, 30, "percent_change")


Running model from 2016-10-27 to 2017-01-23 and predicting on 2017-01-24

True value of percent_change on 2017-01-24 is -0.03443693330937814
The predicted value for percent_change on 2017-01-24 is 0.014545719346050198

Running model from 2016-10-28 to 2017-01-24 and predicting on 2017-01-25

True value of percent_change on 2017-01-25 is 0.0030611457804408104
The predicted value for percent_change on 2017-01-25 is 0.005000287356037803

Running model from 2016-10-29 to 2017-01-25 and predicting on 2017-01-26

True value of percent_change on 2017-01-26 is 0.025654484338356154
The predicted value for percent_change on 2017-01-26 is -0.009211191450551935

Running model from 2016-10-30 to 2017-01-26 and predicting on 2017-01-27

True value of percent_change on 2017-01-27 is 0.0036284484111550608
The predicted value for percent_change on 2017-01-27 is 0.010857611700461803

Running model from 2016-10-31 to 2017-01-27 and predicting on 2017-01-28

True value of percent_change on 2017-01-28 is 0.0011231619101801371
The predicted value for percent_change on 2017-01-28 is 0.0005725586406039497

Running model from 2016-11-01 to 2017-01-28 and predicting on 2017-01-29

True value of percent_change on 2017-01-29 is -0.004758439323175323
The predicted value for percent_change on 2017-01-29 is -0.005043020192131991

Running model from 2016-11-02 to 2017-01-29 and predicting on 2017-01-30

True value of percent_change on 2017-01-30 is 0.005727847997615498
The predicted value for percent_change on 2017-01-30 is 0.003128126000652143

Running model from 2016-11-03 to 2017-01-30 and predicting on 2017-01-31

True value of percent_change on 2017-01-31 is 0.04725111475987598
The predicted value for percent_change on 2017-01-31 is 0.011042799955887474

Running model from 2016-11-04 to 2017-01-31 and predicting on 2017-02-01

True value of percent_change on 2017-02-01 is 0.01554649689544041
The predicted value for percent_change on 2017-02-01 is 0.011297117125958094

Running model from 2016-11-05 to 2017-02-01 and predicting on 2017-02-02

True value of percent_change on 2017-02-02 is 0.028488034203192347
The predicted value for percent_change on 2017-02-02 is -0.0004014676291122666

Running model from 2016-11-06 to 2017-02-02 and predicting on 2017-02-03

True value of percent_change on 2017-02-03 is 0.005372383714954632
The predicted value for percent_change on 2017-02-03 is 0.012648686316410317

Running model from 2016-11-07 to 2017-02-03 and predicting on 2017-02-04

True value of percent_change on 2017-02-04 is 0.017741296628816627
The predicted value for percent_change on 2017-02-04 is 0.003664228677648267

Running model from 2016-11-08 to 2017-02-04 and predicting on 2017-02-05

True value of percent_change on 2017-02-05 is -0.015675748505821874
The predicted value for percent_change on 2017-02-05 is 0.003447636701915022

Running model from 2016-11-09 to 2017-02-05 and predicting on 2017-02-06

True value of percent_change on 2017-02-06 is 0.00904186430397047
The predicted value for percent_change on 2017-02-06 is 0.004642502494881326

Running model from 2016-11-10 to 2017-02-06 and predicting on 2017-02-07

True value of percent_change on 2017-02-07 is 0.025484276944523376
The predicted value for percent_change on 2017-02-07 is 0.015511550037996733

Running model from 2016-11-11 to 2017-02-07 and predicting on 2017-02-08

True value of percent_change on 2017-02-08 is 0.0021584524478388545
The predicted value for percent_change on 2017-02-08 is 0.015725400876580777

Running model from 2016-11-12 to 2017-02-08 and predicting on 2017-02-09

True value of percent_change on 2017-02-09 is -0.07247748723606307
The predicted value for percent_change on 2017-02-09 is 0.00010653974054402639

Running model from 2016-11-13 to 2017-02-09 and predicting on 2017-02-10

True value of percent_change on 2017-02-10 is 0.023563599333267238
The predicted value for percent_change on 2017-02-10 is 0.004197916142231067

Running model from 2016-11-14 to 2017-02-10 and predicting on 2017-02-11

True value of percent_change on 2017-02-11 is 0.009751905082906687
The predicted value for percent_change on 2017-02-11 is 0.012194720919522459

Running model from 2016-11-15 to 2017-02-11 and predicting on 2017-02-12

True value of percent_change on 2017-02-12 is -0.008169762369610756
The predicted value for percent_change on 2017-02-12 is -0.001960465235072822

Running model from 2016-11-16 to 2017-02-12 and predicting on 2017-02-13

True value of percent_change on 2017-02-13 is -0.0007268105521699431
The predicted value for percent_change on 2017-02-13 is 0.005672566358878934

Running model from 2016-11-17 to 2017-02-13 and predicting on 2017-02-14

True value of percent_change on 2017-02-14 is 0.011904334769051061
The predicted value for percent_change on 2017-02-14 is 0.01552408288561077

Running model from 2016-11-18 to 2017-02-14 and predicting on 2017-02-15

True value of percent_change on 2017-02-15 is 0.0005393834283680121
The predicted value for percent_change on 2017-02-15 is 0.007443578842252822

Running model from 2016-11-19 to 2017-02-15 and predicting on 2017-02-16

True value of percent_change on 2017-02-16 is 0.022603526712288602
The predicted value for percent_change on 2017-02-16 is -0.010475381007837105

Running model from 2016-11-20 to 2017-02-16 and predicting on 2017-02-17

True value of percent_change on 2017-02-17 is 0.01963733138203483
The predicted value for percent_change on 2017-02-17 is 0.011046045378525436

Running model from 2016-11-21 to 2017-02-17 and predicting on 2017-02-18

True value of percent_change on 2017-02-18 is 0.0010423961039354168
The predicted value for percent_change on 2017-02-18 is 0.010351902729515961

Running model from 2016-11-22 to 2017-02-18 and predicting on 2017-02-19

True value of percent_change on 2017-02-19 is -0.003651065866227432
The predicted value for percent_change on 2017-02-19 is 0.001150116789129487

Running model from 2016-11-23 to 2017-02-19 and predicting on 2017-02-20

True value of percent_change on 2017-02-20 is 0.030372678508790774
The predicted value for percent_change on 2017-02-20 is 0.006644676830157459

Running model from 2016-11-24 to 2017-02-20 and predicting on 2017-02-21

True value of percent_change on 2017-02-21 is 0.03598362360314811
The predicted value for percent_change on 2017-02-21 is 0.026063735561249822

Running model from 2016-11-25 to 2017-02-21 and predicting on 2017-02-22

True value of percent_change on 2017-02-22 is -0.0005029781914971387
The predicted value for percent_change on 2017-02-22 is 0.012803119309673264

This TS model for BTC has an MSE score of 0.000488913299898903

In [9]:
r2_score(true_pct, pred_pct)


Out[9]:
-0.020629111221503127

MSE IS 0.000488913299898903


In [10]:
plt.plot(pred_pct)
plt.plot(true_pct)
plt.legend(["Prediction", 'Actual'], loc='upper left')
plt.xlabel("Prediction #")
plt.ylabel("Price")
plt.title("TS FB Prophet Baseline - Price Prediction");


we do terribly at predicting percent change! However, we know that percent change should be applied to the price of the previous day. Let's do that!

Note that the MSE is very close to 0 - we have quite an accurate Model!


In [11]:
prices_to_be_multiplied = df.loc[pd.date_range(start="2017-01-23", end="2017-02-21"), "mkt_price"]

In [12]:
forecast_price_lst = []
for index, price in enumerate(prices_to_be_multiplied):
    predicted_percent_change = 1+float(pred_pct[index])
    forecasted_price = (predicted_percent_change)*price
    forecast_price_lst.append(forecasted_price)

In [13]:
ground_truth_prices = df.loc[pd.date_range(start="2017-01-24", end="2017-02-22"), "mkt_price"]
ground_truth_prices = list(ground_truth_prices)

In [14]:
r2_score(ground_truth_prices, forecast_price_lst) # such an incredible result! This is what we have to beat with my nested TS model


Out[14]:
0.87247841566280471

In [15]:
plt.plot(forecast_price_lst)
plt.plot(ground_truth_prices)
plt.legend(["Prediction", 'Actual'], loc='upper left')
plt.xlabel("Prediction #")
plt.ylabel("Price")
plt.title("TS FB Prophet Baseline - Price Prediction");



In [ ]: