This calculator helps you compute the probabilities of a geometric distribution given the probability of success (p). You can find the probability of a value being equal to, less than, greater than, or between certain values. The geometric distribution models the number of failures before the first success in a sequence of independent Bernoulli trials. The distribution chart shows the probability mass function (PMF) and cumulative distribution function (CDF) of the geometric distribution.
Different Conventions
Note: There are two different ways to define the geometric distribution:
- This calculator uses the number of trials until first success convention (as in Python's SciPy), where X ≥ 1
- Some other software (like R's
dgeom
) uses the number of failures before first success convention, where X ≥ 0
If you're calculating the probability of the number of failures before the first success, you can adjust the input values accordingly. For example, if you want to find the probability of 3 failures before the first success, set x1 = 4
in the calculator.
Calculator
Parameters
Interactive Distribution Chart
Related Calculators
Learn More
Geometric Distribution
Definition: The geometric distribution models the number of trials needed to achieve the first success in a sequence of independent Bernoulli trials, where each trial has the same probability of success.
Where:
- is the number of trials until first success (k ≥ 1)
- is the probability of success on each trial
Properties
- Mean:
- Variance:
How to Calculate Geometric Distribution in R
library(tidyverse)
p <- 0.3
x_values <- 0:10
pmf_df <- tibble(
x = x_values,
probability = dgeom(x_values-1, prob = p) # Adjusted for R's 0-based indexing
)
set.seed(42)
samples_df <- tibble(
sample = rgeom(1000, prob = p)
)
ggplot(pmf_df, aes(x = x, y = probability)) +
geom_col(fill = "skyblue", alpha = 0.7) +
geom_text(aes(label = round(probability, 3)), vjust = -0.5, size = 3) +
labs(title = "Geometric Distribution PMF",
subtitle = paste("p =", p),
x = "Number of failures before first success",
y = "Probability") +
theme_minimal()
How to Calculate Geometric Distribution in Python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import geom
# Set style for better-looking plots
plt.style.use('seaborn-v0_8')
sns.set_palette("husl")
# Set probability parameter
p = 0.3
# Create x values (number of failures before first success)
x_values = np.arange(0, 11)
# Calculate PMF values
# Note: scipy.stats.geom represents number of trials until first success
# To get number of failures before first success, we use geom.pmf(x+1, p)
pmf_values = geom.pmf(x_values + 1, p)
# Create DataFrame
pmf_df = pd.DataFrame({
'x': x_values,
'probability': pmf_values
})
# Generate random samples
np.random.seed(42)
samples = geom.rvs(p, size=1000) - 1 # Subtract 1 to get failures before success
samples_df = pd.DataFrame({'sample': samples})
# Create the plot
fig, ax = plt.subplots(figsize=(10, 6))
# Create bar plot
bars = ax.bar(pmf_df['x'], pmf_df['probability'],
color='skyblue', alpha=0.7, edgecolor='black', linewidth=0.5)
# Add probability labels on top of bars
for i, (x, prob) in enumerate(zip(pmf_df['x'], pmf_df['probability'])):
ax.text(x, prob + 0.01, f'{prob:.3f}',
ha='center', va='bottom', fontsize=9)
# Customize the plot
ax.set_title('Geometric Distribution PMF', fontsize=16, fontweight='bold')
ax.set_xlabel('Number of failures before first success', fontsize=12)
ax.set_ylabel('Probability', fontsize=12)
ax.text(0.02, 0.98, f'p = {p}', transform=ax.transAxes,
fontsize=12, verticalalignment='top',
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.8))
# Set x-axis ticks
ax.set_xticks(x_values)
ax.set_xlim(-0.5, 10.5)
# Add grid for better readability
ax.grid(True, alpha=0.3)
# Adjust layout and show
plt.tight_layout()
plt.show()