# Math Help - The Mathematics in BlackJack Basic Strategy

1. ## The Mathematics in BlackJack Basic Strategy

I am doing a school project about the mathematics involved in card games, and I thought the basic strategy of BlackJack would be a great subject to touch on.

For things like the basic strategy charts, what kind of math was used to generate the best possible moves. Now, I know that these are the best possible moves, not counting deviations, because it's just a static "in the moment" comparison, versus taking into account what cards have been dynamically changing the game.

So, is it also possible, that I could do some of the math myself to prove in a sense those "basic strategy" charts?

A link to these charts can be found here: Blackjack - Wikipedia, the free encyclopedia

Thanks!

2. There are two aways to go about deriving your own version of the strategy chart. The hard way is to calculate the odds of each possible outcome starting from yor initial conditions (such as you are dealt 17 and the dealer is showing a ten), and develop an expected return for each. Since there are thousands of possible games, you can see that it takes a lot of work to get the final answers. For example, from these starting conditions if the dealer's second card turns out to be an A, 8, 9, 10, J, Q or K, you lose. If it's a 7 you push. If it's a 2, 3, 4, 5 or 6 he has to take another card. Depending on what card he draw snext he may win, push, bust, or be required to draw again. Throw in teh rules for hitting a "soft 17" and it really gets hairy. Once you have an answer, then repeat for all other initial starting conditions.

An easier way is to run a series of Monte Carlo simulations. This technique involves running thousands of games on a computer using a randomized card dealing simulation and seeing which strategies improve your odds of winning. You can do this with some modest programing skills, or even on a spreadsheet. You write the rules for whether the dealer stands or hits, and run the simulation a few thousand times to see how you do. It's pretty straight forward if you assume that the odds of any card being drawn never changes, which basically models the dealer's shoe as infinitely deep. In other words, if he deals an ace of spades the next card he deals is as likely to be another ace of spades as any other card. If you want to get into understanding stategies as the shoe gets worked down (i.e,, takimng into account what cards have already been dealt), it gets too complicated for a spreadsheet, and you would need to write a computer program to simulate it. But in general I think Monte Carlo is the best way to attack this.

3. Originally Posted by ebaines
An easier way is to run a series of Monte Carlo simulations. This technique involves running thousands of games on a computer using a randomized card dealing simulation and seeing which strategies improve your odds of winning. You can do this with some modest programing skills, or even on a spreadsheet. You write the rules for whether the dealer stands or hits, and run the simulation a few thousand times to see how you do. It's pretty straight forward if you assume that the odds of any card being drawn never changes, which basically models the dealer's shoe as infinitely deep. In other words, if he deals an ace of spades the next card he deals is as likely to be another ace of spades as any other card. If you want to get into understanding stategies as the shoe gets worked down (i.e,, takimng into account what cards have already been dealt), it gets too complicated for a spreadsheet, and you would need to write a computer program to simulate it. But in general I think Monte Carlo is the best way to attack this.
Thanks a lot for the help, that really simplifies what I need to do! Of course, I like the second option you gave better, and I agree that a computer program would be best because I could calculate a lot of different inputs and outputs pretty easily. Now, for the Monte Carlo method, is there a standard computer application that I could download and set up, or does it need to be coded.

4. Originally Posted by qcom
Now, for the Monte Carlo method, is there a standard computer application that I could download and set up, or does it need to be coded.

The end result will be that if you start with 18 and the dealer is showing 10 you're expected return is about 0.38, meaning that you would expect on average to win only 38% of the time. But if you draw on 18 the chances of busting (and losing) is 10/13 = 0.77, so your odds of winning go down. Hence, even though you probably won't win with an 18, it's best to stand.

Good luck with it!

