Standard deviation ($\sigma$) and variance ($\sigma^2$) quantify the spread or dispersion of data points around their arithmetic mean.
import numpy as np
from typing import Tuple
def calculate_dispersion(data: np.ndarray, ddof: int = 0) -> Tuple[float, float]:
"""Calculate population (ddof=0) or sample (ddof=1) variance and standard deviation."""
variance = float(np.var(data, ddof=ddof))
std_dev = float(np.std(data, ddof=ddof))
return variance, std_dev
if __name__ == "__main__":
# Dataset A: Low Variance / Tight Cluster
group_a = np.array([49, 50, 51, 50, 50])
# Dataset B: High Variance / Widespread
group_b = np.array([10, 30, 50, 70, 90])
var_a, std_a = calculate_dispersion(group_a)
var_b, std_b = calculate_dispersion(group_b)
print(f"Group A -> Mean: {np.mean(group_a)}, Std Dev: {std_a:.2f}")
print(f"Group B -> Mean: {np.mean(group_b)}, Std Dev: {std_b:.2f}")
ddof=1): Use ddof=1 when calculating sample standard deviation from a subset of a population. NumPy defaults to ddof=0 (population).Compute the sample standard deviation (ddof=1) of [2, 4, 4, 4, 5, 5, 7, 9] using np.std().
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With