Calculate probabilities for discrete random variables
Exactly: P(X = k)
At Most: P(X ≤ k)
Less Than: P(X < k)
At Least: P(X ≥ k)
More Than: P(X > k)
function combinations(n, k) {
if (k n) return 0;
if (k === 0 || k === n) return 1;
if (k > n / 2) k = n – k;
var res = 1;
for (var i = 1; i <= k; i++) {
res = res * (n – i + 1) / i;
}
return res;
}
function binomialPMF(n, k, p) {
return combinations(n, k) * Math.pow(p, k) * Math.pow(1 – p, n – k);
}
function calculateBinomial() {
var n = parseInt(document.getElementById('num_trials').value);
var p = parseFloat(document.getElementById('prob_p').value);
var k = parseInt(document.getElementById('num_k').value);
var type = document.getElementById('calc_type').value;
var errorDiv = document.getElementById('error_msg');
var resultBox = document.getElementById('binom_result_box');
errorDiv.style.display = 'none';
resultBox.style.display = 'none';
if (isNaN(n) || isNaN(p) || isNaN(k)) {
errorDiv.innerText = "Please fill in all fields with valid numbers.";
errorDiv.style.display = 'block';
return;
}
if (p 1) {
errorDiv.innerText = "Probability (p) must be between 0 and 1.";
errorDiv.style.display = 'block';
return;
}
if (k n) {
errorDiv.innerText = "Successes (k) must be between 0 and the number of trials (n).";
errorDiv.style.display = 'block';
return;
}
var finalProb = 0;
var label = "";
if (type === "equal") {
finalProb = binomialPMF(n, k, p);
label = "P(X = " + k + ")";
} else if (type === "less_equal") {
for (var i = 0; i <= k; i++) {
finalProb += binomialPMF(n, i, p);
}
label = "P(X ≤ " + k + ")";
} else if (type === "less") {
for (var i = 0; i < k; i++) {
finalProb += binomialPMF(n, i, p);
}
label = "P(X < " + k + ")";
} else if (type === "greater_equal") {
for (var i = k; i <= n; i++) {
finalProb += binomialPMF(n, i, p);
}
label = "P(X ≥ " + k + ")";
} else if (type === "greater") {
for (var i = k + 1; i " + k + ")";
}
var mean = n * p;
var variance = n * p * (1 – p);
var stdDev = Math.sqrt(variance);
document.getElementById('main_result').innerHTML = label + " = " + finalProb.toFixed(6) + " (" + (finalProb * 100).toFixed(4) + "%)";
document.getElementById('res_mean').innerHTML = "Expected Value (Mean): " + mean.toFixed(4);
document.getElementById('res_variance').innerHTML = "Variance: " + variance.toFixed(4);
document.getElementById('res_std').innerHTML = "Std. Deviation: " + stdDev.toFixed(4);
document.getElementById('res_q').innerHTML = "Prob. of Failure (q): " + (1 – p).toFixed(4);
resultBox.style.display = 'block';
}
Understanding Binomial Probability Distribution
The Binomial Distribution is a fundamental probability distribution used in statistics that models the number of successes in a fixed number of independent trials. It is used when there are only two possible outcomes for each trial (often termed "Success" and "Failure").
The Four Criteria for a Binomial Experiment
Fixed Trials: There must be a specific number of trials ($n$).
Independent: Each trial must be independent of the others.
Two Outcomes: Each trial has only two possible results (success or failure).
Constant Probability: The probability of success ($p$) remains the same for every trial.
The Mathematical Formula
P(X = k) = nCk * pk * (1-p)n-k
Where:
n = Number of trials
k = Number of successes
p = Probability of success on a single trial
nCk = The combination formula (n! / [k!(n-k)!])
Practical Example
Imagine a factory produces light bulbs, and 5% (0.05) of them are defective. If you randomly select 10 light bulbs ($n = 10$), what is the probability that exactly 2 are defective ($k = 2$)?
n: 10
p: 0.05
k: 2
Using the calculator, the probability P(X = 2) is approximately 0.0746 (7.46%). This allows quality control managers to determine if a batch of products meets specific standards based on sample data.
Cumulative vs. Exact Probability
While an Exact calculation looks for exactly $k$ successes, Cumulative calculations look for a range:
At Most (P ≤ k): The probability of getting 0, 1, …, up to $k$ successes.
At Least (P ≥ k): The probability of getting $k, k+1, …, n$ successes.