1
$\begingroup$

I am working on a prediction model to predict whether a stock should sell, hold or buy in n days. Each day (or row in the dataset), I classify whether this should be sell, hold or buy based on the percentage change and a new column will be created to indicate what is the action for that particular day.

How should I deal with unbalance classification in my dataset when training my model? The train set as it is looks like this:

 1    1401
 0     835
-1     413

# 1 is buy, 0 is hold, -1 is sell

From reading up, balancing depends on the problem. Do I need to balance my data for a stock market prediction classification?

Thanks in advance.

PS: I am using SVM and Naive Bayes.

$\endgroup$
5
  • $\begingroup$ When you fit the models, what is your $y$ variable? $\endgroup$
    – Dave
    Sep 26, 2021 at 13:58
  • $\begingroup$ y is the binary classification of -1, 0 and 1. $\endgroup$ Sep 26, 2021 at 14:23
  • $\begingroup$ That’s not binary, and no measurement of yours tells you what decision you should make. I don’t think you have a classification problem. $\endgroup$
    – Dave
    Sep 26, 2021 at 14:29
  • $\begingroup$ Sorry I don't get you. In fit(), my x_train is my dataset (7 features) and my y_train is the outcome (-1, 0, 1). This is not classification as I am classifying my data based on whether it should be a buy, hold or sell? $\endgroup$ Sep 26, 2021 at 15:11
  • $\begingroup$ But why should you buy, hold, or sell based on those exact criteria? What if someone else wants to buy when you want to sell and sell when you want to buy? (You need such a counterpart for a real financial transaction. I don’t get to sell my stock or bond or swap or house unless someone wants to buy it.) $\endgroup$
    – Dave
    Sep 26, 2021 at 16:28

3 Answers 3

0
$\begingroup$

What percentages are you using for buy, hold and sell classes? From the data you share in the question, I am guessing it is a stock that has been going up rather than down for the most of the days. So, if you increase percentage cutoffs you have for the stock, you will have a balanced data.

As you don't share the details in your question, let's assume you set your classes to signal "buy" if change is larger than %1, sell for lower than -%1 and hold anywhere in between. But if you set the "buy" cutoff to be -let's say- %2 and "sell" cutoff to be %0, you might end up with a better balanced data.

To get the exact points which will give you balanced data, you can use quantiles method with q= 1/3.

$\endgroup$
5
  • $\begingroup$ Hi serali, the percentage I am using is +- 5% of the closing price as the signal. In theory, it is therefore required to balance such stock market dataset when performing classification? $\endgroup$ Sep 26, 2021 at 11:13
  • $\begingroup$ It is always advised to have a balanced dataset as your model will end up picking the highest weight top improve its metrics. I never used SVM so I have very basic knowledge about them but in neural networks you can get away with adjusting class weight instead. I think the simples way for you would be to find the correct percentages which will make the classes of equal weight. If the stock is going up %3, you want to beat that average anyway, so the limits should be lifted slightly up in an upwards trend. Try the quantiles to find exact points which divides your dataset into 3 equal pieces. $\endgroup$
    – serali
    Sep 26, 2021 at 11:42
  • $\begingroup$ There are more than one way to deal with this issue, check this for further discussion. $\endgroup$
    – serali
    Sep 26, 2021 at 11:44
  • $\begingroup$ I have read on that article before. Actually, does it make sense to consider oversample or undersample time-series stock data? I believe the time-series needs to be maintain as is in order to predict n days forward and performing oversample or undersample will make the prediction result sound logically wrong. $\endgroup$ Sep 26, 2021 at 13:33
  • $\begingroup$ That is a totally different question, I suggest you post separately. I think the simplest way for you to change pct points for you signals such that you have balanced data. $\endgroup$
    – serali
    Sep 26, 2021 at 13:37
0
$\begingroup$

The usual approach with unbalanced classes is just to make the train and test sets as homogenous as possible. So make sure that proportions of the classes in both sets are the same. There are many factors that can be taken into account when splitting data, but I'm gonna guess that you just need the basic approach. In sklearn that would be any stratified sampling.

To investigate if the class imbalances do not cause a problem you can then see if the model predicts some classes in the test set worse than the others. You could then adjust the thresholds for classifying samples to get rid of some imbalances. Although I would not suspect that to be the case with the two models you are using, but I might be wrong and it doesn't hurt to check.

Also in Naive Bayes the proportions of classes are an informative input for the model. They are known as the priors. I think most libraries take care of calculating the priors themselves and you shouldn't change them, unless you have a good reason to do so.

$\endgroup$
0
$\begingroup$

There is a perfect mapping between the rule and the decision. You know the percentage change; now apply your rule to map that to a buy/sell/hold decision.

There is no machine learning to do, but even if there were, now is a nice time to post a reminder that class imbalance isn't much of a problem when proper statistical methods are used. Models like the SVM and naïve Bayes approaches are useful because they discover rules that you did not know. Logistic regression figures out the optimal coefficients, even if we specify the functional form. However, you already know the rule for mapping percent change to a buy/hold/sell decision. You don't have to figure it out.

$\endgroup$

Your Answer

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

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