programmieren_wise_24_25/Material/wise_24_25/lernmaterial/5.SciPy.ipynb

1231 lines
371 KiB
Plaintext
Raw Permalink Normal View History

2024-11-21 17:56:00 +01:00
{
"cells": [
{
"cell_type": "markdown",
"id": "fd2f3bf7-b314-4449-95dc-f07d1a13f1c6",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-04300aa5f61f7f12",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# 5. Programmierübung: SciPy\n",
"\n",
"<div style=\"display:flex;\">\n",
" <div style=\"text-align: left\">\n",
" Willkommen zur fünften Programmierübung Einführung in Python 3.\n",
" </div>\n",
" <img style=\"float: right; margin: 0px 15px 15px 0px\" src=\"https://www.python.org/static/img/python-logo-large.c36dccadd999.png?1576869008\" width=\"100\" />\n",
"</div>\n",
"\n",
"Wenn Sie Fragen oder Verbesserungsvorschläge zum Inhalt oder Struktur der Notebooks haben, dann können sie eine E-Mail an Phil Keier ([p.keier@hbk-bs.de](mailto:p.keier@hbk-bs.de?subject=[SigSys]%20Feedback%20Programmierübung&amp)) oder Martin Le ([martin.le@tu-bs.de](mailto:martin.le@tu-bs.de?subject=[SigSys]%20Feedback%20Programmierübung&amp)) schreiben.\n",
"\n",
"Link zu einem Python Spickzettel: [hier](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/PythonForDataScience.pdf)\n",
"\n",
"Der Großteil des Python-Tutorials stammt aus der Veranstaltung _Deep Learning Lab_ und von [www.python-kurs.eu](https://www.python-kurs.eu/python3_kurs.php) und wurde für _Signale und Systeme_, sowie _Einführung in die Programmierung für Nicht Informatiker_ angepasst.\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "bb677cea-65c2-486d-9442-6495b5063bd2",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-110034393ecbe731",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# Was ist SciPy\n",
"\n",
"SciPy steht für Scientific Python und ist eine Open-Source-Bibliothek, die auf der bewährten Architektur von NumPy aufbaut. Sie bietet eine Vielzahl von Funktionen, die speziell für ingenieurtechnische und wissenschaftliche Anwendungen entwickelt wurden. In diesem Zusammenhang möchten wir uns insbesondere mit Teilen des Statistikmoduls von SciPy vertraut machen.\n",
"\n",
"__Für dieses Notebook schauen Sie bitte in die [SciPy Docs](https://docs.scipy.org/doc/scipy/tutorial/index.html)!!!__ Dort sind alle Funktionen beschrieben die wir hier bearbeiten und noch mehr!\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "3dde4289-bc10-49fc-875d-ebc44fbf807a",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-82f61f58224c5db9",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"SciPys wird meist als `sp` importiert da für diese Aufgabe nur das Statistik modul nötig ist wird einfach dieses importiert. Aufgrund des kurzen schlüssigen namens findet keine umbenennung statt:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e058b67a-ab34-4685-b023-084bfa80f171",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-fe3ab9d39498717f",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"from scipy import stats"
]
},
{
"cell_type": "markdown",
"id": "57ada7f0-5eea-45cc-b8d0-85f52c7f6b55",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-eec41b75718f0e4e",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"id": "5983dc91-91f3-4b6b-b6c2-9873b9ea8db9",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-65e8f06a2b373c8c",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# Lineare Regression\n",
"\n",
"## Motivation\n",
"\n",
"Die **lineare Regression** ist eine grundlegende Methode zur Modellierung von Beziehungen zwischen Variablen. Sie hilft, Zusammenhänge zu verstehen und ermöglicht Vorhersagen auf Basis vorhandener Daten. Durch die Bestimmung einer linearen Beziehung zwischen einer abhängigen und einer oder mehreren unabhängigen Variablen können wir Trends identifizieren und zukünftige Werte schätzen. Ihre Einfachheit, Effizienz und die Möglichkeit, auch bei großen Datensätzen präzise Ergebnisse zu erzielen, machen sie zu einem wertvollen Werkzeug in der Datenanalyse und ein Fundament für komplexere Modelle.\n",
"\n",
"Dafür geht man davon aus, dass es zwei unabhängige Variablen $x_1$ & $x_2$ gibt, für die eine Zukünftige Aussage getätigt werden soll. Hierfür wird eine fehlerminimierte Gerade $g(x)$ zwischen die Datenpunkte gelegt. Daraus resultierenden lässt sich mit $g(x)$ eine zukünftige Vorhersage in einem bestimmten Fehlerbereich tätigen.\n",
"\n",
"Aus der Schule sollte die Geraden Gleichung $g(x) = m\\cdot x+b$ bekannt sein. Dabei beschreibt $m$ die Steigung (Slope) & $b$ den Schnittpunkt mit der y-Achse (interception). Die **lineare regression** bietet daher die Möglichkeit diese beiden Parameter herauszufinden.\n",
"\n",
"Für das folgende Beispiel werden zwei (pseudo-)zufällig erzeugte Datensets generiert, auf welche dann die lineare regression angewandt wird. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "60712a1d-7a55-44f4-89e9-cae5ceab952a",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-fbb9bfd35036d55e",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjIAAAHHCAYAAACle7JuAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA3K0lEQVR4nO3de3wU1f3/8fdmCSEJWe6XkAtERBIQRQGtQCAKGCxiMA3ol4uBtkotaCKlFasoIIhipYloQWwrrf1ipTGIWkVSrrFSQRCKgoAWBSJ3hQViQ9yd3x9fs78sm4QNJJmZ5PV8PHw8umfOzn52zta8nXNmxmEYhiEAAAAbCjG7AAAAgItFkAEAALZFkAEAALZFkAEAALZFkAEAALZFkAEAALZFkAEAALZFkAEAALZFkAEAALZFkAFQI1JSUpSSkmJ2GRfliy++kMPh0JIlS8wupdaMHz9enTp1MrsMoMYRZIAq7NixQxkZGerYsaOaNGmimJgYDRkyRAsWLKi1z1y6dKlycnIC2r/66ivNmDFD27Ztq7XPrgudOnWSw+Hw/RMZGanrrrtOf/7zn80uzRK2bt0qh8OhRx55pNI+e/fulcPh0JQpU+q0NsCKCDJAJd5//3317t1b27dv1913363nnntOP/3pTxUSEqLc3Nxa+9yqgszMmTNtH2QkqWfPnnr55Zf18ssva8aMGTp16pQyMzP14osvml2a6a699lolJibqlVdeqbTP0qVLJUljx46tw8oAa2pkdgGAVc2ZM0fNmjXT5s2b1bx5c79tR48eNa2umnb27FlFRkbW6WfGxMT4/REeP368LrvsMv32t7/V3XffXae1WNGYMWM0ffp0/etf/9IPfvCDgO2vvPKKEhMTde2115pSH2AlnJEBKvH555+re/fuASFGktq2bRvQ9pe//EXXXXedIiIi1KJFCw0YMECrVq3ybV+xYoWGDRumDh06KCwsTJ07d9bjjz8uj8fj65OSkqK///3v+vLLL31TL506ddK6devUp08fSdKECRN828qv6fjggw80dOhQNWvWTBERERo4cKD++c9/+tU4Y8YMORwO7dy5U6NHj1aLFi3Uv39/v+/Qq1cvhYeHq2XLlrrzzjt14MCBgO+6ePFide7cWeHh4bruuutUWFh4Uce4TJs2bZSYmKjPP//cr72wsFAjR45UfHy8wsLCFBcXpwceeEDffvutX7/x48eradOmKioq0ogRI9S0aVO1adNGU6dO9Tu+knTy5EmNHz9ezZo1U/PmzZWZmamTJ09WWNeaNWuUnJysyMhINW/eXGlpadq1a5dfn7JjumfPHo0dO1bNmjVTmzZtNH36dBmGoQMHDigtLU0ul0vt27fXM888c8HjMWbMGKncmZfytmzZot27d/v6BPO7qsi6devkcDi0bt06v/bK1gt9+umnysjIUMuWLdWkSR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hPPnkk772b775xggPDzcyMzMveJyef/55Q5Lx7rvv+to8Ho8RExNj3HDDDVUeo4p+V5mZmX6/q7Vr1xqSjLVr1/q9d9++fQHHYtCgQUaPHj389uf1eo2+ffsaXbp08bVdffXVxrBhwy743YCaxBkZoBJDhgzRxo0bddttt2n79u2aN2+eUlNTFRMT43dK/fXXX5fX69Wjjz6qkBD//0s5HA7f/w4PD/f979OnT+v48eNKTk5WcXGxPv3004uuc9u2bdq7d69Gjx6tEydO6Pjx4zp+/LjOnj2rQYMGacOGDfJ6vX7v+dnPfub3Oj8/X16vV6NGjfK9//jx42rfvr26dOmitWvXSpI+/PBDHT16VD/72c/UuHFj3/vLpmqCtWrVKrVp00Zt2rRRjx499PLLL2vChAl6+umn/fqVP2Znz57V8ePH1bdvXxmGoY8++ihgv+d/r+TkZP3nP//xvX777bfVqFEj3Xvvvb42p9Op++67z+99hw4d0rZt2zR+/Hi1bNnS137VVVdpyJAhevvttwM++6c//anfPnv37i3DMPSTn/zE1968eXN17drVr6bK3HHHHQoNDfWbXlq/fr2Kiop800rnH6Oa/F2V+frrr7VmzRqNGjXKt//jx4/rxIkTSk1N1d69e1VUVOT7fp988on27t17yZ8LBIvFvkAV+vTpo/z8fJ07d07bt2/X8uXL9dvf/lYZGRnatm2bunXrps8//1whISHq1q1blfv65JNP9Mgjj2jNmjVyu91+206dOnXRNZb90ahoeqT8/stP2yQkJATswzAMdenSpcL3h4aGSpK+/PJLSQroFxoaqssuuyzomsumXjwejz7++GPNnj1b33zzjV84kqT9+/fr0Ucf1RtvvKFvvvkm4DuV16RJE7Vp08avrUWLFn7v+/LLLxUdHa2mTZv69evatavf67LveX67JCUlJendd98NWCQdHx/v169Zs2Zq0qSJWrduHdB+4sSJSo7M/9eqVSulpqZq+fLlWrRokZo0aaKlS5eqUaNGGjVqlK9fbf2uynz22WcyDEPTp0/X9OnTK+xz9OhRxcTEaNasWUpLS9MVV1yhK6+8UkOHDtW4ceN01VVXXXIdQGUIMkAQGjdurD59+qhPnz664oorNGHCBP3tb3/TY489FtT7T548qYEDB8rlcmnWrFnq3LmzmjRpoq1bt+rBBx8MOGNSHWXvffrpp9WzZ88K+5z/h7v8f8WX7cPhcOidd96R0+m84PsvVevWrTV48GBJUmpqqhITE3XrrbcqNzfXd28Uj8ejIUOG6Ouvv9aDDz6oxMRERUZGqqioSOPHjw84ZhXVXZcq+vzKajIMI6h9jh07Vm+99Zbeeust3XbbbXrttdd08803+wLbpfyuyp8tLO/8NTll+5g6dapSU1MrfM/ll18uSRowYIA+//xzrVixQqtWrdLvf/97/fa3v9WiRYv8zlgBNYkgA1RT7969pe+nHySpc+fO8nq92rlzZ6VBYt26dTpx4oTy8/N9C3Q
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"rand = np.random.default_rng(42) # RNG with fixed seed\n",
"\n",
"# Generate, Scale, round and sort random numbers for an increasing slope\n",
"scalar: int = 10\n",
"x: np.array = np.round(np.sort(rand.random(20) * scalar), decimals=2)\n",
"y: np.array = np.round(np.sort(rand.random(20) * scalar), decimals=2)\n",
"\n",
"# Plot these values\n",
"plt.title(\"Scattered Random Values\")\n",
"plt.grid()\n",
"plt.xlabel(\"X\")\n",
"plt.ylabel(\"Y\")\n",
"plt.scatter(x,y, color='g')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "083ba4ea-8e6f-4286-8049-1c8e3f22c004",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-caa85fe6a76214bf",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Aus den erzeugten Daten ist klar ersichtlich, dass diese einem Trend folgen. Mittels SciPy wollen wir diesen Trend darstellen.\n",
"\n",
"Dazu wird die Funktion `linregress` verwendet. Diese verlangt, wie aus der [Dokumentation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html#scipy.stats.linregress) zu entnehmen, zwei arrays mit Daten. \n",
"\n",
"Die Ausgabe ist aufgespalten in 5 Parameter:\n",
"- slope: die Steigung `m` der Geraden\n",
"- intercept: der Punkt an dem die Gerade die y-Achse trifft oder das `b`\n",
"- rvalue: der Pearson Korrelations Koefficient, welcher voerst ignoriert wird\n",
"- pvalue: der p-Wert das die Nullhypothese stimmt, wird auch ignoriert\n",
"- stderr: der Standard Error der Steigung, unter der Annahme einer Normalverteilung der Daten *(PCGs sind Normalverteilt)*"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "9c1959d8-5bac-45fa-9aa2-47fa79943d72",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-553a583b633273d5",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"slope, intercept, _, _, stderr = stats.linregress(x,y)"
]
},
{
"cell_type": "markdown",
"id": "2c2bd060-bb4c-440f-9a04-72382c9a33eb",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-111845c39238eeee",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Aus `slope` & `intercept` lässt sich folglich eine Gerade definieren:"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "1e30868c-f11f-43bd-a60b-200ec00e903f",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-49d79f2b8dcf7315",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"def line(x: float) -> float:\n",
" '''\n",
" Evaluates the rounded line from the previous\n",
" evaluated linear regression model\n",
"\n",
" Note: Output rounded to 2 decimal places\n",
" '''\n",
" res: float = slope * x + intercept\n",
" rounded: np.float64 = np.round(res, decimals=2)\n",
" return float(rounded)"
]
},
{
"cell_type": "markdown",
"id": "8fdd9724-5c61-471d-a121-787f2d56f132",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-a63e440ba0b57186",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Diese kann über den gesatme bereich dargestellt werden. Dazu werden die bereits bekannten x-Werte verwendet:"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "0a59d146-4ae3-429a-a67e-7c158d5ebac7",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-5239bfcad5788ae3",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjIAAAHHCAYAAACle7JuAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAABktElEQVR4nO3deVyU5f7/8dfMsO+KKMgiCAi4pKa2WKZWpq2ax6yTKVknW7Q087RbaprtaatppdnJlmNYnb4d0yzLslNqappsLogiiriAgMIwc//+MPhJoIICMwPv5+PR49Hcc9/3fGau0Xl73dd9XSbDMAxEREREXJDZ0QWIiIiInCkFGREREXFZCjIiIiLishRkRERExGUpyIiIiIjLUpARERERl6UgIyIiIi5LQUZERERcloKMiIiIuCwFGRGpF/369aNfv36OLuOMZGVlYTKZWLBggaNLaTC33nor0dHRji5DpN4pyIicwqZNmxg2bBjt2rXDy8uL8PBwBgwYwKuvvtpgr7lo0SJmzZpVbfuePXuYMmUKGzZsaLDXbgzR0dGYTKbK/3x9fTnvvPNYuHCho0tzCr/99hsmk4nHH3/8pPtkZmZiMpmYOHFio9Ym4owUZEROYvXq1fTs2ZONGzdyxx138Nprr/GPf/wDs9nM7NmzG+x1TxVkpk6d6vJBBqBbt268//77vP/++0yZMoWCggKSk5OZN2+eo0tzuHPPPZfExEQ+/PDDk+6zaNEiAG655ZZGrEzEObk5ugARZzVjxgwCAwNZs2YNQUFBVZ7Ly8tzWF31rbi4GF9f30Z9zfDw8Co/wrfeeivt27fn5Zdf5o477mjUWpzRiBEjmDx5Mv/73/+44IILqj3/4YcfkpiYyLnnnuuQ+kSciXp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D/Ltt98yfPjwyvPn5+dz4MABBg4cSGZmJjk5OZXv748//iAzM/OsX1ektjTYV+QUevXqRUpKCmVlZWzcuJElS5bw8ssvM2zYMDZs2EDHjh3Ztm0bZrOZjh07nvJcf/zxB48//jjffvsthYWFVZ4rKCg44xorfjRqujxy4vlPvGwTExNT7RyGYRAfH1/j8e7u7gDs3LkToNp+7u7utG/fvtY1V1x6sdlsbN68menTp3Po0KEq4QggOzubJ554gi+++IJDhw5Ve08n8vLyIiQkpMq2Fi1aVDlu586dhIWF4efnV2W/hISEKo8r3udftwMkJSXx9ddfVxskHRUVVWW/wMBAvLy8aNWqVbXtBw4cOMkn8/8FBwczcOBAlixZwpw5c/Dy8mLRokW4ubkxfPjwyv0a6ntVYevWrRiGweTJk5k8eXKN++Tl5REeHs60adMYPHgwHTp0oHPnzgwaNIiRI0dyzjnnnHUdIiejICNSCx4eHvTq1YtevXrRoUMHRo8ezb///W+efPLJWh1/+PBh+vbtS0BAANOmTSM2NhYvLy9+++03HnrooWo9JnVRcezzzz9Pt27datznrz/cJ/4rvuIcJpOJ//73v1gsltMef7ZatWrF5ZdfDsDAgQNJTEzkmmuuYfbs2ZVzo9hsNgYMGMDBgwd56KGHSExMxNfXl5ycHG699dZqn1lNdTemml7/ZDUZhlGrc95yyy18+eWXfPnll1x33XV8+umnXHHFFZWB7Wy+Vyf2Fp7or2NyKs4xadIkBg4cWOMxcXFxAFxyySVs27aNzz//nGXLlvH222/z8ssvM2fOnCo9ViL1SUFGpI569uwJf15+AIiNjcVut7Nly5aTBomVK1dy4MABUlJSKgfoAuzYsaPavif7gTnZ9tjYWAACAgIqw0FdxcbGYhgGMTExdOjQ4aT7tWvXDv7swbn
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Calculate the Line using vectorization\n",
"regline: np.array = np.vectorize(line)(x)\n",
"\n",
"# Plot tvalues\n",
"plt.title(\"Scattered Random Values\")\n",
"plt.grid()\n",
"\n",
"plt.xlabel(\"X\")\n",
"plt.ylabel(\"Y\")\n",
"\n",
"plt.scatter(x,y, color='g', label=\"Original Data\")\n",
"plt.plot(x, regline, color='m', label=\"Fitted line\")\n",
"\n",
"plt.legend()\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "802bf60a-e402-4912-96bf-baa6cd398c6e",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-e0da661ffad2086e",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Mit diesem Model lässt sich dementsprechend die \"Zukunft\" vorhersagen. Hierfür können wir im folgenden einfach die Werte für `-1` & `11` berechnen und diese dem Plot hinzufügen:"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "e62f6248-5813-4df0-9715-a72d3a5467f8",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-92a5bbb041e28a56",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjUAAAHHCAYAAABHp6kXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAABuUElEQVR4nO3dd3hUVf7H8fdk0iuddBICpCCCEiwoTUFQV8EsAgtK0R82VBBZsYGAFLtgxQqWRWAxsi66KKIoKipFWCChExJC6JAQQpLJzP39AZklJMAEkkxm8nk9D4/MnTt3vvcwZj4559xzTYZhGIiIiIi4OA9nFyAiIiJSFRRqRERExC0o1IiIiIhbUKgRERERt6BQIyIiIm5BoUZERETcgkKNiIiIuAWFGhEREXELCjUiIiLiFhRqRKTKde3ala5duzq7jAuSkZGByWRi9uzZzi6l2gwdOpSYmBhnlyFS5RRqRBy0fv16+vbtS7NmzfD19SUiIoIePXrw+uuvV9t7zpkzh+nTp5fbvmfPHiZMmMDatWur7b1rQkxMDCaTyf4nICCAK664go8//tjZpdUKa9aswWQy8fTTT591n61bt2IymRg9enSN1iZSGynUiDjg119/JTk5mXXr1jF8+HDeeOMN/u///g8PDw9mzJhRbe97rlAzceJElw81AO3ateOTTz7hk08+YcKECeTm5jJkyBDee+89Z5fmdJdffjkJCQl89tlnZ91nzpw5ANxxxx01WJlI7eTp7AJEXMGUKVMICQlh5cqV1KtXr8xz+/fvd1pdVe348eMEBATU6HtGRESU+UIeOnQozZs359VXX2X48OE1WkttNGjQIMaNG8dvv/3GVVddVe75zz77jISEBC6//HKn1CdSm6inRsQB27dvp3Xr1uUCDUCTJk3Kbfv000+54oor8Pf3p379+nTu3Jlvv/3W/vy//vUvbr75ZsLDw/Hx8SEuLo5nn30Wq9Vq36dr16589dVX7Nq1yz48ExMTw7Jly+jQoQMAw4YNsz93+hyQ33//nV69ehESEoK/vz9dunThl19+KVPjhAkTMJlMpKWlMXDgQOrXr8+1115b5hzat2+Pn58fDRo0YMCAAWRlZZU713fffZe4uDj8/Py44oorWL58+QW1canGjRuTkJDA9u3by2xfvnw5t99+O9HR0fj4+BAVFcUjjzzCiRMnyuw3dOhQAgMDyc7Opk+fPgQGBtK4cWPGjBlTpn0Bjh49ytChQwkJCaFevXoMGTKEo0ePVljX999/T6dOnQgICKBevXr07t2b9PT0MvuUtumWLVu44447CAkJoXHjxowbNw7DMMjKyqJ3794EBwcTGhrKyy+/fN72GDRoEJzWI3O61atXs3nzZvs+jnyuKrJs2TJMJhPLli0rs/1s84s2bdpE3759adCgAb6+viQnJ/Pll1+W2cdisTBx4kRatmyJr68vDRs25Nprr2XJkiXnPWeRC6VQI+KAZs2asXr1ajZs2HDefSdOnMidd96Jl5cXkyZNYuLEiURFRfH999/b95k9ezaBgYGMHj2aGTNm0L59e8aPH8/jjz9u3+epp56iXbt2NGrUyD48M336dBITE5k0aRIA99xzj/25zp07w6kv386dO5OXl8czzzzD1KlTOXr0KNdddx1//PFHuXpvv/12CgoKmDp1qr1nZMqUKQwePJi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30Ezabrcxr7rvvvjKPU1NTsdls9OvXz/76gwcPEhoaSsuWLfnhhx8AWLVqFfv37+e+++7D29vb/vrS4RxHffvttzRu3JjGjRvTpk0bPvnkE4YNG8aLL75YZr/T2+z48eMcPHiQjh07YhgGf/75Z7njnnlenTp1YseOHfbHX3/9NZ6entx///32bWazmYceeqjM63Jycli7di1Dhw6lQYMG9u2XXnopPXr04Ouvvy733v/3f/9X5pjJyckYhsHdd99t316vXj3i4+PL1HQ2/fv3x8vLq8wQ1I8//kh2drZ96OnMNqrKz1Wpw4cP8/3339OvXz/78Q8ePMihQ4fo2bMnW7duJTs7235+GzduZOvWrRf9viKO0kRhEQd16NCB1NRUiouLWbduHV988QWvvvoqffv2Ze3atSQlJbF9+3Y8PDxISko657E2btzI008/zffff09eXl6Z53Jzcy+4xtIvkIqGUE4//ulDO7GxseWOYRgGLVu2rPD1Xl5eAOzatQug3H5eXl40b97c4ZpLh2esVisbNmxg8uTJHDlypExQAsjMzGT8+PF8+eWXHDlypNw5nc7X15fGjRuX2Va/fv0yr9u1axdhYWEEBgaW2S8+Pr7M49LzPHM7QGJiIt988025CdbR0dFl9gsJCcHX15dGjRqV237o0KGztMz/NGzYkJ49e/LFF18wc+ZMfH19mTNnDp6envTr18++X3V9rkpt27YNwzAYN24c48aNq3Cf/fv3ExERwaRJk+jduzetWrXikksuoVevXtx5551ceumlF12HyNko1IhUkre3Nx06dKBDhw60atWKYcOG8c9//pNnnnnGodcfPXqULl26EBwczKRJk4iLi8PX15c1a9YwduzYcj0plVH62hdffJF27dpVuM+ZX+Kn/3ZfegyTycR//vMfzGbzeV9/sRo1akT37t0B6NmzJwkJCfzlL39hxowZ9rVXrFYrPXr04PDhw4wdO5aEhAQCAgLIzs5m6NCh5dqsorprUkXvf7aaDMNw6Jh33HEHixYtYtGiRdx66618/vnn3HDDDfbwdjGfq9N7EU935hye0mOMGTOGnj17VviaFi1aANC5c2e2b9/Ov/71L7799lvef/99Xn31VWbOnFmmJ0ukKinUiFyE5ORkODVEARAXF4fNZiMtLe2soWLZsmUcOnSI1NRU++RegJ07d5bb92xfNmfbHhcXB0BwcLA9KFRWXFwchmEQGxtLq1atzrpfs2bN4FTPznXXXWffbrF
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Future Values\n",
"fut: np.array = np.array([line(-1), line(11)])\n",
"\n",
"# Calculate extended Line using vectorization\n",
"ext_range: np.array = np.arange(-1,12)\n",
"regline: np.array = np.vectorize(line)(ext_range)\n",
"\n",
"# Plot values\n",
"plt.title(\"Scattered Random Values\")\n",
"plt.grid()\n",
"\n",
"plt.xlabel(\"X\")\n",
"plt.ylabel(\"Y\")\n",
"\n",
"plt.scatter(x,y, color='g', label=\"Original Data\")\n",
"plt.plot(ext_range, regline, color='m', label=\"Fitted Line\")\n",
"plt.scatter((-1, 11), fut, color='r', label=\"Predicted Data\")\n",
"\n",
"plt.legend()\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "04f002a5-4339-4225-9515-848513347697",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-6c8576c776d11325",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Wie zu erwarten liegen beide Werte auf der Geraden.\n",
"\n",
"Um deren Werte zu ermitteln lassen sich diese mittels print einfach ausgeben, dies sollte immer mit Angabe des Standard Errors erfolgen. Sonst ist unklar wie genau die Daten sind:"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "563d452b-350c-4628-a97d-5d9ce268f4c4",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-7677ec5dda3e8e13",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prediction of f(11) = 10.16, with standard deviation 0.07\n"
]
}
],
"source": [
"print(\"Prediction of f(11) = {}, with standard deviation {}\".format(line(11), np.round(stderr, decimals=2)))"
]
},
{
"cell_type": "markdown",
"id": "5e81b1a9-4155-4bfd-8613-2530dbe61f03",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-facd8e8ef9f4aed1",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Aufgabe\n",
"\n",
"*7 Punkte*\n",
"\n",
"Bestimme mittels Linearer Regression die *best fit* Funktion für die beiden gegebenen Datensets `x_data` & `y_data`, unter beachtung folgender Punkte:\n",
"\n",
"- Plotte das Ergebnis angemessen\n",
"- Nutze SciPys `linregress` Funktion, speichere den Output vor dem entpacken in der Variablen `l`\n",
"- Definiere die Funktion `reg_line` mit einem Eingabeparameter\n",
"- Bestimme die Werte für `-0.3` & `3.4` speichere diese als liste in variablen `future`\n"
]
},
{
"cell_type": "code",
"execution_count": 147,
"id": "f5b801bc-450e-417b-aeea-9b69d638fb64",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-cb5b277089c75c40",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"random = np.random.default_rng(420)\n",
"\n",
"# 2 scuffed up One-Liners :)\n",
"x_data: np.array = np.sort(np.round(random.random(40)*np.pi, decimals=2))\n",
"y_data: np.array = np.flip(np.sort(np.round(random.random(40)*np.sqrt(2), decimals=2)))"
]
},
{
"cell_type": "code",
"execution_count": 155,
"id": "2645541f-c30f-487e-be65-3ec5ac22d427",
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-12b5b631856ffe41",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkMAAAHHCAYAAAC88FzIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAACIVUlEQVR4nOzdeVhUZfvA8e/MsC/DoijIIriD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tWqV9zpw5g7e3N6+//jqDBw+mXbt2dOrUiYEDBzJy5Ei6dOlyy3EIcSNSDAlRD2xsbOjWrRvdunWjXbt2jBkzhu+//54ZM2ZU6fhLly7Rp08ftFotr7/+Oq1bt8bOzo6EhASmTJlS4c5NdZQf+9577xESElJpn2t/+V99N6H8HCqViv/9739oNJqbHn+rmjZtSr9+/QAYMGAAHTp04P7772f+/PmGvXN0Oh39+/fnwoULTJkyhQ4dOuDo6EhGRgZPPPFEhc+ssrjrU2XXv15MiqJU6ZyPP/4469evZ/369TzwwAP88MMP3H333Yai71Z+rq6+a3m1a+colZ9j8uTJDBgwoNJj2rRpA8Add9zB0aNH+fHHH/n111/58ssv+fDDD1m4cKHRnTMhapsUQ0LUs/DwcLgylALQunVr9Ho9Bw4cuG4xEhMTw/nz54mOjjZMegZITU2t0Pd6v6Su1966dWsAtFqtocCortatW6MoCgEBAbRr1+66/Vq2bAlX7iTdeeedhvbS0lJSU1MJDg6u0fXvu+8++vTpw1t
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# BEGIN SOLUTION\n",
"l = stats.linregress(x_data,y_data)\n",
"slope, intercept, _, _, stderr = l\n",
"\n",
"def reg_line(x: float) -> float:\n",
" return float(np.round(slope*x+intercept,decimals=2))\n",
"\n",
"ext: tuple = (-0.3, 3.4)\n",
"rl: np.array = np.vectorize(reg_line)(ext)\n",
"\n",
"future: list = [reg_line(ext[0]), reg_line(ext[1])]\n",
"# Plot values\n",
"plt.title(\"Scattered Random Values\")\n",
"plt.grid()\n",
"\n",
"plt.xlabel(\"X\")\n",
"plt.ylabel(\"Y\")\n",
"\n",
"plt.scatter(x_data,y_data, color='g', label=\"Original Data\")\n",
"plt.plot(ext, rl, color='m', label=\"Fitted Line\")\n",
"plt.scatter(ext, future, color='r', label=\"Predicted Data\")\n",
"\n",
"plt.legend()\n",
"\n",
"plt.show()\n",
"# END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": 154,
"id": "aa34ed84-f200-4863-be0b-40ff5492c654",
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-8f20c3645d0a9b46",
"locked": true,
"points": 6,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# Hier werden ihre Lösungen getestet\n",
"\n",
"# Check if reg_line is defined\n",
"assert 'reg_line' in dir()\n",
"\n",
"# Check if reg_line gives right output\n",
"assert reg_line(10) == -3.04\n",
"\n",
"# Check if future values are calculated right\n",
"assert future == [1.51, -0.12]\n",
"\n",
"### BEGIN HIDDEN TESTS\n",
"sl, ip, _, _, se = stats.linregress(x_data,y_data)\n",
"slope, intercept, _, _, stderr = l\n",
"\n",
"assert slope == sl\n",
"assert intercept == ip\n",
"assert stderr == se\n",
"\n",
"### END HIDDEN TESTS"
]
},
{
"cell_type": "markdown",
"id": "8369e788-5862-488b-a003-d3cd31ebaa99",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-59872a1e95590378",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"id": "02656b99-d250-4a21-bc2d-46007806d81d",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-455ed6a04d0d701f",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# Verteilungen\n",
"\n",
"## Probability Density Function - Normal Verteilung\n",
"\n",
"### Motivation\n",
"\n",
"Es wurden Daten über die Körperlänge eines Bienenvolkes erhoben, dementsprechend liegen diskrete Werte über die deren Körperlängen vor. Logischerweise repräsentiert dies nur das erhobene Bienenvolk, dieses arbeitet aber kontinuierlich weiter und erzeugt nachkommen. Die Population verändert sich. \n",
"\n",
"Daher ist anzunehmen, dass auch Werte zwischen den bereits Erhobenen auftreten können. Um dies zu Modellieren wird eine Normalverteilung benötigt.\n",
"\n",
"Schauen wir uns dazu erst die Gaussche Normalverteilung an, diese hat ihren Mittelpunkt bei $x=0$, bezeichnet als Erwartungswert $\\mu$, und eine Standardabweichung $\\sigma = 1$. Mathematisch ist sie definiert als $$p(x|\\mu,\\sigma)=\\frac{1}{\\sqrt(2\\pi\\sigma^2)}e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}$$\n",
"\n",
"SciPy bietet hierfür das `norm` objekt aus dem `stats` Modul an, dieses verlangt die beiden Parameter $\\mu$ & $\\sigma$, das auf dem `norm` Objekt kann dann die Funktion `pdf` mit einem Paramter als Stepsize oder einem Array aufgerufen werden. Nach Plotten ergibt sich folglich die Normalverteilung (von -4 bis 4):"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "2803dcac-bb3b-4aff-a3c9-c0085148c376",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-54174b5f7d8309db",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiwAAAGzCAYAAAAMr0ziAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAABl40lEQVR4nO3deVxU9f4/8NfMwMwwLAMIsiiK4IKoQLngrimJZaXeLPVWGnXtRlr65bZc7r1J240s6/rLTMt7LfNm2arlLdJINBN3cUdFQTaHVRj2gZnz+wOYHAFlEDizvJ6PxzyUM+cc3odtXnM+m0QQBAFEREREFkwqdgFEREREN8PAQkRERBaPgYWIiIgsHgMLERERWTwGFiIiIrJ4DCxERERk8RhYiIiIyOIxsBAREZHFY2AhIiIii8fAQmRDPv74Y0gkEhw+fFjsUqzao48+isDAQLHL6LCUlBRIJBKkpKQYt1n7NRExsJBdyMzMxJIlSzBw4ECoVCqoVCqEhoZi8eLFOHHihNjlWZ3mF0SJRIIjR460eP7RRx+Fi4uLKLVZu+rqarz00ksmYYOIAAexCyDqatu3b8fcuXPh4OCAhx56COHh4ZBKpUhP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27VqMHz8ex44da1cHTTc3N/zf//0fli9fftO7LC+99BJefvllREVFITY2FufOncPatWtx6NAh/Pbbb3B0dDTuW1JSgrvuugvz5s3Dww8/DB8fH+Nzn376KXQ6HZ5++mmUlpbizTffxIMPPogpU6YgJSUFL7zwAjIyMrB69Wo8++yz2LBhg/HYjz/+GC4uLoiLi4OLiwt++eUXLF++HFqtFm+99VardTs6OmL27Nn45ptv8MEHH5h8nbdu3Yq6ujrMmzcPaLpLct9992Hv3r144oknMHjwYJw8eRL/+te/cP78eWzdutXk3L/88gu++OILLFmyBF5eXggPD8fatWsRGxuL2bNn4w9/+AMAICwsDGj6WRo3bhx69eqFv/71r3B2dsYXX3yBWbNm4euvv8bs2bNv+j3rKL1ej+joaERGRmLlypX4+eef8fbbbyM4OBixsbHG67/33ntx8OBBxMbGIiQkBNu2bcPChQu7rC6yIwKRjSovLxcACLNmzWrx3NWrV4WioiLjo7q62vjctf9v9tlnnwkAhD179hi3LVy4UOjbt2+LfRMSEoRrf7X+9a9/CQCEoqKiNmudOXOmMGTIkBtez4IFCwSpVCocOnSoxXMGg0EQBEH46KOPBABCVFSUcZsgCML//d//CTKZTCgrKxMEQRAqKioEd3d3YdGiRSbn0Wg0glqtbrH9ert27RIACF9++aVQVlYmeHh4CPfdd5/x+YULFwrOzs7GjwsLCwW5XC5MmzZN0Ov1xu3vvfeeAEDYsGGDcdukSZMEAMK6detMPmdmZqYAQPD29jZehyAIQnx8vABACA8PF+rr643b58+fL8jlcqG2tta4rbXv7Z///GdBpVKZ7Hf99/ann34SAAjff/+9ybF33323EBQUZPx406ZNglQqFX799VeT/datWycAEH777TfjNgCCVCoVTp8+bbJvUVGRAEBISEhoUevUqVOFYcOGmdRqMBiEsWPHCgMGDDBua/7+7Nq1q81ram0f4Zqv80cffWRyLADhlVdeMdn3tttuE4YPH278+OuvvxYACKtWrTJu0+v1wpQpU1qck8hcbBIim6XVagGg1dEqkydPhre3t/GxZs0a43NOTk7G/9fW1qK4uBijR48GABw9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eeKHNu1/N5s2bhw8++AA//vgjZs2ahS+++AIhISEIDw837mMwGDBs2DC88847rZ7j+lB1o+u8XnN9zz77LKKjo1vdx5xg0N6f32ZtfX2Jugs
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = np.arange(-4, 4, 0.01) # Plot between -4 and 4 with stepsize 0.01\n",
"y = stats.norm(0,1).pdf(x) # Calculate pdf with mu=0, sigma=1\n",
"\n",
"# Plot\n",
"plt.title(\"Gaussche Normalverteilung\")\n",
"plt.plot(x, y)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "ca2f768f-16ff-47ff-b898-fe0b8cf3511f",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-259b8d6cb9d76981",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Um herauszufinden wie viel Prozent einer Population innerhalb dieser Normalverteilung fallen wird die Funktion `ppf` (Percent Point Function) verwendet.\n",
"\n",
"Am Beispiel 90% der Population:"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "0e5e7709-90a2-4a30-b49f-cbb243aa4e4b",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-5a9ed9588ce2ea27",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"90% of the Population fall into the range 1.282.\n"
]
}
],
"source": [
"percentile = stats.norm(0,1).ppf(0.9)\n",
"print(f\"90% of the Population fall into the range {percentile:0.3f}.\")"
]
},
{
"cell_type": "markdown",
"id": "96afa424-d697-4c60-aa9b-4655491f4f13",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-5102a53fda328278",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Um dies zu veranschaulichen:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "4f5d7f54-856f-4e12-8251-d0cdc5c0f002",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-5d84b05b2808a672",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvUAAAIQCAYAAAABy5G8AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAABzoUlEQVR4nO3dd3gU5cLG4d8kIQktAQQSokiXokCOgIiCWKIB9SjHAlaKioqVLzbwKEU9UmwoIihKFaQ3EYIYARu9CNIEBKkJCZgKJJCd749NgoGA2ZDk3fLc1zXXTnZnN89u2pPZd96xbNu2ERERERERj+VnOoCIiIiIiFwYlXoREREREQ+nUi8iIiIi4uFU6kVEREREPJxKvYiIiIiIh1OpFxERERHxcCr1IiIiIiIeTqVeRERERMTDqdSLiIiIiHg4lXoRH7Znzx4sy2LcuHF51w0YMADLsvJtV7t2bbp3716in1fy6969O7Vr1853nWVZDBgwwFimC+HJ2d3JuHHjsCyLPXv2mI4iIm5GpV7Ei+UWgIKWPn36mI73j5YuXZovc5kyZahbty5du3bljz/+MB3vgh08eJABAwawYcMG01G8Su73y3vvvXfWbbk/E2vWrDGSrbRt2LCBhx56iJo1axIUFESVKlWIiopi7NixZGdnm44nIsUowHQAESl5b7zxBnXq1Ml33RVXXEGtWrU4fvw4ZcqUMZatMJ577jlatWrFyZMnWbduHZ999hnffPMNmzZtIiIiwnS8Ijt48CADBw6kdu3aREZG5rtt9OjROBwOY9m8wTvvvEOvXr0oV66c6ShGfP755zz55JOEhYXx8MMP06BBA9LS0oiLi+PRRx/l0KFDvPrqq6ZjikgxUakX8QEdO3akZcuWBd4WHBxc6nlc1a5dO+655x4AevTowWWXXcZzzz3H+PHj6du37wU9dkZGBuXLly+mpMXH3f/RMunEiRMEBgbi53fuN5sjIyPZsGEDo0aNIiYmpsSyuOv3z4oVK3jyySdp06YNCxYsoGLFinm39e7dmzVr1vDbb78Vy+dy19dAxNdo+I2ID7uQse3Jycn07t077239+vXrM2TIkLP2LicnJ9O9e3dCQ0OpVKkS3bp1Izk5+YJy33jjjQDs3r0777qFCxfSrl07ypcvT8WKFbntttvYvHlzvvt1796dChUqsGvXLm699VYqVqzIgw8+CIDD4eDDDz+kadOmBAcHU61aNTp06HDWMI0vv/ySFi1aULZsWapUqcJ9993Hvn378m1z/fXXc8UVV7BlyxZuuOEGypUrx8UXX8zQoUPztlm6dCmtWrWCnH9UcoeM5H4tChpTX5ADBw7wyCOPEBYWRlBQEJdffjljxoz5x/ud72t/5vj33OMsdu7cSffu3alUqRKhoaH06NGDY8eO5btvZmYm//d//0e1atWoWLEid9xxB/v37y9y9twhWFOmTOG1117j4osvply5cqSmpp73+V177bXceOONDB06lOPHj//j6/H999/nff9UqlSJO++8k61bt+bbJvd12LJlCw888ACVK1embdu2kHPcye23387SpUtp2bIlZcuWpWnTpixduhSAWbNm5X1vtWjRgvXr1+d77I0bN9K9e3fq1q1LcHAw4eHhPPLIIxw5cuQfsxdk4MCBWJbFpEmT8hX6XC1btsw7Tib3Nc7Nmqug75Fz/Qw988wzVKhQ4azvB4D777+f8PDwfMN9CvPzKiKu0Z56ER+QkpJCUlJSvuuqVq1a5Mc7duwY7du358CBAzzxxBNceuml/PLLL/Tt25dDhw4xbNgwAGzb5s477+Snn37iySefpHHjxsyePZtu3bpd0PPZtWsXABdddBEAEydOpFu3bkRHRzNkyBCOHTvGyJEjadu2LevXr89Xjk+dOkV0dDRt27bl3XffzRua8eijjzJu3Dg6duzIY489xqlTp/jxxx9ZsWJF3rsc//vf/3j99dfp3Lkzjz32GImJiQwfPpzrrruO9evXU6lSpbzP89dff9GhQwfuuusuOnfuzIwZM3jllVdo2rQpHTt2pHHjxrzxxhv069ePxx9/nHbt2gFwzTXXFPp1SEhI4Oqrr8ayLJ555hmqVavGwoULefTRR0lNTaV3794X9DqfqXPnztSpU4dBgwaxbt06Pv/8c6pXr86QIUPytnnsscf48ssveeCBB7jmmmv4/vvvue222y44+5tvvklgYCAvvvgimZmZBAYG/mPeAQMGcN111zFy5Mjz7q3/7rvv6NixI3Xr1mXAgAEcP36c4cOHc+2117Ju3bqz/rm69957adCgAW+//Ta2beddv3PnTh544AGeeOIJHnroId59913+/e9/M2rUKF599VWeeuopAAYNGkTnzp3Zvn173rsNixcv5o8//qBHjx6Eh4ezefNmPvvsMzZv3syKFSvOOnj9fI4dO0ZcXBzXXXcdl156aaHvV1gF/QzVrl2bESNG8M0333Dvvffmy/L111/TvXt3/P39wcWfVxFxgS0iXmvs2LE2UOBi27a9e/duG7DHjh2bd5/+/fvbZ/5qqFWrlt2tW7e8j9988027fPny9u+//55vuz59+tj+/v723r17bdu27Tlz5tiAPXTo0LxtTp06Zbdr1+6sz1uQJUuW2IA9ZswYOzEx0T548KD9zTff2LVr17Yty7JXr15tp6Wl2ZUqVbJ79uyZ777x8fF2aGhovuu7detmA3afPn3ybfv999/bgP3cc8+dlcHhcNi2bdt79uyx/f397f/973/5bt+0aZMdEBCQ7/r27dvbgD1hwoS86zIzM+3w8HD77rvvzrtu9erV53wdunXrZteqVSvfdYDdv3//vI8fffRRu0aNGnZSUlK+7e677z47NDTUPnbsWIGvq32Or/25Pk/u98QjjzySb7v//Oc/9kUXXZT38YYNG2zAfuqpp/Jt98ADDxQ5e+73QN26dc/7fM7M//TTT9u2bds33HCDHR4ennff3J+J1atX520fGRlpV69e3T5y5Ejedb/++qvt5+dnd+3a9azX4f777z/rc9aqVcsG7F9++SXvukWLFtmAXbZsWfvPP//Mu/7TTz+1AXvJkiV51xX03L766isbsH/44Ye863Lz7969+5zP/9dff7UB+/nnny/Eq3X6Nf57Hvsc3yPn+hlyOBz2xRdfnO/727Zte9q0afmegys/ryLiGg2/EfEBI0aMYPHixfmWCzF9+nTatWtH5cqVSUpKyluioqLIzs7mhx9+AGDBggUEBATQq1evvPv6+/vz7LPPuvT5HnnkEapVq0ZERAS33XYbGRkZjB8/npYtW7J48WKSk5O5//7782Xx9/endevWLFmy5KzH+3segJkzZ2JZFv379z9r29w9pLNmzcLhcNC5c+d8nyc8PJwGDRqc9XkqVKjAQw89lPdxYGAgV111VbHN2mPbNjNnzuTf//43tm3nyxQdHU1KSgrr1q0rls+V68knn8z3cbt27Thy5EjeUJgFCxZAzoHNf3fmXveiZO/WrRtly5Z1OfOAAQOIj49n1KhRBd5+6NAhNmzYQPfu3alSpUre9c2aNePmm2/Oe07nex1yNWnShDZt2uR93Lp1a8gZLvb3Pea51//9e+Hvz+3EiRMkJSVx9dVXA7j8dcz9ehQ07Ka4nPkzZFkW9957LwsWLCA9PT3v+qlTp3LxxRfnDVMqys+riBSOht+I+ICrrrrqnAfKFsWOHTvYuHEj1apVK/D2w4cPA/Dnn39So0YNKlSokO/2hg0buvT5+vXrR7t27fD396dq1ao0btyYgICAvCz8bZz9mUJCQvJ9HBAQwCWXXJLvul27dhEREZGv1J1px44d2LZNgwYNCrz9zANbL7nkkrOGTFSuXJmNGzee97kWVmJiIsnJyXz22Wd89tlnBW6T+3UoLmcO5ahcuTLkDDUKCQnhzz//xM/Pj3r16uXb7syvd1Gynzl7U2Fdd9113HDDDQwdOrTAMv7nn38WmBGgcePGLFq06KwDQc+V5czXJzQ0FICaNWsWeP1ff/2Vd93Ro0cZOHAgU6ZMOeu5p6SkFOq55sr9nk9LS3PpfoVV0M8QQJc
"text/plain": [
"<Figure size 900x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots(figsize=(9,6))\n",
"ax.plot(x,y, color='r')\n",
"\n",
"# filling under the curve\n",
"x_fill = np.arange(-4, percentile, 0.01)\n",
"y_fill = stats.norm(0,1).pdf(x_fill)\n",
"ax.fill_between(x_fill, y_fill, 0, color='g')\n",
"plt.title(\"Filled Percentile under Normal Curve\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "1f7a26f6-36d8-4561-ad68-db6c19c7720e",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-c469fe12a69725b1",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"Machen wir dies alles am Beispiel der Fuchsrote Lockensandbiene fest. Diese ist laut [Bundesministerium für Ernährung und Landwirtschaft](https://www.bmel.de/DE/themen/landwirtschaft/artenvielfalt/bienen-fuettern/wildbienen-honigbienen-und-co.html) 12-14 mm groß. Daher lässt sich annehmen das die meisten Bienen $\\mu=13mm$, mit einer Standardabweichung von $\\sigma=1mm$ haben. Wir wollen nun wissen wie groß 95% der Bienen sind, rechnen wir dies mittels `ppf` aus:"
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "6d57bdf0-8e75-4760-8159-d42effd9ea62",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-360e11812d2e9932",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAvUAAAIQCAYAAAABy5G8AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAACHKUlEQVR4nOzdd3gU1dvG8e+k0xJKIAGM9KKCRIoIUkQioXcIoFIsKP7Egg0sFEUpgiIdURQrNsSOJYrlpSgggogKCFJDTwIB0va8f4wJBBLIhpDZTe7Pde21J7Ozk3uz2eTZs2fOsYwxBhERERER8Vo+TgcQEREREZELo6JeRERERMTLqagXEREREfFyKupFRERERLycinoRERERES+nol5ERERExMupqBcRERER8XIq6kVEREREvJyKehERERERL6eiXqQI2759O5Zl8eqrr2ZuGzt2LJZlZdmvatWqDB48+KJ+X8lq8ODBVK1aNcs2y7IYO3asY5kuhDdn9ySvvvoqlmWxfft2p6OIiIdRUS9SiGUUANldRo4c6XS881q2bFmWzP7+/lSvXp2BAwfyzz//OB3vgu3Zs4exY8eybt06p6MUKhm/L1O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MCXHbZZXz55ZdnnQiaU5Yzfz4hISEAREREZLv9yJEjmdsOHz7MuHHjWLRo0VmPPSEhIVePNUPG7/zRo0fdul9uZfcaAoiJiWHatGl8/PHHDBgwgGPHjvH
"text/plain": [
"<Figure size 900x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mu = 13 # Mean\n",
"sigma = 1 # std deviation in mm\n",
"\n",
"x_norm = np.linspace(10, 16, 100) # Normaldistribution range from 10-16mm \n",
"y_norm = stats.norm(mu, sigma).pdf(x) # Calculate normal\n",
"\n",
"# Height of 95th percentile of bees\n",
"percentile = stats.norm(mu,sigma).ppf(0.95)\n",
"\n",
"x_percentile = np.arange(x_norm[0], percentile, 0.01)\n",
"y_percentile = stats.norm(mu,sigma).pdf(x_percentile)\n",
"\n",
"# Plot\n",
"fig, ax = plt.subplots(figsize=(9,6))\n",
"ax.plot(x_norm, y_norm, color='r')\n",
"\n",
"# filling under the curve\n",
"ax.fill_between(x_percentile, y_percentile, 0, alpha=0.2, color='#FE0000')\n",
"\n",
"\n",
"# Set text\n",
"ax.text(0.5,0.25,\n",
" f\"95th percentile of bees fall under {percentile:.2f}mm\",\n",
" ha='center', va='center', transform=ax.transAxes,\n",
" bbox={'facecolor':'#fafafa','alpha':1,'edgecolor':'none','pad':1},\n",
" color='#de2e0b'\n",
" )\n",
"\n",
"# Show\n",
"plt.title(\"Filled Percentile under Normal Curve\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "e9c59eb8-bfb1-46f2-bf1f-af1ffe59e190",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-7718d285f36c2fad",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Aufgabe \n",
"\n",
"*6 Punkte*\n",
"\n",
"Gegeben sind die nach Altersgruppe aufgeschlüsselten Durschnittskörpergrößen (in cm) von Frauen in Deutschland. (Zu finden beim [Statistischen Bundesamt](https://www.destatis.de/DE/Themen/Gesellschaft-Umwelt/Gesundheit/Gesundheitszustand-Relevantes-Verhalten/Tabellen/koerpermasse-frauen.html))\n",
"\n",
"Gehe wie folgt vor:\n",
"\n",
"- Berechne das arithmetische Mittel nutze dafür NumPy. und speichere das Ergebnis mit einer Genauigkeit von 1 Dezimalstelle nach dem Komma in der Variablen `avg_height`.\n",
"- Gegeben ist auch die Standardabweichung von 15cm, stelle die Normalverteilung mittels `norm.pdf` auf. Speichere den Wert in `norm_height` und finde einen geeigneten linespace zum plotten.\n",
"- Berechne folgend die Körpergröße unter die 80% aller Frauen (nach Datenset) fallen. Speichere den Wert in der Variablen `avg_percentile`.\n",
"- Plotte das Ergebnis. Orientiere dich gerne an dem Bienenbeispiel. Finde eine geeignete Darstellung. *Tipp: Da die Y-Achse in diesem Beispiel keinen Sinn ergibt kannst du sie einfach austellen mit `plt.yticks([])`*\n"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "3983d224-e885-43bd-aadd-4e276a56a370",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-048d8330d555432a",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# Given\n",
"avg_height_per_woman = {\n",
" \"18 - 20\": 167.6,\n",
" \"20 - 25\": 167.7,\n",
" \"25 - 30\": 167.3,\n",
" \"30 - 35\": 167.2,\n",
" \"35 - 40\": 167.3,\n",
" \"40 - 45\": 167.5,\n",
" \"45 - 50\": 167.1,\n",
" \"50 - 55\": 167.1,\n",
" \"55 - 60\": 166.9,\n",
" \"60 - 65\": 165.4,\n",
" \"65 - 70\": 164.5,\n",
" \"70 - 75\": 163.9,\n",
" \"75+\": 162.8\n",
"}\n",
"\n",
"avg_height = None\n",
"norm_height = None\n",
"avg_percentile = None"
]
},
{
"cell_type": "code",
"execution_count": 138,
"id": "2c2e2cf6-c743-4cb0-a329-2240205fc7da",
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-a59f2b3230aad3d6",
"locked": false,
"points": 3,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [
{
"data": {
"image/png": "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8kqysrJ47733uPjii/n888/p379/ka/3eNSuXZtevXoxd+5ckpOTiYqKKvS4Cy+8kNWrV3PzzTdTt25ddu7cyezZs9m0aRN169bl2Wef5eabbyYyMpJ7770XgOrVq+c7x/Dhw6lWrRr3338/aWlpx6xr3bp1XHrppQwbNoxBgwYxadIkLr74YmbNmkXfvn2P6zUWp7bDTZ48mSFDhtCpUycee+wxduzYwXPPPceiRYtYsWIFlStX9h6bm5vLWWedxSmnnMJTTz3Ft99+y/jx42nQoAE33njjcdUpIsVgRKRUrF692gDmoYceMsYYk52dbSIiIsyUKVOMMcZUr17dTJgwwRhjTHJysvF4POa///2vMcaYlStXGsBcd911+c552223GcDMmTPHu69OnToGMIsXL/bu+/rrrw1gwsLCzMaNG737X3nlFQOYuXPnevcdOHCgQO3vvvuuAcz333/v3TdmzBgDmGuvvTbfsRdccIGpWrVqke/HoEGDDHDM20033eQ9ftq0aSYgIMAsWLAg33kmTpxoALNo0aJ878GgQYO83998883GsiyzYsUK7749e/aYKlWqGMBs2LChwPt3+GvduXOnCQkJMaNHj/bumz59eoH37ljy3q9du3aZ+fPnG8A8/fTT+Z63f//+3u8//vhjA5iHH34433kuuugiY1mWWb9+vXcfYAICAszq1avzHTtp0iQDmLPOOsvYtu3d36VLF2NZlhk2bJh3X05OjqlVq5bp1atXvnMc+fOQlZVlWrZsaU477bR8+498z4/myOt6pFtuucUA5pdffjHGGLNhwwYDmEmTJhljjNm3b58BzJNPPnnM52nRokWB12IOe0+6d+9ucnJyCr2vsJ+Hjz76yLsvKSnJ1KhRw7Rr1867L+/6Hu35Dj/n0WqbO3duvp+prKwsExcXZ1q2bGnS09O9x33++ecGMPfff793X96/pwcffDDfOdu1a2c6dOhwzPdKRE6MumeIlJJmzZpRtWpVb1/lX375hbS0NO/sGF27dvUOBlyyZAm5ubne/sxffvklAP/3f/+X75yjR48G4Isvvsi3v3nz5nTp0sX7/SmnnALAaaedRu3atQvs//vvv737Dm/5zMjIYPfu3Zx66qkA/PzzzwVe17Bhw/J936NHD/bs2UNycnKR70loaCizZ88u9Hak6dOn06xZM5o2bcru3bu9t9NOOw2g0NlH8syaNYsuXbrQtm1b774qVapw5ZVXFnp88+bN6dGjh/f7atWq0aRJk3zv08no2bMnffr04YknnsjXXedwX375JR6Ph5EjR+bbP3r0aIwxfPXVV/n29+rVi+bNmxd6rqFDh+b7hOCUU07BGMPQoUO9+zweDx07dizwGg//edi3bx9JSUn06NGj0J+FkpA3o0pKSkqh94eFhREcHMy8efPYt2/fCT/Pf//732L3X05ISOCCCy7wfh8VFcU111zDihUr2L59+wnXUJSffvqJnTt3Mnz48Hx9nfv370/Tpk0L/LvnKP8eS+rnVkTyU/cMkVJiWRZdu3bl+++/x7ZtFi1aRFxcHA0bNoSDofnFF1+Eg10zOGwQ4MaNGwkICPAemyc+Pp7KlSuzcePGfPsPD8YA0dHRACQmJha6//DwsXfvXh544AHee+89du7cme/4pKSkAq/ryOeKiYnxnvN
"text/plain": [
"<Figure size 900x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# BEGIN SOLUTION\n",
"avg_height = np.round(np.mean(list(avg_height_per_woman.values())), decimals=1)\n",
"std_sigma = 15\n",
"\n",
"norm_x = np.linspace(120, 220, 1000)\n",
"norm_height = stats.norm(avg_height, std_sigma).pdf(norm_x)\n",
"\n",
"# Height of 80th percentile of woman heights\n",
"avg_percentile = stats.norm(avg_height, std_sigma).ppf(0.8)\n",
"\n",
"x_percentile = np.arange(norm_x[0], avg_percentile, 0.01)\n",
"y_percentile = stats.norm(avg_height, std_sigma).pdf(x_percentile)\n",
"\n",
"# Plot\n",
"fig, ax = plt.subplots(figsize=(9,6))\n",
"ax.plot(norm_x, norm_height, color='r')\n",
"\n",
"# filling under the curve\n",
"ax.fill_between(x_percentile, y_percentile, 0, alpha=.5, color='#fa0000')\n",
"\n",
"\n",
"# Set text\n",
"ax.text(0.4,0.18,\n",
" f\"80th percentile of Womens heigth\\n fall under {avg_percentile:.1f}cm\",\n",
" ha='center', va='center', transform=ax.transAxes,\n",
" bbox={'facecolor':'#fafafa','alpha':1,'edgecolor':'none','pad':1},\n",
" color='#de2e0b'\n",
" )\n",
"\n",
"# Show\n",
"plt.title(\"Woman Height Normal Distribution\")\n",
"plt.xlabel(\"Height\")\n",
"plt.yticks([]) # hide y\n",
"plt.show()\n",
"# END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": 150,
"id": "d9f8aeae-6b51-4645-b578-19693a07ece3",
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-26f141bc8b12d7fe",
"locked": true,
"points": 3,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"# Hier werden ihre Lösungen getestet...\n",
"import math \n",
"\n",
"# Check if average height is close to real value\n",
"assert math.isclose(avg_height, 166.3, rel_tol=.2) # 1 Punkt\n",
"\n",
"# Check if norm height is close to real value\n",
"assert math.isclose(np.round(np.sum(norm_height), decimals=1), 10, rel_tol=.2) # 1 Punkt\n",
"\n",
"# Check if percentile is close to real value\n",
"assert math.isclose(avg_percentile, 179, rel_tol=.2) # 1 Punkt"
]
},
{
"cell_type": "markdown",
"id": "f50b8883-e018-4a44-831a-1a3c14640955",
"metadata": {},
"source": [
"## Probabillity Mass Function - Binomial Verteilung\n",
"\n",
"Im Gegensatz zur Normal Verteilung folgen Binomiale Verteilungen Diskreten Werten. Dies lässt sich am besten am Beispiel zweier unabhängiger Würfel zeigen.\n",
"\n",
"Wir wollen im folgenden also aufzeigen wie die Wahrscheinlichkeits Verteilung zweier 6 seitiger Würfel ist. Da wir die Augenzahl beider Bestimmen führen wir eine Addition aller möglichen Werte vor. Da uns die Auftrittswahrscheinlichkeit interessiert eignet sich ein dict zum speichern der Daten. Dabei ist der Schlüssel die Augenzahl und der Wert die Autrittsanzahl:"
]
},
{
"cell_type": "code",
"execution_count": 160,
"id": "7a58ad07-9593-4c58-9a86-23a002ef5e94",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 5, 9: 4, 10: 3, 11: 2, 12: 1}\n"
]
}
],
"source": [
"# Two Dices\n",
"first_dice: np.array = np.arange(1,7)\n",
"second_dice: np.array = np.arange(1,7)\n",
"\n",
"# Creating a dict with all keys\n",
"dist: dict = {el: 0 for el in range(2,13)}\n",
"\n",
"# Summing all possible combinations and store them inside the dict\n",
"for el1 in first_dice:\n",
" for el2 in second_dice:\n",
" dice_roll: int = el1 + el2\n",
" dist[dice_roll] += 1\n",
"\n",
"# Print the result\n",
"print(dist)"
]
},
{
"cell_type": "markdown",
"id": "0b2ff2d3-e8b0-45e9-9429-8db1acc5e526",
"metadata": {},
"source": [
"Wie wir erstmal sehen können folgen die Zahl einem Muster, dieses lässt sich mittels Balkendiagramm visualisieren:"
]
},
{
"cell_type": "code",
"execution_count": 176,
"id": "f7915dad-d12a-4db1-b691-3fd53ead4027",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAioAAAHHCAYAAACRAnNyAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA/HElEQVR4nO3deVxVdeL/8fcV4QoIuISKqYho7rigmWuWppFTWk0uWe41U5jbNCnTGGDulWO2aKs2NabVpJmJiKY4LqW45ULuC6WmuYCiosD5/fGT+/UKKJcunoO8no/Hfczczz33nDe3K/fNOZ9zrs0wDEMAAAAWVMrsAAAAAPmhqAAAAMuiqAAAAMuiqAAAA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z4+Pvnu4g8NDZUkVapUSZ07d3bhJyuYr776ShcvXrzh4YycDDt27Mi30OUs4+/v79ac1/73uH6v0u7dux2P55fJMAyFhIQ49i4UVGBgoLy9vfP8b5bXeyMvFSpU0MCBAzVw4ECdP39eHTp0UExMjKOouPMqutnZ2Tpw4IDTz7lnzx7p6hlJumbPxfXv2bzerwXNFhgYKB8fn3z/vZQqVUrVq1d38acBCoY5Krjtff/993r11VcVEhKivn37SlePrTdt2lSffPKJ0y/0HTt2aNmyZXrooYccY6GhoUpNTdVPP/3kGDt27JgWLFiQ5/bWr1+vzZs3O+6npKTom2++UZcuXfL
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.bar(dist.keys(), dist.values(), color='#F00a00')\n",
"plt.xticks(list(dist.keys()))\n",
"plt.title(\"Double Dice role distribution\")\n",
"plt.xlabel(\"Points rolled\")\n",
"plt.ylabel(\"Number of occurrences\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "13e3406d-bb54-482c-a76c-a45a4c309a76",
"metadata": {},
"source": [
"Selbiges nur in Prozent statt absolut"
]
},
{
"cell_type": "code",
"execution_count": 214,
"id": "db27df8d-7bc1-418b-997a-01684eb6fc1e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(nan)"
]
},
"execution_count": 214,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vals = np.arange(2,13)\n",
"mean, var = stats.binom.stats(13, 0.5)\n",
"dist = stats.binom.pmf(7, mean, var)\n",
"dist"
]
},
{
"cell_type": "code",
"execution_count": 198,
"id": "41b90981-ef18-40b4-bb03-9cd3407869b4",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiwAAAGdCAYAAAAxCSikAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZ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AyRMsAEDyBAsAkDzBAgAkT7AAAMkTLABA8gQLAJA8wQIAJE+wAADJEywAQPIECwCQPMECACRPsAAAyRMsAEDyBAsAkDzBAgAkT7AAAMkTLABA8gQLAJA8wQIAJE+wAADJEywAQPIECwCQPMECACRPsAAAyRMsAEDyBAsAkDzBAgAkT7AAAMkTLABA8gQLAJA8wQIAJG9goQeA/1Uz/yeFHuEQLy65stAjUCT8/w194woLAJA8wQIAJE+wAADJEywAQPIECwCQPMECACRPsAAAyRMsAEDyBAsAkDzBAgAkT7AAAMkTLABA8gQLAJA8wQIAJE+wAADJEywAQPIECwCQPMECACRPsAAAyRMsAEDyBAsAkDzBAgAkT7AAAMkTLABA8gQLAJA8wQIAJE+wAADJ61OwrF69OmpqamLw4MFRW1sbmzZtOuzaP/7xj3HNNddETU1NlJSUxMqVK9/xMQGA40vewbJ+/fpobGyMpqam2LJlS4wfPz7q6+tj165dva7fv39/jB49OpYsWRKVlZVH5JgAwPEl72BZsWJFXH/99dHQ0BBjx46NtWvXxgknnBD33ntvr+svvPDCuOOOO2LGjBlRVlZ2RI4
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.bar(np.arange(2,12),stats.binom.pmf(np.arange(2,12), 12, 0.5))\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 209,
"id": "1d3d7273-ed46-42e4-8ce6-cded64f81e5c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<BarContainer object of 6 artists>"
]
},
"execution_count": 209,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiwAAAGdCAYAAAAxCSikAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAfZElEQVR4nO3dfWyV9f3/8VdvaItI64TR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xcTEfLuzAwAAY0KYx+PxBLJDXl6ecnJytG7dOkmS2+1WcnKylixZohUrVpy0vqysTENDQ9q2bZt32/XXX6/MzEw1NjZKku666y6NGzdOv/vd70Z1EoODg4qLi9PAwIBiY2NHdQzAitQVLwV7hKD4ZE1JsEcAcIEF8ud3QHdYjh8/rs7OThUVFX1zgPBwFRUVqb293e8+7e3tPuslqbi42Lve7XbrpZde0tVXX63i4mJNmTJFeXl52rJlyynnGB4e1uDgoM8LAACMXQEFy5EjRzQyMqL4+Hif7fHx8XK5XH73cblcp13f39+vY8eOac2aNZo3b55effVV3XHHHfrRj36kXbt2+T2m0+lUXFyc95WcnBzIaQAAgBAT9O8ScrvdkqTbb79dDzzwgDIzM7VixQr98Ic/9L5l9H9VV1drYGDA++rt7b3AUwMAgAspMpDFkydPVkREhPr6+ny29/X1KSEhwe8+CQkJp10/efJkRUZGavbs2T5rZs2apddff93vMaOjoxUdHR3I6AAAIIQFdIclKipKWVlZam1t9W5zu91qbW1Vfn6+333y8/N91kv
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.bar(np.arange(1,7),stats.binom.pmf(np.arange(1,7), 6, 0.5) * stats.binom.pmf(np.arange(1,7), 6, 0.5))"
]
},
{
"cell_type": "code",
"execution_count": 203,
"id": "2285d290-73a2-4c2d-8542-46c6aea2c71b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.00878906, 0.05493164, 0.09765625, 0.05493164, 0.00878906])"
]
},
"execution_count": 203,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stats.binom.pmf(np.arange(1,6), 6, 0.5) * stats.binom.pmf(np.arange(1,6), 6, 0.5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b142059-c19b-49c4-bc4b-02ca875b1fbf",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}