Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Math Help - The Mathematics in BlackJack Basic Strategy

  1. #1
    Junior Member
    Joined
    Jan 2010
    Posts
    47

    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!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor ebaines's Avatar
    Joined
    Jun 2008
    From
    Illinois
    Posts
    1,069
    Thanks
    299
    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.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Jan 2010
    Posts
    47
    Quote Originally Posted by ebaines View Post
    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.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    MHF Contributor ebaines's Avatar
    Joined
    Jun 2008
    From
    Illinois
    Posts
    1,069
    Thanks
    299
    Quote Originally Posted by qcom View Post
    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.
    I'm not sure if there are commercially available programs that make this easy - perhaps someone else can answer that. However, the spreadsheet approach is not overly difficult. In Excel, to deal a card you can use the function =CEILING(RAND()*13), which returns a random number from 1 to 13. Let 1 = ace, 2 = 2, 3=3, ... 11 = jack, 12 = queen, 13 = king. If the card dealt is 10 or greater count it as 10. Start with your initial condition, for example you have 18 and dealer shows 10. The next card the dealer shows is found using the above function. If the sum < 17 he has to go again, if >17 he stands (in your spreadsheet you can simulate standing by dealing a 0). Do this six times over (which is the max number of cards a delaer can ever take if he starts with 10), add up the dealer's points, and see how it compares against your 18. If you lose you get 0 points, if you win you get 1 point, and if you push you get 0.5 points. All of this can be done on one line of a spreadsheet. Then copy and paste this 1000 times, add a facility to find the average score for all 1000 lines and you're done. If you know Excel you can do this in an hour. The only hard part is handling aces, since they can count as either 1 or 11 points. I suggest you count them initially as 11 points, and if the dealer goes over 21 with a hand that has an ace subtract ten points (turning the value of the ace into a 1).

    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!
    Follow Math Help Forum on Facebook and Google+

  5. #5
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by qcom View Post
    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.)
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Junior Member
    Joined
    Jan 2010
    Posts
    47
    Quote Originally Posted by undefined View Post
    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!


    I'm not sure if there are commercially available programs that make this easy - perhaps someone else can answer that. However, the spreadsheet approach is not overly difficult. In Excel, to deal a card you can use the function =CEILING(RAND()*13), which returns a random number from 1 to 13. Let 1 = ace, 2 = 2, 3=3, ... 11 = jack, 12 = queen, 13 = king. If the card dealt is 10 or greater count it as 10. Start with your initial condition, for example you have 18 and dealer shows 10. The next card the dealer shows is found using the above function. If the sum < 17 he has to go again, if >17 he stands (in your spreadsheet you can simulate standing by dealing a 0). Do this six times over (which is the max number of cards a delaer can ever take if he starts with 10), add up the dealer's points, and see how it compares against your 18. If you lose you get 0 points, if you win you get 1 point, and if you push you get 0.5 points. All of this can be done on one line of a spreadsheet. Then copy and paste this 1000 times, add a facility to find the average score for all 1000 lines and you're done. If you know Excel you can do this in an hour. The only hard part is handling aces, since they can count as either 1 or 11 points. I suggest you count them initially as 11 points, and if the dealer goes over 21 with a hand that has an ace subtract ten points (turning the value of the ace into a 1).

    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.
    Last edited by qcom; May 19th 2010 at 07:51 PM.
    Follow Math Help Forum on Facebook and Google+

  7. #7
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by qcom View Post
    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.
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Junior Member
    Joined
    Jan 2010
    Posts
    47
    OK, from here on, we'll PM the rest, and then see if we can use IMing.

    Thanks for the help everone!
    Follow Math Help Forum on Facebook and Google+

  9. #9
    MHF Contributor ebaines's Avatar
    Joined
    Jun 2008
    From
    Illinois
    Posts
    1,069
    Thanks
    299
    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.
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Junior Member
    Joined
    Jan 2010
    Posts
    47
    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?
    Follow Math Help Forum on Facebook and Google+

  11. #11
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    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).

    Quote Originally Posted by ebaines View Post
    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.

    Quote Originally Posted by ebaines View Post
    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.

    Quote Originally Posted by ebaines View Post
    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.
    Last edited by undefined; May 20th 2010 at 08:41 AM.
    Follow Math Help Forum on Facebook and Google+

  12. #12
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    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>();
                    cards.add(j);
                    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 (dealerTot > 21) {
                if (!dealerHasAce) {
                    // dealer bust
                    expRet += p;
                    //System.out.println("dealer bust " + cards + " (" + playerTot + ", " + dealerTot + ") " + expRet);
                    return;
                }
                dealerTot -= 10;
                dealerHasAce = false;
            }
            
            if (dealerTot >= 17) {
                // we do not need to check dealerHasAce because dealer must stand on all 17s
                if (dealerTot > playerTot) {
                    expRet -= p;
                    //System.out.println("player low " + cards + " (" + playerTot + ", " + dealerTot + ") " + expRet);
                }
                if (dealerTot == playerTot) {
                    //System.out.println("push " + cards + " (" + playerTot + ", " + dealerTot + ") " + expRet);
                }
                if (dealerTot < playerTot) {
                    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++) {
                int nextDealerTot = dealerTot;
                if (i == 1) nextDealerTot += 11;
                else if (i > 9) nextDealerTot += 10;
                else nextDealerTot += i;
                boolean nextDealerHasAce = dealerHasAce;
                if (i == 1) nextDealerHasAce = true;
                ArrayList<Integer> nextCards = new ArrayList<Integer>(cards);
                nextCards.add(i);
                recurse(nextP, playerTot, nextDealerTot, nextDealerHasAce, nextCards);
            }
        }
    }
    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 dealerTot > 21:
            if not dealerHasAce:
                # dealer bust
                currExpRet += p
                return
            dealerTot -= 10
            dealerHasAce = False
        if dealerTot >= 17:
            # 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):
            nextDealerTot = dealerTot
            nextDealerHasAce = dealerHasAce
            if i == 1:
                nextDealerTot += 11
                nextDealerHasAce = True
            elif i > 9: nextDealerTot += 10
            else: nextDealerTot += i
            nextCards = cards[:]
            nextCards.append(i)
            recurse(nextP, playerTot, nextDealerTot, nextDealerHasAce, nextCards)
        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.
    Last edited by undefined; May 20th 2010 at 02:14 PM.
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Junior Member
    Joined
    Jan 2010
    Posts
    47
    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?
    Follow Math Help Forum on Facebook and Google+

  14. #14
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by qcom View Post
    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.
    Follow Math Help Forum on Facebook and Google+

  15. #15
    Junior Member
    Joined
    Jan 2010
    Posts
    47
    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?
    Follow Math Help Forum on Facebook and Google+

Page 1 of 2 12 LastLast

Similar Math Help Forum Discussions

  1. Replies: 1
    Last Post: September 16th 2011, 01:22 PM
  2. Strategy games
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: June 24th 2011, 12:56 AM
  3. sampling strategy
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: August 6th 2010, 12:45 AM
  4. Need Blackjack Spreadsheet
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: January 27th 2010, 03:49 PM
  5. Mathematics: Discrete-Mathematics (Algorithems)
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: November 2nd 2008, 06:27 AM

Search Tags


/mathhelpforum @mathhelpforum