Skip to content

Instantly share code, notes, and snippets.

@halexus
Last active May 7, 2019 21:20
Show Gist options
  • Save halexus/5a0402c6285cced022648add99e4ed3d to your computer and use it in GitHub Desktop.
Save halexus/5a0402c6285cced022648add99e4ed3d to your computer and use it in GitHub Desktop.
Aufgabe 31 UE Wahrscheinlichkeitstheorie
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Aufgabe 31"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Das Ziegenproblem wurde als ein Beispiel für bedingte Wahrscheinlichkeit in der Vorlesung erwähnt. (Siehe z.B. https://de.wikipedia.org/wiki/Ziegenproblem oder das Buch *Das Ziegenproblem: Denken in Wahrscheinlichkeiten von Gero von Randow* für eine weitere Erklärung.) Führen Sie ein Experiment mit 30 Versuchsreihen durch und stellen Sie Ihre Erfahrungen mit zwei Stabdiagrammen grafisch dar!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import plotly.offline as pl # Für die Barcharts\n",
"import plotly.graph_objs as go # Für die Barcharts"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"N=1000"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def spiel(wechseln):\n",
" hinter_türen = list('zza') # z=Ziege, a=Auto\n",
" random.shuffle(hinter_türen)\n",
" spieler_tür=random.randint(0,2) # Spieler wählt zufällig eine Tür\n",
" if not wechseln:\n",
" return hinter_türen[spieler_tür]\n",
" if hinter_türen[spieler_tür]=='a':\n",
" return 'z'\n",
" return 'a'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def simulate(wechseln, N=N):\n",
" return (''.join(spiel(wechseln) for _ in range(N))).count('a')/N\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"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": [
{
"type": "bar",
"uid": "6651a7fc-2b09-421a-b18a-14593ee7e360",
"x": [
"Wechseln",
"Bleiben"
],
"y": [
0.661,
0.311
]
}
],
"layout": {
"barmode": "relative",
"title": {
"text": "Relativer Anteil an Spielen in denen das Auto gewonnen wird. N=1000"
},
"xaxis": {
"title": {
"text": "Strategie"
}
},
"yaxis": {
"title": {
"text": "Relative Häufigkeit"
}
}
}
},
"text/html": [
"<div id=\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\" 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(\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\")) {\n",
" Plotly.newPlot(\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\", [{\"x\": [\"Wechseln\", \"Bleiben\"], \"y\": [0.661, 0.311], \"type\": \"bar\", \"uid\": \"6651a7fc-2b09-421a-b18a-14593ee7e360\"}], {\"barmode\": \"relative\", \"title\": {\"text\": \"Relativer Anteil an Spielen in denen das Auto gewonnen wird. N=1000\"}, \"xaxis\": {\"title\": {\"text\": \"Strategie\"}}, \"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(\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\")) {window._Plotly.Plots.resize(document.getElementById(\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\"));};})</script>"
],
"text/vnd.plotly.v1+html": [
"<div id=\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\" 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(\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\")) {\n",
" Plotly.newPlot(\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\", [{\"x\": [\"Wechseln\", \"Bleiben\"], \"y\": [0.661, 0.311], \"type\": \"bar\", \"uid\": \"6651a7fc-2b09-421a-b18a-14593ee7e360\"}], {\"barmode\": \"relative\", \"title\": {\"text\": \"Relativer Anteil an Spielen in denen das Auto gewonnen wird. N=1000\"}, \"xaxis\": {\"title\": {\"text\": \"Strategie\"}}, \"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(\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\")) {window._Plotly.Plots.resize(document.getElementById(\"2036a17f-4a60-4b9b-8ea6-ca485aef90fe\"));};})</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pl.init_notebook_mode(connected=True)\n",
"layout = {\n",
" 'xaxis': {'title': 'Strategie'},\n",
" 'yaxis': {'title': 'Relative Häufigkeit'},\n",
" 'barmode': 'relative',\n",
" 'title': 'Relativer Anteil an Spielen in denen das Auto gewonnen wird. N=%d'%N\n",
"}\n",
"data = [go.Bar(\n",
" x = ['Wechseln', 'Bleiben'],\n",
" y = [simulate(True), simulate(False)]\n",
" )]\n",
"fig = go.FigureWidget(data, layout)\n",
"pl.iplot(fig)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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