Skip to content

Instantly share code, notes, and snippets.

@mehdirezaie
Last active June 22, 2023 18:52
Show Gist options
  • Save mehdirezaie/3228a8550aa8297b0693196a16170911 to your computer and use it in GitHub Desktop.
Save mehdirezaie/3228a8550aa8297b0693196a16170911 to your computer and use it in GitHub Desktop.
Table of Chi-2 values given confidence and degree of freedom
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Untitled0.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPqe9+JlcOsgoTTxW34AsUn",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/mehdirezaie/3228a8550aa8297b0693196a16170911/untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# $\\chi^{2}$ Distribution\n",
"This notebook provides credible levels for a $\\chi^{2}$ distribution. Adapted from http://www.reid.ai/2012/09/chi-squared-distribution-table-with.html. Note that only for dof=1, 1sigma corresponds to 68.3.\n"
],
"metadata": {
"id": "B5rkEFA89nl9"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "z-zh501D9i7E"
},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.stats import chi2"
]
},
{
"cell_type": "code",
"source": [
"sigmas = [1.0, 2.0, 3.0, 4.0] # standard deviations\n",
"confs = [chi2.cdf(s*s, 1) for s in sigmas] # confidence intervals for k=1\n",
"\n",
"\n",
"print('Confidence \\t'+'\\t'.join([\"{:8.2f}%\".format(c*100) for c in confs]))\n",
"print('p-value \\t'+'\\t'.join([\"{:8.5f}\".format(1.-c) for c in confs]))\n",
"print('sigma(k=1) \\t'+'\\t'.join([\"{:8.2f}\".format(s) for s in sigmas]))\n",
"\n",
"dof = np.arange(1, 11)\n",
"\n",
"print(73*'-')\n",
"for d in dof:\n",
" chi2s = [chi2.ppf(c, d) for c in confs]\n",
" print('chi2(d=%d) \\t'%d+'\\t'.join(['%8.2f'%ch for ch in chi2s]))\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "N4zfo3jf_E8A",
"outputId": "a31f23f4-8f72-410e-95e6-be3cb5dae03a"
},
"execution_count": 44,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Confidence \t 68.27%\t 95.45%\t 99.73%\t 99.99%\n",
"p-value \t 0.31731\t 0.04550\t 0.00270\t 0.00006\n",
"sigma(k=1) \t 1.00\t 2.00\t 3.00\t 4.00\n",
"-------------------------------------------------------------------------\n",
"chi2(d=1) \t 1.00\t 4.00\t 9.00\t 16.00\n",
"chi2(d=2) \t 2.30\t 6.18\t 11.83\t 19.33\n",
"chi2(d=3) \t 3.53\t 8.02\t 14.16\t 22.06\n",
"chi2(d=4) \t 4.72\t 9.72\t 16.25\t 24.50\n",
"chi2(d=5) \t 5.89\t 11.31\t 18.21\t 26.77\n",
"chi2(d=6) \t 7.04\t 12.85\t 20.06\t 28.91\n",
"chi2(d=7) \t 8.18\t 14.34\t 21.85\t 30.96\n",
"chi2(d=8) \t 9.30\t 15.79\t 23.57\t 32.93\n",
"chi2(d=9) \t 10.42\t 17.21\t 25.26\t 34.85\n",
"chi2(d=10) \t 11.54\t 18.61\t 26.90\t 36.72\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment