{"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.0"}},"nbformat":4,"nbformat_minor":0,"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"057e149e-1a95-6b0a-6bc2-ac6c92504393","_active":false},"outputs":[],"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"metadata":{"_cell_guid":"a1199d1c-9de1-8538-370a-f1f188f69dd8","_active":false,"collapsed":false},"source":"---\n\nImplementation of the statistical expectation of a dice. The expectation then is compared to the simulated average of a certain number of throws.\n\n---","execution_count":null,"cell_type":"markdown","outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b85d7a5a-14dc-0151-a8f1-b1120f6013bd","_active":false},"outputs":[],"source":"import random as rnd\nimport math\nimport matplotlib.pyplot as plt \n\n\nit = 1000 # Number of iterations.\n\n# Expected value of the RV : counts of pips of a normal dice. \nexp = 1/6 + 2/6 + 3/6 + 4/6 + 5/6 + 6/6\n\n# Simulation of 100 dice throws.\nsim = np.random.randint(1,7, it)\n\n# Compute the average for all the throws.\navgs = np.array([sim[0]])\n\nfor n in range (2, it + 1):\n    # We update the previous averages so we can compute in O(N)\n    avgs = np.append(avgs, (avgs[n - 2] * (n - 1) + sim[n - 1]) / n) \n    \n# Variance computation\nvar = (1 - exp)**2/6 + (2 - exp)**2/6 + (3 - exp)**2/6 + (4 - exp)**2/6 + (5 - exp)**2/6 + (6 - exp)**2/6\n\n# Plot expectance vs average \nplt.plot(np.arange(it), avgs, np.arange(it), np.array([exp] * it), \n                              np.arange(it), np.array([exp + math.sqrt(var)] * it), 'green', \n                              np.arange(it), np.array([exp - math.sqrt(var)] * it), 'green')\nplt.axis([0, it, 1, 6])\nplt.show()"},{"metadata":{"_cell_guid":"2e57e234-2fa5-eb39-b7ea-b947c0eebf48","_active":false,"collapsed":false},"source":"---\nSimulation of the Monty Hall problem\n---","execution_count":1,"cell_type":"markdown","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"41dc8f4f-e609-50b9-4ed9-141f9a94b22e","_active":true,"collapsed":false},"source":"import random as rnd\nimport numpy as np\n\nn_doors = 5 # Number of doors\nwins = 0\nlost = 0\n\ncar_door = np.random.randint(0, n_doors)\ndoors    = np.delete(np.arange(0, n_doors), car_door)\n\nfirst_pick = np.random.randint(0, n_doors)\n\nif first_pick == car_door:\n    print('Winner!')\n    wins = wins + 1\nelse:\n    print('Beeeeh')\n    lost = lost + 1","execution_count":62,"cell_type":"code","outputs":[],"execution_state":"idle"}]}