From 8e1c73880988744a7efb035abdac8de24edd0e3e Mon Sep 17 00:00:00 2001 From: irustam <49302555+irustam@users.noreply.github.com> Date: Sun, 22 Sep 2019 17:01:21 +0300 Subject: [PATCH] Add files via upload --- homework5.ipynb | 854 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 854 insertions(+) create mode 100644 homework5.ipynb diff --git a/homework5.ipynb b/homework5.ipynb new file mode 100644 index 0000000..5f69a2e --- /dev/null +++ b/homework5.ipynb @@ -0,0 +1,854 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import itertools as it\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.mlab as mlab\n", + "import itertools" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Задания к 5 уроку\n", + "#### 1. Напишите код, моделирующий выпадение поля в рулетке (с учетом поля зеро)." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "roulete = {}\n", + "numbers = list(range(37))\n", + "for i in numbers:\n", + " roulete[str(i)] = 0" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "n = 1000000\n", + "for _ in range(0, n):\n", + " x = np.random.randint(0, 37)\n", + " roulete[str(x)] += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ver = 0\n", + "for v in roulete.values():\n", + " ver += v/n\n", + "ver" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. 1) Напишите код, проверяющий любую из теорем сложения или умножения вероятности на примере рулетки или подбрасывания монетки." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "499998 500002\n" + ] + } + ], + "source": [ + "k = 0\n", + "m = 0\n", + "n = 1000000\n", + "for _ in range(0, n):\n", + " x = np.random.uniform(0, 1)\n", + " if x < 0.5:\n", + " k += 1\n", + " else:\n", + " m += 1\n", + "print(k, m)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "ver_k = k/n\n", + "ver_m = m/n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Складываем вероятности\n", + "ver_k+ver_m" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.24999999999599998" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Умножаем вероятности, чтобы узнать вероятность выпадения последовательности из решки и орла\n", + "ver_k*ver_m" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2) Сгенерируйте десять выборок случайных чисел х0, …, х9. И постройте гистограмму распределения случайной суммы х0+х1+ …+ х9." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "n = 10\n", + "m = 100000\n", + "x = {}\n", + "for i in range(0, n):\n", + " x[str(i)] = np.random.rand(m)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([5.58633162, 3.68387149, 4.14942075, ..., 7.01081517, 4.29948679,\n", + " 5.74087405])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_x = 0\n", + "for v in x.values():\n", + " sum_x += v\n", + "sum_x" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZEAAAEGCAYAAACk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r6+/c03IiLG0I0iMgQM2b6j7N9AVVR+WIapKO+7GtrPazh/LvDwOPGIiOiQjheR8gj5nZKOLaFFVEvubgRGZlgtA24u2xuB88ssrZOBx8pw1y3A6ZJmlxvqp5dYRER0SKvPzpps7wI+I+kgYDtwAVVB2yDpQuAh4C2l7Saq5XkHgZ+VttjeLelS4K7S7oO2d3fuEiIioitFxPa3qB6nMtqiJm0NrBjjc9YCayc3u4iIaFU37olERMSzRIpIRETU1q17IhHxLDF/5edrn7tj1dmTmEl0Q3oiERFRW4pIRETUliISERG1pYhERERtKSIREVFbikhERNSWIhIREbWliERERG0pIhERUVuKSERE1JYiEhERtaWIREREbSkiERFRW4pIRETU1rUiImmGpHslfa7sL5B0h6Rtkq4vS+ci6eCyP1iOz2/4jItL/HuSzujOlURETF/d7IlcBGxt2L8CuNL2QmAPcGGJXwjssf0y4MrSDknHAUuB44HFwMclzehQ7hERQZeKiKS5wNnANWVfwGnADaXJOuDcsr2k7FOOLyrtlwDrbT9h+0FgEDixM1cQERHQvZ7IR4D3Ab8s+0cCj9reW/aHgDllew6wE6Acf6y0///xJuc8g6TlkgYkDQwPD0/mdURETGsdXx5X0puAXbbvlvT6kXCTpp7g2HjnPDNorwZWA/T39zdtE5PvQJZNjYje0I011k8BzpF0FnAIcDhVz2SWpJmltzEXeLi0HwLmAUOSZgIvBHY3xEc0nhMRER3Q8eEs2xfbnmt7PtWN8S/bfitwG/Dm0mwZcHPZ3lj2Kce/bNslvrTM3loALATu7NBlREQE3emJjOX9wHpJlwH3AmtKfA3wKUmDVD2QpQC2t0jaADwA7AVW2H6q82lHRExfXS0itr8CfKVsb6fJ7CrbPwfeMsb5lwOXty/DiIgYT36xHhERtaWIREREbSkiERFRW4pIRETUliISERG1pYhERERtKSIREVFbikhERNSWIhIREbWliERERG0pIhERUdtUegBjREwzB7LmzI5VZ09iJlFXeiIREVFbikhERNSWIhIREbWliERERG0pIhERUVvHi4ikeZJuk7RV0hZJF5X4EZI2S9pW3meXuCRd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5KY3oX8JkyXLQduKDL+eyjjN+/EfijbucyFtt3SLoBuIdqiOhepuYjUG6UdCTwC2CF7T0TnZApvhERUVuGsyIiorYUkYiIqC1FJCIiaksRiYiI2lJEIiKithSRiIioLUUkIiJqSxGJ6CJJr5V0X1mr5bCyjsOUeh5ZxHjyY8OILpN0GXAI8DyqZ1X9RZdTimhZikhEl5VHitwF/Bx4ne2nupxSRMsynBXRfUcAzwdeQNUjiegZ6YlEdJmkjVSPrV9AtUzyO7ucUkTL8hTfiC6SdD6w1/bflqcj/4Ok02x/udu5RbQiPZGIiKgt90QiIqK2FJGIiKgtRSQiImpLEYmIiNpSRCIiorYUkYiIqC1FJCIiavt/sGr5ntgr21sAAAAASUVORK5CYII=\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "num_bins = 20\n", + "plt.hist(sum_x, num_bins)\n", + "plt.xlabel('x')\n", + "plt.ylabel('Probability')\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. 1) Дополните код Монте-Карло последовательности независимых испытаний расчетом соответствующих вероятностей (через биномиальное распределение) и сравните результаты." + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "n = 100000\n", + "a = np.random.randint(0, 2, n)\n", + "b = np.random.randint(0, 2, n)\n", + "c = np.random.randint(0, 2, n)\n", + "d = np.random.randint(0, 2, n)\n", + "x = a + b + c + d" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "37557 100000 0.37557\n" + ] + } + ], + "source": [ + "k = 0\n", + "for i in range(0, n):\n", + " if x[i] == 2:\n", + " k += 1\n", + "print(k, n, k/n)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6.0" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c4_2 = np.math.factorial(4)/(np.math.factorial(2)*np.math.factorial(4-2))\n", + "c4_2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Видно, что выше вероятность при увеличении кол-ва экспериментов стремится к 0,375, что совпадает с расчетом по формуле Бернулли" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.375" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p4_2 = c4_2 * (1/2**4)\n", + "p4_2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2) Повторите расчеты биномиальных коэффициентов и вероятностей k успехов в последовательности из n независимых испытаний, взяв другие значения n и k." + ] + }, + { + "cell_type": "code", + "execution_count": 234, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2, 2, 5, ..., 1, 3, 3])" + ] + }, + "execution_count": 234, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "z = 10000\n", + "k = 3\n", + "n = 5\n", + "q = 2\n", + "a = {}\n", + "x = 0\n", + "for i in range(0, n):\n", + " a[str(i)] = np.random.randint(0, q, z)\n", + " x += a[str(i)]\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 235, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3079 10000 0.3079\n" + ] + } + ], + "source": [ + "y = 0\n", + "for i in range(0, z):\n", + " if x[i] == k:\n", + " y += 1\n", + "print(y, z, y/z)" + ] + }, + { + "cell_type": "code", + "execution_count": 236, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 236, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ck_n = np.math.factorial(n)/(np.math.factorial(k)*np.math.factorial(n-k))\n", + "ck_n" + ] + }, + { + "cell_type": "code", + "execution_count": 237, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.5" + ] + }, + "execution_count": 237, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p = (1/q)\n", + "p" + ] + }, + { + "cell_type": "code", + "execution_count": 238, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.3125" + ] + }, + "execution_count": 238, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pk_n = ck_n * (p**k) * ((1-p)**(n-k))\n", + "pk_n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Из урока по комбинаторике повторите расчеты, сгенерировав возможные варианты перестановок для других значений n и k" + ] + }, + { + "cell_type": "code", + "execution_count": 248, + "metadata": {}, + "outputs": [], + "source": [ + "n = 3" + ] + }, + { + "cell_type": "code", + "execution_count": 249, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "000\n", + "001\n", + "010\n", + "011\n", + "100\n", + "101\n", + "110\n", + "111\n" + ] + } + ], + "source": [ + "for p in itertools.product('01', repeat=n):\n", + " print(''.join(p))" + ] + }, + { + "cell_type": "code", + "execution_count": 262, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "012\n", + "013\n", + "014\n", + "015\n", + "016\n", + "021\n", + "023\n", + "024\n", + "025\n", + "026\n", + "031\n", + "032\n", + "034\n", + "035\n", + "036\n", + "041\n", + "042\n", + "043\n", + "045\n", + "046\n", + "051\n", + "052\n", + "053\n", + "054\n", + "056\n", + "061\n", + "062\n", + "063\n", + "064\n", + "065\n", + "102\n", + "103\n", + "104\n", + "105\n", + "106\n", + "120\n", + "123\n", + "124\n", + "125\n", + "126\n", + "130\n", + "132\n", + "134\n", + "135\n", + "136\n", + "140\n", + "142\n", + "143\n", + "145\n", + "146\n", + "150\n", + "152\n", + "153\n", + "154\n", + "156\n", + "160\n", + "162\n", + "163\n", + "164\n", + "165\n", + "201\n", + "203\n", + "204\n", + "205\n", + "206\n", + "210\n", + "213\n", + "214\n", + "215\n", + "216\n", + "230\n", + "231\n", + "234\n", + "235\n", + "236\n", + "240\n", + "241\n", + "243\n", + "245\n", + "246\n", + "250\n", + "251\n", + "253\n", + "254\n", + "256\n", + "260\n", + "261\n", + "263\n", + "264\n", + "265\n", + "301\n", + "302\n", + "304\n", + "305\n", + "306\n", + "310\n", + "312\n", + "314\n", + "315\n", + "316\n", + "320\n", + "321\n", + "324\n", + "325\n", + "326\n", + "340\n", + "341\n", + "342\n", + "345\n", + "346\n", + "350\n", + "351\n", + "352\n", + "354\n", + "356\n", + "360\n", + "361\n", + "362\n", + "364\n", + "365\n", + "401\n", + "402\n", + "403\n", + "405\n", + "406\n", + "410\n", + "412\n", + "413\n", + "415\n", + "416\n", + "420\n", + "421\n", + "423\n", + "425\n", + "426\n", + "430\n", + "431\n", + "432\n", + "435\n", + "436\n", + "450\n", + "451\n", + "452\n", + "453\n", + "456\n", + "460\n", + "461\n", + "462\n", + "463\n", + "465\n", + "501\n", + "502\n", + "503\n", + "504\n", + "506\n", + "510\n", + "512\n", + "513\n", + "514\n", + "516\n", + "520\n", + "521\n", + "523\n", + "524\n", + "526\n", + "530\n", + "531\n", + "532\n", + "534\n", + "536\n", + "540\n", + "541\n", + "542\n", + "543\n", + "546\n", + "560\n", + "561\n", + "562\n", + "563\n", + "564\n", + "601\n", + "602\n", + "603\n", + "604\n", + "605\n", + "610\n", + "612\n", + "613\n", + "614\n", + "615\n", + "620\n", + "621\n", + "623\n", + "624\n", + "625\n", + "630\n", + "631\n", + "632\n", + "634\n", + "635\n", + "640\n", + "641\n", + "642\n", + "643\n", + "645\n", + "650\n", + "651\n", + "652\n", + "653\n", + "654\n" + ] + } + ], + "source": [ + "for p in itertools.permutations('0123456', 3):\n", + " print(''.join(str(x) for x in p))" + ] + }, + { + "cell_type": "code", + "execution_count": 273, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0123\n", + "0124\n", + "0125\n", + "0134\n", + "0135\n", + "0145\n", + "0234\n", + "0235\n", + "0245\n", + "0345\n", + "1234\n", + "1235\n", + "1245\n", + "1345\n", + "2345\n" + ] + } + ], + "source": [ + "for p in itertools.combinations('012345', 4):\n", + " print(''.join(p))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Дополните код расчетом коэффициента корреляции x и y по формуле R" + ] + }, + { + "cell_type": "code", + "execution_count": 302, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.5365173571086087 0.245770089241387\n", + "0.536517357108608 0.24577008924138732\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\Admin\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:15: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.\n", + "To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.\n", + " from ipykernel import kernelapp as app\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEGCAYAAABo25JHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3de3xU5b3v8c+TECAIEgUEE1SoBQRBRaJWqW3QKlS3QtF616K27NNdu3erpbvVantq94ZXrfXYU09bK2i1WvC2kVYruxVTu2uxBMJFUARRkcQLAgGBQG7P+WNNYDLMytzWmrVm1vf9evF6kcmameeZNVm/9Vx/xlqLiIhEV0nQBRARkWApEIiIRJwCgYhIxCkQiIhEnAKBiEjE9Qi6AJkaOHCgHTZsWFbP3bNnD4cddpi3BQo51TkaVOdoyKXOy5cv/8haOyjZ7wouEAwbNoy6urqsnltbW0tNTY23BQo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ZwF/XiIi4oFi7BoSEZEMKBCIiEScAoGISMQpEIiIRJwCgYhIxCkQiIhEnAKBiEjEKRCI5MgYc1psX/jexpjDYnkAxgZdLpF0aUGZiAeMMT/C2dqgHNhirZ0dcJFE0qZAIOKB2D44y3DyHpxlrW0PuEgiaVPXkIg3jsTZy6kfTstApGCoRSDiAWPMIpzsWcOBo621NwVcJJG0Fd3uoyL5Zoy5Dmiz1j5mjCkFXjbGnGOtXRJ02UTSoRaBiEjEaYxARCTiFAhERCJOgUBEJOIUCEREIk6BQEQk4hQIREQiToFARCTi/j+yACD6dBX+YgAAAABJRU5ErkJggg==\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "n = 100\n", + "r = 0.6\n", + "x = np.random.rand(n)\n", + "y = r*x + (1-r)*np.random.rand(n)\n", + "\n", + "plt.plot(x, y, 'o')\n", + "plt.xlabel('x')\n", + "plt.ylabel('y')\n", + "plt.grid(True)\n", + "\n", + "a = (np.sum(x)*np.sum(y) - n*np.sum(x*y))/(np.sum(x)**2 - n*np.sum(x**2))\n", + "b = (np.sum(y) - a*np.sum(x))/n\n", + "\n", + "A = np.vstack([x, np.ones(len(x))]).T\n", + "a1, b1 = np.linalg.lstsq(A, y)[0]\n", + "print(a, b)\n", + "print(a1, b1)\n", + "plt.plot([0, 1], [b, a + b])\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 303, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.7917224037373413" + ] + }, + "execution_count": 303, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x_mean = np.mean(x)\n", + "y_mean = np.mean(y)\n", + "R = np.sum((x - x_mean)*(y - y_mean))/(np.sum((x - x_mean)**2)*np.sum((y - y_mean)**2))**0.5\n", + "R" + ] + } + ], + "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.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}