Skip to content

Instantly share code, notes, and snippets.

@halexus
Created April 7, 2019 17:58
Show Gist options
  • Save halexus/ed91933f63e40e4ad9d3e70ac77b7632 to your computer and use it in GitHub Desktop.
Save halexus/ed91933f63e40e4ad9d3e70ac77b7632 to your computer and use it in GitHub Desktop.
Aufgabe 26 UE Wahrscheinlichkeitstheorie
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Aufgabe 26\n",
"\n",
"Eine Münze wird viermal hintereinander geworfen. Betrachten wir den Grundraum $\\Omega=\\{K,Z\\}^4$, den Wahrscheinlichkeitsraum $(\\Omega, \\mathcal{P}(\\Omega), \\mathbb{P}_u)$ und die\n",
"Ereignisse $A_j =$ \\{Kopf tritt j-mal ein\\}, $j = 0, 1, 2, 3, 4$.\n",
"\n",
"(a) Bestimmen Sie die Wahrscheinlichkeiten $\\mathbb{P}(A_j)$, $j = 0, 1, 2, 3, 4$, und zeichnen Sie ein Stabdiagramm der Verteilung von der Zufallsvariable $X(A_j) = j$.\n",
"\n",
"(b) Wie ändert sich das Stabdiagramm, wenn die Münze nicht homogen (regelmäßig, echt, fair) ist? Nehmen Sie an, dass die Wahrscheinlichkeit für Kopf $p = 0.4$ ist.\n",
"***"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import plotly.offline as pl # Für die Säulendiagramme\n",
"import plotly.graph_objs as go # Für die Säulendiagramme"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def simulation(N=1000000, fair=True):\n",
" muenze = ('K', 'Z') # Eine Muenze besteht aus 'K'opf und 'Z'ahl\n",
" anzahl_kopf = [0]*5 # Wie oft kommt 0-5 mal Kopf vor\n",
" if fair:\n",
" weights = None\n",
" else:\n",
" weights = (0.4, 0.6) # Kopf hat Wahrscheinlichkeit p=0.4, Zahl hat p=0.6\n",
" for _ in range(N):\n",
" wurf = random.choices(muenze, weights, k=4)\n",
" anzahl_kopf[wurf.count('K')] += 1\n",
" return [x/N for x in anzahl_kopf]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Faire Münze: [0.062293, 0.249993, 0.374244, 0.250522, 0.062948]\n",
"Kopf p=0.4: [0.129555, 0.34569, 0.345465, 0.153553, 0.025737]\n"
]
}
],
"source": [
"N = 1000000\n",
"anzahl_fair = simulation(N)\n",
"print(\"Faire Münze:\", anzahl_fair)\n",
"anzahl_unfair = simulation(N, fair=False)\n",
"print(\"Kopf p=0.4:\", anzahl_unfair)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Säulendiagramme für die relativen Häufigkeiten der beiden Münzen (fair/unfair)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
],
"text/vnd.plotly.v1+html": [
"<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"linkText": "Export to plot.ly",
"plotlyServerURL": "https://plot.ly",
"showLink": false
},
"data": [
{
"name": "Fair",
"type": "bar",
"uid": "dd3e9583-945e-4659-a6e2-61bc0fb2a300",
"x": [
0,
1,
2,
3,
4
],
"y": [
0.062293,
0.249993,
0.374244,
0.250522,
0.062948
]
},
{
"name": "Unfair",
"type": "bar",
"uid": "f936c8d3-c587-4174-a39d-fe3edda7edd9",
"x": [
0,
1,
2,
3,
4
],
"y": [
0.129555,
0.34569,
0.345465,
0.153553,
0.025737
]
}
],
"layout": {
"barmode": "group",
"title": {
"text": "Relative Häufigkeiten für die Anzahl an \"Kopf\" beim 4 fachen Münzwurf bei einer Simulation von 1000000 Runden "
},
"xaxis": {
"title": {
"text": "Anzahl Kopf"
}
},
"yaxis": {
"title": {
"text": "Relative Häufigkeit"
}
}
}
},
"text/html": [
"<div id=\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";\n",
"if (document.getElementById(\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\")) {\n",
" Plotly.newPlot(\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\", [{\"name\": \"Fair\", \"x\": [0, 1, 2, 3, 4], \"y\": [0.062293, 0.249993, 0.374244, 0.250522, 0.062948], \"type\": \"bar\", \"uid\": \"dd3e9583-945e-4659-a6e2-61bc0fb2a300\"}, {\"name\": \"Unfair\", \"x\": [0, 1, 2, 3, 4], \"y\": [0.129555, 0.34569, 0.345465, 0.153553, 0.025737], \"type\": \"bar\", \"uid\": \"f936c8d3-c587-4174-a39d-fe3edda7edd9\"}], {\"barmode\": \"group\", \"title\": {\"text\": \"Relative H\\u00e4ufigkeiten f\\u00fcr die Anzahl an \\\"Kopf\\\" beim 4 fachen M\\u00fcnzwurf bei einer Simulation von 1000000 Runden \"}, \"xaxis\": {\"title\": {\"text\": \"Anzahl Kopf\"}}, \"yaxis\": {\"title\": {\"text\": \"Relative H\\u00e4ufigkeit\"}}}, {\"showLink\": false, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"}); \n",
"}\n",
"});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){if (document.getElementById(\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\")) {window._Plotly.Plots.resize(document.getElementById(\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\"));};})</script>"
],
"text/vnd.plotly.v1+html": [
"<div id=\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";\n",
"if (document.getElementById(\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\")) {\n",
" Plotly.newPlot(\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\", [{\"name\": \"Fair\", \"x\": [0, 1, 2, 3, 4], \"y\": [0.062293, 0.249993, 0.374244, 0.250522, 0.062948], \"type\": \"bar\", \"uid\": \"dd3e9583-945e-4659-a6e2-61bc0fb2a300\"}, {\"name\": \"Unfair\", \"x\": [0, 1, 2, 3, 4], \"y\": [0.129555, 0.34569, 0.345465, 0.153553, 0.025737], \"type\": \"bar\", \"uid\": \"f936c8d3-c587-4174-a39d-fe3edda7edd9\"}], {\"barmode\": \"group\", \"title\": {\"text\": \"Relative H\\u00e4ufigkeiten f\\u00fcr die Anzahl an \\\"Kopf\\\" beim 4 fachen M\\u00fcnzwurf bei einer Simulation von 1000000 Runden \"}, \"xaxis\": {\"title\": {\"text\": \"Anzahl Kopf\"}}, \"yaxis\": {\"title\": {\"text\": \"Relative H\\u00e4ufigkeit\"}}}, {\"showLink\": false, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"}); \n",
"}\n",
"});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){if (document.getElementById(\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\")) {window._Plotly.Plots.resize(document.getElementById(\"9c1695e5-b9b5-4470-a80c-1b3c4fbc6184\"));};})</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pl.init_notebook_mode(connected=True)\n",
"layout = {\n",
" 'xaxis': {'title': 'Anzahl Kopf'},\n",
" 'yaxis': {'title': 'Relative Häufigkeit'},\n",
" 'barmode': 'group',\n",
" 'title': 'Relative Häufigkeiten für die Anzahl an \"Kopf\" beim 4 fachen Münzwurf bei einer Simulation von %d Runden ' % N\n",
"}\n",
"data1 = go.Bar(\n",
" x = list(range(5)),\n",
" y = anzahl_fair,\n",
" name = 'Fair'\n",
" )\n",
"data2 = go.Bar(\n",
" x = list(range(5)),\n",
" y = anzahl_unfair,\n",
" name = 'Unfair'\n",
" )\n",
"data=[data1, data2]\n",
"fig = go.FigureWidget(data, layout)\n",
"pl.iplot(fig)"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment