Negative Binomial Mean and Variance Calculator
This negative binomial mean and variance calculator describes how many failures occur before a chosen number of successes is reached.
Run — free
Enter the required success count r and the constant success probability p for each independent trial. The calculator returns the expected failure count and its variance immediately, using the standard failures-before-the-r-th-success convention. It is useful for coursework, reliability planning, repeated-attempt processes, and any model where trials continue until a fixed success target is met.
Choose the negative binomial convention
The phrase negative binomial distribution is used for two closely related random variables, so identifying the convention is the first important step. This calculator defines the random variable as the number of failures observed before the r-th success. Under that definition, a process can finish with zero failures when its first r trials all succeed. Some textbooks and software instead count the total number of trials required to reach the r-th success. That total-trials variable is exactly r larger than the failure-count variable, so its mean is larger by r while its variance is unchanged. Enter r as a positive whole number representing the success target. Enter p as the probability that any one trial succeeds, written as a decimal greater than zero and no greater than one. For example, use 0.4 for a 40 percent success probability. The model assumes independent trials and the same p on every trial. If attempts become easier, harder, or dependent on earlier outcomes, these formulas no longer describe the process reliably. The calculator rejects zero because a positive success target can never be reached when success is impossible, leaving no finite mean or variance to return.
Understand how the moments are calculated
For the failure-count convention, the mean is r multiplied by one minus p, then divided by p. The variance is r multiplied by one minus p, then divided by p squared. The mean is the long-run average number of failures across many repetitions of the full process; it does not predict the exact outcome of one run. The variance describes dispersion around that mean in squared failure-count units. As p approaches one, successes become nearly certain, so both values approach zero. At p equal to one, every trial succeeds and the process reaches r successes without any failures, making both the mean and variance exactly zero. As p moves closer to zero, failures become more common and both moments grow quickly. Variance grows especially fast because its denominator contains p squared. These patterns are useful for checking an answer: neither result can be negative, variance is at least as large as the mean for valid inputs, and both values are zero at p equal to one. Floating-point arithmetic may produce long decimal expansions for some inputs. Preserve sufficient precision for later calculations and round only when displaying a final report, because early rounding can accumulate error in downstream formulas.
Apply the result to an appropriate process
Use this calculator when observations are independent Bernoulli trials that continue until a fixed number of successes occurs. A support team might model failed contact attempts before completing several successful contacts, a test laboratory might model rejected units before recording a target number of passing units, or a communications engineer might model unsuccessful transmissions before a required number of successful deliveries. In each case, r must be fixed in advance and p must remain stable from attempt to attempt. The returned mean helps estimate average failure volume, staffing, spare capacity, or expected waste. The variance helps describe uncertainty and can feed other analytic work, but it is not the same as a confidence interval or a probability of exceeding a threshold. This capability does not calculate point probabilities, cumulative probabilities, quantiles, or parameters estimated from observed data. It also does not test whether independence or constant probability is plausible. If successes are sampled without replacement from a small population, if attempts influence later attempts, or if different subjects have different success probabilities, another model may be more suitable. Treat the output as a consequence of the assumptions you provide, not as evidence that those assumptions hold.
What you can do with it
Plan repeated attempts
Estimate the average number of unsuccessful attempts and their variability before a fixed success target is reached.
Check probability coursework
Verify negative binomial moments while keeping the failure-count convention explicit.
Estimate process waste
Summarize expected rejected units before a required number of passing units under a stable pass probability.
FAQ
What formulas does the calculator use?
For failures before the r-th success, it uses mean = r × (1 − p) / p and variance = r × (1 − p) / p².
Does the distribution count failures or total trials?
It counts failures before the r-th success. To obtain the mean total trial count, add r to the returned mean; the variance stays the same.
Why is p equal to zero rejected?
When p is zero, success is impossible, so a positive target r is never reached and the distribution has no finite mean or variance.
Can p equal one?
Yes. Every trial then succeeds, so there are zero failures before the r-th success and both returned values are zero.
Must r be a whole number?
Yes. It represents a count of required successes and must be a positive integer.
What does an API calculation cost?
Each API request costs $0.002. The browser calculator can run without an API request.
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/stat/negative-binomial-mean-variance \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"r":5,"p":0.4}'const res = await fetch("https://api.kit.forhosting.com/stat/negative-binomial-mean-variance", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"r": 5,
"p": 0.4
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/stat/negative-binomial-mean-variance",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"r": 5,
"p": 0.4
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/stat/negative-binomial-mean-variance", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"r":5,"p":0.4}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"r":5,"p":0.4}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/stat/negative-binomial-mean-variance", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"r": 5,
"p": 0.4
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "stat.negative_binomial_mean_variance",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |