Created
March 31, 2019 20:07
-
-
Save halexus/a2e8f2e80eab70170336c540e414da06 to your computer and use it in GitHub Desktop.
Beispiel 21 UE Wahrscheinlichkeitstheorie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Simulation von Lottospielen" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import random\n", | |
"import collections\n", | |
"import pprint # Schöne Ausgabe von dictionaries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"****************************************************************************************************\n", | |
"Kontrolle: 1000000\n", | |
"defaultdict(<class 'int'>,\n", | |
" {'0': 338857,\n", | |
" '0Z': 61707,\n", | |
" '1': 368900,\n", | |
" '1Z': 54402,\n", | |
" '2': 136649,\n", | |
" '2Z': 15659,\n", | |
" '3': 20681,\n", | |
" '3Z': 1738,\n", | |
" '4': 1291,\n", | |
" '4Z': 72,\n", | |
" '5': 42,\n", | |
" '5Z': 1,\n", | |
" '6': 1})\n" | |
] | |
} | |
], | |
"source": [ | |
"def simulation(gewaehlte_zahlen, N=1000000):\n", | |
" '''Simuliert N (default=10^6) Lottoziehungen.\n", | |
" Gibt die Anzahl der jeweils richtig getippten Zahlen in einem dictionary zurück.'''\n", | |
" moegliche_zahlen = list(range(1, 46)) # Es sind die Zahlen 1-45 erlaubt\n", | |
" ergebnis = collections.defaultdict(int)\n", | |
" gewaehlte_zahlen = set(gewaehlte_zahlen)\n", | |
" for i in range(N):\n", | |
" if i%(N//100)==0: # Fortschrittsbalken; Gib 100 '*' aus um den Fortschritt der Simulation zu sehen\n", | |
" print(\"*\", end=\"\", flush=True)\n", | |
" ziehung = random.sample(moegliche_zahlen, 7) # Ziehe 6 Zahlen und eine Zusatzzahl\n", | |
" zusatzzahl = ziehung[6] # Zuletzt gewählte Zahl ist Zusatzzahl\n", | |
" ziehung = set(ziehung[:6]) # Ziehung besteht aus den ersten 6 Zahlen. \n", | |
" anzahl_richtiger_zahlen = len(ziehung & gewaehlte_zahlen) \n", | |
" if zusatzzahl in gewaehlte_zahlen:\n", | |
" ergebnis['%dZ' % anzahl_richtiger_zahlen] += 1\n", | |
" else:\n", | |
" ergebnis[str(anzahl_richtiger_zahlen)] += 1\n", | |
" print()\n", | |
" return ergebnis\n", | |
"\n", | |
"ergebnis = simulation({1, 2, 3, 4, 5, 6}) # Da jede mögliche Folge an Zahlen gleichwahrscheinlich, wähle ich einfach die Zahlen 1 bis 6\n", | |
" # Simuliere 1 Million Lottospiele\n", | |
"print(\"Kontrolle:\",sum(ergebnis.values()))\n", | |
"pprint.pprint(ergebnis)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Ich habe die Simulation mit $10^{10}$ Lottorunden durchgeführt (das hat auf meinem PC einen Nachmittag benötigt) und das folgende Ergebnis erhalten:\n", | |
"***\n", | |
"\n", | |
" defaultdict(<class 'int'>,\n", | |
" {'0': 3389424243,\n", | |
" '0Z': 616274885,\n", | |
" '1': 3697478438,\n", | |
" '1Z': 543729648,\n", | |
" '2': 1359379079,\n", | |
" '2Z': 155365327,\n", | |
" '3': 207147579,\n", | |
" '3Z': 17264021,\n", | |
" '4': 12948012,\n", | |
" '4Z': 699918,\n", | |
" '5': 280333,\n", | |
" '5Z': 7351,\n", | |
" '6': 1166})" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Wir wollen davon die relativen Häufigkeiten berechnen." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"h( X = 0 ) = 0.3389424243\n", | |
"h( X = 0Z ) = 0.0616274885\n", | |
"h( X = 1 ) = 0.3697478438\n", | |
"h( X = 1Z ) = 0.0543729648\n", | |
"h( X = 2 ) = 0.1359379079\n", | |
"h( X = 2Z ) = 0.0155365327\n", | |
"h( X = 3 ) = 0.0207147579\n", | |
"h( X = 3Z ) = 0.0017264021\n", | |
"h( X = 4 ) = 0.0012948012\n", | |
"h( X = 4Z ) = 6.99918e-05\n", | |
"h( X = 5 ) = 2.80333e-05\n", | |
"h( X = 5Z ) = 7.351e-07\n", | |
"h( X = 6 ) = 1.166e-07\n" | |
] | |
} | |
], | |
"source": [ | |
"results = {'0': 3389424243,\n", | |
" '0Z': 616274885,\n", | |
" '1': 3697478438,\n", | |
" '1Z': 543729648,\n", | |
" '2': 1359379079,\n", | |
" '2Z': 155365327,\n", | |
" '3': 207147579,\n", | |
" '3Z': 17264021,\n", | |
" '4': 12948012,\n", | |
" '4Z': 699918,\n", | |
" '5': 280333,\n", | |
" '5Z': 7351,\n", | |
" '6': 1166}\n", | |
"for k,v in results.items():\n", | |
" print('h( X =',k,') =',v/1e10)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Theoretische Wahrscheinlichkeiten für die Ausgänge der Zufallsvariable:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"P( X = 0 ) = 0.338939\n", | |
"P( X = 0Z ) = 0.0616253\n", | |
"P( X = 1 ) = 0.369752\n", | |
"P( X = 1Z ) = 0.0543753\n", | |
"P( X = 2 ) = 0.135938\n", | |
"P( X = 2Z ) = 0.0155358\n", | |
"P( X = 3 ) = 0.0207144\n", | |
"P( X = 3Z ) = 0.0017262\n", | |
"P( X = 4 ) = 0.00129465\n", | |
"P( X = 4Z ) = 6.99811e-05\n", | |
"P( X = 5 ) = 2.79924e-05\n", | |
"P( X = 5Z ) = 7.36643e-07\n", | |
"P( X = 6 ) = 1.22774e-07\n" | |
] | |
} | |
], | |
"source": [ | |
"import operator as op\n", | |
"from functools import reduce\n", | |
"\n", | |
"def ncr(n, r): # Binomialkoeffizient n über r\n", | |
" '''https://stackoverflow.com/a/4941932'''\n", | |
" r = min(r, n-r)\n", | |
" numer = reduce(op.mul, range(n, n-r, -1), 1)\n", | |
" denom = reduce(op.mul, range(1, r+1), 1)\n", | |
" return numer // denom\n", | |
"\n", | |
"for richtige in range(7): # 0 - 5Z\n", | |
" print('P( X = %d ) = %g' %(richtige, ncr(6,richtige)*ncr(39,6-richtige)/ncr(45,6)*(39-6+richtige)/39))\n", | |
" if richtige != 6:\n", | |
" print('P( X = %dZ ) = %g' %(richtige, ncr(6,richtige)*ncr(39,6-richtige)/ncr(45,6)*(6-richtige)/39))" | |
] | |
} | |
], | |
"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.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment