{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "from ipywidgets import *\n", "import matplotlib.pyplot as plt\n", "from IPython.display import set_matplotlib_formats\n", "set_matplotlib_formats('svg')\n", "import numpy as np\n", "import scipy.stats as stats\n", "import matplotlib.patches as mpatches" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def chi_sq(k=1):\n", " fig, axes = plt.subplots(figsize=(10,5))\n", " x = np.linspace(0, 80, 1000)\n", " chi2 = stats.chi2.pdf(x, k)\n", " plt.plot(x, chi2)\n", " plt.xlabel(\"x\")\n", " plt.ylabel(\"f(x)\")\n", " plt.title(r\"$\\chi^2($k=\"+str(k)+\"$)$\")\n", " plt.xlim(0,80)\n", " plt.grid()\n", "\n", "def t_dist(k=1):\n", " fig, axes = plt.subplots(figsize=(10,5))\n", " x = np.linspace(-3.25, 3.25, 1000)\n", " t = stats.t.pdf(x, k)\n", " norm = stats.norm.pdf(x)\n", " plt.plot(x, t, label=\"t(\"+str(k)+\")\")\n", " plt.plot(x, norm, c='k', linestyle=\"--\", label=\"N(0,1)\")\n", " quant = -1.64\n", " plt.vlines(x=quant, ymin=0, ymax=stats.t.pdf(quant, k), color=\"#ff4c4c\", alpha=0.5)\n", " x2 = np.linspace(-3.25, -1.64, 1000)\n", " t2 = stats.t.pdf(x2, k)\n", " plt.fill_between(x2, t2, color=\"#ff4c4c\", alpha=0.5)\n", " plt.xlabel(\"x\")\n", " plt.ylabel(\"f(x)\")\n", " plt.title(r\"$P(T<\"+str(quant)+\")=\"+str(round(stats.t.cdf(quant, k),3))+\"$\")\n", " plt.xlim(-3.25,3.25)\n", " plt.ylim(0, 0.42)\n", " plt.legend()\n", " plt.grid()\n", " \n", "def norm_plain(mu, sigma):\n", " fig, axes = plt.subplots(figsize=(10,5))\n", " x = np.linspace(mu-3.25*sigma, mu+3.25*sigma, 1000)\n", " norm = stats.norm.pdf(x, mu, sigma)\n", " plt.plot(x, norm, mu, sigma)\n", " plt.xlabel(\"x\")\n", " plt.ylabel(\"f(x)\")\n", " plt.xlim(mu-3.25*sigma, mu+3.25*sigma)\n", " ymax = stats.norm.pdf(mu, mu, sigma)\n", " ymax += 0.025*ymax\n", " plt.ylim(0, ymax)\n", " plt.grid()\n", " \n", "def norm(n=30, x_bar=98):\n", " mu = 100\n", " sigma = 15/np.sqrt(n)\n", " norm_plain(mu, sigma)\n", " quant = -1.64*sigma+mu\n", " plt.vlines(x=x_bar, ymin=0, ymax=stats.norm.pdf(x_bar, mu, sigma), color=\"k\", alpha=0.5)\n", " x2 = np.linspace(mu-3.25*sigma, quant, 1000)\n", " norm2 = stats.norm.pdf(x2, mu, sigma)\n", " plt.fill_between(x2, norm2, color=\"#ff4c4c\", alpha=0.5)\n", " plt.xlim(91, 109)\n", " \n", "def conf_int(x_bar=98):\n", " n = 30\n", " mu = 100\n", " sigma = 15/np.sqrt(n)\n", " norm_plain(mu, sigma)\n", " alpha = 0.05\n", " quants = [stats.norm.ppf(a, mu, sigma) for a in [alpha/2, 1-alpha/2]]\n", " for start, end in zip([mu-3.25*sigma, quants[1]], [quants[0], mu+3.25*sigma]):\n", " x2 = np.linspace(start, end, 1000)\n", " norm2 = stats.norm.pdf(x2, mu, sigma)\n", " plt.fill_between(x2, norm2, color=\"#ff4c4c\", alpha=0.5)\n", " quant = stats.norm.ppf(1-alpha/2)\n", " delta = quant*sigma\n", " lower, upper = x_bar-delta, x_bar+delta\n", " plt.scatter(x_bar, 0.001, c='r', zorder=2)\n", " plt.hlines(y=0.001, xmin=lower, xmax=upper, color='r')\n", " plt.vlines(x=lower, ymin=0, ymax=0.005, color='r')\n", " plt.vlines(x=upper, ymin=0, ymax=0.005, color='r')\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Testy t i Z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rozkład $\\chi^2$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$Z_i \\sim N(0,1)$\n", "\n", "$$Q = \\sum_{i=1}^{k}Z_i^2$$\n", "\n", "$$Q \\sim \\chi^2(k)$$" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8a0c0f1cd7c34c91a0dfe3c863d646e3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(IntSlider(value=1, description='k', max=40, min=1), Output()), _dom_classes=('widget-int…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interact(chi_sq, k=(1,40,1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$E[Q]=k$\n", "\n", "$D^2[Q] = 2k$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rozkład t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$Z \\sim N(0,1)$\n", "
$V \\sim \\chi^2(k)$\n", "\n", "$$ T = \\frac{Z}{\\sqrt{\\frac{V}{k}}} \\sim t(k)$$" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c5e0188e1c734048829867e5a5eda294", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(IntSlider(value=1, description='k', min=1), Output()), _dom_classes=('widget-interact',)…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interact(t_dist, k=(1,100,1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$E[T] = 0 \\;\\textrm{ dla }\\; k > 1$\n", "\n", "$D^2[T] = \\frac{k}{k-2} \\;\\textrm{ dla }\\; k > 2$\n", "\n", "---\n", "\n", "$\\displaystyle \\sum_{i=1}^{n}X_i^2 \\sim \\chi^2(n) \\textrm{,}\\;\\; X_i \\sim N(0,1)$\n", "\n", "$\\displaystyle \\sum_{i=1}^{n}\\left(\\frac{X_i-\\mu}{\\sigma}\\right)^2 \\sim \\chi^2(n) \\textrm{,}\\;\\; X_i \\sim N(\\mu,\\sigma)$\n", "\n", "$\\displaystyle \\sum_{i=1}^{n}\\left(\\frac{X_i-\\bar{X}}{\\sigma}\\right)^2 = \\frac{(n-1)S^2}{\\sigma^2} \\sim \\chi^2(n-1)$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "$ T = \\frac{\\bar{X} - \\mu}{S/\\sqrt{n}} $\n", "$= \\frac{\\frac{\\bar{X} - \\mu}{\\sigma/\\sqrt{n}}}{\\sqrt{\\frac{(n-1)S^2}{(n-1)\\sigma^2}}}$\n", "$= \\frac{Z}{\\sqrt{\\frac{V}{(n-1)}}}\\sim t(n-1) $\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test Z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- $H_0: \\mu = \\mu_0$\n", "
$H_1: \\mu > / \\neq / < \\mu_0$\n", "- statystyka:\n", "$$\\displaystyle Z=\\frac{\\bar{X}-\\mu_0}{\\frac{\\sigma}{\\sqrt{n}}}$$\n", "- zbiór krytyczny:\n", " - test prawostronny: $(z_{kr},\\infty)$\n", " - test lewostronny: $(-\\infty, -z_{kr})$\n", " - test dwustronny: $(-\\infty, -z_{kr}) \\cup (z_{kr},\\infty)$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- $H_0: \\mu = \\mu_0$\n", "
$H_1: \\mu > / \\neq / < \\mu_0$\n", "- statystyka:\n", "$$t=\\frac{\\bar{X}-\\mu_0}{\\frac{S}{\\sqrt{n}}}$$\n", "- zbiór krytyczny:\n", " - test prawostronny: $(t_{kr},\\infty)$\n", " - test lewostronny: $(-\\infty, -t_{kr})$\n", " - test dwustronny: $(-\\infty, -t_{kr}) \\cup (t_{kr},\\infty)$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Wybór testu" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Wybór testu](http://www.cs.put.poznan.pl/amensfelt/pub/wybor_testu.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Przykład" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zmierzono czas działania baterii 16 losowo wybranych smartfonów modelu X podczas przeciętnego użytkowania i otrzymano średni czas 22.5 godziny i odchylenie standardowe 4 godziny. Wiedząc, że czas działania baterii ma rozkład normalny sprawdź, czy dla modelu X jest on mniejszy niż 24 godziny. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$H_0:$\n", "
$H_1:$\n", "\n", "$\\alpha=$\n", "\n", "$C_{kr} = $\n", "
$t(,) = $\n", "
$C_{kr} = $\n", "\n", "$t=\\frac{\\bar{X}-\\mu_0}{\\frac{S}{\\sqrt{n}}} = $\n", "\n", "$t ? C_{kr}$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test statystyczny a przedział ufności" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a4cfc9ac2f604c35a4e03412818425b3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(IntSlider(value=98, description='x_bar', max=110, min=90), Output()), _dom_classes=('wid…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interact(conf_int, x_bar=(90, 110, 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Wpływ rozmiaru próby i istotność praktyczna" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9b5a3f74700049cc8671b7c3b29a02a5", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(IntSlider(value=30, description='n', max=1000, min=30, step=10), FloatSlider(value=99.0,…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "interact(norm, n=(30, 1000, 10), x_bar=(99, 100, 0.1))" ] } ], "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.9" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 4 }