"""Resting heart rate: places a resting pulse among U.S. adults of the same age group and sex (NHANES 1999-2008 percentiles), and against the 60 to 100 bpm adult normal range. Inputs: resting_hr (bpm, 20 to 220, default 62), age (years, 18 to 110, default 35), sex ("male" or "female", default "male"). Returns: ageBand, percentilesBpm (the 5th, 10th, 25th, 50th, 75th and 90th percentiles, bpm), bandLowPercentile and bandHighPercentile (the reading is above the low one's bpm and at or below the high one's; 0 and 100 at the ends), medianBpm, bpmFromMedian, normalRange ("below", "within" or "above"). mysi, Resting heart rate, mysi.dev/resting-heart-rate, version 1, changed 2026-12-04. MIT. """ # Ostchega and others 2011, National Health Statistics Reports 41, Tables 2 (males) and 3 # (females): resting pulse, 5th, 10th, 25th, 50th, 75th and 90th percentiles, by age group. _RESTING_HEART_RATE_ROWS = [ ("16-19", 19, [52, 56, 61, 69, 78, 87], [58, 62, 69, 77, 85, 94]), ("20-39", 39, [52, 55, 61, 69, 76, 84], [57, 60, 66, 74, 82, 89]), ("40-59", 59, [52, 55, 61, 68, 77, 85], [56, 59, 64, 71, 79, 86]), ("60-79", 79, [50, 54, 60, 67, 75, 84], [56, 59, 64, 70, 78, 86]), ("80+", 999, [51, 54, 61, 68, 78, 86], [56, 59, 64, 71, 77, 85]), ] _RESTING_HEART_RATE_PCTS = [0, 5, 10, 25, 50, 75, 90, 100] def _resting_heart_rate_num(name, value, low, high): if isinstance(value, bool) or not isinstance(value, (int, float)) or not low <= value <= high: raise ValueError(f"{name} must be a number from {low} to {high}") return float(value) def resting_heart_rate(resting_hr=62, age=35, sex="male"): hr = _resting_heart_rate_num("resting_hr", resting_hr, 20, 220) years = _resting_heart_rate_num("age", age, 18, 110) if sex not in ("male", "female"): raise ValueError("sex must be one of: male, female") label, _, male, female = next(r for r in _RESTING_HEART_RATE_ROWS if years <= r[1]) edges = female if sex == "female" else male k = next((i for i, bpm in enumerate(edges) if hr <= bpm), len(edges)) return { "ageBand": label, "percentilesBpm": edges, "bandLowPercentile": _RESTING_HEART_RATE_PCTS[k], "bandHighPercentile": _RESTING_HEART_RATE_PCTS[k + 1], "medianBpm": edges[3], "bpmFromMedian": hr - edges[3], # American Heart Association: 60 to 100 bpm is the normal adult resting range. "normalRange": "below" if hr < 60 else "above" if hr > 100 else "within", } RESTING_HEART_RATE_CASES = [ ({"resting_hr": 62, "age": 35, "sex": "male"}, {"bandLowPercentile": 25, "bandHighPercentile": 50, "medianBpm": 69, "bpmFromMedian": -7}), ({"resting_hr": 74, "age": 52, "sex": "female"}, {"bandLowPercentile": 50, "bandHighPercentile": 75, "medianBpm": 71, "bpmFromMedian": 3}), ({"resting_hr": 90, "age": 85, "sex": "male"}, {"bandLowPercentile": 90, "bandHighPercentile": 100, "medianBpm": 68}), ] def resting_heart_rate_check(tolerance=1e-3): for given, want in RESTING_HEART_RATE_CASES: got = resting_heart_rate(**given) for name, value in want.items(): close = abs(got[name] - value) <= tolerance * max(1, abs(value)) assert close, (name, got[name], value) return "ok" if __name__ == "__main__": import json, sys resting_heart_rate_check() given = dict(a.split("=", 1) for a in sys.argv[1:] if "=" in a) js = lambda v: v.lstrip("-")[:1].isdigit() or v[:1] in '[{".' or v in ("true", "false", "null") given = {k: json.loads(v) if js(v) else v for k, v in given.items()} if given: print(json.dumps(resting_heart_rate(**given)))