{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy import stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# matplotlib for plotting\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "from IPython.core.pylabtools import figsize\n",
    "import matplotlib\n",
    "\n",
    "import json\n",
    "s = json.load(open('style/bmh_matplotlibrc.json'))\n",
    "matplotlib.rcParams.update(s)\n",
    "matplotlib.rcParams['figure.figsize'] = (10, 3)\n",
    "matplotlib.rcParams['font.size'] = 14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "figsize(12, 6)\n",
    "x = np.linspace(-5, 5, 10000)\n",
    "y = stats.norm.pdf(x)\n",
    "n_tests = 1000\n",
    "\n",
    "# Random values\n",
    "x_random = np.random.randn(1000)\n",
    "\n",
    "# Non significant random values at p = 0.05\n",
    "x_random_nonsig = [x for x in x_random if abs(x) < stats.norm.ppf(0.975)]\n",
    "y_constant_nonsig = [0.0 for _ in range(len(x_random_nonsig))]\n",
    "\n",
    "# Significant random values at p = 0.05\n",
    "x_random_sig = [x for x in x_random if abs(x) > stats.norm.ppf(0.975)]\n",
    "y_constant_sig = [0.00 for _ in range(len(x_random_sig))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 45840 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1b1b04508d0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "plt.plot(x_random_nonsig, y_constant_nonsig, 'ko', label = 'Non Significant Values', alpha = 0.2)\n",
    "plt.plot(x_random_sig, y_constant_sig, 'ro', label = 'Significant Values')\n",
    "plt.plot(x, y, '-', color = 'navy'); plt.xlabel('z-score', size =16); plt.ylabel('Probability Density');\n",
    "plt.vlines([stats.norm.ppf(0.975), stats.norm.ppf(0.025)],linestyles='--',\n",
    "           color = 'red', linewidth=4,\n",
    "           ymin = 0, ymax = y.max(), label='Significance Limits for $p = 0.05$')\n",
    "plt.yticks([])\n",
    "plt.legend(prop={'size': 14});\n",
    "plt.title('Random Observations with significance level $p=0.05$', size = 22);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Non significant random values at p = 0.05 with correction\n",
    "x_random_nonsig = [x for x in x_random if abs(x) < stats.norm.ppf(1 - (0.025/n_tests))]\n",
    "y_constant_nonsig = [0.0 for _ in range(len(x_random_nonsig))]\n",
    "\n",
    "# Significant random values at p = 0.05 with correction\n",
    "x_random_sig = [x for x in x_random if abs(x) > stats.norm.ppf(1 - (0.025/n_tests))]\n",
    "y_constant_sig = [0.0 for _ in range(len(x_random_sig))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 50536 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1b1b225fcc0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(x_random_nonsig, y_constant_nonsig, 'ko', label = 'Non Significant Values', alpha = 0.2)\n",
    "plt.plot(x_random_sig, y_constant_sig, 'go', label = 'Significant Values')\n",
    "plt.plot(x, y, '-', color = 'navy'); plt.xlabel('z-score', size = 16); \n",
    "plt.ylabel('Probability Density'); plt.yticks([])\n",
    "plt.vlines([stats.norm.ppf(1 - (0.025/n_tests)), stats.norm.ppf(0.025/n_tests)],linestyles='--',\n",
    "           color = 'darkgreen', linewidth=4,\n",
    "           ymin = 0, ymax = y.max(), label='Significance Limits \\nwith Bonferroni Correction')\n",
    "plt.legend(prop={'size': 14});\n",
    "plt.title('Random Observations with Bonferroni Correction', size = 22);\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
