25
$\begingroup$

Let $S_i \in \{0,1\}$, $i=1,\dots,N$ be $N$ independent random binary variables, each taking the value 1 with probability $0 \le p_i \le 1$ (and the value 0 with probability $1-p_i$).

I am interested in the sum,

$$X = \sum_{i=1}^N S_i$$

Because the $p_i$ can be different, the distribution of $X$ can be complicated (https://en.wikipedia.org/wiki/Poisson_binomial_distribution).

I'm not interested in the full distribution of $X$, but only on the probability that $X$ is even (divisible by 2). Can this probability be computed in a simple (or efficient) manner?

$\endgroup$
7
  • 2
    $\begingroup$ It can be computed efficiently in the sense of an $O(N)$ calculation with low memory requirements. For example, with the product of $N$ transition matrices of the form $\begin{pmatrix} 1-p_i & p_i \\ p_i & 1-p_i \end{pmatrix}$ $\endgroup$
    – Henry
    Commented Jan 24 at 11:28
  • $\begingroup$ @Henry Could you expand? $\endgroup$
    – a06e
    Commented Jan 24 at 11:36
  • $\begingroup$ a06e: - you essentially get the same as @StephanKolassa's approach but in matrix form $\endgroup$
    – Henry
    Commented Jan 24 at 15:54
  • $\begingroup$ Are $S_i$ independent? $\endgroup$
    – JiK
    Commented Jan 26 at 9:52
  • $\begingroup$ @JiK Yes ,they are independent $\endgroup$
    – a06e
    Commented Jan 26 at 12:24

4 Answers 4

18
$\begingroup$

You should be able to compute this quickly through a very simple dynamic programming approach.

Let $q_i$ denote the probability that $S_1+\dots+S_i$ is even.

Then $q_1=1-p_1$.

Going from $i-1$ to $i$, you either have $S_1+\dots+S_{i-1}$ odd with probability $1-q_{i-1}$, then $S_1+\dots+S_i$ will be even if $S_i=1$, for a total probability of $(1-q_{i-1})p_i$, or the other way around. Overall,

$$ q_i = (1-q_{i-1})p_i + q_{i-1}(1-p_i). $$

Just calculate $q_N$ by iterating over $i$, and there you are.

$\endgroup$
7
  • $\begingroup$ Thanks! That looks nice. Any chance this recurrence has a closed-form solution? $\endgroup$
    – a06e
    Commented Jan 24 at 11:37
  • $\begingroup$ None that I see immediately... it might be enlightening to write this out for small $N$ and try to spot a pattern. Incidentally, Henry really gave the exact same solution, just using simple Markov Chain terminology. $\endgroup$ Commented Jan 24 at 12:19
  • 3
    $\begingroup$ @a06e: It would indeed be a very poor decision to use that formula to calcuate $q_N$, when Stephan's recurrence relation is so easy to implement! $\endgroup$
    – TonyK
    Commented Jan 24 at 19:46
  • 2
    $\begingroup$ To expand on this slightly: the operation $p\cdot q = pq + (1-p)(1-q)$ that turns the probabilities that independent variables are even into the probability that their sum is even forms a monoid, with identity $1$. ("forms a monoid" means $p\cdot(q\cdot r)=(p\cdot q)\cdot r$ and $1\cdot p=p\cdot 1=p$.) This means you can combine even-ness probabilities in any order without changing the result, and in particular could choose to parallelize by summarizing non-overlapping collections of the variables in each thread. $\endgroup$ Commented Jan 24 at 21:39
  • 1
    $\begingroup$ @Daniel I posted the vectorized solution in a comment to my answer ;-). $\endgroup$
    – whuber
    Commented Jan 25 at 0:10
24
$\begingroup$

There's a nice approach that uses generating functions. If $G(z) = \mathbf{E} z^X$ is the probability generating function of a non-negative random variable $X$, then the probability that $X$ is even is $(1 + G(-1))/2$, as per this discussion: https://math.stackexchange.com/questions/1634021/even-value-in-a-r-v-using-generating-functions.

But we have that \begin{align*} G(z) = \mathbf{E} z^{\sum_{i=1}^N S_i} = \prod_{i=1}^N \mathbf{E} z^{S_i} = \prod_{i=1}^N (p_i z + (1 - p_i)), \end{align*} from which we can calculate \begin{align*} \mathbf{P}(X \text{ is even}) = \frac{1}{2}(1 + G(-1)) = \frac{1}{2}\Bigl(1 + \prod_{i=1}^N (1 - 2p_i)\Bigr). \end{align*}

A fun observation from the above is that if even one of the $p_i$ are equal to $1/2$, then the probability of $X$ being even is $1/2$, regardless of the other probabilities. This is because $S_i$ changing from $0$ to $1$ (or $1$ to $0$) switches the parity of the whole sum, and that that switch happens with probability $1/2$ independent of the rest of the sum.

$\endgroup$
1
  • 4
    $\begingroup$ +1. This approach generalizes to sums modulo $3,$ $4,$ etc. using the power series "decimation" I describe at stats.stackexchange.com/a/35138/919. $\endgroup$
    – whuber
    Commented Jan 25 at 0:15
22
$\begingroup$

There is an elementary solution requiring almost no work to derive.


(I am going to switch the notation because "$S$" is such a handy mnemonic for a sum.)

Let $f_k$ be the chance that the sum $S_k = X_1+X_2+\cdots+X_k$ is even. The event "$S_{k+1}$ is even" can be expressed as the disjoint union of "$S_k$ is odd and $X_{k+1}$ is odd" and "$S_k$ is even and $X_{k+1}$ is even." Thus, their probabilities add; and since $X_{k+1}$ is assumed independent of $S_k,$ the chances of these two component events are computed via multiplication to obtain

$$f_{k+1} = (1 - f_k)p_{k+1} + f_k(1 - p_{k+1}) = p_{k+1} + (1 - 2p_{k+1})f_k.$$

This is the relation reported by Stephan Kolassa elsewhere in this thread. It is a first-order recursion beginning with $f_0 = 1$ (an empty sum is defined to be zero, which is certainly even).

There are many ways to solve it. One of the simplest is to write $q_k = 1 - 2p_k$ and express the recursion in terms of $a_k = 2f_k - 1$ as

$$a_{k+1} = q_{k+1}a_k;\quad a_0 = 2(1) - 1 = 1.$$

This series obviously has the unique solution $a_k = (1)(q_1)(q_2)\cdots(q_k),$ whence (returning to the original notation in terms of $f_k$ and $p_k$)

$$f_k = (1 + a_k)/2 = \frac{1}{2}\left(1 + \prod_{i=1}^k (1 - 2p_i)\right),$$

exactly the formula beautifully obtained by Damian Pavlyshyn elsewhere in this thread.

