Skip to content

Instantly share code, notes, and snippets.

@cgobat
Created February 9, 2023 00:35
Show Gist options
  • Save cgobat/b50d682fb314ce4470fe4dda343ffa83 to your computer and use it in GitHub Desktop.
Save cgobat/b50d682fb314ce4470fe4dda343ffa83 to your computer and use it in GitHub Desktop.
Functions to easily convert between number of sigma (standard deviations) and percent confidence intervals.
# see https://en.wikipedia.org/wiki/68–95–99.7_rule
import numpy as np
from scipy import special
def sigma_to_pct_conf(z: float) -> float:
"""If an uncertainty is ±zσ, what is the corresponding confidence interval?"""
return special.erf(z/np.sqrt(2))
def pct_conf_to_sigma(conf: float) -> float:
"""If we have `conf`% confidence in our result, ± how many σ is that?"""
return special.erfinv(conf)*np.sqrt(2)
if __name__ == "__main__":
print(f"±1σ = {sigma_to_pct_conf(1):.2%} confidence")
print(f"90% confidence = ±{pct_conf_to_sigma(0.9):.3f}σ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment