Loading problem...
In probability theory and statistics, one of the most fundamental distributions is the Binomial Distribution, which models the number of successes in a sequence of independent experiments. This distribution arises naturally when analyzing repeated trials where each trial has exactly two possible outcomes: success or failure.
Consider a scenario where you perform n independent Bernoulli trials, each with a constant probability p of success. A Bernoulli trial is a random experiment with exactly two possible outcomes, often labeled as "success" (with probability p) and "failure" (with probability 1-p). The key insight is that each trial is independent—the outcome of one trial does not influence any other trial.
The probability of observing exactly k successes out of n trials is given by the Binomial Probability Formula:
$$P(X = k) = \binom{n}{k} \cdot p^k \cdot (1-p)^{n-k}$$
Where:
Intuitive Understanding: Think of flipping a biased coin n times. The binomial coefficient counts how many different orderings of k heads (successes) and (n-k) tails (failures) are possible. Each specific ordering has probability (p^k \cdot (1-p)^{n-k}), and since these orderings are mutually exclusive, we multiply the count by this probability.
Your Task: Write a Python function that computes the probability of achieving exactly k successes in n independent Bernoulli trials, where each trial has a probability p of success. Return the result rounded to 5 decimal places.
n = 6, k = 2, p = 0.50.23438We want the probability of getting exactly 2 successes in 6 fair coin flips (50% success rate).
Step 1: Calculate the binomial coefficient C(6,2): C(6,2) = 6! / (2! × 4!) = 720 / (2 × 24) = 720 / 48 = 15
Step 2: Calculate p^k = (0.5)² = 0.25
Step 3: Calculate (1-p)^(n-k) = (0.5)⁴ = 0.0625
Step 4: Multiply all components: P(X = 2) = 15 × 0.25 × 0.0625 = 0.234375
Rounded to 5 decimal places: 0.23438
n = 4, k = 2, p = 0.50.375Computing the probability of exactly 2 successes in 4 trials with p = 0.5.
Step 1: Binomial coefficient C(4,2) = 4! / (2! × 2!) = 24 / 4 = 6
Step 2: p^k = (0.5)² = 0.25
Step 3: (1-p)^(n-k) = (0.5)² = 0.25
Step 4: P(X = 2) = 6 × 0.25 × 0.25 = 0.375
This matches our intuition: with a fair coin and 4 flips, getting exactly 2 heads is the most likely outcome.
n = 5, k = 4, p = 0.80.4096Here we have a biased trial with 80% success probability, and we want exactly 4 successes in 5 trials.
Step 1: Binomial coefficient C(5,4) = 5! / (4! × 1!) = 5
Step 2: p^k = (0.8)⁴ = 0.4096
Step 3: (1-p)^(n-k) = (0.2)¹ = 0.2
Step 4: P(X = 4) = 5 × 0.4096 × 0.2 = 0.4096
With such a high success probability, getting 4 out of 5 successes is quite likely.
Constraints