Created
February 9, 2023 00:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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