A wire flips every bit it carries with probability p, independently of the other bits. To send one bit safely, the sender repeats it: the bit goes down the wire n times (n odd), and the receiver keeps whichever value arrived more often. The vote is wrong exactly when more than half of the n copies were flipped.
Write majority_failure(n, p) that returns the exact probability that the majority vote gives the wrong bit. The number of flipped copies follows the binomial distribution: exactly k of n copies flip with probability
where \binom{n}{k} is math.comb(n, k). For n = 3 the answer works out to 3p^2 - 2p^3, which is smaller than p whenever p < 1/2. Press Run with ps = [i / 50 for i in range(51)] and plot(ps, [majority_failure(5, p) for p in ps]) to see the curve cross the line y = p at p = 1/2.
Examples
Input: n = 3, p = 0.1
Output: 0.028
Explanation: two flips: 3 * 0.1**2 * 0.9 = 0.027; three flips: 0.1**3 = 0.001.
Input: n = 1, p = 0.25
Output: 0.25
Explanation: one copy, no vote: the bit is wrong when it flips.
Input: n = 5, p = 0.5
Output: 0.5
Answers are compared with a tolerance of 1e-6, so return the exact value, not a simulated estimate.
Goals
- Compute the exact probability that a majority vote over $n$ noisy copies is wrong
- See when repetition helps ($p < 1/2$) and when it hurts ($p > 1/2$)