The Most Powerful Algorithm used by Poker Players and Quants
Poker is a game in which you need the capacity to make good decisions under conditions of incomplete information, and so is the stock market. In this article, I will explain how succesful poker players are able to transfer their skills to the stock market and viceversa.
Since the advent of online poker a huge amount of very talented and mathematically oriented people took over the game. That’s why the few people who are really good at modern poker, like Doug Polk, have developed an approach based on modeling the game as a decision tree, in which they assign probabilities to each scenario.
Probably they didn’t realize while doing it, but they were using the most poweful algorithm in probabilistic programming: Bayesian Networks.
Now, let’s see how this works in practice with stock prices.
For example, let’s take a look at a dataset with the historic SP500 monthly prices, on the dataframe below you can see the monthly prices on the “target” column, the monthly percentage change on the “y_change” and finally a binary column “y_bin” in which 1 means stocks went up and 0 means stock went down that month.

Now, our game is to guess whether next month the “y_bin” column will be 1 or 0 and bet accordingly. For this purpose, the first thing a Poker Player would do, is to look at sequences of the game and calculate their probabilities.
Just as poker players calculate the probabilities to get certain cards during the flop or the river in poker, you can calculate the probabilities to get certain combinations of winning or losing months on the stock market.
For instance, let’s do this with sequences of 3 months, you can use this snippet in Python:
#create sequences of lenght = 3
sequences = [tuple(df['y_bin'][i:i+3]) for i in range(len(df) - 2)]
# Count occurrences of each unique sequence
sequence_counts = pd.Series(sequences).value_counts()
# Display the counts of each sequence
print(sequence_counts)And this is what we get:

By looking at the combinations above, a poker player trying to predict stock prices for the next month would think like this:
What happened during the last 2 months?
Let’s say stocks went up in both of the previous 2 months.
What would my decision tree look like then?
Well, it could be represented like this: 1->1->?
How do the probablities for next month look like?
Well, there are 329 combinations starting with (1,1). If next month stocks went down the sequence would be “1,1,0”, this happened 117 times out of 329 times (that means probability=35.56%) while if they went up the sequence would be “1,1,1” which happened 212 times out of 329 (that means probability=64.44%).
This means that the Bayesian Probability of a third month being positive knowing that the 2 previous months have been positive in the stock market is 64.44% and therefore if we bet that stocks will go up in this case we will win 64.44% of the times.
If we apply the same reasoning to the other combinations, we can keep learning a lot about our stock market game.
For example, if one month stocks go up and next month they go down, we would be in this part of the decision tree: 1->0. In that case, the chance that the next month goes up or down are equal because both combinations “1,0,1” and “1,0,0” happen with the same frequence: 86 times.
That means we shouldn’t bet in that case because there is no value in doing so.
On the contrary we can see that the stocks tend to go up on the third month when 2 consecutive months have been negative: “0,0,0” happens only 61 times while “0,0,1” happens 85 times, so there is also value in betting that the stock market will rise in this case.
I hope by now you see the point.
Cool, so now we could create strategies combining different sequences.
For example, let’s implement a very simple strategy in Python basing on the combinations above. I will bet that if the previous month was positive, stocks will go up also this month which covers these cases:
I win if happens “1,1,1” or “0,1,1” (probability =329/500)
I lose if happens “1,1,0” or this “0,1,0” (probability=171/500)
And also we will combine that with betting that stocks will go up if the first 2 months are negative, which covers these cases:
I win if happens “0,0,1” (probability = 85/146)
I lose if happens “0,0,0” (probability= 61/85)
The following code snippet implements this simple strategy and backtests it on the whole historic dataset for the monthly SP500:
#initial capital invested is equal to the initial price of sp500 (42.69)
acc = 42.69
earnings = []
for i in range(2,len(df)):
if df.iloc[i-1]['y_bin'] == 1:
acc+= df.iloc[i]['y_change']*acc
if df.iloc[i-1]['y_bin'] == 0:
if df.iloc[i-2]['y_bin'] == 1:
print('I dont invest')
if df.iloc[i-2]['y_bin'] == 0:
acc+= df.iloc[i]['y_change']*acc
earnings.append(acc)If we run that script and plot it against the actual values we get this:

As we can see, the SP500 has gone from a price of 42.69$ to 4288.39$ in the period between 1955 and 2023 while our strategy would have gone from 42.69$ to 5517.16$ which shows it is a strategy that has positive expected value.
But not only our strategy has outperformed the index, it also has done so consistently for the whole period of time. This is something important that beginners don’t take into account when performing backtesting.
In conclusion, we exploited historic probabilities to spot situations with positive expected value, and we bet only in these favorable cases. This is what poker players do and what good quantitative traders do.
Now, of course you could use much bigger sequences to analize more complex scenarios and do it with daily or weekly datasets, but the bayesian networks mindset will remain the same.
In fact, when you see quants talking about fancy algorithms like deep learning and its popular LSTMs all they are doing is analyzing sequences and looking for patterns as we have done
However, the magic in Bayesian Networks is that they are simple and rational, you know what your model does and why, while other algorithms can be very complex and not understandable for humans. This is the reason that makes Bayesian Networks the most important tool in modeling games of uncertainty.
The core concept of Bayesian Networks applied to games of incomplete information is this: you need to ask yourself at each node of the game, what are the probabilities of the next possible outcomes and bet according to the expected value.
Well, that was all for today, hope you found it interesting.
Happy coding!