$\endgroup$
10
  • 7
    $\begingroup$ R afficionados will appreciate that with $(p_1,p_2,\ldots,p_N)$ stored in a vector p, the answer can be found as f <- (1 + cumprod(1 - 2*p)) / 2. $\endgroup$
    – whuber
    Commented Jan 25 at 0:04
  • 1
    $\begingroup$ The end formula looks very simple. One gets the feeling that there should be some intuitive explanation for it. $\endgroup$
    – a06e
    Commented Jan 25 at 12:53
  • $\begingroup$ I agree, but I haven't found one. The $q_i$ are the expected gains on a bet that wins \$1 for an odd result ($X_i=1$) and loses \$1 for an even result. But why should they multiply? I'm not too discouraged by this, because an intuition seems even less forthcoming for the natural generalizations to computing the chances that $S_k = j$ modulo $m$ for $j=0,1,\ldots,m-1$ and $m=\{2,3,4,\ldots\}.$ $\endgroup$
    – whuber
    Commented Jan 25 at 14:19
  • $\begingroup$ Since $q_i = 1 - 2p_i = (+1)(1-p_i) + (-1)p_i$ isn't the loss on an odd $X_i$ and the gain on an even $X_i$? One of the confusing things here (and it may well be me that's the confused one...) is that $f_k$ is the probability for the k-th sum to be even, whereas $p_k$ is the probability for the k-th term of the sum to be odd $\endgroup$
    – Silverfish
    Commented Jan 26 at 2:30
  • 1
    $\begingroup$ The thing that struck me about the betting analogy is that in $a_k = \prod_{i=1}^k q_i$, we have $a_k = 2f_k - 1 = (+1)f_k + (-1)(1-f_k)$, which (if I've not confused myself) is the expected gain on a bet that wins a dollar for an even sum, and loses a dollar for an odd sum. So it's curious this should be the product of the expected gains on the bets for the parity of the individual terms! $\endgroup$
    – Silverfish
    Commented Jan 26 at 2:37
10
$\begingroup$

Here is a solution to the generalised problem using other modulo

The other answers here are wonderful, but I can't help but scratch the itch to generalise your problem. Consider the generalised problem where we have a sequence $X_1,X_2,X_3,...$ with independent binary values having $X_i \sim \text{Bern}(p_i)$. Now define the modular-remainders of the sums of interest as:

$$M_{n,k} \equiv\bigg( \sum_{i=1}^n X_i \bigg) \text{ mod } k$$

for $n=1,2,3,...$ and $k=2,3,4,...$. We want to compute the probability distribution for this statistic, with probabilities denoted by:

$$\psi_{n,k}(m) \equiv \mathbb{P}(M_{n,k} = m) \quad \quad \quad \quad \quad \text{for } m =0,...,k-1.$$

Your question is seeking the probability $\psi_{n,2}(0)$, which occurs in the case $k=2$ where the value $M_{n,2}$ is an indicator that the sum of the first $n$ values in the sequence is even.


Recursive computation of the probability of interest: In this general case we can use an extension of the recursive algorithm given in the wonderful answer by Stephen Kolassa. To do this, we can use the base equation:

$$\psi_{0,k}(m) = \mathbb{I}(m=0).$$

and the recursive equation:

$$\psi_{n+1,k}(m) = (1-p_{n+1}) \cdot \psi_{n,k}(m) + p_{n+1} \cdot \psi_{n,k} ((m-1) \text{ mod } k).$$

You can find a similar generalisation relating to an associated problem in this related answer. It is possible to obtain a closed-form solution for the probability function of interest (as opposed to a recursive formula) using the method described in the linked question. Here we will proceed using the recursive computation, which allows efficient computation of matrices of probabilities in cases where we are interested in all sample sizes up to some upper bound.


Implementation: We can implement this recursive method in R using a function that takes n, k and p as inputs and generating the vector of probabilities over all relevant values of $m$. (Note: In the function below, for the input p we can take this as either a vector of probabilities that is at least $n$ elements long, or a function that generates a probability for any positive integer input to allow a full sequence of probabilities.) The function defaults to the binary case that is of interest to you but it allows different values of k to be set by the user.

modular.prob <- function(n, p, k=2, keep.all = FALSE) {
  
  #Check inputs n and k
  if (!is.numeric(n))      stop('Error: Input n should be a number')
  if (!is.numeric(k))      stop('Error: Input k should be a number')
  if (length(n) != 1)      stop('Error: Input n should be a single number')
  if (length(k) != 1)      stop('Error: Input k should be a single number')
  if (n != as.integer(n))  stop('Error: Input n should be an integer')
  if (k != as.integer(k))  stop('Error: Input k should be an integer')
  if (min(n < 0))          stop('Error: Input n should be a non-negative integer')
  if (min(k < 1))          stop('Error: Input n should be a positive integer')
  
  #Check input p
  if (is.function(p)) {
    if (length(formals(p)) != 1) {
      stop('Error: If input p is a function, it should only have a single argument') }
    pp <- rep(0, n)
    for (i in 1:n) { pp[i] <- p(i) }
  } else {
    if (!is.numeric(p))    stop('Error: Input p should be a numeric vector')
    if (length(p) < n)     stop('Error: Input p should have at least n elements')
    pp <- p[1:n]
    if (min(pp) < 0)       stop('Error: Input p should have values between zero and one')
    if (max(pp) > 1)       stop('Error: Input p should have values between zero and one') }
  
  #Check input keep.all
  if (!is.logical(keep.all)) stop('Error: Input keep.all should be a logical value')
  if (length(keep.all) != 1) stop('Error: Input keep.all should be a single logical value')
  
  #Set probability matrix
  PROBS <- matrix(0, nrow = n+1, ncol = k)
  rownames(PROBS) <- sprintf('n[%s]', 0:n)
  colnames(PROBS) <- sprintf('m[%s]', 1:k-1)
  if (k == 2) { colnames(PROBS) <- c('Even', 'Odd') }
  
  #Compute probabilities
  #Trivial case where k = 1 is treated without recursion
  PROBS[1, 1] <- 1
  if (k == 1) { PROBS[, 1] <- 1 }
  if (k > 1) { 
  for (i in 1:n) {
    PP <- PROBS[i, ]
    PROBS[i+1, ] <- (1-pp[i])*PP + pp[i]*PP[c(k, 1:(k-1))] } }
   
   #Give output
   if (keep.all) { PROBS } else { PROBS[n+1, ] } }

Here is an example implementing the function with a specified probability vector with $n=20$ elements. We first compute the probabilities for even/odd sums using $k=2$ and we then compute the probabilities for modular remainders with $k=3$. In each case we get a matrix of probabilities for each trial and each modular remainder.

#Set probability vector (with twenty elements)
p <- c(0.1, 0.2, 0.3, 0.2, 0.1, 0.6, 0.2, 0.2, 0.5, 0.1, 
       0.9, 0.2, 0.2, 0.1, 0.4, 0.5, 0.6, 0.4, 0.2, 0.8)

#Compute probabilities of even/odd sums
modular.prob(n = 20, p = p, keep.all = TRUE)

           Even       Odd
n[0]  1.0000000 0.0000000
n[1]  0.9000000 0.1000000
n[2]  0.7400000 0.2600000
n[3]  0.5960000 0.4040000
n[4]  0.5576000 0.4424000
n[5]  0.5460800 0.4539200
n[6]  0.4907840 0.5092160
n[7]  0.4944704 0.5055296
n[8]  0.4966822 0.5033178
n[9]  0.5000000 0.5000000
n[10] 0.5000000 0.5000000
n[11] 0.5000000 0.5000000
n[12] 0.5000000 0.5000000
n[13] 0.5000000 0.5000000
n[14] 0.5000000 0.5000000
n[15] 0.5000000 0.5000000
n[16] 0.5000000 0.5000000
n[17] 0.5000000 0.5000000
n[18] 0.5000000 0.5000000
n[19] 0.5000000 0.5000000
n[20] 0.5000000 0.5000000

#Compute probabilities for ternary modular remainders
modular.prob(n = 20, p = p, k = 3, keep.all = TRUE)

           m[0]      m[1]      m[2]
n[0]  1.0000000 0.0000000 0.0000000
n[1]  0.9000000 0.1000000 0.0000000
n[2]  0.7200000 0.2600000 0.0200000
n[3]  0.5100000 0.3980000 0.0920000
n[4]  0.4264000 0.4204000 0.1532000
n[5]  0.3990800 0.4210000 0.1799200
n[6]  0.2675840 0.4078480 0.3245680
n[7]  0.2789808 0.3797952 0.3412240
n[8]  0.2914294 0.3596323 0.3489382
n[9]  0.3201838 0.3255309 0.3542853
n[10] 0.3235940 0.3249962 0.3514098
n[11] 0.3486283 0.3237342 0.3276375
n[12] 0.3444301 0.3287130 0.3268569
n[13] 0.3409155 0.3318564 0.3272281
n[14] 0.3395467 0.3327623 0.3276909
n[15] 0.3348044 0.3354761 0.3297195
n[16] 0.3322620 0.3351403 0.3325978
n[17] 0.3324635 0.3334133 0.3341233
n[18] 0.3331274 0.3330333 0.3338393
n[19] 0.3332698 0.3330522 0.3336781
n[20] 0.3335964 0.3332262 0.3331773

As exhibited in the outputs above, typically we will see that $M_{n,k}$ converges in distribution to the uniform distribution as $n \rightarrow \infty$. This can be proved formally using the CLT - it occurs due to the fact that the standardised sum of the binary values converges in distribution to a continuous distribution (the normal distribution) and so the modular-remainder over shorter and shorter intervals converges to uniform probabilities.

$\endgroup$
4
  • 1
    $\begingroup$ As I commented elsewhere in this thread at stats.stackexchange.com/questions/637645/…, there is a closed formula for this generalization that is obtained in the same way described by Damian Pavlyshyn. What makes such formulas superior to an algorithm is how they provide insight into the general behavior of the result, as Pavlyshyn illustrates in some commentary at the end of their answer. $\endgroup$
    – whuber
    Commented Jan 25 at 14:21
  • 1
    $\begingroup$ @whuber: Very nice (and +1 to linked answer). I've edited the present answer to note the linked answer. $\endgroup$
    – Ben
    Commented Jan 26 at 2:26
  • $\begingroup$ @whuber is the closed form solution for different modulis as simple as the product formulation $P(even|n) = 0.5 - \prod_{i=1}^n (p_i-0.5)$? $\endgroup$ Commented Jan 26 at 15:05
  • $\begingroup$ @SextusEmpiricus In Pavlyshyn's approach, I believe it would be a linear combination of products: one product for each possible residue. $\endgroup$
    – whuber
    Commented Jan 26 at 19:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.