{
"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",
"plt.rcParams['axes.grid'] = True"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def dice():\n",
" plt.stem(range(1,7), [1/6]*6, use_line_collection=True)\n",
" plt.xlabel(\"Wynik\")\n",
" plt.ylabel(\"Prawdopodobieństwo\")\n",
" \n",
"def dice_dist(): \n",
" y = [1/6*i for i in range(7)]\n",
" plt.hlines(y, range(0, 7), range(1, 8), color='#1f77b4')\n",
" plt.plot(range(1, 7), y[1:], 'o')\n",
" plt.plot(range(1, 7), y[:-1], 'o', fillstyle='none', color='#1f77b4')\n",
" plt.xlabel(\"Wynik\")\n",
" plt.ylabel(\"Dystrybuanta\")\n",
" plt.xlim(0,7)\n",
"\n",
"def normal(density=True):\n",
" x = np.linspace(-3, 3, 100)\n",
" fun = stats.norm.pdf if density else stats.norm.cdf\n",
" plt.plot(x, fun(x, 0, 1))\n",
" plt.xlabel(\"x\")\n",
" ylab = \"f(x)\" if density else \"F(x)\"\n",
" plt.ylabel(ylab)\n",
" \n",
"def binary(p=0.5):\n",
" plt.stem([0,1], [1-p, p], use_line_collection=True)\n",
" plt.xlabel(\"x\")\n",
" plt.ylabel(\"P(X=x)\")\n",
" \n",
"def binom(n=10, p=0.5):\n",
" plt.stem(range(n+1), stats.binom.pmf(range(n+1), n, p), use_line_collection=True)\n",
" plt.xlabel(\"x\")\n",
" plt.ylabel(\"P(X=x)\")\n",
"\n",
"def uniform(a=0, b=1):\n",
" x_lim = 4\n",
" p = 1/(b-a)\n",
" y = [0, p, 0]\n",
" plt.hlines(y, [-x_lim, a, b], [a, b, x_lim], color='#1f77b4')\n",
" plt.plot([a, b], [p, p], 'o')\n",
" plt.plot([a, b], [0, 0], 'o', fillstyle='none', color='#1f77b4')\n",
" plt.xlabel(\"x\")\n",
" plt.ylabel(\"f(x)\")\n",
" plt.xlim(-x_lim, x_lim)\n",
" \n",
"def normal_param(mu=0, sigma=1):\n",
" x_min = -10\n",
" x_max = 10\n",
" x = np.linspace(x_min, x_max, 100)\n",
" plt.plot(x, stats.norm.pdf(x, mu, sigma))\n",
" plt.xlabel(\"x\")\n",
" plt.ylabel(\"f(x)\")\n",
" plt.xlim(x_min,x_max)\n",
" plt.ylim(0,0.45)\n",
" \n",
"def normal_s(mu=0, sigma=1, a=0, b=1):\n",
" x_min = -10\n",
" x_max = 10\n",
" x_s = np.linspace(-3, 3, 100)\n",
" x = np.linspace(x_min, x_max, 100)\n",
" plt.plot(x_s, stats.norm.pdf(x_s, 0, 1))\n",
" plt.plot(x, stats.norm.pdf(x, mu+a, sigma/b))\n",
" plt.xlabel(\"x\")\n",
" plt.ylabel(\"f(x)\")\n",
" plt.xlim(x_min,x_max)\n",
" plt.ylim(0,0.45)\n",
"\n",
"def fill(lower, upper, mu, sigma): \n",
" x = np.linspace(lower, upper, 100)\n",
" y = stats.norm.pdf(x,mu,sigma)\n",
" plt.fill_between(x, y, color='#0b559f', alpha=0.5)\n",
" \n",
"def normal_shade(lower=0, upper=0):\n",
" mu = 0\n",
" sigma = 1\n",
" x = np.linspace(-3.5*sigma, 3.5*sigma, 100)\n",
" plt.plot(x, stats.norm.pdf(x, mu, sigma))\n",
" plt.xlabel(\"x\")\n",
" plt.ylabel(\"f(x)\")\n",
" fill(lower, upper, mu, sigma)\n",
" plt.xlim(-3.25,3.25)\n",
"\n",
"def monte_carlo(k=1):\n",
" n = 10**k\n",
" circle = plt.Circle((0, 0), 1, alpha=0.5)\n",
" fig, ax = plt.subplots(figsize=(5,5))\n",
" ax.add_artist(circle)\n",
" points = np.random.uniform(size=(2,n))\n",
" plt.scatter(*points, s=5, c='r')\n",
" pi = np.mean(np.linalg.norm(points, axis=0)<1)*4\n",
" plt.title(r\"$n=$\"+str(n)+r\" $\\pi=$\"+str(pi))\n",
" plt.xlim(0,1)\n",
" plt.ylim(0,1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rozkłady prawdopodobieństwa"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pojęcia"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Doświadczenie losowe\n",
"- Zbiór zdarzeń elementarnych $\\Omega$\n",
"- Zdarzenie elementarne\n",
"- Zdarzenie\n",
"- Zmienna losowa $X: \\Omega \\rightarrow R$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Zmienna losowa dyskretna"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Funkcja prawdopodobieństwa"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\displaystyle p_i = P (X = x_i)$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"dice()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dystrybuanta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\displaystyle F(x_0) = \\sum_{x_i \\leq x_0} P(X=x_i)$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"dice_dist()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Zmienna losowa ciągła"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Funkcja gęstości"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$f(x)$"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"normal()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dystrybuanta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$F(x_0) = \\int\\limits_{-\\infty}^{x_0}f(x)dx$"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"normal(density=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Wskaźniki położenia i rozproszenia"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Wartość oczekiwana"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$E[X] = \\sum x \\cdot P(X = x)$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Wariancja"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$D^2[X] = \\sum (x - E[X])^2 \\cdot P (X = x) $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Odchylenie standardowe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$D[X] = \\sqrt{D^2[X]}$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Własności wartości oczekiwanej i wariancji"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$E[X+Y]=E[X]+E[Y]$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$E[k*X] = k*E[X]$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$D^2[X+Y] = D^2[X]+D^2[Y]\\textrm{, jeśli } X \\textrm{ i } Y \\textrm{ niezależne}$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$D^2[k*X] = k^2*D^2[X]$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Rozkład dwupunktowy (zero-jedynkowy)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4f5fe01479ef4556b7cfd73992ffb7af",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.5, description='p', max=1.0), Output()), _dom_classes=('widget-inter…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(binary, p=(0,1,0.1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- $X \\sim B_1(p)$\n",
"- $X \\in \\{0,1\\}$\n",
"- $P (X = 1) = p$\n",
"- $P (X = 0) = 1 - p$\n",
"- $E[X] =$\n",
"- $D^2[X]=$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Rozkład dwumianowy"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "debf7a95d947400ea1dca87b23325efb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=10, description='n', max=10, min=5), FloatSlider(value=0.5, description=…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(binom, n=(5,10,1), p=(0,1,0.1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- $X \\sim B_n(p)$\n",
"- $n \\in N$\n",
"- $P (X = k) = {{n}\\choose{k}} p^k(1-p)^{n-k}, k = 0, .., n$\n",
"- $E[X] =$\n",
"- $D^2[X] =$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Rozkład jednostajny ciągły"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "cef6215a5e534e63991de230ca143a0a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=0, description='a', max=3, min=-3), IntSlider(value=1, description='b', …"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(uniform, a=(-3,3,1), b=(-3,3,1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- $X \\sim U(a,b)$\n",
"- $a,b \\in \\mathbf{R}$\n",
"- $f(x)={\\begin{cases}{\\frac {1}{b-a}}&\\mathrm {dla} \\ a\\leq x\\leq b,\\\\[8pt]0&\\mathrm {dla} \\ xb\\end{cases}}$\n",
"- $E[X] = \\frac{a+b}{2}$\n",
"- $D^2[X] = \\frac{(b-a)^2}{12}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Rozkład normalny"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2ab08a7025b44e14b9ec20b36cc0b58b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=0, description='mu', max=10, min=-10), IntSlider(value=1, description='s…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(normal_param, mu=(-10,10,1), sigma=(1,5,1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- $X \\sim N(\\mu,\\sigma)$\n",
"- $\\mu \\in \\mathbf{R}, \\sigma \\in \\mathbf{R}_+$\n",
"- $f(x) = \\frac{1}{\\sigma\\sqrt{2\\pi}}e^{-\\frac{(x - \\mu)^2}{2\\sigma^2}}$, $x \\in \\mathbf{R}$ \n",
"- $E[X] = \\mu$\n",
"- $D^2[X] = \\sigma^2$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reguła 3 sigm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Źródło](https://upload.wikimedia.org/wikipedia/commons/thumb/3/37/Standard_deviation_diagram_%28decimal_comma%29.svg/1024px-Standard_deviation_diagram_%28decimal_comma%29.svg.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Standaryzacja"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"m = -5 sd = 1\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4293308be28e49a1a45428328e31deda",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=0, description='a', max=5, min=-5), IntSlider(value=1, description='b', …"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mu = np.random.randint(-5, 5)\n",
"sigma = np.random.randint(1,5)\n",
"print(\"m =\", mu, \"sd =\", sigma)\n",
"interact(normal_s, mu=fixed(mu), sigma=fixed(sigma), a = (-5,5,1), b=(1,5,1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$Z = \\frac{X - E[X]}{D[X]}$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- $E[Z] = 0$\n",
"- $D^2[Z] = 1$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Przykłady"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "788d4550c7f04e72ac83ba37f78c021a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='lower', max=3.5, min=-3.5), FloatSlider(value=0.0, d…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(normal_shade, lower=(-3.5,3.5,0.1), upper=(-3.5,3.5,0.1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$X \\sim N(0, 1)$\n",
"\n",
"- $P(X < 1.5) =$ \n",
"- $P(X > 1.5) =$ \n",
"- $P(-1.5 \\leq X \\leq 2) =$\n",
"- $P(X < x) = 0.6591$\n",
"\n",
"$X \\sim N(10, 5)$\n",
"\n",
"- $P(8 \\leq X \\leq 12) =$\n",
"- $Y=\\sum_{i=1}^4X_i$\n",
"- $P(Y < 50) = $\n",
"\n",
"$IQ \\sim N(100, 15)$\n",
"- top 10%?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Centralne twierdzenie graniczne"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Niech $X_1, X_2, ..., X_n$ będzie ciągiem zmiennych losowych:\n",
"- niezależnych\n",
"- o takim samym rozkładzie\n",
"- takich że $E[X_i] = \\mu < \\infty$\n",
"- takich że $0 < D^2[X_i] = \\sigma^2 < \\infty$\n",
"Niech:\n",
"$$\\bar{X_n} = \\frac{1}{n} \\sum\\limits_{i=1}^{n} X_i$$\n",
"\n",
"$$U_n = \\frac{\\bar{X_i}-\\mu}{\\sigma} \\cdot \\sqrt{n}$$\n",
"\n",
"Wtedy:\n",
"$$\\forall u \\in \\mathbf{R} \\lim\\limits_{n\\rightarrow\\infty} P(U_n < u) = \\Phi(u)$$\n",
"\n",
"Dla sum:\n",
"$$S_n = \\sum\\limits_{i=1}^{n}X_i$$\n",
"\n",
"$$Z_n = \\frac{S_n - n\\cdot\\mu}{\\sigma\\cdot\\sqrt{n}}$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Metoda Monte Carlo"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e42bc1529b0048ce95ccaa7120ef5c39",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=1, description='k', max=4, min=1), Output()), _dom_classes=('widget-inte…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
""
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(monte_carlo, k=(1,4,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
}