By Jamal Hussein · Last updated:
Geometric Distribution Calculator P(X = k) = (1 − p)k−1 p
Enter the chance of success on a single trial and a value of k, then choose whether k counts trials or failures. You get the exact probability, the four cumulative probabilities, the mean, variance and standard deviation, and a chart of the distribution with your k marked.
The geometric distribution is the probability distribution of how many independent trials it takes to get the first success when every trial succeeds with the same probability p. This geometric distribution calculator returns P(X = k), the four cumulative probabilities, the mean, the variance and the standard deviation, whether you count trials or failures.
What is the geometric distribution?
It answers waiting-time questions: how many tries until something works for the first time? Flip a coin until it lands heads, roll a die until a six shows, resend a request until the server accepts it. In each case you repeat the same yes-or-no experiment and stop at the first yes.
Three conditions have to hold. Each trial has only two outcomes, the trials do not influence one another, and the chance of success p is the same every time. When they hold, the number of the trial on which the first success lands is a geometric random variable, and the whole distribution is fixed by the single number p.
What is the geometric distribution formula?
For the first success to arrive on trial k, the first k − 1 trials must all fail and trial k must succeed. Independence lets you multiply those probabilities:
- p is the probability of success on one trial, with 0 < p ≤ 1.
- 1 − p is the probability of failure on one trial.
- k is the trial on which the first success happens.
- X is the random number of trials needed.
There is no “n choose k” factor of the kind the combination calculator evaluates, because only one ordering fits: every failure comes first, then the success. The cumulative probability has a closed form too, since the success is still missing after k trials only if all k of them failed:
The mean is the probability-weighted average of 1, 2, 3 and so on, the same sum the expected value calculator works out for a finite list of outcomes. Here the list never ends, and the series collapses to 1/p.
Worked example: the first accepted draw comes on the third try
A rejection sampler accepts each proposed draw with probability p = 0.2. What is the chance that the first accepted draw is the third one? These are the calculator’s default inputs.
- Chance of a rejection on one draw: 1 − 0.2 = 0.8.
- Two rejections, then an acceptance: \(P(X = 3) = 0.8^{2} \times 0.2 = 0.64 \times 0.2 = 0.128\).
- Accepted within three draws: \(P(X \le 3) = 1 – 0.8^{3} = 1 – 0.512 = 0.488\).
- Still waiting after three draws: \(P(X > 3) = 0.8^{3} = 0.512\).
- Mean \(1/0.2 = 5\) draws, variance \(0.8/0.2^{2} = 20\), standard deviation \(\sqrt{20} = 4.472136\).
So 12.8% of runs get their first acceptance on exactly the third draw, and slightly more than half are still waiting after three.
Does k count trials or failures?
Two definitions are in common use, and they differ by exactly one. The first counts every trial up to and including the success, so the smallest possible value is 1. The second counts only the failures that come before the success, so it starts at 0. If X is the trials version, the failures version is Y = X − 1. The table shows the worked example under both.
| Quantity | Trials until first success (X) | Failures before first success (Y) |
|---|---|---|
| Possible values | 1, 2, 3, … | 0, 1, 2, … |
| Probability formula | (1 − p)k−1 p | (1 − p)k p |
| Two failures, then a success (p = 0.2) | P(X = 3) = 0.128 | P(Y = 2) = 0.128 |
| Success within the first three trials | P(X ≤ 3) = 0.488 | P(Y ≤ 2) = 0.488 |
| Mean | 1/p = 5 | (1 − p)/p = 4 |
| Variance | (1 − p)/p2 = 20 | (1 − p)/p2 = 20 |
| Used by | scipy.stats.geom, NumPy Generator.geometric | R dgeom and pgeom, many textbooks |
The probabilities describe the same event and only the label changes. The mean shifts by one and the variance does not move, because subtracting a constant does not change spread. SciPy and NumPy use the trials version, which matches the question “how many attempts will this take?”. R counts failures, which makes the geometric a special case of its negative binomial functions. Use the convention selector above to match whichever tool you are checking against.
What does memoryless mean?
Past failures tell you nothing about how much longer you will wait. In symbols, \(P(X > m+n \mid X > m) = P(X > n)\). The geometric is the only discrete distribution with this property.
Take p = 0.2 again. Before you start, the chance of needing more than 3 tries is 0.83 = 0.512. After 10 failures in a row, the chance of needing more than 3 further tries is 0.813 / 0.810 = 0.512, exactly the same, and the expected number of additional tries is still 5. You can check the division with the conditional probability calculator. A success is never “due”.
Where is the geometric distribution used in machine learning?
- Retries for a flaky request. If each attempt succeeds with probability 0.9, the expected number of attempts is 1/0.9 = 1.111111, and the chance that three attempts all fail is 0.13 = 0.001. That is the number to use when you set a retry limit.
- Samples until the first positive in a rare-class dataset. With 2% positives you expect to label 1/0.02 = 50 examples before the first one appears, yet the chance of seeing it within those 50 is only 1 − 0.9850 = 0.635830. To be 95% sure of at least one positive you need 149 samples.
- Rejection sampling. When the envelope constant is M, each proposal is accepted with probability 1/M and the number of proposals per accepted sample is geometric with mean M. For M = 4 that is 4 draws on average, with a standard deviation of 3.464102 and a 5.6% chance (0.7510 = 0.056314) of needing more than 10.
Common mistakes
- Mixing the two conventions. Passing a trial count to a function that expects failures returns the wrong term: with p = 0.2, R’s
dgeom(3, 0.2)is 0.1024, not 0.128. The mean is off by exactly one as well. - Reading the mean as the typical wait. The distribution is strongly right-skewed. With a mean of 5 tries the median is 4, the single most likely value is 1, and 32.768% of runs need more than 5. A percentile describes the wait better than the average does.
- Confusing “on the k-th try” with “by the k-th try”. The first is P(X = k) = 0.128 in the example, the second is P(X ≤ k) = 0.488.
- Believing a success is overdue. A long run of failures does not raise the chance on the next trial.
- Using it when p changes. Drawing without replacement from a small pool changes the odds after every draw, which is the setting of the hypergeometric distribution. Retries that hit the same outage are not independent either.
How do you compute it in Python with SciPy?
scipy.stats.geom uses the trials convention, so its support starts at 1. Shifting it with loc=-1 gives the failures convention.
from scipy.stats import geom
p = 0.2
rv = geom(p) # X = number of trials, k = 1, 2, 3, ...
print(rv.pmf(3)) # 0.128 P(X = 3)
print(rv.cdf(3)) # 0.488 P(X <= 3)
print(rv.sf(3)) # 0.512 P(X > 3)
print(rv.mean()) # 5.0
print(rv.var()) # 20.0
fails = geom(p, loc=-1) # Y = number of failures, k = 0, 1, 2, ...
print(fails.pmf(2)) # 0.128 P(Y = 2)
print(fails.mean()) # 4.0
The printed values agree with the worked example up to floating-point rounding. The definition and the loc shift are described on the scipy.stats.geom reference page. To simulate waiting times, numpy.random.Generator.geometric draws samples under the same trials convention.
Frequently asked questions
What is the difference between the geometric and binomial distributions?
The binomial distribution fixes the number of trials and counts how many successes occur. The geometric distribution fixes the number of successes at one and counts how many trials it takes to get there. Both assume independent trials with the same success probability.
What is the expected number of trials until the first success?
It is 1 divided by p. With a success probability of 0.2 the expected number of trials is 5, and with a fair die the expected number of rolls until the first six is 6. If you count only failures, the expected value is one less.
Does the geometric distribution start at 0 or 1?
It depends on the convention. Counting trials up to and including the first success starts at 1, which is what SciPy and NumPy do. Counting failures before the first success starts at 0, which is what R does. The two differ by exactly one.
What does it mean that the geometric distribution is memoryless?
The chance of waiting a given number of further trials does not depend on how many failures have already happened. After any run of failures, the remaining wait has the same distribution as it had at the start.
Related calculators
Browse every tool in the statistics calculators hub, or continue with one of these: