# How do I deal with unbalance classes in a stock market prediction problem?

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?

PS: I am using SVM and Naive Bayes.

• When you fit the models, what is your $y$ variable?
– Dave
Sep 26 at 13:58
• y is the binary classification of -1, 0 and 1. Sep 26 at 14:23
• 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.
– Dave
Sep 26 at 14:29
• 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? Sep 26 at 15:11
• 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.)
– Dave
Sep 26 at 16:28

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.

• 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? Sep 26 at 11:13
• 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. Sep 26 at 11:42
• There are more than one way to deal with this issue, check this for further discussion. Sep 26 at 11:44
• 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. Sep 26 at 13:33
• 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. Sep 26 at 13:37

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.