> ChrisAcheson.net_

Math for Programmers

I bookmarked a blog post called “Math for Programmers” years ago on one of my random “scratch space” wiki pages, and just rediscovered it tonight.  I figured I’d share:

The right way to learn math is breadth-first, not depth-first. You need to survey the space, learn the names of things, figure out what’s what. [...]

I think the best way to start learning math is to spend 15 to 30 minutes a day surfing in Wikipedia. It’s filled with articles about thousands of little branches of mathematics. You start with pretty much any article that seems interesting (e.g. String theory, say, or the Fourier transform, or Tensors, anything that strikes your fancy.) Start reading. If there’s something you don’t understand, click the link and read about it. Do this recursively until you get bored or tired.

My timing on this is interesting.  Over the past few days I’ve started playing a bit of poker (Texas hold ‘em, against computer players in PokerTH), in lieu of my usual habit of compulsively playing quick games of FreeCell at random moments.  For the sheer nerdy fun of it, I want to write a program to calculate the exact odds that I have a better hand than all of my opponents, given the cards that are visible at any particular time (pre-flop, flop, turn, and river).

I’m sure it’s a solvable problem, but I’m having trouble simplifying the staggering number of permutations involved.  For example, assuming you’ve got your two-card hand, there are 1,225 possible hands that a single opponent could have from the remaining 50 cards.  If you have six opponents, there are approximately 9×1017 possible combinations of hands that they could have.  We only care about the best hand among our opponents, so I think we can simplify things by assuming that the math is the same for six opponents as it is for one, except that any given hand is six times more likely to occur.

Anyway, I’m thinking of reading up on combinatorics.  I got a bit from my discrete math and probability & statistics courses in college, but I feel like I’m missing something here.  I’ve always been interested in probability and permutations in games, so it seems like a good subject to pursue.

Tags: ,

2 Responses to “Math for Programmers”

  1. Mauvis says:

    Hi Chris!

    Long time no see, but I’m an avid reader of your blog(s).

    I love playing Texas Hold’em, too, and thought about programming it all out as a web app in JS. I’d have a Cards class a Game class and a Player interface. It would be cool then, to put it up and have other people extend the player interface and we can have a bunch of AI players versing each other to see who can come up with the better AI.

    Everything above would be pretty straight forward except for actually creating an “intelligent” player. Like you, the staggering number of permutations overwhelm me and hence I haven’t executed on that idea as of yet.

    I think it’s a fun project to work on when I have a free chance, though, maybe you can help with those calculations. ;)

  2. Hey Mauvis, glad to see you found me. :-)

    I’m not sure how poker bot AI is usually programmed. I assume they sacrifice some accuracy in order to obtain reasonable performance. What I’m after here is a total understanding of my odds in any situation, rather than merely a set of good heuristics.

    I’m working on the program now, in C. I was going to use python, but I really need the extra performance. Right now I’m just simulating with a single opponent, and there’s about 2 billion permutations to go through for the pre-flop (50 choose 5 for the table, times 45 choose 2 for the opponent). I’ve only got it checking for a straight flush and then high card right now, and it takes 6 and a half minutes to finish, even with full compiler optimizations. I’m wondering if there’s some kind of time/memory trade-off that I could use to speed it up.

    Let me know if you get into working on your poker thing, or if you want to see my code.

Leave a Reply

You must be logged in to post a comment.