{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "import scipy.stats as stats\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# T test\n",
    "    A t-test is a type of inferential statistic which is used to determine if there is a significant difference between the means of two groups which may be related in certain features\n",
    "\n",
    "    T-test has 2 types : 1. one sampled t-test 2. two-sampled t-test.\n",
    "    \n",
    "## One-sample T-test with Python\n",
    "    The test will tell us whether means of the sample and the population are different\n",
    "\n",
    "### to perform ttest we use ttest_1samp in scipy.stats\n",
    "    which Calculate the T-test for the mean of ONE group of scores.\n",
    "    \n",
    "![](oneSampleTtest.jpeg)\n",
    "\n",
    "[click here to know more](https://www.google.com/url?sa=i&url=https%3A%2F%2Fdataanalyze.wordpress.com%2Fone-sample-t-test%2F&psig=AOvVaw0-cvqoN_fX09bvc_LSee70&ust=1585133714093000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKCLs-b6sugCFQAAAAAdAAAAABAD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "ages=[10,20,35,50,28,40,55,18,16,55,30,25,43,18,30,28,14,24,16,17,32,35,26,27,65,18,43,23,21,20,19,70]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30.34375\n"
     ]
    }
   ],
   "source": [
    "mean_ages = np.mean(ages)\n",
    "print(mean_ages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[40 18 32 18 30 23 14 16 43 55]\n"
     ]
    }
   ],
   "source": [
    "sample_size = 10\n",
    "sample_data = np.random.choice(ages, sample_size)\n",
    "print(sample_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import ttest_1samp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.12797537105159223\n",
      "0.898995071686391\n"
     ]
    }
   ],
   "source": [
    "ttest , p_value = ttest_1samp(ages , 30)\n",
    "print(ttest)\n",
    "print(p_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'p_value' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-10-0ce9459fd632>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      5\u001b[0m         \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"We are accepting null hypothesis\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mfunction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp_value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'p_value' is not defined"
     ]
    }
   ],
   "source": [
    "def function(p_value):\n",
    "    if p_value > 0.05: # alpha values is 0.05 or 5%\n",
    "        print('We are rejecting null hypothesis')\n",
    "    else: \n",
    "        print(\"We are accepting null hypothesis\")\n",
    "\n",
    "function(p_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Another Example \n",
    "    Consider the ages of students in college in class A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(6)\n",
    "school_ages=stats.poisson.rvs(loc=18,mu=35,size=1500)\n",
    "classA_ages=stats.poisson.rvs(loc=18,mu=30,size=60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "46.9"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(classA_ages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.139027071016194e-13\n"
     ]
    }
   ],
   "source": [
    "_,p_value=stats.ttest_1samp(a=classA_ages,popmean=school_ages.mean())\n",
    "print(p_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We are rejecting null hypothesis\n"
     ]
    }
   ],
   "source": [
    "function(p_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Two-sample T-test With Python\n",
    "    The Independent Samples t Test or 2-sample t-test compares the means of two independent groups in order to determine whether there is statistical evidence that the associated population means are significantly different. The Independent Samples t Test is a parametric test. This test is also known as: Independent t Test\n",
    "\n",
    "### to perform we use stats.ttest_ind\n",
    "    which Calculate the T-test for the means of *two independent* samples of scores.\n",
    "    \n",
    "![two sampled Ttest image](twoSampledTtest.png)\n",
    "    \n",
    "[click here to know more](https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.statsdirect.co.uk%2Fhelp%2Fparametric_methods%2Futt.htm&psig=AOvVaw3_8468tYDZxdHpIzFxdVkI&ust=1585136119822000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCKCVzeCCs-gCFQAAAAAdAAAAABAD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.63333333333333"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(12)\n",
    "ClassB_ages=stats.poisson.rvs(loc=18,mu=33,size=60)\n",
    "ClassB_ages.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " we are rejecting null hypothesis\n"
     ]
    }
   ],
   "source": [
    "_,p_value=stats.ttest_ind(a=classA_ages,b=ClassB_ages,equal_var=False)\n",
    "\n",
    "if p_value < 0.05:    # alpha value is 0.05 or 5%\n",
    "    print(\" we are rejecting null hypothesis\")\n",
    "else:\n",
    "    print(\"we are accepting null hypothesis\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Paired T-test With Python\n",
    "    When you want to check how different samples from the same group are, you can go for a paired T-test\n",
    "    \n",
    "### to perform this we use stats.ttest_rel\n",
    "    which Calculate the t-test on TWO RELATED samples of scores, a and b.\n",
    "    \n",
    "![paired Ttest](pairedTtest.jpg)\n",
    "\n",
    "[watch this to know more](https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Driqq4uNXZt0&psig=AOvVaw1V8kwzlrqZXITv6kzQvr_E&ust=1585137107303000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCMjuxZ2Gs-gCFQAAAAAdAAAAABAD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[25, 30, 28, 35, 28, 34, 26, 29, 30, 26, 28, 32, 31, 30, 45]\n",
      "[16.1845637  24.93234756 27.99601605 25.80095696 21.85237039 33.36380814\n",
      " 33.19646047 28.63874933 28.9100301  34.41607782 21.43952644 27.08685451\n",
      " 33.96370697 34.13369994 46.03845269]\n"
     ]
    }
   ],
   "source": [
    "weight1=[25,30,28,35,28,34,26,29,30,26,28,32,31,30,45]\n",
    "weight2=weight1+stats.norm.rvs(scale=5,loc=-1.25,size=15)\n",
    "\n",
    "print(weight1)\n",
    "print(weight2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "weight_df=pd.DataFrame({\"weight_10\":np.array(weight1),\n",
    "                         \"weight_20\":np.array(weight2),\n",
    "                       \"weight_change\":np.array(weight2)-np.array(weight1)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>weight_10</th>\n",
       "      <th>weight_20</th>\n",
       "      <th>weight_change</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>25</td>\n",
       "      <td>16.184564</td>\n",
       "      <td>-8.815436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>30</td>\n",
       "      <td>24.932348</td>\n",
       "      <td>-5.067652</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28</td>\n",
       "      <td>27.996016</td>\n",
       "      <td>-0.003984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>35</td>\n",
       "      <td>25.800957</td>\n",
       "      <td>-9.199043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>28</td>\n",
       "      <td>21.852370</td>\n",
       "      <td>-6.147630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>34</td>\n",
       "      <td>33.363808</td>\n",
       "      <td>-0.636192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>26</td>\n",
       "      <td>33.196460</td>\n",
       "      <td>7.196460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>29</td>\n",
       "      <td>28.638749</td>\n",
       "      <td>-0.361251</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>30</td>\n",
       "      <td>28.910030</td>\n",
       "      <td>-1.089970</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>26</td>\n",
       "      <td>34.416078</td>\n",
       "      <td>8.416078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>28</td>\n",
       "      <td>21.439526</td>\n",
       "      <td>-6.560474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>32</td>\n",
       "      <td>27.086855</td>\n",
       "      <td>-4.913145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>31</td>\n",
       "      <td>33.963707</td>\n",
       "      <td>2.963707</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>30</td>\n",
       "      <td>34.133700</td>\n",
       "      <td>4.133700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>45</td>\n",
       "      <td>46.038453</td>\n",
       "      <td>1.038453</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    weight_10  weight_20  weight_change\n",
       "0          25  16.184564      -8.815436\n",
       "1          30  24.932348      -5.067652\n",
       "2          28  27.996016      -0.003984\n",
       "3          35  25.800957      -9.199043\n",
       "4          28  21.852370      -6.147630\n",
       "5          34  33.363808      -0.636192\n",
       "6          26  33.196460       7.196460\n",
       "7          29  28.638749      -0.361251\n",
       "8          30  28.910030      -1.089970\n",
       "9          26  34.416078       8.416078\n",
       "10         28  21.439526      -6.560474\n",
       "11         32  27.086855      -4.913145\n",
       "12         31  33.963707       2.963707\n",
       "13         30  34.133700       4.133700\n",
       "14         45  46.038453       1.038453"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "_,p_value=stats.ttest_rel(a=weight1,b=weight2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We are rejecting null hypothesis\n"
     ]
    }
   ],
   "source": [
    "function(p_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pearson corelation\n",
    "\n",
    "![corelation formula](corelation.png)\n",
    "\n",
    "[to know more click here](https://www.google.com/url?sa=i&url=https%3A%2F%2Fbyjus.com%2Fcommerce%2Fkarl-pearson-coefficient-of-correlation%2F&psig=AOvVaw1xcTaDBAJZd2YsSnNHXMS3&ust=1585137793328000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPCMw9SIs-gCFQAAAAAdAAAAABAD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width',\n",
      "       'species'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "df = sns.load_dataset('iris')\n",
    "print(df.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(150, 5)\n",
      "              sepal_length  sepal_width  petal_length  petal_width\n",
      "sepal_length      1.000000    -0.117570      0.871754     0.817941\n",
      "sepal_width      -0.117570     1.000000     -0.428440    -0.366126\n",
      "petal_length      0.871754    -0.428440      1.000000     0.962865\n",
      "petal_width       0.817941    -0.366126      0.962865     1.000000\n"
     ]
    }
   ],
   "source": [
    "print(df.shape)\n",
    "print(df.corr())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.PairGrid at 0x7f3c8444f748>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 141568 characters]\n",
      "text/plain": [
       "<Figure size 804.75x720 with 20 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.pairplot(df , hue = 'species')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# -------------------------------------------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## chi-square test\n",
    "    The test is applied when you have two categorical variables from a single population. It is used to determine whether there is a significant association between the two variables.\n",
    "\n",
    "### to perform chi square test we use stats.chi2_contingency\n",
    "    Chi-square test of independence of variables in a contingency table.\n",
    "    \n",
    "#### also stats.chi2\n",
    "\n",
    "![Chi Square test formula](chiSquaretest.png)\n",
    "    \n",
    "[click to know more](https://www.google.com/url?sa=i&url=https%3A%2F%2Ftowardsdatascience.com%2Fchi-square-test-for-feature-selection-in-machine-learning-206b1f0b8223&psig=AOvVaw0NZ4RnulS5G-6VfGRfOk9T&ust=1585146116117000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCNDektyns-gCFQAAAAAdAAAAABAD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = sns.load_dataset('tips')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_bill</th>\n",
       "      <th>tip</th>\n",
       "      <th>sex</th>\n",
       "      <th>smoker</th>\n",
       "      <th>day</th>\n",
       "      <th>time</th>\n",
       "      <th>size</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>16.99</td>\n",
       "      <td>1.01</td>\n",
       "      <td>Female</td>\n",
       "      <td>No</td>\n",
       "      <td>Sun</td>\n",
       "      <td>Dinner</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.34</td>\n",
       "      <td>1.66</td>\n",
       "      <td>Male</td>\n",
       "      <td>No</td>\n",
       "      <td>Sun</td>\n",
       "      <td>Dinner</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>21.01</td>\n",
       "      <td>3.50</td>\n",
       "      <td>Male</td>\n",
       "      <td>No</td>\n",
       "      <td>Sun</td>\n",
       "      <td>Dinner</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>23.68</td>\n",
       "      <td>3.31</td>\n",
       "      <td>Male</td>\n",
       "      <td>No</td>\n",
       "      <td>Sun</td>\n",
       "      <td>Dinner</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>24.59</td>\n",
       "      <td>3.61</td>\n",
       "      <td>Female</td>\n",
       "      <td>No</td>\n",
       "      <td>Sun</td>\n",
       "      <td>Dinner</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   total_bill   tip     sex smoker  day    time  size\n",
       "0       16.99  1.01  Female     No  Sun  Dinner     2\n",
       "1       10.34  1.66    Male     No  Sun  Dinner     3\n",
       "2       21.01  3.50    Male     No  Sun  Dinner     3\n",
       "3       23.68  3.31    Male     No  Sun  Dinner     2\n",
       "4       24.59  3.61  Female     No  Sun  Dinner     4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "smoker  Yes  No\n",
      "sex            \n",
      "Male     60  97\n",
      "Female   33  54\n"
     ]
    }
   ],
   "source": [
    "dataset_table=pd.crosstab(dataset['sex'],dataset['smoker'])\n",
    "print(dataset_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[60 97]\n",
      " [33 54]]\n"
     ]
    }
   ],
   "source": [
    "print(dataset_table.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Observed Values :-\n",
      " [[60 97]\n",
      " [33 54]]\n"
     ]
    }
   ],
   "source": [
    "#Observed Values\n",
    "Observed_Values = dataset_table.values \n",
    "print(\"Observed Values :-\\n\",Observed_Values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "val=stats.chi2_contingency(dataset_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.008763290531773594, 0.925417020494423, 1, array([[59.84016393, 97.15983607],\n",
       "        [33.15983607, 53.84016393]]))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[59.84016393 97.15983607]\n",
      " [33.15983607 53.84016393]]\n"
     ]
    }
   ],
   "source": [
    "Expected_Values=val[3]\n",
    "print(Expected_Values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Degree of Freedom:- 1\n"
     ]
    }
   ],
   "source": [
    "no_of_rows=len(dataset_table.iloc[0:2,0])\n",
    "no_of_columns=len(dataset_table.iloc[0,0:2])\n",
    "ddof=(no_of_rows-1)*(no_of_columns-1)\n",
    "print(\"Degree of Freedom:-\",ddof)\n",
    "alpha = 0.05"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import chi2\n",
    "chi_square=sum([(o-e)**2./e for o,e in zip(Observed_Values,Expected_Values)])\n",
    "chi_square_statistic=chi_square[0]+chi_square[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chi-square statistic:- 0.001934818536627623\n"
     ]
    }
   ],
   "source": [
    "print(\"chi-square statistic:-\",chi_square_statistic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "critical_value: 3.841458820694124\n"
     ]
    }
   ],
   "source": [
    "critical_value=chi2.ppf(q=1-alpha,df=ddof)\n",
    "print('critical_value:',critical_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p-value: 0.964915107315732\n",
      "Significance level:  0.05\n",
      "Degree of Freedom:  1\n",
      "p-value: 0.964915107315732\n"
     ]
    }
   ],
   "source": [
    "#p-value\n",
    "p_value=1-chi2.cdf(x=chi_square_statistic,df=ddof)\n",
    "print('p-value:',p_value)\n",
    "print('Significance level: ',alpha)\n",
    "print('Degree of Freedom: ',ddof)\n",
    "print('p-value:',p_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Retain H0,There is no relationship between 2 categorical variables\n",
      "Retain H0,There is no relationship between 2 categorical variables\n"
     ]
    }
   ],
   "source": [
    "if chi_square_statistic>=critical_value:\n",
    "    print(\"Reject H0,There is a relationship between 2 categorical variables\")\n",
    "else:\n",
    "    print(\"Retain H0,There is no relationship between 2 categorical variables\")\n",
    "    \n",
    "if p_value<=alpha:\n",
    "    print(\"Reject H0,There is a relationship between 2 categorical variables\")\n",
    "else:\n",
    "    print(\"Retain H0,There is no relationship between 2 categorical variables\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Anova Test(F-Test)\n",
    "    The t-test works well when dealing with two groups, but sometimes we want to compare more than two groups at the same time.\n",
    "\n",
    "    For example, if we wanted to test whether petal_width age differs based on some categorical variable like species, we have to compare the means of each level or group the variable\n",
    "\n",
    "### One Way F-test(Anova) :-\n",
    "    It tell whether two or more groups are similar or not based on their mean similarity and f-score.\n",
    "\n",
    "    Example : there are 3 different category of iris flowers and their petal width and need to check whether all 3 group are similar or not\n",
    "    \n",
    "![One way F-test](oneWayFtest.png)\n",
    "\n",
    "[to know more click here](https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.statsdirect.com%2Fhelp%2Fanalysis_of_variance%2Fone_way.htm&psig=AOvVaw1aIlgu5ivUzUtE3E1HMgqz&ust=1585194693127000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPi0is7ctOgCFQAAAAAdAAAAABAI)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = sns.load_dataset('iris')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_anova = df1[['petal_width','species']]\n",
    "\n",
    "grps = pd.unique(df_anova.species.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['setosa', 'versicolor', 'virginica'], dtype=object)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "d_data = {grp:df_anova['petal_width'][df_anova.species == grp] for grp in grps}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'setosa': 0     0.2\n",
       " 1     0.2\n",
       " 2     0.2\n",
       " 3     0.2\n",
       " 4     0.2\n",
       " 5     0.4\n",
       " 6     0.3\n",
       " 7     0.2\n",
       " 8     0.2\n",
       " 9     0.1\n",
       " 10    0.2\n",
       " 11    0.2\n",
       " 12    0.1\n",
       " 13    0.1\n",
       " 14    0.2\n",
       " 15    0.4\n",
       " 16    0.4\n",
       " 17    0.3\n",
       " 18    0.3\n",
       " 19    0.3\n",
       " 20    0.2\n",
       " 21    0.4\n",
       " 22    0.2\n",
       " 23    0.5\n",
       " 24    0.2\n",
       " 25    0.2\n",
       " 26    0.4\n",
       " 27    0.2\n",
       " 28    0.2\n",
       " 29    0.2\n",
       " 30    0.2\n",
       " 31    0.4\n",
       " 32    0.1\n",
       " 33    0.2\n",
       " 34    0.2\n",
       " 35    0.2\n",
       " 36    0.2\n",
       " 37    0.1\n",
       " 38    0.2\n",
       " 39    0.2\n",
       " 40    0.3\n",
       " 41    0.3\n",
       " 42    0.2\n",
       " 43    0.6\n",
       " 44    0.4\n",
       " 45    0.3\n",
       " 46    0.2\n",
       " 47    0.2\n",
       " 48    0.2\n",
       " 49    0.2\n",
       " Name: petal_width, dtype: float64, 'versicolor': 50    1.4\n",
       " 51    1.5\n",
       " 52    1.5\n",
       " 53    1.3\n",
       " 54    1.5\n",
       " 55    1.3\n",
       " 56    1.6\n",
       " 57    1.0\n",
       " 58    1.3\n",
       " 59    1.4\n",
       " 60    1.0\n",
       " 61    1.5\n",
       " 62    1.0\n",
       " 63    1.4\n",
       " 64    1.3\n",
       " 65    1.4\n",
       " 66    1.5\n",
       " 67    1.0\n",
       " 68    1.5\n",
       " 69    1.1\n",
       " 70    1.8\n",
       " 71    1.3\n",
       " 72    1.5\n",
       " 73    1.2\n",
       " 74    1.3\n",
       " 75    1.4\n",
       " 76    1.4\n",
       " 77    1.7\n",
       " 78    1.5\n",
       " 79    1.0\n",
       " 80    1.1\n",
       " 81    1.0\n",
       " 82    1.2\n",
       " 83    1.6\n",
       " 84    1.5\n",
       " 85    1.6\n",
       " 86    1.5\n",
       " 87    1.3\n",
       " 88    1.3\n",
       " 89    1.3\n",
       " 90    1.2\n",
       " 91    1.4\n",
       " 92    1.2\n",
       " 93    1.0\n",
       " 94    1.3\n",
       " 95    1.2\n",
       " 96    1.3\n",
       " 97    1.3\n",
       " 98    1.1\n",
       " 99    1.3\n",
       " Name: petal_width, dtype: float64, 'virginica': 100    2.5\n",
       " 101    1.9\n",
       " 102    2.1\n",
       " 103    1.8\n",
       " 104    2.2\n",
       " 105    2.1\n",
       " 106    1.7\n",
       " 107    1.8\n",
       " 108    1.8\n",
       " 109    2.5\n",
       " 110    2.0\n",
       " 111    1.9\n",
       " 112    2.1\n",
       " 113    2.0\n",
       " 114    2.4\n",
       " 115    2.3\n",
       " 116    1.8\n",
       " 117    2.2\n",
       " 118    2.3\n",
       " 119    1.5\n",
       " 120    2.3\n",
       " 121    2.0\n",
       " 122    2.0\n",
       " 123    1.8\n",
       " 124    2.1\n",
       " 125    1.8\n",
       " 126    1.8\n",
       " 127    1.8\n",
       " 128    2.1\n",
       " 129    1.6\n",
       " 130    1.9\n",
       " 131    2.0\n",
       " 132    2.2\n",
       " 133    1.5\n",
       " 134    1.4\n",
       " 135    2.3\n",
       " 136    2.4\n",
       " 137    1.8\n",
       " 138    1.8\n",
       " 139    2.1\n",
       " 140    2.4\n",
       " 141    2.3\n",
       " 142    1.9\n",
       " 143    2.3\n",
       " 144    2.5\n",
       " 145    2.3\n",
       " 146    1.9\n",
       " 147    2.0\n",
       " 148    2.3\n",
       " 149    1.8\n",
       " Name: petal_width, dtype: float64}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "F, p = stats.f_oneway(d_data['setosa'], d_data['versicolor'], d_data['virginica'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.169445839443116e-85\n"
     ]
    }
   ],
   "source": [
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We are accepting null hypothesis\n"
     ]
    }
   ],
   "source": [
    "function(p)"
   ]
  },
  {
   "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
