Thread Rating:
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.
Quote: MichaelBluejayMy 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.
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.
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
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.
@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!
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.
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.
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;
}
The problem with blocking members is that you end up posting redundant info!
I sometimes feel nobody reads my posts.
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
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.Quote: kubikulann...?
The problem with blocking members is that you end up posting redundant info!
I sometimes feel nobody reads my posts.
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!
I said that the combination of x among n is equal to the combination of (n-x) among n.Quote: MichaelBluejaykubikulann, 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.
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!
n=37;Quote: kubikulannI said that the combination of x among n is equal to the combination of (n-x) among n.
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
No !Quote: 7crapsn=37;
k=35;
binomial(37,35) == binomial(37,2) ?
combin(37,35)==combin(37,2)
but binomial is combin*p35q2< > combin*p2q35
combin(37,35)==binomial(37,2)Quote: kubikulannNo !
combin(37,35)==combin(37,2)
but binomial is combin*p35q2< > combin*p2q35
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
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.
OH.Quote: kubikulannYes I do HATE such kind of FUCKING MORON as 7craps is.
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.
(Note: if I call someone else « 7craps », is it considered a personal insult?)
No. But your previous post was undoubtedly an insult.Quote: kubikulannTry binomial[37,35], mo… 7craps.
(Note: if I call someone else « 7craps », is it considered a personal insult?)
See ya later.
kubikulann blocked.Quote: kubikulannWhat 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.
Quote: MichaelBluejaykubikulann 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.
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.
Quote: MichaelBluejayThank 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.