Hacker News new | past | comments | ask | show | jobs | submit login
Monte Carlo methods (easylang.dev)
312 points by redbell on May 14, 2023 | hide | past | favorite | 48 comments



Hi. Author here. It is shown here on different in the browser executable and editable programs with Monte Carlo methods that one cannot win at roulette if one plays many times. Many know this, but many do not, and therefore this can be helpful. After all, there are more than a few who lose their financial existence in the casino.


The last method, where you always double the bet (called Martingale) "works" and can guarantee you are at least even - if you have infinite time and money.


And if you're allowed to make bets of an infinite size, which casinos restrict in part to defeat this strategy (as well as manage their risk).


Though even if you are allowed to make infinite bets, most people have finite money to bet with.


Thanks for the name "Martingale", I had wondered what this was called since an old gambler I met on a contract about 10 years ago described 'his' system.

After a quick wikipedia rabbithole, I now grok the St Petersburg paradox, which is fascinating - https://en.wikipedia.org/wiki/St._Petersburg_paradox - but mostly tells me that the 'expected value' is not a good measure of whether you should bet on something, because games can be constructed in such a way that the expected value is infinite, but the chances of you even getting your stake back even at a $16 buy in are 1/8...


the St. Petersburg paradox also reminds me of Pascal's mugging: https://en.wikipedia.org/wiki/Pascal's_mugging


So much so that I think they are likely to be different narratives around more or less the same phenomenon!


If you have an infinite amount of money, you already own the casino.


Thank you for writing. Very clearly explained.

Can you explain how do you derive this?

print 10 * 10000 * (18 / 37 - 19 / 37)


That's an expected value calculation.

It breaks down into three parts:

1. The wager per round ($10). This is, in the game being simulated, the amount won or lost based on the game's outcome.

2. The number of rounds (10000).

3. The probability of winning minus probability of losing.

Forgetting the number of rounds for a moment, the way to calculate the expected value of a single wager like this is:

  E[X] = x1*p1 + x2*p2 + ... + xn*pn
Where each `xN` is the value (amount won or amount lost since we're talking about wagers) of an event and `pN` is the probability of that event. In this case there is an 18/37 chance of winning and 19/37 chance of losing. The value of winning is +10 and the cost of losing is -10. So the expected value of a $10 wager ends up being:

  10 * 18/37 - 10 * 19/37
Multiply that by 10,000 for the number of rounds played and you get the expected value of a series of games.


Note that's the calculation for a European (single green 0) wheel, not the American (green 0 and green 00) wheel, in which you'd replace with +18/38 (or 9/19) and -20/38 (or 10/19).

Yes, the American version is twice as bad for the player, but it doesn't matter; people are still plenty eager to play it...


I'll note that if you can deterministically calculate an EV, a Monte Carlo simulation isn't necessary. The fact that the EV of roulette is so easy to calculate defeats the purpose of this exercise.

Perhaps the MC can help you conceptualize volatility and distribution of results, but once you know that the EV is negative, there's no good (mathematical) reason to participate.


> but once you know that the EV is negative, there's no good (mathematical) reason to participate.

Unless of course you know that the casino is willing to pay you out as part of their marketing budget (based on your ev to them); usually in the form of compensated room, food, and beverage, and sometimes even transportation. At that point your choice to participate becomes vacation planning for the mathematically literate.


That has nothing to do with the example presented in this article.


It is also about the "law of large numbers". If you go to the casino with $1000 and want to go home with $2000, the chance is more than 48% if you bet the $1000 on red. But if you play often and always bet $10 on red until you have $2000, your chance is less than 1%.


...which is why I stated "the MC can help you conceptualize distribution of results", however again this doesn't really require an MC simulation. To achieve $2000 while betting $1000 requires you to win one bet more than you lose. To get there while betting $10 requires you to win 100 more. Common sense should tell you which is significantly easier to achieve.


It is dangerous to rely on common sense. Many people believe that with a little luck you can win at roulette. Even according to my common sense, I would not believe that the chance is so low. But a little MC simulation tells me how it really is.

https://easylang.dev/ide/#code=n%20%3D%2010000%0Afor%20i%20%...


You're trying to justify your own article. I don't think many people believe you just need "a little luck" to win $1000 at any casino game while betting just $10 per hand -- most recognize that it's close to impossible when betting that small, because it requires 100 more wins than losses. Perhaps you did not -- that's on you.


You are trying to justify your comment. You overestimate the mathematical judgment of most people. But all due respect to you for assessing it so accurately. I myself prefer to rely on a MC simulation.


Per round expected value of +/- 10, times 10k rounds, 18/37 lose probability 19/37 win probability.


I love how lightweight and interactive this is.

Related: Norvig's runnable intro probability notebooks at https://github.com/norvig/pytudes#pytudes-index-of-jupyter-i...



On the following blackjack page (link at the bottom of the page), the phrase "Buy up to 17" can be misleading.

The author means "if your total is 17 or less, get another card". But it can easily be interpreted as "keep asking for more cards until you have 17 or more"; almost the same, but different.


A closely related idea is Approximate Bayesian Computation: https://www.sumsar.net/blog/2014/10/tiny-data-and-the-socks-...


We can do math integration and solve radiation transport problems with Monte Carlo.


It was actually invented for this.

Open source radiation transport Monte Carlo code here if you'd like to play around:

https://github.com/openmc-dev/openmc


and ENIAC's first programs were Monte Carlo simulations.


yep its beautiful how this can be done, its basically just taking the mean of values of function derived from random values from a uniform distribution of points in a specific range. Once done many times, we can approximate the integration value. Because E[X] = integration (f(u)p(u)du), p(u) will be 1 as the number is derived from a uniform distribution in the range of u.


Any integral you can sample can be solved with monte carlo methods. This doesn't really have anything to do with the link.


Actually it does, because MC integration works due to the law of large numbers - exactly what is presented in the article.


Right, that's what is in the article, but just naming a random arbitrary use doesn't make any sense.


Some people actually find learning new things to be interesting and would therefore appreciate a comment giving them a new avenue of exploration to pursue.


Yep, several particle accelerator studies use it to model neutrino's.


Just finished embedding a Python component on my site - https://www.actuarialopensource.org/book/welcome.

Uses react-py. Some effort required to implement tabs and manage lifecycle of Pyodide for separate editors.


Very nicely polished! Thank you for sharing!

For the purposes of teaching Python, I once created something similar---but without the explanations:

https://www.speicherleck.de/iblech/zufall-im-browser/index.e...


I haven't completed or deployed it yet but I wrote a C# API to calculate your chance of winning a hand of poker based on all known cards at the table.

All unknown cards are randomly assigned and it just loops a bunch, reasonably easy to implement and actually is reasonably fast.

https://github.com/JohnFarrellDev/PokerMonteCarloAPI/

With the amount of possible outcomes Bayesian statistics just didn't seem reasonable to implement.

Goes without saying this tool is still fairly basic, it shouldn't be used to inform how much to bet or when to fold as it doesn't take into account information such as how much your opponents are betting.


I wrote a Haskell version that includes two components:

A very efficient function to rank a set of Texas Hold’em hands.

A Monte Carlo situation that gives you the probability of winning each hand from any known amount of information.

It is available here: https://github.com/ghais/poker


Not sure if you guys are aware but there are solvers existing (most of which are proprietary) that actually give optimal strategies with every possible hands given a betting pattern. They are used extensively as study tools by professional players.


Can you link to some projects? Ideally also Haskell or also functional?


I use GTO+ which is proprietary.

I just tried this one which works like a charm (just run the exe from the zip in github releases ; even comes pre-loaded with a wide amount of preflop ranges, which seem to come from a previous solve) : https://github.com/bupticybee/TexasSolver

Searching with "poker solver haskell" only seem to show very immature projects.


Very cool. At first I thought this would be about project management using Monte Carlo for target dates.


What's about online poker. Is there also a bank advantage if you play perfectly? Is it allowed?


Poker is quite different. In roulette, the house advantage is probabilistic. As the linked page shows, that advantage is overwhelmingly likely to materialize, and rather quickly at that. But the probability that you play a million rounds of roulette and end up ahead isn’t exactly zero, just such a ridiculously small number you might as well call it zero for real life purposes.

Casinos make money on poker play differently, and in a few ways. The simplest is “rake”. They take a fixed percentage of every pot. This is different because it’s not probabilistic. The casino isn’t gambling its own money. It’s just a tax.

Money only comes in from players. So the EV of all players is negative, because a little is flowing out to the casino every hand. The chance players as a whole end up ahead isn’t near zero, it is zero.

If everyone was playing theoretically perfect poker every hand, all individual players EV are also negative. But, no one is playing perfect all the time. So you can get ahead because you’re not beating the odds. You’re beating other players.



Fun! and just as "21" taught us card counting is always the answer.


This is so cool - simple and educative!


Really nice presentation/learning page. Being able to change the code on the fly is also nice.


This is wrong.

Monte Carlo has nothing to do with gambling, historically or in mathematics. Historically it was about playing solitaire and casinos also have cards.

Monte Carlo is more like AI.

It looks at the world and returns with intuitive answers. Except it's intuition is billions of times better than yours.

As an example I proved this HN post wrong using Monto Carlo - The Mystery of the Frog Riddle - https://news.ycombinator.com/item?id=32490611 I've deleted the code but write your own and check.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: