2
$\begingroup$

I have created a random forest classification model in skicit-learn, but I am unsure how to finalize my forecast.

I have built the model and it is showing good results on the testing data. I get a mean accuracy of 85%. Predicting whether the stock price will go up or down. I used data from Yahoo finance consisting of open, high, low, close, and volume. From there I worked out some technical indicators such as the RSI, ROC, stochastic oscillators (fast and slow), macd, on balance volume, and the 200 days moving average and used these as features (independent variables) in the random forest classifier. I created another column, showing 1 when the price went up and 0 when the price went down. This column was used as the dependent variable. (the thing I want to predict)

The thing I am trying to find out now is how can I run the forecast into the unknown future? For now, I have split my data into training and testing, trained the model on the training dataset, and then used the predict function on the testing dataset. The model performs well and after a little more tweaking it can be used.

But how? I can't seem to find anywhere in the sklearn random forest documentation about how to actually run the forecast for the future (not on the testing data), say for example for the next 10 days from the last day of data. I hope you understand what I mean. Below is my code.

Here is my code:

X_train2, X_test2, y_train2, y_test2 =
train_test_split(data2.drop('prediction',axis=1),data2.prediction,test_size=0.02)

from sklearn.ensemble import RandomForestClassifier
model1 = RandomForestClassifier(random_state=13)
model1.fit(X_train2,y_train2)

predicted = model1.predict(X_test2)
model1.score(X_test2, y_test2)

from sklearn.metrics import roc_auc_score 
probabilities = model1.predict_proba(X_test)
probabilities

roc_auc_score(y_test2, probabilities[:,1])

from sklearn.metrics import confusion_matrix

confusion_matrix(y_test2, predicted)

enter image description here

$\endgroup$
2
  • $\begingroup$ What features does your data have and can you share a snippet? $\endgroup$
    – Jonathan
    Commented Jul 10, 2021 at 22:16
  • $\begingroup$ @Sammy I have edited the question and added a picture. $\endgroup$ Commented Jul 10, 2021 at 22:28

3 Answers 3

0
+150
$\begingroup$

In your case X is not the future data.

X is today data here as you try to predict tomorrow increase or decrease of value 1 or 0.

So model1.predict(X) with X being today data, will give you the prediction 0 or 1.

And this is it with your model

$\endgroup$
0
$\begingroup$

short answer, you cannot run your current model as described into the future. However, there is hope.

When building a forecasting model, you're typically using an "autoregressive" model, which is predicting, for example, the price in the future based on the price in the past. The reason this works is you are both predicting the next value, and generating the next input for your model. Let's say you have an autoregressive model $F(X) \rightarrow Y$ that predict the price of the stock tomorrow ($Y$) based on the price today ($X$). You can run this into the future by recursive calls to your model. $Y_{10}$ = $F(Y_9)$ = $F(F(Y_8))$ ... $= F(...F(X)$.

So let's take a look at your case. Your model takes as input a variety of technical indicators $\color{grey}{\text{RSI, ROC, stochastic oscillators (fast and slow), macd, on balance volume and the 200 day moving average}}$ So with your current model, in order to project into the future 10 days, we need an estimate of these features 9 days into the future.

Here are a couple solutions.

You could train a model to predicts 10 days into the future. So instead of training $F(X_i) \rightarrow Y_{i+1}$ you train $F(X_i) \rightarrow Y_{i+10}$.

You could build a model to predict your features into the future. $F(X_i) \rightarrow X_{i+1}$ , and then use these new estimated features with your current model. This is easier if $\color{grey}{\text{RSI, ROC, stochastic oscillators, ...}}$ are derived from the price. In which case, we can estimate the price and then derive these values. Although, I'm not familiar with the finance to say if you can derive them from price.

Although, as a word of warning, you should think deeply about your model performance, and if it's fooling you. You may consider the following: Have you compared performance to any simple baseline models? Is there any "target leakage"? If you look at interpreting your model, do the patterns it found make sense?

$\endgroup$
2
  • $\begingroup$ This is what I'm trying to predct - the price. If I knw the price, then I wouldn't need fture estimates of my features, since I wouldn't need the model due to knowing the price. This is the reason for the model, to be able to estimate the price, based on the features that I have. To your first point, training a model 10 days into the fture leavesus with the same question. How does one actualy predict into the future? No point training a model n days in the future if there is no way to run an actual forecast. We still need an array X, with n features to predict n days in the future $\endgroup$ Commented Jul 11, 2021 at 20:48
  • 1
    $\begingroup$ Let me clarify, I'm not suggesting you have the price, but you can estimate the future price using a model $F(X_i) \rightarrow Y_{i+1}$ then use $Y_{i+1}$ to derive $X_{i+1}$, allowing you to recursively project into the future. The other two points are true. Training $F(X_i) \rightarrow Y_{i+10}$ wouldn't allow you to project $n$ days into the future. To project an arbitrary length forecast you'd still need to project $X$ into the future, or retrain a model to predict $F(X_i) \rightarrow Y_{i+n}$. Sadly, I'm not aware with any other option. $\endgroup$ Commented Jul 12, 2021 at 3:37
0
$\begingroup$

You have completed the training phase. The next phase is commonly called prediction / inference. That is when already trained model predicts labels for data.

Since you are using scikit-learn, you should the call .predict method. In your code, it will be model1.predict(X) where X is the numpy-like array that contains the data features. The result will be a predicted class (0 or 1) for each entry in X.

$\endgroup$
4
  • $\begingroup$ this is what I'm trying to predct - the future, based on the past. I don't have future data, nor is ther any way to acquire such. Since then I wouldn't need the model, right? Essentially what you are saying that for us to be able to run an actual forecast we need an array X with n days worth of data to predict n days in the future. But then, that information is not available since we cannot teleport through time. Hence why need to use the model to predict Y. If were to try to predict X, again using Random Forest in sklearn, then we'd still have the same problem that we have now. $\endgroup$ Commented Jul 11, 2021 at 20:43
  • 1
    $\begingroup$ X is not the future data. X is today data here as you try to predict tomorrow increase or decrease of value 1 or 0. So model1.predict(X) with X being today data, will give you the prediction 0 or 1. And this is it with your model. $\endgroup$
    – Malo
    Commented Jul 11, 2021 at 21:27
  • $\begingroup$ @Malo if you write this out in an answer I'll give you the bounty. Your comment was all I needed to know. $\endgroup$ Commented Jul 14, 2021 at 22:09
  • $\begingroup$ I think I better understand your question now. I have revised my answer to more directly answer your question. $\endgroup$ Commented Jul 14, 2021 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.