{ "cells": [ { "cell_type": "markdown", "id": "ecd454b4", "metadata": {}, "source": [ "# First simulation with Python\n", "\n", "In this notebook we will try some simple simulation tasks" ] }, { "cell_type": "code", "execution_count": null, "id": "3b1c26a5", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "id": "e1ab9fab", "metadata": {}, "outputs": [], "source": [ "np.random.seed(8366)" ] }, { "cell_type": "markdown", "id": "3a774103", "metadata": {}, "source": [ "### Simmulating a discrete random variable (stochastic variable)" ] }, { "cell_type": "markdown", "id": "46edd01b", "metadata": {}, "source": [ "Consider the stochastic variabel X with following f(x) (pdf):\n", "\n", "| X: |0|1|2|3|\n", "|:--:|:--:|:--:|:--:|:--:|\n", "| f(x): | 0.1 | 0.3 | 0.4 | 0.2 |" ] }, { "cell_type": "code", "execution_count": null, "id": "994c2c69", "metadata": {}, "outputs": [], "source": [ "# simulate ONE realization of the random variable:\n", "result = np.random.choice([0,1,2,3], p=[0.1,0.3,0.4,0.2], size=1) # we do not need replace=True - it is the default\n", "print(result)" ] }, { "cell_type": "markdown", "id": "6d67474a", "metadata": {}, "source": [ "Repeat the code in the cell above. \n", "\n", "Does the value change? \n", "\n", "Is the simulation behaving as you expect?" ] }, { "cell_type": "code", "execution_count": null, "id": "78be264e", "metadata": {}, "outputs": [], "source": [ "# we can also \"draw\" many obervation in one go:\n", "sample = np.random.choice([0,1,2,3], p=[0.1,0.3,0.4,0.2], size=100)\n", "print(sample)" ] }, { "cell_type": "markdown", "id": "4a664cba", "metadata": {}, "source": [ "Do these values look as expected?" ] }, { "cell_type": "markdown", "id": "8bf8556b", "metadata": {}, "source": [ "Lets make a histogram:" ] }, { "cell_type": "code", "execution_count": null, "id": "6f8c7c9b", "metadata": {}, "outputs": [], "source": [ "plt.hist(sample, bins=[-0.5,0.5, 1.5,2.5,3.5], edgecolor='black')\n", "plt.xticks([0,1,2,3])\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "37dd7337", "metadata": {}, "source": [ "Try re-making the sample a few times.\n", "\n", "Does the histogram fit with the simulated probabilities?\n", "\n", "What happens if you make a larger sample (e.g., 1000 observations)?" ] }, { "cell_type": "markdown", "id": "c06e01a5", "metadata": {}, "source": [ "### Simulate rolling a dice" ] }, { "cell_type": "markdown", "id": "7c2aefc8", "metadata": {}, "source": [ "Consider the stochastic variabel X describing rolling a (fair) dice. \n", "The f(x) (pdf) is:\n", "\n", "| X: |1|2|3|4|5|6|\n", "|:--:|:--:|:--:|:--:|:--:|:--:|:--:|\n", "| f(x): | 1/6 | 1/6 | 1/6 | 1/6 | 1/6 | 1/6 |" ] }, { "cell_type": "code", "execution_count": null, "id": "0eb5d9aa", "metadata": {}, "outputs": [], "source": [ "# a single roll (try repeating a few times):\n", "dice_1 = np.random.choice([1,2,3,4,5,6], size=1) # we do not need to specify p if all probabilities are equal\n", "print(dice_1)" ] }, { "cell_type": "code", "execution_count": null, "id": "23bba644", "metadata": {}, "outputs": [], "source": [ "# 10 rolls (try repeating a few times):\n", "dice_10 = np.random.choice([1,2,3,4,5,6], size=10) \n", "print(dice_10)" ] }, { "cell_type": "code", "execution_count": null, "id": "9980b3c3", "metadata": {}, "outputs": [], "source": [ "# count the number of each outcome:\n", "print(np.bincount(dice_10))" ] }, { "cell_type": "markdown", "id": "dbe34955", "metadata": {}, "source": [ "What do these counts represent? Can you make sense of them (compare with the values in dice_10)" ] }, { "cell_type": "code", "execution_count": null, "id": "22ce6720", "metadata": {}, "outputs": [], "source": [ "# compare the counts with the histogram:\n", "plt.hist(dice_10, bins=[-0.5,0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5], edgecolor='black')\n", "plt.xticks([0,1,2,3,4,5,6,7])\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "255eb3fc", "metadata": {}, "source": [ "KAHOOT (x1)" ] } ], "metadata": { "kernelspec": { "display_name": "pernille", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }