{
"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": [
"