HP Forums
Bug in BINOMIAL_ICDF - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Bug in BINOMIAL_ICDF (/thread-4965.html)



Bug in BINOMIAL_ICDF - Arno K - 10-18-2015 06:23 PM

On writing a program for the kind of hypothesis test that are usual in Germany I discovered the following bug: BINOMIAL_ICDF() does not provide the result below the given probability, instead it results to the nearest integer value:
BINOMIAL_ICDF(20,0.6,0.05) -> 8
BINOMIAL_CDF(20,0.6,8) -> 0.0565 which is too big.
The oncalc-help says:
Returns the number of successes, k, out of n trials, each with a probability of p, such that the probability of k or fewer successes is q.
So, in my opinion, it should return 7, not 8.
Arno


RE: Bug in BINOMIAL_ICDF - Dieter - 10-18-2015 08:46 PM

(10-18-2015 06:23 PM)Arno K Wrote:  On writing a program for the kind of hypothesis test that are usual in Germany I discovered the following bug: BINOMIAL_ICDF() does not provide the result below the given probability, instead it results to the nearest integer value:

It seems to provide the next higher integer, which IMHO is the usual way results of this kind are returned.

(10-18-2015 06:23 PM)Arno K Wrote:  BINOMIAL_ICDF(20,0.6,0.05) -> 8

That's what my own inverse Binomial program returns as well.

(10-18-2015 06:23 PM)Arno K Wrote:  BINOMIAL_CDF(20,0.6,8) -> 0.0565 which is too big.

Right. The continuous extension of the Binomial CDF (using the Beta function) would yield an exact result of 7,8652865... If an integer result is desired, the usual way (at least as I know it) is to round up to the next higher integer, i.e. ceil(x).

This way the CDF of the returned result k is greater or equal to the given probability: CDF(k) ≥ q.

(10-18-2015 06:23 PM)Arno K Wrote:  The oncalc-help says:
Returns the number of successes, k, out of n trials, each with a probability of p, such that the probability of k or fewer successes is q.

In other words: P(x≤k) = 0,05. Since there is no integer k that yields a probability of exactly 0.05, the calculator actually returns an integer result so that P(x≤k) ≥ 0,05. Which in my humble opinion is perfectly okay.

For instance Matlab's "binoinv" seems to work the same way, take a look here:
Quote:"X = binoinv(Y,N,P) returns the smallest integer X such that the binomial cdf evaluated at X is equal to or exceeds Y."

The (real) results (with n=162 and p=0,5) for q=0,05 and 0,95 are ~70,04 and 90,96, which Matlab returns as 71 and 91.

(10-18-2015 06:23 PM)Arno K Wrote:  So, in my opinion, it should return 7, not 8.

This (k=7) is the largest integer for which P(x≤k) ≤ 0,05. Which may also be a valid solution, but I think it's not a common one.

That's why my 34s program for the Binomial (and Poisson) distribution optionally returns real results (if desired by the user). If you know that the "exact" value is 7,865... you can decide on your own if you want to round up or down. ;-)

Dieter


RE: Bug in BINOMIAL_ICDF - Tim Wessman - 10-18-2015 08:50 PM

(10-18-2015 06:23 PM)Arno K Wrote:  On writing a program for the kind of hypothesis test that are usual in Germany I discovered the following bug: BINOMIAL_ICDF() does not provide the result below the given probability, instead it results to the nearest integer value:
BINOMIAL_ICDF(20,0.6,0.05) -> 8
BINOMIAL_CDF(20,0.6,8) -> 0.0565 which is too big.
The oncalc-help says:
Returns the number of successes, k, out of n trials, each with a probability of p, such that the probability of k or fewer successes is q.
So, in my opinion, it should return 7, not 8.
Arno

So help me understand your thinking here. I'm sick today and not thinking well so chances are I am just not understanding right.

You have:

BINOMIAL_CDF(20,0.6,7) -> ~.0210
BINOMIAL_CDF(20,0.6,8) -> ~.0565

The target is .05. There only seems to be one possible result there that works to me. Note that every math program I've tried here agrees it should be 8 as well.

Why then would the inverse not hold true? 7 would be too small. 8 contains your desired target.

Are you advocating a non-integer result? How would that be definted since we are talking about trials here?


RE: Bug in BINOMIAL_ICDF - lrdheat - 10-18-2015 08:53 PM

I know that I should do the math, but a quick check with my CASIO 9860gii comes up with the same answers for CDF and ICDF as the Prime. I have a problem with the Prime handling of the binomial distribution for large "n" such as n>10^7, but for smaller "n", the Prime seems good.


RE: Bug in BINOMIAL_ICDF - Dieter - 10-18-2015 09:05 PM

(10-18-2015 08:50 PM)Tim Wessman Wrote:  The target is .05. There only seems to be one possible result there that works to me. Note that every math program I've tried here agrees it should be 8 as well.
Why then would the inverse not hold true? 7 would be too small. 8 contains your desired target.

I'd agree here.

(10-18-2015 08:50 PM)Tim Wessman Wrote:  Are you advocating a non-integer result? How would that be definted since we are talking about trials here?

The two major discrete distributions (Binomial and Poisson) can be expressed in terms of Beta resp. Gamma functions. This way they can be extended to the real domain (think Gamma and factorial) and non-integer results become possible. In this particular case the real result is 7,86528650471. But I haven't seem this kind of result on a commercially available calculator yet. ;-)

Dieter


RE: Bug in BINOMIAL_ICDF - Arno K - 10-18-2015 09:15 PM

(10-18-2015 08:50 PM)Tim Wessman Wrote:  You have:

BINOMIAL_CDF(20,0.6,7) -> ~.0210
BINOMIAL_CDF(20,0.6,8) -> ~.0565

The target is .05. There only seems to be one possible result there that works to me. Note that every math program I've tried here agrees it should be 8 as well.

Why then would the inverse not hold true? 7 would be too small. 8 contains your desired target.

Are you advocating a non-integer result? How would that be definted since we are talking about trials here?

Well, I do not use a lot of other mathematics programs and did not check the ones I have, I wrote a program to provide the integer with P(x<=k)<=alpha which is one of the usual hypothesis-tests at least here in German schools, k may not be too big is the aim, and so I awaited BINOMIAL_ICDF to give the proper answer, 7, it was not very difficult to correct that as a simple -1 does the job. I was astonished when I found that P(X>=k)<=0.05 was correctly handled by the Prime, but I will have to check that with other examples.
Ok, when this behaviour is usual then I will not bother anymore.
Thank you
Arno


RE: Bug in BINOMIAL_ICDF - lrdheat - 10-18-2015 09:52 PM

Hi Dieter,

The WP 34S also has the same problem with large "n".

It returns a binomial CDF of less than .5 for n=100,000,000
K=50,000,000
P=.5

Returns ~.4887

For n=1EEX9, k successes=5EEX8, p of individual success=.5, the binomial CDF wanders all the way down to ~.1815.

For this reason, I feel that n>~1EEX7 should not be accepted in the WP 34S , the CASIO 9860gii, and on the Prime.


RE: Bug in BINOMIAL_ICDF - Dieter - 10-20-2015 06:48 PM

(10-18-2015 09:52 PM)lrdheat Wrote:  The WP 34S also has the same problem with large "n".
(...)
For this reason, I feel that n>~1EEX7 should not be accepted in the WP 34S , the CASIO 9860gii, and on the Prime.

Yes, there are some issues with the Binomial and Poisson distribution, probably caused by the underlying Incomplete Gamma and Beta functions, cf. for instance this thread. I hope that sooner or later there will be an update. Pauli and I have been discussing this some months ago. But it seems to take its time – Pauli is very busy, and I'm not sure if the cause of the Beta/Gamma problem has already been found.

Dieter