Weighted And Harmonic Means In statistics
fmean applies weights, harmonic_mean averages equal-distance speeds honestly, and geometric_mean fits growth factors like 2x then 8x.
Code
from statistics import fmean, geometric_mean, harmonic_mean
# Weighted average: projects count more than maintenance work
scores = [3.2, 4.5, 4.0]
weights = [1, 3, 2]
print(f"weighted = {fmean(scores, weights=weights):.3f}")
# Unweighted average of the same scores for contrast
print(f"plain fmean = {fmean(scores):.3f}")
# Harmonic mean is the honest average speed for equal-distance legs
legs_kmh = [60, 120]
print(f"harmonic = {harmonic_mean(legs_kmh)}")
# Geometric mean fits growth factors: 2x then 8x is 4x per step
growth = [2.0, 8.0]
print(f"geometric = {geometric_mean(growth)}")Output
Captured from a sandboxed run of this snippet in the build pipeline.
weighted = 4.117
plain fmean = 3.900
harmonic = 80.0
geometric = 4.0The logic behind the lines
Top-level bindings scores, weights, legs_kmh, growth hold the demo state — nothing sits inside a function scope.
Watch out for
- Nothing third-party is pulled in; the snippet is dependency-free.
Applying it
Output lines are prefixed (weighted =, plain fmean =, harmonic =), so the printed order mirrors the order of the steps.
Interactive Tools for This Topic
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Statistics CalculatorCompute count, sum, mean, median, minimum, maximum, range, variance, and standard deviation for a number list.Text StatisticsCount characters, words, lines, sentences, paragraphs, and estimated reading time entirely in the browser with no text sent to a server.
FAQ
- Are there any imports to resolve outside the language?
- No. Every import resolves against the language's own modules. Its state lives in module-level bindings: `scores`, `weights`, `legs_kmh`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.