Page 1 of 1

Weird calculation error? Odd results when var=5*2^powerVar

Posted: Thu Sep 30, 2010 6:31 am
by MichaelBluejay
I wrote a simple craps simulator to calculate the percent of wagered money lost, which should be around -1.41%, since that's the mathematical house edge in craps. And my sim does report the correct result when it bets a static amount every round, say $5. But if I vary the bet dynamically with a formula, then the result is way off... +4%. The result should always be -1.41% regardless of how much is bet each round, that's just a universally accepted principle about gaming math. If you look at code below, it returns the spurious +4% when it runs as listed, but if I comment out the formula and un-comment the $5 bet, then I get the proper -1.41%.

I can't figure out whether I'm doing something wrong or whether the problem is with LiveCode. On the one hand my code ought to work, but on the other hand, it doesn't.

Code: Select all

on mouseUp
   put 0 into LosingStreak
   
   repeat with rounds = 1 to 5000000
      put 5*2^LosingStreak into theBet
      --put 5 into theBet
      add theBet to totalBet
      
      put (random(495)<=244) into wonPassLine
      if wonPassLine then
         add theBet to bankroll
         put 0 into LosingStreak
      else
         subtract theBet from bankroll
         add 1 to LosingStreak
      end if
   end repeat
   
   put bankroll/totalBet *100 & "%"
end mouseUp

Re: Weird calculation error? Odd results when var=5*2^powerVar

Posted: Thu Sep 30, 2010 10:42 am
by Regulae
Hi there,

This has been an interesting model to look at, and I have been able to reproduce the behaviour you describe- if theBet is a constant, it yields around the expected -1.4%, however the line:

Code: Select all

put 5*2^LosingStreak into theBet
... results in +ve values around 4%. As an experiment, I made a modification to list the bankroll during a “run” of the model (this requires the presence of a scrolling field “Test”, and I reduced the number of rounds to something more manageable):

Code: Select all

on mouseUp
   put 0 into LosingStreak
   
   repeat with rounds = 1 to 50000
      put 5*2^LosingStreak into theBet
      --put 5 into theBet
      add theBet to totalBet
      
      put (random(495)<=244) into wonPassLine
      if wonPassLine then
         add theBet to bankroll
         put 0 into LosingStreak
      else
         subtract theBet from bankroll
         add 1 to LosingStreak
      end if
      put bankRoll&return after accumulate
   end repeat
   put accumulate into fld "Test"
   put bankroll/totalBet *100 & "%"
end mouseUp
... and I noticed that there was no provision to exit the repeat, should the bankroll become 0. Perhaps bankroll should be loaded with a value before the repeat, and then have an escape from the repeat should bankroll drop to 0. Also, the formula seems to call for the placing of dramatically larger bets, the longer the “losing streak”, which is an interesting strategy. For what it’s worth, I don’t think that there is an error in the calculation of the exponent, but the structure of the model, as written, produces counterintuitive behaviour. Still, I mightn’t have fully understood what’s intended.
Regards,
Michael

Re: Weird calculation error? Odd results when var=5*2^powerVar

Posted: Thu Sep 30, 2010 7:16 pm
by MichaelBluejay
Thanks for your help. It's okay for the bankroll to be zero, and in fact it starts at zero, and in most cases it will be negative after the very first round. It's the kind of "bankroll" where all your bets are made on credit. In a casino you generally start with a certain amount of money, but with my sim you start with zero. That's okay -- I need only measure how much we lost, and that's the same amount whether the calculation is 0 minus 100,000, or 1,000,000 minus 1,100,000.

The strategy of doubling your bet after a loss is an ancient system called the Martingale. Whenever you win a bet, you'll win one unit. But it doesn't work in real life because once you lose, say, 15 times in a row, you have to come up with over $100,000 to make your next bet. (And even if you had that much money, the casinos don't take bets that large.)

But anyway, no matter what the bet size, craps is a -1.41% game, and that's what the sim should return, but it doesn't work that way with a dynamic bet size. I'm wondering if there is some problem with LiveCode, like perhaps the RNG quality suffers once the script starts using exponents or something....

Re: Weird calculation error? Odd results when var=5*2^powerVar

Posted: Thu Sep 30, 2010 8:37 pm
by Regulae
I understand the model much better now- I was quite “wide of the mark” originally. And it is indeed when the exponent is introduced that the weird behaviour is seen. My latest theory, having played around a bit with the code, is that the introduction of exponents makes fluctuations in theBet relatively more extreme, which amplifies limitations of the RNG implementation. I will continue to ponder. I had the feeling there was some practical limitation on the use of a “follow your losses” betting strategy- thanks for taking the trouble to explain it. I’ve just looked up Martingale, and it has made for interesting reading.

Regards,
Michael

Re: Weird calculation error? Odd results when var=5*2^powerVar

Posted: Thu Sep 30, 2010 9:08 pm
by MichaelBluejay
I did some testing, and I ruled out some possible problems, but I still don't have a solution.

These bet sizes work fine (they produce the correct result):
  • put 5*2^(random(30)) into theBet
  • put random(10000000) into theBet
  • put 5 into theBet

This is the only one that doesn't.
  • put 5*2^LosingStreak into theBet
In a short test run (which produced incorrect results), the max value for LosingStreak was only 20 (less than what I tested by choosing random(30)), and the max bet size was only 5,242,880, so it couldn't have been an out of range error.

I'm getting closer to thinking it's some bug in LiveCode.

Re: Weird calculation error? Odd results when var=5*2^powerVar

Posted: Thu Sep 30, 2010 11:21 pm
by dunbarx
Am I way off base here? I made a simple modification to the script, just checking various values.:

Code: Select all

on mouseUp
      put 0 into LosingStreak
   put "" into fld "test"
      repeat with y = 1 to 10 
         repeat with rounds = 1 to 50000
               put 5*2^y into theBet
               add theBet to totalBet
               
               put (random(495)<=244) into wonPassLine
               if wonPassLine then
                     add theBet to bankroll
                     put 0 into LosingStreak
               else
                     subtract theBet from bankroll
                     add 1 to LosingStreak
               end if
         end repeat
         put y && bankroll/totalBet *100 & "%" & return after fld "test"
      end repeat
   
end mouseUp
The results are reasonably close, around -1.4%, and an exponential calculation is part of it. There has to be something in the variable "losingStreak" that is fouling up the works.

Craig Newman

Re: Weird calculation error? Odd results when var=5*2^powerVar

Posted: Fri Oct 01, 2010 1:56 am
by MichaelBluejay
Okay, I figured out the problem. I was going under the assumption that either my code was wrong, or else there was a bug in LiveCode. But in fact, it's neither! Which means that my sim was indeed reporting the correct result.

The reason it reports a positive return when I expected a negative one is related to the mathematics of the problem. The Martingale is dismissed as an unworthy betting system because it would require an infinite bankroll, and nobody has an infinite amount of money. But my sim asked the question, "What if you DID have an infinite bankroll?" However, while the bet size was unlimited in my sim, the number of trials was not. The sim ran only 50 million rounds, which isn't even close to infinity. If I could run my sim to infinity, then it should produce a result of -1.41%.

(Okay, math-heads will know that there's some disagreement among math experts about what the EV of an infinite betting system is, but what would not be in dispute was that the reason I got the "wrong" result from my sim was that I used an infinite bet amount for only a finite number of trials).

I put in some code to limit the amount of the bets (e.g., "if LosingStreak is not 10 then add 1 to LosingStreak"), and then it reports the expected result, -1.41%.

Sorry for the false "bug" inquiry, but the explanation was kind of interesting, I thought.

Re: Weird calculation error? Odd results when var=5*2^powerVar

Posted: Fri Oct 01, 2010 3:04 am
by Regulae
Thanks alot for explaining the answer. Indeed, it was an interesting matter to explore. It was quite a puzzle, and a nice problem to engage with.

Regards,
Michael