10-03-2022, 07:46 AM
Python Time Series Forecast on Bitcoin Data (Part II)
5/5 – (1 vote)
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FQxHPH6Bn2Tc%2Fhqdefault.jpg)
A Time Series is essentially a tabular data with the special feature of having a time index. The common forecast taks is ‘knowing the past (and sometimes the present), predict the future’. This task, taken as a principle, reveals itself in several ways: in how to interpret your problem, in feature engineering and in which forecast strategy to take.
This is the second article in our series. In the first article we discussed how to create features out of a time series using lags and trends. Today we follow the opposite direction by highlighting trends as something you want directly deducted from your model.
Reason is, Machine Learning models work in different ways. Some are good with subtractions, others are not.
For example, for any feature you include in a Linear Regression, the model will automatically detect whether to deduce it from the actual data or not. A Tree Regressor (and its variants) will not behave in the same way and usually will ignore a trend in the data.
Therefore, whenever using the latter type of models, one usually calls for a hybrid model, meaning, we use a Linear(ish) first model to detect global periodic patterns and then apply a second Machine Learning model to infer more sophisticated behavior.
We use the Bitcoin Sentiment Analysis data we captured in the last article as a proof of concept.
The hybrid model part of this article is heavily based on Kaggle’s Time Series Crash Course, however, we intend to automate the process and discuss more in-depth the class.
Trends, as something you don’t want to have
(Or that you want it deducted from your model)
An aerodynamic way to deal with trends and seasonality is using, respectively, and from . Let us start with the former.
aims at creating features to be used in a Regression model to determine trend and periodicity. It takes your and a few other parameters and returns a DataFrame full of features for your ML model.
A usual instance of the class will read like the one below. We use the column to illustrate.
![[Image: image-12.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-12.png)
We can use and as features and target to train a model. In this way, the will learn whatever characteristics from can be inferred (in our case) solely out of:
Check out the result:
Comparing predictions and actual values gives:
![[Image: image-13.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-13.png)
Even the quadratic term seems ignorable here. The class also helps us with future predictions since it carries a method that provides the appropriate future form of the chosen features.
Specifically, the method of takes the number of time intervals we want to predict as input and generates the needed features for you.
We use 60 days below as an example:
![[Image: image-14.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-14.png)
Let us repeat the process with to have a feeling of a higher-order trend.
As a rule of thumb, the order should be one plus the total number of (trending) hills + peaks in the graph, but not much more than that.
We choose 3 for and compare the output with the result (we do not write the code twice, though).
![[Image: image-15.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-15.png)
![[Image: image-16.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-16.png)
Although the order-three polynomial fits the data better, use discretion in deciding whether the sentiment count will decrease so drastically in the next 60 days or not. Usually, trust short-time predictions rather than long ones.
accepts other parameters, making it a very interesting tool. Find a description of the almost full list below.
Seasonality
As a hardened Mathematician, seasonality is my favorite part because it deals with Fourier analysis (and wave functions are just… cool!):
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F2awbKQ2DLRE%2Fhqdefault.jpg)
Do you remember your first ML course when you heard Linear Regression can fit arbitrary functions, not only lines? So, why not a wave function? We just did it for polynomials and didn’t even feel like it![[Image: 1f609.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f609.png)
In general, for any expression which is a function of a feature or of your , you can create a feature column whose ith row is the value of corresponding to the ith index.
Then linear regression finds the constant coefficient multiplying that best fits your data. Again, this procedure works in general, not only with Datetime indexes – the term above is an example of it.
For seasonality, we use a second‘s amazing class: . It is another ‘s class (i.e., with the and methods) and instantiates with two parameters, and .
As a, the class expects a string such as ‘D’, ‘W’, ‘M’ for day, week or month, respectively, or any of the quite comprehensive Pandas Datetime offset aliases.
The is the Fourier expansion order which should be understood as the number of waves you are expecting in your chosen frequency (count the number of ups and downs – one wave would be understood as one up and one down)
integrates swiftly with by including an instance of it in the list of .
Here is the full code for:
![[Image: image-17.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-17.png)
If we take inside , we get a crispier line:
![[Image: image-18.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-18.png)
Including before zooms the graph in:
![[Image: image-19.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-19.png)
Although I suggest using the parameter with care, it does find interesting patterns (with huge RMSE error, though).
For instance, look at this BTC percentage change zoomed chart:
![[Image: image-20.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-20.png)
Here period is set to 30 and. I also manually rescaled the predictions to be better visible in the graphic. Although the predictions are far away from truth, thinking as a trader, isn’t it impressive how many peaks and hills it gets right? At least for this zoomed month…
To maintain the workflow promise, I prepared a code that does everything so far in one shot:
Cycles and Hybrid Models
Let us move on to a complete Machine Learning prediction. We use and compare its performance among three instances:
The first part will be the bulkier since the other two follow from simple modifications in the resulting code.
Preparing the data
Before any analysis, we split the data in train and test sets. Since we are dealing with time series, this means we set the ‘present date’ as a point in the past and try to predict its respective ‘future’. Here we pick 22 days in the past.
We made this first split in order to not leak data while doing any analysis.
Next, we prepare target and feature sets. Recall our SentiCrypto’s data was set to be available everyday at 8AM. Imagine we are doing the prediction by 9AM.
In this case, anything until the present data (the ‘‘) can be used as features, and our target is ‘s first lead (which we define as a -1 lag). To choose other lags as features, we examine theirs statsmodel’s partial auto-correlation plot:
![[Image: image-21.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-21.png)
We use the first four for and the first seven + the 11th for (you can easily test different combinations with the code below.)
Now we finish choosing features, we go back to the full series for engineering. We apply to and the function we defined in the last article (which we transcribe here for convenience).
![[Image: image-22-1024x524.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-22-1024x524.png)
![[Image: image-23-1024x524.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-23-1024x524.png)
Now a train-test split with is convenient (Notice the parameter, that is key for time series):
![[Image: image-24-1024x536.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-24-1024x536.png)
(Observe that the final date is set correctly, in accordance with our analysis’ split.)
Applying the regressor:
![[Image: image-25-1024x819.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-25-1024x819.png)
![[Image: image-26-1024x868.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-26-1024x868.png)
You can reduce overfitness by reducing the number of estimators, but the R2 test score maintains negative.
We can replicate the process for (or whatever you want). Below is a function to automate it.
![[Image: image-27-1024x472.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-27-1024x472.png)
![[Image: image-28-1024x479.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-28-1024x479.png)
Predicting with Seasons
Since the features created by are only time-dependent, we can add them harmlessly to the feature DataFrame we automated get from our univariate predictions.
The predictions, though, are still univariate. We use the deseasonalize function to obtain the season features. The data preparation is as follows:
With a bit of copy and paste, we arrive at:
![[Image: image-29-1024x477.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-29-1024x477.png)
And we actually perform way worse!![[Image: 1f631.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f631.png)
Deseasonalizing
Nevertheless, the right-hand graphic illustrates the inability of grasping trends. Our last shot is a hybrid model.
Here we follow three steps:
Although Bitcoin-related data are hard to predict, there was a huge improvement on the (finally something positive!). We define the used function below.
![[Image: image-30-1024x475.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-30-1024x475.png)
Instead of going through every detail, we will also automate this code. In order to get the code running smoothly, we revisit the deseasonalize and the functions in order to remove the plotting part of them.
The final function only plots graphs and returns nothing. It intends to give you a baseline for a hybrid model score. Change the function at will to make it return whatever you need.
A note of warning: if you do not expect your data to follow time patterns, do focus on cycles! The hybrid model succeeds well for many tasks, but it actually decreases the R2 score of our previous Bitcoin prediction:
![[Image: image-31-1024x474.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-31-1024x474.png)
The former score was around 0.31.
Conclusion
This article aims at presenting functions for your time series workflow, specially for lags and deseasonalization. Use them with care, though: apply them to have baseline scores before delving into more sophisticated models.
In future articles we will bring forth multi-step predictions (predict more than one day ahead) and compare performance of different models, both univariate and multivariate.
5/5 – (1 vote)
A Time Series is essentially a tabular data with the special feature of having a time index. The common forecast taks is ‘knowing the past (and sometimes the present), predict the future’. This task, taken as a principle, reveals itself in several ways: in how to interpret your problem, in feature engineering and in which forecast strategy to take.
This is the second article in our series. In the first article we discussed how to create features out of a time series using lags and trends. Today we follow the opposite direction by highlighting trends as something you want directly deducted from your model.
Reason is, Machine Learning models work in different ways. Some are good with subtractions, others are not.
For example, for any feature you include in a Linear Regression, the model will automatically detect whether to deduce it from the actual data or not. A Tree Regressor (and its variants) will not behave in the same way and usually will ignore a trend in the data.
Therefore, whenever using the latter type of models, one usually calls for a hybrid model, meaning, we use a Linear(ish) first model to detect global periodic patterns and then apply a second Machine Learning model to infer more sophisticated behavior.
We use the Bitcoin Sentiment Analysis data we captured in the last article as a proof of concept.
The hybrid model part of this article is heavily based on Kaggle’s Time Series Crash Course, however, we intend to automate the process and discuss more in-depth the
Code:
DeterministicProcessTrends, as something you don’t want to have
(Or that you want it deducted from your model)
An aerodynamic way to deal with trends and seasonality is using, respectively,
Code:
DeterministicProcessCode:
CalendarFourierCode:
statsmodelCode:
DeterministicProcessCode:
DatetimeIndexA usual instance of the class will read like the one below. We use the
Code:
sentic_meanCode:
from statsmodels.tsa.deterministic import DeterministicProcess y = dataset['sentic_mean'].copy() dp = DeterministicProcess(
index=y.index, constant=True, order=2
) X = dp.in_sample() X![[Image: image-12.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-12.png)
We can use
Code:
XCode:
yCode:
LinearRegressionCode:
LinearRegressionCode:
y- the number of elapsed time intervals (column);Code:
trend
- the last number squared (); andCode:
trend_squared
- a bias term ().Code:
const
Check out the result:
Code:
from sklearn.linear_model import LinearRegression model = LinearRegression().fit(X,y) predictions = pd.DataFrame( model.predict(X), index=X.index, columns=['Deterministic Curve']
)Comparing predictions and actual values gives:
Code:
import matplotlib.pyplot as plt plt.figure()
ax = plt.subplot()
y.plot(ax=ax, legend=True)
predictions.plot(ax=ax)
plt.show()![[Image: image-13.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-13.png)
Even the quadratic term seems ignorable here. The
Code:
DeterministicProcessSpecifically, the
Code:
out_of_sampleCode:
dpWe use 60 days below as an example:
Code:
X_out = dp.out_of_sample(60) predictions_out = pd.DataFrame( model.predict(X_out), index=X_out.index, columns=['Future Predictions']
) plt.figure()
ax = plt.subplot()
y.plot(ax=ax, legend=True)
predictions.plot(ax=ax)
predictions_out.plot(ax=ax, color='red')
plt.show()![[Image: image-14.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-14.png)
Let us repeat the process with
Code:
sentic_count
As a rule of thumb, the order should be one plus the total number of (trending) hills + peaks in the graph, but not much more than that.We choose 3 for
Code:
sentic_countCode:
order=2Code:
y = dataset['sentic_count'].copy() from statsmodels.tsa.deterministic import DeterministicProcess, CalendarFourier dp = DeterministicProcess( index=y.index, constant=True, order=3
)
X = dp.in_sample() model = LinearRegression().fit(X,y) predictions = pd.DataFrame( model.predict(X), index=X.index, columns=['Deterministic Curve']
) X_out = dp.out_of_sample(60) predictions_out = pd.DataFrame( model.predict(X_out), index=X_out.index, columns=['Future Predictions']
) plt.figure()
ax = plt.subplot()
y.plot(ax=ax, legend=True)
predictions.plot(ax=ax)
predictions_out.plot(ax=ax, color='red')
plt.show()![[Image: image-15.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-15.png)
![[Image: image-16.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-16.png)
Although the order-three polynomial fits the data better, use discretion in deciding whether the sentiment count will decrease so drastically in the next 60 days or not. Usually, trust short-time predictions rather than long ones.
Code:
DeterministicProcessCode:
dp = DeterministicProcess( index, # the DatetimeIndex of your data period: int or None, # in case the data shows some periodicity, include the size of the periodic cycle here: 7 would mean 7 days in our case constant: bool, # includes a constant feature in the returned DataFrame, i.e., a feature with the same value for everyone. It returns the equivalent of a bias term in Linear Regression order: int, # order of the polynomial that you think better approximates your trend: the simplest the better seasonal: bool, # make it True if you think the data has some periodicity. If you make it True and do not specify the period, the dp will try to infer the period out of the index additional_terms: tuple of statsmodel's DeterministicTerms, # we come back to this next drop: bool # drops resulting features which are collinear to others. If you will use a linear model, make it True
)As a hardened Mathematician, seasonality is my favorite part because it deals with Fourier analysis (and wave functions are just… cool!):
Do you remember your first ML course when you heard Linear Regression can fit arbitrary functions, not only lines? So, why not a wave function? We just did it for polynomials and didn’t even feel like it
![[Image: 1f609.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f609.png)
In general, for any expression
Code:
fCode:
DatetimeIndexCode:
fThen linear regression finds the constant coefficient multiplying
Code:
fCode:
trend_squaredFor seasonality, we use a second
Code:
statsmodelCode:
CalendarFourierCode:
statsmodelCode:
DeterministicTermCode:
in_sampleCode:
out_of_sampleCode:
'frequency'Code:
'order'As a
Code:
'frequency'The
Code:
'order'Code:
CalendarFourierCode:
DeterministicProcessCode:
additional_termsHere is the full code for
Code:
sentic_meanCode:
from statsmodels.tsa.deterministic import DeterministicProcess, CalendarFourier y = dataset['sentic_mean'].copy() fourier = CalendarFourier(freq='A',order=2) dp = DeterministicProcess( index=y.index, constant=True, order=2, seasonal=False, additional_terms=[fourier], drop=True
)
X = dp.in_sample() from sklearn.linear_model import LinearRegression model = LinearRegression().fit(X,y) predictions = pd.DataFrame( model.predict(X), index=X.index, columns=['Prediction']
) X_out = dp.out_of_sample(60) predictions_out = pd.DataFrame( model.predict(X_out), index=X_out.index, columns=['Prediction']
) plt.figure()
ax = plt.subplot()
y.plot(ax=ax, legend=True)
predictions.plot(ax=ax)
predictions_out.plot(ax=ax, color='red')
plt.show()![[Image: image-17.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-17.png)
If we take
Code:
seasonal=TrueCode:
DeterministicProcess![[Image: image-18.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-18.png)
Including
Code:
ax.set_xlim(('2022-08-01', '2022-10-01'))Code:
plt.show()![[Image: image-19.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-19.png)
Although I suggest using the
Code:
seasonal=TrueFor instance, look at this BTC percentage change zoomed chart:
![[Image: image-20.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-20.png)
Here period is set to 30 and
Code:
seasonal=TrueTo maintain the workflow promise, I prepared a code that does everything so far in one shot:
Code:
def deseasonalize(df: pd.Series, season_freq='A', fourier_order=0, constant=True, dp_order=1, dp_drop=True, model=LinearRegression(), fourier=None, dp=None, **DeterministicProcesskwargs)->(pd.Series, plt.Axes, pd.DataFrame): """ Returns a deseasonalized and detrended df, a seasonal plot, and the fitted DeterministicProcess instance. """ if fourier is None: fourier = CalendarFourier(freq=season_freq, order=fourier_order) if dp is None: dp = DeterministicProcess( index=df.index, constant=True, order=dp_order, additional_terms=[fourier], drop=dp_drop, **DeterministicProcesskwargs ) X = dp.in_sample() model = LinearRegression().fit(X, df) y_pred = pd.Series( model.predict(X), index=X.index, name=df.name+'_pred' ) ax = plt.subplot() y.plot(ax=ax, legend=True) predictions.plot(ax=ax) y_pred.columns = df.name y_deseason = df - y_pred y_deseason.name = df.name +'_deseasoned' return y_deseason, ax, dp The sentic_mean analyses get reduced to: y_deseason, ax, dp= deseasonalize(y, season_freq='A', fourier_order=2, constant=True, dp_order=2, dp_drop=True, model=LinearRegression() )Let us move on to a complete Machine Learning prediction. We use
Code:
XGBRegressor- Predict directly using lags;Code:
sentic_mean
- Same prediction adding the seasonal/trending with a ;Code:
DeterministicProcess
- A hybrid model, using to infer and remove seasons/trends, and then apply aCode:
LinearRegression.Code:XGBRegressor
The first part will be the bulkier since the other two follow from simple modifications in the resulting code.
Preparing the data
Before any analysis, we split the data in train and test sets. Since we are dealing with time series, this means we set the ‘present date’ as a point in the past and try to predict its respective ‘future’. Here we pick 22 days in the past.
Code:
s = dataset['sentic_mean'] s_train = s[:'2022-09-01']We made this first split in order to not leak data while doing any analysis.
Next, we prepare target and feature sets. Recall our SentiCrypto’s data was set to be available everyday at 8AM. Imagine we are doing the prediction by 9AM.
In this case, anything until the present data (the ‘
Code:
lag_0Code:
s_trainCode:
from statsmodels.graphics.tsaplots import plot_pacf plot_pacf(s_train, lags=20)![[Image: image-21.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-21.png)
We use the first four for
Code:
sentic_meanCode:
sentic_countNow we finish choosing features, we go back to the full series for engineering. We apply to
Code:
s_maenCode:
s_countCode:
make_lagsCode:
def make_lags(df, n_lags=1, lead_time=1): """ Compute lags of a pandas.Series from lead_time to lead_time + n_lags. Alternatively, a list can be passed as n_lags. Returns a pd.DataFrame whose ith column is either the i+lead_time lag or the ith element of n_lags. """ if isinstance(n_lags,int): lag_list = list(range(lead_time, n_lags+lead_time)) else: lag_list = n_lags lags ={ f'{df.name}_lag_{i}': df.shift(i) for i in lag_list } return pd.concat(lags,axis=1) X = make_lags(s, [0,1,2,3,4]) y = make_lags(s, [-1]) display(X)
y![[Image: image-22-1024x524.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-22-1024x524.png)
![[Image: image-23-1024x524.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-23-1024x524.png)
Now a train-test split with
Code:
sklearnCode:
shuffle=FalseCode:
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=22, shuffle=False) X_train![[Image: image-24-1024x536.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-24-1024x536.png)
(Observe that the final date is set correctly, in accordance with our analysis’ split.)
Applying the regressor:
Code:
xgb = XGBRegressor(n_estimators=50) xgb.fit(X_train,y_train) predictions_train = pd.DataFrame( xgb.predict(X_train), index=X_train.index, columns=['Prediction']
) predictions_test = pd.DataFrame( xgb.predict(X_test), index=X_test.index, columns=['Prediction']
) print(f'R2 train score: {r2_score(y_train[:-1],predictions_train[:-1])}') plt.figure()
ax = plt.subplot()
y_train.plot(ax=ax, legend=True)
predictions_train.plot(ax=ax)
plt.show() plt.figure()
ax = plt.subplot()
y_test.plot(ax=ax, legend=True)
predictions_test.plot(ax=ax)
plt.show() print(f'R2 test score: {r2_score(y_test[:-1],predictions_test[:-1])}')![[Image: image-25-1024x819.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-25-1024x819.png)
![[Image: image-26-1024x868.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-26-1024x868.png)
You can reduce overfitness by reducing the number of estimators, but the R2 test score maintains negative.
We can replicate the process for
Code:
sentic_countCode:
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from statsmodels.tsa.stattools import pacf def apply_univariate_prediction(series, test_size, to_predict=1, nlags=20, minimal_pacf=0.1, model=XGBRegressor(n_estimators=50)): ''' Starting from series, breaks it in train and test subsets; chooses which lags to use based on pacf > minimal_pacf; and applies the given sklearn-type model. Returns the resulting features and targets and the trained model. It plots the graph of the training and prediction, together with their r2_score. ''' s = series.iloc[:-test_size] if isinstance(to_predict,int): to_predict = [to_predict] from statsmodels.tsa.stattools import pacf s_pacf = pd.Series(pacf(s, nlags=nlags)) column_list = s_pacf[s_pacf>minimal_pacf].index X = make_lags(series, n_lags=column_list).dropna() y = make_lags(series,n_lags=[-x for x in to_predict]).loc[X.index] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, shuffle=False) model.fit(X_train,y_train) predictions_train = pd.DataFrame( model.predict(X_train), index=X_train.index, columns=['Train Predictions'] ) predictions_test = pd.DataFrame( model.predict(X_test), index=X_test.index, columns=['Test Predictions'] ) fig, (ax1,ax2) = plt.subplots(1,2, figsize=(14,5), sharey=True) y_train.plot(ax=ax1, legend=True) predictions_train.plot(ax=ax1) ax1.set_title('Train Predictions') y_test.plot(ax=ax2, legend=True) predictions_test.plot(ax=ax2) ax2.set_title('Test Predictions') plt.show() print(f'R2 train score: {r2_score(y_train[:-1],predictions_train[:-1])}') print(f'R2 test score: {r2_score(y_test[:-1],predictions_test[:-1])}') return X, y, model apply_univariate_prediction(dataset['sentic_count'],22)![[Image: image-27-1024x472.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-27-1024x472.png)
Code:
apply_univariate_prediction(dataset['BTC-USD'], 22)![[Image: image-28-1024x479.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-28-1024x479.png)
Predicting with Seasons
Since the features created by
Code:
DeterministicProcessThe predictions, though, are still univariate. We use the deseasonalize function to obtain the season features. The data preparation is as follows:
Code:
s = dataset['sentic_mean'] X, y, _ = apply_univariate_prediction(s,22); s_deseason, _, dp = deseasonalize(s, season_freq='A', fourier_order=2, constant=True, dp_order=2, dp_drop=True, model=LinearRegression() );
X_f = dp.in_sample().shift(-1) X = pd.concat([X,X_f], axis=1, join='inner').dropna()With a bit of copy and paste, we arrive at:
![[Image: image-29-1024x477.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-29-1024x477.png)
And we actually perform way worse!
![[Image: 1f631.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f631.png)
Deseasonalizing
Nevertheless, the right-hand graphic illustrates the inability of grasping trends. Our last shot is a hybrid model.
Here we follow three steps:
- We use the to capture the seasons and trends, rendering the seriesCode:
LinearRegression. Then we acquire a deseasonalized targetCode:y_s;Code:y_ds = y-y_s
- Train an onCode:
XGBRegressorand the lagged features, resulting in deseasonalized predictionsCode:y_ds;Code:y_pred
- Finally, we incorporate back toCode:
y_sto compare the final result.Code:y_pred
Although Bitcoin-related data are hard to predict, there was a huge improvement on the
Code:
r2_scoreCode:
get_hybrid_univariate_prediction(dataset['sentic_mean'], 22, season_freq='A', fourier_order=2, constant=True, dp_order=2, dp_drop=True, model1=LinearRegression(), fourier=None, is_seasonal=True, season_period=7, dp=None, to_predict=1, nlags=20, minimal_pacf=0.1, model2=XGBRegressor(n_estimators=50) )![[Image: image-30-1024x475.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-30-1024x475.png)
Instead of going through every detail, we will also automate this code. In order to get the code running smoothly, we revisit the deseasonalize and the
Code:
apply_univariate_predictionThe final function only plots graphs and returns nothing. It intends to give you a baseline for a hybrid model score. Change the function at will to make it return whatever you need.
Code:
def get_season(series: pd.Series, test_size, season_freq='A', fourier_order=0, constant=True, dp_order=1, dp_drop=True, model1=LinearRegression(), fourier=None, is_seasonal=False, season_period=None, dp=None): """ Decompose series in a deseasonalized and a seasonal part. The parameters are relative to the fourier and DeterministicProcess used. Returns y_ds and y_s. """ se = series.iloc[:-test_size] if fourier is None: fourier = CalendarFourier(freq=season_freq, order=fourier_order) if dp is None: dp = DeterministicProcess( index=se.index, constant=True, order=dp_order, additional_terms=[fourier], drop=dp_drop, seasonal=is_seasonal, period=season_period ) X_in = dp.in_sample() X_out = dp.out_of_sample(test_size) model1 = model1.fit(X_in, se) X = pd.concat([X_in,X_out],axis=0) y_s = pd.Series( model1.predict(X), index=X.index, name=series.name+'_pred' ) y_s.name = series.name y_ds = series - y_s y_ds.name = series.name +'_deseasoned' return y_ds, y_s def prepare_data(series, test_size, to_predict=1, nlags=20, minimal_pacf=0.1): ''' Creates a feature dataframe by making lags and a target series by a negative to_predict-shift. Returns X, y. ''' s = series.iloc[:-test_size] if isinstance(to_predict,int): to_predict = [to_predict] from statsmodels.tsa.stattools import pacf s_pacf = pd.Series(pacf(s,nlags=nlags)) column_list = s_pacf[s_pacf>minimal_pacf].index X = make_lags(series, n_lags=column_list).dropna() y = make_lags(series,n_lags=[-x for x in to_predict]).loc[X.index].squeeze() return X, y def get_hybrid_univariate_prediction(series: pd.Series, test_size, season_freq='A', fourier_order=0, constant=True, dp_order=1, dp_drop=True, model1=LinearRegression(), fourier=None, is_seasonal=False, season_period=None, dp=None, to_predict=1, nlags=20, minimal_pacf=0.1, model2=XGBRegressor(n_estimators=50) ): """ Apply the hybrid model method by deseasonalizing/detrending a time series with model1 and investigating the resulting series with model2. It plots the respective graphs and computes r2_scores. """ y_ds, y_s = get_season(series, test_size, season_freq=season_freq, fourier_order=fourier_order, constant=constant, dp_order=dp_order, dp_drop=dp_drop, model1=model1, fourier=fourier, dp=dp, is_seasonal=is_seasonal, season_period=season_period) X, y_ds = prepare_data(y_ds,test_size=test_size) X_train, X_test, y_train, y_test = train_test_split(X, y_ds, test_size=test_size, shuffle=False) y = y_s.squeeze() + y_ds.squeeze() model2 = model2.fit(X_train,y_train) predictions_train = pd.Series( model2.predict(X_train), index=X_train.index, name='Prediction' )+y_s[X_train.index] predictions_test = pd.Series( model2.predict(X_test), index=X_test.index, name='Prediction' )+y_s[X_test.index] fig, (ax1,ax2) = plt.subplots(1,2, figsize=(14,5), sharey=True) y_train_ps = y.loc[y_train.index] y_test_ps = y.loc[y_test.index] y_train_ps.plot(ax=ax1, legend=True) predictions_train.plot(ax=ax1) ax1.set_title('Train Predictions') y_test_ps.plot(ax=ax2, legend=True) predictions_test.plot(ax=ax2) ax2.set_title('Test Predictions') plt.show() print(f'R2 train score: {r2_score(y_train_ps[:-to_predict],predictions_train[:-to_predict])}') print(f'R2 test score: {r2_score(y_test_ps[:-to_predict],predictions_test[:-to_predict])}')A note of warning: if you do not expect your data to follow time patterns, do focus on cycles! The hybrid model succeeds well for many tasks, but it actually decreases the R2 score of our previous Bitcoin prediction:
Code:
get_hybrid_univariate_prediction(dataset['BTC-USD'], 22, season_freq='A', fourier_order=4, constant=True, dp_order=5, dp_drop=True, model1=LinearRegression(), fourier=None, is_seasonal=True, season_period=30, dp=None, to_predict=1, nlags=20, minimal_pacf=0.05, model2=XGBRegressor(n_estimators=20) )![[Image: image-31-1024x474.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-31-1024x474.png)
The former score was around 0.31.
Conclusion
This article aims at presenting functions for your time series workflow, specially for lags and deseasonalization. Use them with care, though: apply them to have baseline scores before delving into more sophisticated models.
In future articles we will bring forth multi-step predictions (predict more than one day ahead) and compare performance of different models, both univariate and multivariate.

