Bernoulli hit probability by given sum of probabilities

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html)
+--- Forum: HP Prime Software Library (https://www.hpmuseum.org/forum/forum-15.html)
+--- Thread: Bernoulli hit probability by given sum of probabilities (/thread-23611.html)



Bernoulli hit probability by given sum of probabilities - peacecalc - 2025-05-25

Hello folks,

by written such program for the HP 32SII, I tried to write such a program in HP-PPL for the prime. I use the prime G2 with the last beta version.
The program solve the following equation:

\[  \sum_{i=0}^{k} B(n,p,i) = w \]

With given n, k, and w, the unknown variable is the hit probability p.
In algebraic mode you have to input f. i. BERNOULLI_P(50,12,09373) = 0,16667. The good old HP 32SII needs for this 1 minute and 5 seconds. The Prime shows you the result immediatly (although the prim program corrected p until +/- 0,5^20 ~ 1E-6 the HP 32II only until +/- 0,5^14 ~ 6E-5). For great values n it is better to use the functions for the normal distribution. The program uses the program BINOMIAL_CDF, which it is a very astonishing funktion, because it is a very fast funktion. You get the results immediatly, even when n is large.

  Bernoulli_P.txt (Size: 502 bytes)

RE: Bernoulli hit probability by given sum of probabilities - raprism - 2025-05-28

(2025-05-25, 15:09)peacecalc Wrote: Hello folks,

by written such program for the HP 32SII, I tried to write such a program in HP-PPL for the prime. I use the prime G2 with the last beta version.
The program solve the following equation:

\[  \sum_{i=0}^{k} B(n,p,i) = w \]

With given n, k, and w, the unknown variable is the hit probability p.
In algebraic mode you have to input f. i. BERNOULLI_P(50,12,09373) = 0,16667. The good old HP 32SII needs for this 1 minute and 5 seconds. The Prime shows you the result immediatly (although the prim program corrected p until +/- 0,5^20 ~ 1E-6 the HP 32II only until +/- 0,5^14 ~ 6E-5). For great values n it is better to use the functions for the normal distribution. The program uses the program BINOMIAL_CDF, which it is a very astonishing funktion, because it is a very fast funktion. You get the results immediatly, even when n is large.

As I don't (yet) write PPL programmes, but was interested in your program, I had the idea of writing it in Python. Thereafter it was only a little step to try this code also on HP Prime. So that's a Python version:

Code:
#PYTHON EXPORT BERNOULLI_P()

import sys
from math import log10

from hpprime import eval as hpeval
def cdf(k, n, p):
    return hpeval("BINOMIAL_CDF({},{},{})".format(n, p, k))

max_step = 20
eps = 1e-6

def bernoulli_p(n, k, w, max_step=max_step, eps=eps, verbose=False):
    p = p0 = 0.5
    fround = -int(log10(eps)) if eps<.01 else 3
    for i in range(max_step):
         res = cdf(k, n, p)
         if verbose:
             print('({:2n}) p = {}\tres = {}'.format(i, *[round(f, fround) for f in (p, res)]))
         if abs(res-w)<eps:
             return p
         p += (-1)**(res<w) * p0**(i+2)
    return p

print('bernoulli_p(50, 12, 0.9373244) = ', bernoulli_p(50, 12, 0.9373244))

Here is bit longer variant that can be called also on PC (using scipy):


  bernoulli_p.py (Size: 1490 bytes)