{ "cells": [ { "cell_type": "markdown", "id": "471b22f4-5b9d-423d-bf7d-6cef078b175e", "metadata": {}, "source": [ "# Simulationen\n", "\n", "Aufbauend auf der letzten Aufgabe möchten wir jetzt simulieren, welches Ertrag wir erwarten können, wenn der Zinnsatz zufällig schwankt. Dafür benötigen wir eine Möglichkeit, diesen Zufall zu berechnen." ] }, { "cell_type": "markdown", "id": "b298aa38-556e-43d4-aee1-0047477a53fa", "metadata": {}, "source": [ "## Zufallszahlen\n", "\n", "Das Package `random` enthält Funktionen zu Erzeugung von Zufallszahlen.\n", "Probieren wir es aus:" ] }, { "cell_type": "code", "execution_count": 5, "id": "2f9a1ee9-0a3f-4ee0-a6e1-d6bc61291889", "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "markdown", "id": "327159f5-1e19-4334-9738-e9cc8a0c9d47", "metadata": {}, "source": [ "`help(random)` liefert eine lange Dokumentation. Wir brauchen nur einen kleinen Teil davon." ] }, { "cell_type": "code", "execution_count": 6, "id": "7ba2a1a0-745d-4c9b-bbdf-8e562a445204", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.27\n", "0.57\n", "0.12\n", "3.79\n", "1.4\n", "1.25\n", "1.14\n", "0.71\n", "3.77\n", "0.52\n" ] } ], "source": [ "# Initialisierung - gleicher seed liefert gleiche Zufallszahlen!\n", "random.seed(42)\n", "samples = 10\n", "for i in range(samples):\n", " tmp = random.randint(0, 400)\n", " print(tmp / 100)" ] }, { "cell_type": "markdown", "id": "b8a2a8fe-fbb4-4ab7-b927-841d949f3cf6", "metadata": {}, "source": [ "## Aufgabe\n", "Was tut das kleine Beispiel?\n", "Baue eine Funktion, die einen Array der Länge **n** zurückliefert.\n", "Der Inhalt sollen Zufallszahlen mit 2 Nachkommastellen sein die zwischen **from** und **to* liegen." ] }, { "cell_type": "markdown", "id": "7ed88639-c037-415a-b93f-bcdc8cb49a7c", "metadata": {}, "source": [ "## Lösung" ] }, { "cell_type": "code", "execution_count": 7, "id": "3ffef006-eac3-41f6-9334-f22db10dc0d0", "metadata": {}, "outputs": [], "source": [ "def rand_numbers(n, start, end):\n", " digits = 2; # falls es mal nur 1 oder 3 Nachkommastellen werden sollen\n", " factor = 10 ** digits;\n", " result = []\n", " for i in range(n):\n", " result.append(random.randint(start * factor, end * factor) / factor)\n", " \n", " return result" ] }, { "cell_type": "code", "execution_count": 8, "id": "34cda81b-7611-439e-8ba2-3f01c210fa76", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9.12, 36.56, 4.6, 4.44, 9.67]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_numbers(5, 2, 44)" ] }, { "cell_type": "markdown", "id": "976b757c-09fa-4492-8996-8129049dc0bf", "metadata": {}, "source": [ "## Aufgabe\n", "\n", "Schreibe eine neue Funktion, die diese Funktion als Zinsen in der Funktion zur variablen Zinsberechnung verwendet." ] }, { "cell_type": "code", "execution_count": 13, "id": "cf269399-9603-4fc6-ae16-c40562d4d8dc", "metadata": { "tags": [ "hide-input" ] }, "outputs": [], "source": [ "def variabler_zins(anlagebetrag, zinsen):\n", " jahre = len(zinsen)\n", " result = [anlagebetrag]\n", " for i in range(0, jahre):\n", " result.append(result[i] * (1+ zinsen[i]/100))\n", " return result" ] }, { "cell_type": "code", "execution_count": 14, "id": "16d443c6-6b7c-4362-9048-92f23f44da3a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[100, 101.49999999999999, 104.03749999999998, 107.67881249999998]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variabler_zins(100, [1.5, 2.5, 3.5])" ] }, { "cell_type": "markdown", "id": "ebbfb7e2-f7e1-4c1d-9f2e-6b5ad896be11", "metadata": {}, "source": [ "## Lösung" ] }, { "cell_type": "code", "execution_count": 11, "id": "ac0de40d-c851-40e8-9f18-2c68c25cd7f0", "metadata": {}, "outputs": [], "source": [ "def variabler_zufallszins(anlagebetrag, jahre, min_zins, max_zins):\n", " return variabler_zins(anlagebetrag, rand_numbers(jahre, min_zins, max_zins))" ] }, { "cell_type": "code", "execution_count": 12, "id": "5d41f256-22ff-4604-9680-011cfbe77511", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1000,\n", " 1005.5000000000001,\n", " 1011.4324500000001,\n", " 1024.479928605,\n", " 1040.256919505517,\n", " 1040.8810736572204]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variabler_zufallszins(1000, 5, 0, 2)" ] }, { "cell_type": "markdown", "id": "5af14f52-76be-448b-b15f-2fe0d7d50a13", "metadata": {}, "source": [ "## Aufgabe\n", "Eine einzelne Simulation hat nicht viel Aussagekraft. Schreibe eine Funktion, die die Simulation **n** mal ausführt und von jeder Ausführung das Endergebnis zurückgibt." ] }, { "cell_type": "markdown", "id": "b4192e1f-8e0d-4de8-b6fb-215849aeb02f", "metadata": {}, "source": [ "## Lösung" ] }, { "cell_type": "code", "execution_count": 22, "id": "e5ef01b5-bdce-4a3e-8d29-a3081126bdab", "metadata": {}, "outputs": [], "source": [ "def simulate(anlagebetrag, jahre, min_zins, max_zins, n):\n", " result = []\n", " for i in range(n):\n", " tmp = variabler_zufallszins(anlagebetrag, jahre, min_zins, max_zins)\n", " result.append(tmp[-1]) # letztes Element in Array hat Index -1, vorletztes -2, usw.\n", " \n", " return result" ] }, { "cell_type": "code", "execution_count": 23, "id": "441e6fc3-66e5-41bf-9bb2-c01c475ad0b8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1052.6442959071103,\n", " 1064.427091942078,\n", " 1083.9725955171834,\n", " 1089.3384949096728,\n", " 1093.7917438123886,\n", " 1099.7624262587865,\n", " 1112.5348861022283,\n", " 1123.567897245912,\n", " 1128.9355107957267,\n", " 1167.8728995649053]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results = simulate(1000, 5, 0, 4, 10)\n", "results.sort()\n", "results" ] } ], "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }