Can the binomial distribution be solved for k

binomial distributionclosed-form

I am working on a problem where, given the binomial probability density function:
$$X(p,n,k) = \left(\frac{n!}{k!(n-k)!}\right)p^k(1-p)^\left(n-k\right)$$
I need a function $Y(p,n,x) = k$ where $p, n$ and $k$ carry the same meanings as the parameters in the binomial distribution, and $x$ is a random number $\in [0,1]$ chosen via an arbitrary random process.

The purpose of $Y$ is to allow for the random generation of integers, $k \in [0,n]$, for a given value of $p$ and $n$, via the random parameter $x$, such that the likelihood of generating k is exactly $X$. I need a closed form for $Y$ or an approximation method that is not stochastic. There are functions in the C++ standard library that claim to provide this functionality but are somehow internally stochastic and inconsistent across hardware/operating system changes are therefore unusable to me.

I've seen a similar question posed in this stack exchange page but it did not provide what I am looking for.

Is such a function $Y$ even possible? In attempting to solve it myself I came across this stack exchange page on calculating the inverse of $n!$ using the Lambert W function, which I will need some time to fully understand before I can comfortable use in my work. An additional issue with the $W$ function is that I would eventually need an algorithmic way to represent $W$ in a computer program.

Best Answer

More than a full answer, this is a first step in looking for a solution.

P_Inv_Bin_1

Consider a function $f(k)$ such that $$ \Delta \,f(k) = \Delta \,x = \left( \matrix{ n \cr k \cr} \right)p^{\,k} \left( {1 - p} \right)^{\,n - k} $$

That is precisely the Cumulative of the binomial distribution you give $$ C_X (k\,;\,n,p) = \sum\limits_{j = 0}^{k - 1} {\binom{n}{j} p^{\,j} \left( {1 - p} \right)^{\,n - j} } $$

We shall make it continuous in $k$, using
for the sum the Indefinite Sum, i.e.
$$ \eqalign{ & \Delta F(x) = F(x + 1) - F(x) = f(x)\quad \Rightarrow \cr & \Rightarrow \quad F(x) = \Delta ^{\, - 1} f(x) = \sum\nolimits_{\;j = 0}^{\;x} {f(j)} \cr} $$

So we write $$ C_X (k\,;\,n,p) = \left( {1 - p} \right)^{\,n} \sum\nolimits_{\;j = 0}^{\;k} {\binom{n}{j}\left( {{p \over {1 - p}}} \right)^{\,j} } $$

The floor of its functional inverse (in $k$) is then the function you are looking for.

But unfortunately there is no close expression available for the partial sum on the lower index of a binomial.

Related Question