{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "oEidUj12Pc1s",
    "outputId": "a711cf45-3051-447f-be8a-495e10e7f2fc",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Confidence Interval: (222.1160773511857, 257.8839226488143)\n"
     ]
    }
   ],
   "source": [
    "import scipy.stats as stats\n",
    "import math\n",
    "\n",
    "# Given values\n",
    "sample_mean = 240\n",
    "sample_std_dev = 25\n",
    "sample_size = 10\n",
    "confidence_level = 0.95\n",
    "\n",
    "# DF\n",
    "df = sample_size - 1\n",
    "\n",
    "# Significance level (α)\n",
    "alpha = (1 - confidence_level) / 2\n",
    "\n",
    "# t-value from the t-distribution table\n",
    "t_value = stats.t.ppf(1 - alpha, df)\n",
    "\n",
    "margin_of_error = t_value * (sample_std_dev / math.sqrt(sample_size))\n",
    "\n",
    "lower_limit = sample_mean - margin_of_error\n",
    "upper_limit = sample_mean + margin_of_error\n",
    "\n",
    "print(f\"Confidence Interval: ({lower_limit}, {upper_limit})\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "6_w0e9bIRjPe",
    "outputId": "c4217e46-18d6-4c89-8f37-ffeae785c76d",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z-Score : 4.714045207910317\n",
      "Critical Z-Score : 1.6448536269514722\n",
      "Reject Null Hypothesis\n",
      "p-value : 1.2142337364462463e-06\n",
      "Reject Null Hypothesis\n"
     ]
    }
   ],
   "source": [
    "# Import the necessary libraries\n",
    "import numpy as np\n",
    "import scipy.stats as stats\n",
    "\n",
    "# Given information\n",
    "sample_mean = 110\n",
    "population_mean = 100\n",
    "population_std = 15\n",
    "sample_size = 50\n",
    "alpha = 0.05\n",
    "\n",
    "# compute the z-score\n",
    "z_score = (sample_mean-population_mean)/(population_std/np.sqrt(sample_size))\n",
    "print('Z-Score :',z_score)\n",
    "\n",
    "# Approach 1: Using Critical Z-Score\n",
    "\n",
    "# Critical Z-Score\n",
    "z_critical = stats.norm.ppf(1-alpha)\n",
    "print('Critical Z-Score :',z_critical)\n",
    "\n",
    "# Hypothesis\n",
    "if z_score >  z_critical:\n",
    "    print(\"Reject Null Hypothesis\")\n",
    "else:\n",
    "  print(\"Fail to Reject Null Hypothesis\")\n",
    "\n",
    "# Approach 2: Using P-value\n",
    "\n",
    "# P-Value : Probability of getting less than a Z-score\n",
    "p_value = 1-stats.norm.cdf(z_score)\n",
    "\n",
    "print('p-value :',p_value)\n",
    "\n",
    "# smaller p-value indicates stronger evidence against the null hypothesis.\n",
    "\n",
    "# Hypothesis\n",
    "if p_value <  alpha:\n",
    "    print(\"Reject Null Hypothesis\")\n",
    "else:\n",
    "  print(\"Fail to Reject Null Hypothesis\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "qvw1L2grR7zT",
    "outputId": "06a8c967-d75f-47a7-9c84-fab208475777"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Chi2ContingencyResult(statistic=0.8640353908896108, pvalue=0.6491978887380976, dof=2, expected_freq=array([[115. ,  92.5,  42.5],\n",
       "       [115. ,  92.5,  42.5]]))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scipy.stats as stats\n",
    "data = [[120, 90, 40],\n",
    "        [110, 95, 45]]\n",
    "#perform the Chi-Square Test of Independence\n",
    "stats.chi2_contingency(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "ZXiu_YyOKJTn",
    "outputId": "c76ed879-e1b2-49e3-a922-192111b1895c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T-statistic value: -1.298928415375236\n",
      "P-value: 0.2205360614433977\n"
     ]
    }
   ],
   "source": [
    "import scipy.stats as stats\n",
    "\n",
    "# Define the data\n",
    "data = [300, 315, 320, 311, 314, 309, 300, 308, 305, 303, 305, 301]\n",
    "\n",
    "# Perform the one-sample t-test\n",
    "t_stat, p_value = stats.ttest_1samp(a=data, popmean=310)\n",
    "\n",
    "print(\"T-statistic value:\", t_stat)\n",
    "print(\"P-value:\", p_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "j88Ty-VdLp12"
   },
   "source": [
    "Interpret the Results:\n",
    "If the p-value is less than 0.05, we reject the null hypothesis and conclude that the mean height is less than 310 cm.\n",
    "Otherwise, we fail to reject the null hypothesis."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "cLRVXAOTM_ON"
   },
   "source": [
    "## Paired-T test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "0YB6wBh8NFbS"
   },
   "outputs": [],
   "source": [
    "pre = [88, 82, 84, 93, 75, 78, 84, 87, 95, 91, 83, 89, 77, 68, 91]\n",
    "post = [91, 84, 88, 90, 79, 80, 88, 90, 90, 96, 88, 89, 81, 74, 92]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5oLo7rYpNYdT"
   },
   "source": [
    "H0: The mean pre-test and post-test scores are equal\n",
    "\n",
    "HA:The mean pre-test and post-test scores are not equal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ywnT9GbINJ0s"
   },
   "source": [
    "ttest_rel() function from Scipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "D3AHkkrqNLea",
    "outputId": "9630680a-f2d3-43d4-dbde-ff3361e1b994"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TtestResult(statistic=-2.9732484231168796, pvalue=0.01007144862643272, df=14)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scipy.stats as stats\n",
    "\n",
    "#perform the paired samples t-test\n",
    "stats.ttest_rel(pre, post)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EzDmxtc3NaVV"
   },
   "source": [
    "Since the p-value (0.0101) is less than 0.05, we reject the null hypothesis. We have sufficient evidence to say that the true mean test score is different for students before and after participating in the study program."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python [conda env:ns-user-38_v1]",
   "language": "python",
   "name": "conda-env-ns-user-38_v1-py"
  },
  "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.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
