Thread Rating:

MichaelBluejay
MichaelBluejay
  • Threads: 87
  • Posts: 1706
Joined: Sep 17, 2010
August 20th, 2019 at 12:19:22 AM permalink
My software is giving me unexpected results for binomial probabilities.

I'm trying to solve the question, what is the probability of x hits of a certain number on a 37-slot roulette wheel over a certain number of spins?

e.g.:
p = 1/37 (probability of success of one trial)
q = 1-p (probability of failure of one trial)
x = 1 (number of successes)
n = (number of spins)

Binomial formula: n! ÷ ( x! * (n-x)! ) * px * q(n-x)

With n=170, I get 0.0448 in both LibreOffice Calc (spreadsheet) and my Javascript code.

With n=171, LibreOffice gives a "#VALUE!" error and JS says the answer is Infinity.

Surely I haven't gone from 4.5% to Infinity from just one additional spin!

With n=200, JS returns "NaN" (not a number).

I'm confused.
I run Easy Vegas ( https://easy.vegas )
OnceDear
OnceDear
  • Threads: 64
  • Posts: 7543
Joined: Jun 1, 2014
Thanked by
MichaelBluejay
August 20th, 2019 at 3:39:24 AM permalink
Quote: MichaelBluejay

My software is giving me unexpected results for binomial probabilities.

I'm trying to solve the question, what is the probability of x hits of a certain number on a 37-slot roulette wheel over a certain number of spins?

e.g.:
p = 1/37 (probability of success of one trial)
q = 1-p (probability of failure of one trial)
x = 1 (number of successes)
n = (number of spins)

Binomial formula: n! ÷ ( x! * (n-x)! ) * px * q(n-x)

With n=170, I get 0.0448 in both LibreOffice Calc (spreadsheet) and my Javascript code.

With n=171, LibreOffice gives a "#VALUE!" error and JS says the answer is Infinity.

Surely I haven't gone from 4.5% to Infinity from just one additional spin!

With n=200, JS returns "NaN" (not a number).

I'm confused.


170! is pretty big at 7.257416e+306
The biggest/largest integer that can be stored in a double without losing precision is the same as the largest possible value of a double. That is, DBL_MAX or approximately 1.8 × 10^308 (if your double is an IEEE 754 64-bit double).
You are close to that limit.
Psalm 25:16 Turn to me and be gracious to me, for I am lonely and afflicted. Proverbs 18:2 A fool finds no satisfaction in trying to understand, for he would rather express his own opinion.
kubikulann
kubikulann
  • Threads: 27
  • Posts: 905
Joined: Jun 28, 2011
Thanked by
MichaelBluejay
August 20th, 2019 at 3:49:27 AM permalink
Maybe use the Sterling approximate for a factorial? Maybe it would allow simplification of the fraction before actually computing 171!

The problem is you ask the software to compute a huge number (171!) to get a much smaller one.

Note: Excel computes COMBIN(171,x) without problem.
Reperiet qui quaesiverit
Ace2
Ace2
  • Threads: 32
  • Posts: 2706
Joined: Oct 2, 2017
Thanked by
MichaelBluejay
August 20th, 2019 at 9:35:49 AM permalink
The factorials cancel.

No need to use excel for something that can be done by any calculator or even by hand or slide rule.

(36/37)^169 * 170/37 = 0.0448 = a

a * 36/37 * 171/170 = 0.0438

In 171 spins you expect to hit your 1/37 number 4.62 times. You have a 1 - (36/37)^171 = 99.08 % chance of hitting it at least once, a 4.38% chance of hitting it exactly once and a 94.7 % chance of hitting it twice or more
Last edited by: Ace2 on Aug 20, 2019
It’s all about making that GTA
CrystalMath
CrystalMath
  • Threads: 8
  • Posts: 1911
Joined: May 10, 2011
Thanked by
MichaelBluejay
August 20th, 2019 at 9:51:40 AM permalink
Try this:


double binomDist(int x, int n, double p)
{
double q = 1.0-p;

double result = 1.0;
for(int d = n+1-x; d<=n; d++)
{
result*=d;
}
result*=Math.pow(p,x);
result*=Math.pow(q,n-x);
return result;
}


It avoids getting huge numbers because most terms cancel.
I heart Crystal Math.
MichaelBluejay
MichaelBluejay
  • Threads: 87
  • Posts: 1706
Joined: Sep 17, 2010
Thanked by
CrystalMath
August 20th, 2019 at 12:08:32 PM permalink
Thanks everyone for the help!

@OnceDear, yes, I should have figured out that one of my variables besides the actual result was going out of bounds. (Duh.)

@CrystalMath, your solution is perfect and exceptionally helpful. Thanks so much!
I run Easy Vegas ( https://easy.vegas )
MichaelBluejay
MichaelBluejay
  • Threads: 87
  • Posts: 1706
Joined: Sep 17, 2010
August 21st, 2019 at 6:18:52 AM permalink
Okay, I'm stuck again.

So, my understanding of canceling the factorials doesn't jibe with CrystalMath's Java/C code above. (CrystalMath, I'm referring to you in the third person not out of disrespect, but because other people might be helping besides you.)

So I must be missing something. Here's my understanding:

Binomial coefficient:

    n!
-----------
(n-x)! * x!


n = 10
x = 7

n! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2
x! = 7 x 6 x 5 x 4 x 3 x 2
(n-x)! = 3 x 2


So, to do the most cancelation, I should cancel n! with x!, not with (n-x)!

The binomial coefficient formula can be rewritten as:

    n!         n!       1      
---------- = ---- * ------
(n-x)! * x! x! (n-x)!



So, I'll calculate the factorials after canceling, then divide by the factorial of n-x, then multiply by the rest of the binomial probability formula.

After cancelation, I'll be multiplying 8 x 9 x 10. So, my code starts with:

for (i=x-0+1; i <= n; x++) { // Starts at 8, finishes at 10


CrystalMath's code is:

for(int d = n+1-x; d<=n; d++) // Starts at 4, ends at 10


So, that's the first thing that doesn't make sense to me. I'm guessing he's taking a shortcut.



However, if I use his code, looking for the probability of at least one hit of a single number over 37 spins, I get 38%. That does jibe with what I get in my spreadsheet. But it's not intuitive (seems low, seems like it should be >50%), and it doesn't jibe with the formula:

1 - (ways to win / total ways) ^ trials = 1 - (36/37)^37 = 64%.

So, that's the second thing I'm confused about.



Finally, when I use my own JS code, my results are even farther off. My understanding for "at least" is that I find the probability for exactly x matches, then multiply it by the number of trials.



p = 1/37;
x = 1;
n = 37;

sum = 0;
for (hitcounter=1; hitCounter <= x; hitCounter++) {
oneRun = probExactlyXhitsInNtrials(p, hitCounter, n);
sum += oneRun * n;
}
showResult(sum);

function probExactlyXhitsInNtrials(p, x, n) {
result = 1;
for (i=x-0+1; i<=n; i++) result *=i; // This line differs from CM's version
result /= fact(n-x); // This line doesn't appear in CM's version
result *= Math.pow(p,x);
result *= Math.pow(q,n-x);
return result;
}


That gives me probability of <0.1%, which is clearly wrong. That's the third thing I'm lost on.
I run Easy Vegas ( https://easy.vegas )
kubikulann
kubikulann
  • Threads: 27
  • Posts: 905
Joined: Jun 28, 2011
August 21st, 2019 at 6:35:42 AM permalink
1st thing.
There is a symmetry : Cnx = Cnn-x
So you could put some code like x:= max(x,n-x)?

2d thing and 3d thing.
P(at least) is not P(exactly), but it is also not [1-P(exact)]^n or *n. You need to compute P(exact) for all the x’s from x to n, then sum it.
Reperiet qui quaesiverit
CrystalMath
CrystalMath
  • Threads: 8
  • Posts: 1911
Joined: May 10, 2011
August 21st, 2019 at 6:58:14 AM permalink
I see, I wasn't dividing by the x! term.

Looking at this example:
Quote: MichaelBluejay



n! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2
x! = 7 x 6 x 5 x 4 x 3 x 2
(n-x)! = 3 x 2



We can simplify this to 10 x 9 x 8/(3 x 2 x 1). So, the loop should just do three steps.

I've modified my code below.


double binomDist(int x, int n, double p)
{
double q = 1.0-p;

double numerator = n;
double result = 1.0;
for( int i=x; i>=1; i-- ) result *= numerator--/i;

result*=Math.pow(p,x);
result*=Math.pow(q,n-x);
return result;
}


If you want "at least," you will need to loop through all possibilities and sum them.

This gives the cumulative distribution from 0 hits up to x. If you want x or greater, then calculate 1 - cumulativeBinomDist(x-1,n,p).


double cumulativeBinomDist(int x, int n, double p)
{
double result = 0.0;
for(int i = 0; i <= x; i++)
{
result += binomDist(i, n, p);
}
return result;
}
I heart Crystal Math.
kubikulann
kubikulann
  • Threads: 27
  • Posts: 905
Joined: Jun 28, 2011
Thanked by
CrystalMath
August 21st, 2019 at 7:06:57 AM permalink
...?
The problem with blocking members is that you end up posting redundant info!

I sometimes feel nobody reads my posts.
Reperiet qui quaesiverit
7craps
7craps
  • Threads: 18
  • Posts: 1977
Joined: Jan 23, 2010
August 21st, 2019 at 8:41:26 AM permalink
for a working example of a calculator that uses javascript online for binomial probabilities, I like this one.
https://www.di-mgt.com.au/binomial-calculator.html

It gives lots of info at one time (shows a table) and has those funny to see negative values that who knows how accurate it is.
It only accepts a decimal value for p (frustrating for many)
where many other javascript sites accept a decimal or fraction.

this one has been around for a long time and prefers a fraction over a decimal for p.
http://vassarstats.net/binomialX.html
winsome johnny (not Win some johnny)
MichaelBluejay
MichaelBluejay
  • Threads: 87
  • Posts: 1706
Joined: Sep 17, 2010
Thanked by
kubikulann
August 21st, 2019 at 3:07:59 PM permalink
Quote: kubikulann

...?
The problem with blocking members is that you end up posting redundant info!

I sometimes feel nobody reads my posts.

kubikulann, I haven't blocked you, I just either don't understand what you're saying, or it doesn't seem to apply to my questions. I do appreciate your trying to help.
I run Easy Vegas ( https://easy.vegas )
gordonm888
Administrator
gordonm888
  • Threads: 61
  • Posts: 5375
Joined: Feb 18, 2015
Thanked by
kubikulann
August 21st, 2019 at 7:05:08 PM permalink
Quote: kubikulann

...?
The problem with blocking members is that you end up posting redundant info!

I sometimes feel nobody reads my posts.



I read all your posts. You are an excellent addition to our forum!
So many better men, a few of them friends, are dead. And a thousand thousand slimy things live on, and so do I.
kubikulann
kubikulann
  • Threads: 27
  • Posts: 905
Joined: Jun 28, 2011
Thanked by
Boz
August 21st, 2019 at 7:51:20 PM permalink
Quote: MichaelBluejay

kubikulann, I haven't blocked you, I just either don't understand what you're saying, or it doesn't seem to apply to my questions. I do appreciate your trying to help.

I said that the combination of x among n is equal to the combination of (n-x) among n.

n! / x! n-x! = n!/ n-x! x!

So to reduce size you can choose to simplify n! with the largest of x! or n-x!
Reperiet qui quaesiverit
7craps
7craps
  • Threads: 18
  • Posts: 1977
Joined: Jan 23, 2010
August 21st, 2019 at 8:42:46 PM permalink
Quote: kubikulann

I said that the combination of x among n is equal to the combination of (n-x) among n.

n=37;
k=35;
binomial(n,k) == binomial(n,n-k)
binomial(37,35) == binomial(37,2) ?

https://www.wolframalpha.com/input/?i=binomial%2837%2C35%29

https://www.wolframalpha.com/input/?i=binomial%2837%2C2%29
winsome johnny (not Win some johnny)
kubikulann
kubikulann
  • Threads: 27
  • Posts: 905
Joined: Jun 28, 2011
Thanked by
Boz
August 22nd, 2019 at 6:23:25 AM permalink
Quote: 7craps

n=37;
k=35;

binomial(37,35) == binomial(37,2) ?

No !
combin(37,35)==combin(37,2)
but binomial is combin*p35q2< > combin*p2q35
Reperiet qui quaesiverit
7craps
7craps
  • Threads: 18
  • Posts: 1977
Joined: Jan 23, 2010
August 22nd, 2019 at 6:38:12 AM permalink
Quote: kubikulann

No !
combin(37,35)==combin(37,2)
but binomial is combin*p35q2< > combin*p2q35

combin(37,35)==binomial(37,2)
Looks like you are NOT the expert you think you are.
that is so sad, HUMAN!

binomial(n,k) is a function used for combinations in many languages
Wolfram Alpha and pari/gp use it, just a couple that come to mind.

gp > binomial(37,2)
%1 = 666

that you for sharing you FALSE opinion on the subject
winsome johnny (not Win some johnny)
kubikulann
kubikulann
  • Threads: 27
  • Posts: 905
Joined: Jun 28, 2011
August 22nd, 2019 at 6:46:10 AM permalink
7craps, go to bed and drain that alcohol from your brain, then come back to talk sense. (If you can, that is...)

What should you make of a subhuman calling you human? I know, they suffer from their impropriety, they need to lash out at people who are not as limited as they are. They can’t stand their inferiority, so they blame the normal people for being relatively superior to them.
On the other hand, if they call themselves ‘crap’...

Oignez vilain, il vous poindra.
Poignez vilain, il vous oindra.

Don’t give pearls to pigs; they’ll bite you.

Some people are plain stupid.
Others are just mean.
But being mean AND stupid labels you for the dustbins of society.
Last edited by: kubikulann on Aug 22, 2019
Reperiet qui quaesiverit
7craps
7craps
  • Threads: 18
  • Posts: 1977
Joined: Jan 23, 2010
August 22nd, 2019 at 7:17:43 AM permalink
Quote: kubikulann

Yes I do HATE such kind of FUCKING MORON as 7craps is.

OH.
imo, YOU ARE THE EXPERT WAY MORE THAN WOLFRAM ALPHA IS.
GREAT! that is SUPER DUPER

Wolfram Language code:
Binomial[37, 2]
Result: 666

LOL

you just got more to learn, in my opinion
I like to learn, so thanks for sharing your FALSE information
nice to know what is NOT correct.
winsome johnny (not Win some johnny)
kubikulann
kubikulann
  • Threads: 27
  • Posts: 905
Joined: Jun 28, 2011
Thanked by
Boz
August 22nd, 2019 at 7:22:19 AM permalink
Try binomial[37,35], mo… 7craps.

(Note: if I call someone else « 7craps », is it considered a personal insult?)
Reperiet qui quaesiverit
OnceDear
OnceDear
  • Threads: 64
  • Posts: 7543
Joined: Jun 1, 2014
Thanked by
bobbartop
August 22nd, 2019 at 7:43:50 AM permalink
Quote: kubikulann

Try binomial[37,35], mo… 7craps.

(Note: if I call someone else « 7craps », is it considered a personal insult?)

No. But your previous post was undoubtedly an insult.
See ya later.
Psalm 25:16 Turn to me and be gracious to me, for I am lonely and afflicted. Proverbs 18:2 A fool finds no satisfaction in trying to understand, for he would rather express his own opinion.
MichaelBluejay
MichaelBluejay
  • Threads: 87
  • Posts: 1706
Joined: Sep 17, 2010
August 22nd, 2019 at 4:31:57 PM permalink
Quote: kubikulann

What should you make of a subhuman calling you human? I know, they suffer from their impropriety, they need to lash out at people who are not as limited as they are. They can’t stand their inferiority...But being mean AND stupid labels you for the dustbins of society.

kubikulann blocked.
I run Easy Vegas ( https://easy.vegas )
Boz
Boz
  • Threads: 155
  • Posts: 5701
Joined: Sep 22, 2011
Thanked by
rawtuff
August 22nd, 2019 at 5:38:00 PM permalink
Quote: MichaelBluejay

kubikulann blocked.



Why do you and others have to publicly announce you “Blocked” someone? Are you proud? Does ANYONE care? Just asking.

Odds are you have me blocked so you won’t see this.

But it appears to me you came back to AGAIN to push a money making venture on your end. Like Norm, you come back again and again when you have an agenda. Face it, you are a capitalist, like most of us.

You may hide under the Tiny House living, bike riding “environmentalist” label. But at the end of the day you still use this site to push your business ventures.

While I legally burn $13.99 cases of foam takeout containers in your name every “Earth Day”, you still push your agenda. I just wish I knew what it was.

Either way, I’ll keep flying extra segments across the country. Because it’s saves me money, gets me more miles and better status. If it upsets hippies, that’s a bonus.

And if anything I posted “triggers” you, I’m sure you will report it to The Wizard and demand a suspension or apology like you did when I claimed you were a closeted Trump supporter. Obviously you thought it may hurt the “business” you have going.

PS, sorry about your upbringing, no one should ever have to go through what you did Mr BJ.
MichaelBluejay
MichaelBluejay
  • Threads: 87
  • Posts: 1706
Joined: Sep 17, 2010
August 23rd, 2019 at 2:17:20 AM permalink
Thank you very much to everyone who helped with the math. Between that and my self-study, I now have a much better understanding of combinations, permutations, the binomial coefficient, canceling factors to avoid out-of-bounds errors, and binomial probabilities.

I just released the point of all this, my Roulette Calculator, which is in beta, since there's a good chance there's something that doesn't work right (if not the math, then the interface). So, if you like, bang on it and see if you can break it.
I run Easy Vegas ( https://easy.vegas )
Boz
Boz
  • Threads: 155
  • Posts: 5701
Joined: Sep 22, 2011
Thanked by
RS
August 25th, 2019 at 3:34:25 PM permalink
Quote: MichaelBluejay

Thank you very much to everyone who helped with the math. Between that and my self-study, I now have a much better understanding of combinations, permutations, the binomial coefficient, canceling factors to avoid out-of-bounds errors, and binomial probabilities.

I just released the point of all this, my Roulette Calculator, which is in beta, since there's a good chance there's something that doesn't work right (if not the math, then the interface). So, if you like, bang on it and see if you can break it.



Of course you did Bovada shill.

Good gimmick you have going with them.

They pay you, you occasionally act like you don’t agree with them, then justify it with the “negative” feedback.

Honestly, pure respect for your money making skills.

Using your intellectual ability to never work an regular job, and live like a King, pushing your superiority.

Sounds a lot like people you hate.

You and Trump have more in common then you believe.
  • Jump to: