StatsCalculators.com

Geometric Distribution

Created:November 11, 2024

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.

Calculator

Parameters

Interactive Distribution Chart

Enter parameters and calculate to view the distribution

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.

Formula:P(X=k)=p(1p)k1P(X = k) = p(1-p)^{k-1}

Where:

  • kk is the number of trials until first success (k ≥ 1)
  • pp is the probability of success on each trial

Properties

  • Mean: E(X)=1pE(X) = \frac{1} {p}
  • Variance: Var(X)=1pp2Var(X) = \frac{1 - p} {p ^ 2}

How to Calculate Geometric Distribution in R

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

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()