5. Originally Posted by qcom
Thanks a lot for the help, that really simplifies what I need to do! Of course, I like the second option you gave better, and I agree that a computer program would be best because I could calculate a lot of different inputs and outputs pretty easily. Now, for the Monte Carlo method, is there a standard computer application that I could download and set up, or does it need to be coded.
If you google search "blackjack monte carlo simulation" you will get some relevant hits. But my recommendation would be to code it from scratch, then you don't have to read someone else's documentation, and you'll have total control over everything. Also you can gain confidence from learning a new skill. You'll just need to know how to handle loops, random numbers, and a few other basic operations. The language isn't particularly important; you could try Python or Java, for example. Among those two, Java will be a bit faster, and of course there are many other options.

I could give you some sample code if you'd like. I wouldn't be able to work on it for a day or so due to time constraints. Let me know. (And so that I don't defeat the purpose of what I wrote above, I would try to make it an instructive experience, not just giving you code with instructions.)

6. Originally Posted by undefined
If you google search "blackjack monte carlo simulation" you will get some relevant hits. But my recommendation would be to code it from scratch, then you don't have to read someone else's documentation, and you'll have total control over everything. Also you can gain confidence from learning a new skill. You'll just need to know how to handle loops, random numbers, and a few other basic operations. The language isn't particularly important; you could try Python or Java, for example. Among those two, Java will be a bit faster, and of course there are many other options.

I could give you some sample code if you'd like. I wouldn't be able to work on it for a day or so due to time constraints. Let me know. (And so that I don't defeat the purpose of what I wrote above, I would try to make it an instructive experience, not just giving you code with instructions.)
Only problems include the fact that my project is due Wednesday ( a week from today), and this would only be part of it, because I would have to present it into a PowerPoint based on my findings / conclusions.

Also, on top of it all, I barely have any experience in coding of any languages so... Would it be possible for me to code my own based on yours in an easy language in about 3-5 days?

Sorry, it's kind of a lot to ask!

The end result will be that if you start with 18 and the dealer is showing 10 you're expected return is about 0.38, meaning that you would expect on average to win only 38% of the time. But if you draw on 18 the chances of busting (and losing) is 10/13 = 0.77, so your odds of winning go down. Hence, even though you probably won't win with an 18, it's best to stand.

Good luck with it!
Thanks a lot too, if I cannot code my own generator of sorts, then I will totally go with an excel spreadsheet for the project.

7. Originally Posted by qcom
Only problems include the fact that my project is due Wednesday ( a week from today), and this would only be part of it, because I would have to present it into a PowerPoint based on my findings / conclusions.

Also, on top of it all, I barely have any experience in coding of any languages so... Would it be possible for me to code my own based on yours in an easy language in about 3-5 days?

Sorry, it's kind of a lot to ask!
Hmm, well I stand by my statement that the programming only requires basic understanding of loops, random numbers, etc., but after reading more about the game blackjack I see that modeling requires some thought (at least for me, not having analysed this game before).

I have found an excellent resource, the blackjack page at Wizard of Odds. Especially of interest for your project is appendix 1, which gives expected returns and the modified basic strategy table for an infinite deck. (There are a few differences between that chart and the chart for 4-8 decks.)

I think that in order to accurately reproduce the basic strategy chart via simulation, we will need to express expected return in terms of not-necessarily-positive numbers as on the site I just referenced. In other words, I think that the scheme: (win = 1, lose = 0, push = 0.5) is a useful tool but is not sufficient for our purposes. I think we should use: a win without doubling (or splitting) is 1, a lose without doubling (or splitting) is -1, and a push is 0. Doubling and splitting are handled accordingly (e.g., a bet that is doubled and lost yields -2).

The charts on expected return are very valuable to us, for testing that we have not made mistakes. After we can reproduce the expected return charts, we will automatically have the basic strategy chart (we choose the lowest expected return available to us given our situation).

So, now that that aspect is addressed, there are some logistical issues regarding programming. Are you using a Windows operating system? If so, maybe the easiest way for me (not necessarily for you ) is to use Java. That's because I've installed the Java software on Windows machines like 10 times and I'm very familiar with the process (it's only a few steps, but not necessarily obvious to a beginner); and I'm set up to code Java quickly. If you use Linux then Java should be even easier to set up; if you have a Mac, I'm guessing the procedure is comparable, but I'd have to do some internet searches to know how to do certain stuff. If for some reason you have a preference for a language other than Java, please mention it.

Also, regarding logistics, it may be easier if we conversed using an IM program; we could work something out through PM if you want, otherwise here is fine; but probably instructions on how to install the Java software should be through PM at least so as not to bore everyone here.

So, let me know what operating system you're using and we'll go from there.

8. OK, from here on, we'll PM the rest, and then see if we can use IMing.

Thanks for the help everone!

9. Just a couple of more thoughts...

The idea of scoring -1, 0 and +1 is good (a bit more intuitive than what I first proposed). But there is really no need to worry about keeping track of winnings due to splitting or doubling down with +2's or -2's. Remember that the purpose of the exercise is to determine your strategy for what to do next, not on keeping count of winnings. For example, you always double down (if you're allowed to) based on whether your odds of winning is > 50%. No need to keep traclk of the winnings itself, because after all you're only simulating one hand at a time, not a full game. As for splitting - you do that if the expected winnings for the two split cards are better for you than the winnings if left unsplit (note that you want to split even for cases where you are expected to lose, if the expected losses are less by splitting split than not).

One other thing - don't forget to consider the strategy of buying insurance if the dealer is showing a 10 or ace. Actually this one aspect of the game may be conducive to a nice short (and not too difficult) analysis.

10. OK, thanks for the additional info, in the mean time, when I wait for undefined to help me out with the program, I have been experimenting with the Excel spreadsheat idea that you proposed, and it is pretty intuitive (I have hardly used excel before).

I did this however, because, for some reason, when I put your CEILING formula into a cell, I didn't get correct results, instead I entered: =RANDBETWEEN(1,13)

Does that work?

Then I set up 4 columns (2 for the dealers cards and 2 for player 1's cards.

What do I do from there?

11. Another note, using an infinite deck it shouldn't be entirely hard to get exact answers with a computer program, just by trying every combination and finding the probability of each, etc. Come to think of it, this might be easier than Monte Carlo, I'll have to investigate a bit.

Also, forgot to mention that getting blackjack would be +1.5 (or 0 if push).

Originally Posted by ebaines
For example, you always double down (if you're allowed to) based on whether your odds of winning is > 50%.
I haven't thought enough about it to address this theoretically, but this seems not to match up with the charts on the site I gave. For example, if player has 9 and dealer shows 3, expected return of Hit is 0.1013 and expected return of Double is 0.1208. It's possible I'm misinterpreting something though.

Originally Posted by ebaines
As for splitting - you do that if the expected winnings for the two split cards are better for you than the winnings if left unsplit (note that you want to split even for cases where you are expected to lose, if the expected losses are less by splitting split than not).
I'll definitely try to implement this.

Originally Posted by ebaines
One other thing - don't forget to consider the strategy of buying insurance if the dealer is showing a 10 or ace. Actually this one aspect of the game may be conducive to a nice short (and not too difficult) analysis.
Hmm, the wizard of odds says never to take insurance, but this could be looked into.

Thanks for the thoughts!

Edit: One last thing, since it's possible to hit multiple times, I think a direct Monte Carlo approach would actually involve an aspect of what I'd consider intermediate-level programming, namely recursion. Anyway we'll see how it goes.

12. Hmm, I wrote a program to duplicate the first chart on this page, and I can't figure out why some values match and others don't. (This is not Monte Carlo but rather direct calculation through brute force.) I've omitted the entries where dealer or player has possibility for blackjack, because I haven't yet determined what rules are being used there, but I'd like to get the other numbers to match first. Either I'm doing something wrong or the website is wrong... anyone care to comment? I suspected precision errors but got the same result using greater precision.

For example, here is one row that I get (rounded to 4 decimal places), compared with the appendix:

Code:
Mine:
2       3       4       5       6       7       8       9
20   0.6410  0.6512  0.6620  0.6704  0.7040  0.7732  0.7918  0.7584
Appendix:
2       3       4       5       6       7       8       9
20   0.6400  0.6503  0.6610  0.6704  0.7040  0.7732  0.7918  0.7584
Java:

Code:
import java.util.ArrayList;

public class BlackjackStrat {
static double expRet = 0;

public static void main(String[] args) {
for (int i = 16; i < 21; i++) {
for (int j = 2; j < 10; j++) {
expRet = 0;
ArrayList<Integer> cards = new ArrayList<Integer>();
recurse(1, i, j, false, cards);
System.out.println("(" + i + ", " + j + ") " + expRet);
}
}
}

public static void recurse(double p, int playerTot, int dealerTot, boolean dealerHasAce, ArrayList<Integer> cards) {
// assume playerTot <= 21

if (!dealerHasAce) {
// dealer bust
expRet += p;
//System.out.println("dealer bust " + cards + " (" + playerTot + ", " + dealerTot + ") " + expRet);
return;
}
dealerHasAce = false;
}

// we do not need to check dealerHasAce because dealer must stand on all 17s
expRet -= p;
//System.out.println("player low " + cards + " (" + playerTot + ", " + dealerTot + ") " + expRet);
}
//System.out.println("push " + cards + " (" + playerTot + ", " + dealerTot + ") " + expRet);
}
expRet += p;
//System.out.println("dealer low " + cards + " (" + playerTot + ", " + dealerTot + ") " + expRet);
}
return;
}

// iterate through all possible cards
double nextP = p / 13.0;
for (int i = 1; i < 14; i++) {
if (i == 1) nextDealerTot += 11;
else if (i > 9) nextDealerTot += 10;
boolean nextDealerHasAce = dealerHasAce;
if (i == 1) nextDealerHasAce = true;
ArrayList<Integer> nextCards = new ArrayList<Integer>(cards);
}
}
}
Python:

Code:
from decimal import *

currExpRet = Decimal(0)

def main():
global currExpRet
for i in xrange(16,21):
for j in xrange(2,10):
currExpRet = Decimal(0)
cards = [j]
recurse(Decimal(1), i, j, False, cards)
print '(' + str(i) + ', ' + str(j) + ') ' + str(currExpRet)
return

def recurse(p, playerTot, dealerTot, dealerHasAce, cards):
global currExpRet
# assume playerTot <= 21
if not dealerHasAce:
# dealer bust
currExpRet += p
return
dealerHasAce = False
# no need to check dealerHasAce; stand on soft 17
if dealerTot > playerTot: currExpRet -= p
if dealerTot < playerTot: currExpRet += p
return
# iterate through all possible cards
nextP = p / Decimal(13)
for i in xrange(1,14):
nextDealerHasAce = dealerHasAce
if i == 1:
nextDealerHasAce = True
elif i > 9: nextDealerTot += 10
nextCards = cards[:]
nextCards.append(i)
return

main()
Anyone care to comment? I'll keep looking. I think either I missed a rule, or the website is wrong... but the website seemed pretty reputable.. and I haven't been able to find another chart like it on the internet to compare against.

Update: I found a pdf here that confirms The Wizard of Odds's results, but I still haven't found my error.

Update 2: Now I suspect there is an error involving when the dealer gets two aces, and my program only recognizes one of them, which would cause the dealer to bust more often making my results slightly higher than the correct values.. I'll rework it in a bit.

13. Remember to make the rules in your simulation the same as the one on their site.

Their site has the following rules: "dealer stands on a soft 17, an infinite deck, the player may double after a split, split up to three times except for aces, and draw only one card to split aces. Based on these rules the player's expected value is -0.511734%" (thewizardofodds.com).

Look the same?

14. Originally Posted by qcom
Remember to make the rules in your simulation the same as the one on their site.

Their site has the following rules: "dealer stands on a soft 17, an infinite deck, the player may double after a split, split up to three times except for aces, and draw only one card to split aces. Based on these rules the player's expected value is -0.511734%" (thewizardofodds.com).

Look the same?
Fixed! It was the multiple aces problem I suspected.

15. Nice!!! Thanks for being persistent, ok, will you be able to Wave in about 1.5 to 2 hours from the time of this post?

Page 1 of 2 12 Last