Skip to content

Instantly share code, notes, and snippets.

@halexus
Last active April 7, 2019 16:59
Show Gist options
  • Save halexus/b7318b4098d379b38e7a47e59bf04a82 to your computer and use it in GitHub Desktop.
Save halexus/b7318b4098d379b38e7a47e59bf04a82 to your computer and use it in GitHub Desktop.
Aufgabe 25 UE Wahrscheinlichkeitstheorie
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Aufgabe 25\n",
"\n",
"Eine Urne enthält 9 Kugeln, davon 5 rote. Es werden 3 Kugeln mit Zurücklegen gezogen. Wie groß ist die Wahrscheinlichkeit, mindestens eine rote Kugel zu ziehen?\n",
"\n",
"Wie ändert sich die Wahrscheinlichkeit, wenn die 3 Kugeln ohne Zurücklegen gezogen werden?\n",
"***"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ich verwende die beiden Funktionen [`random.choices`](https://docs.python.org/3/library/random.html#random.choices) und [`random.sample`](https://docs.python.org/3/library/random.html#random.sample) um aus der Urne mit bzw. ohne zurücklegen zu ziehen.\n",
"\n",
"Sei `X` eine Zufallsvariable, welche die Anzahl der gezogenen roten Kugeln beschreibt."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def simulation(N=1000000, zuruecklegen=True):\n",
" urne = ['R']*5 + ['S']*4 # 5 Rote und 4 schwarze Kugeln\n",
" anzahl_spiele_mit_mindestens_einer_roten_kugel = 0\n",
" if zuruecklegen:\n",
" wahl = random.choices # In Python sind Funktionen Objekte die Variablen zugewiesen werden können\n",
" else:\n",
" wahl = random.sample\n",
" for _ in range(N):\n",
" if wahl(urne, k=3).count('R') >= 1:\n",
" anzahl_spiele_mit_mindestens_einer_roten_kugel += 1\n",
" return anzahl_spiele_mit_mindestens_einer_roten_kugel/N"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mit Zurücklegen: P(X>=1) = 0.911779\n",
"Ohne Zurücklegen: P(X>=1) = 0.952637\n"
]
}
],
"source": [
"print(\"Mit Zurücklegen: P(X>=1) =\", simulation())\n",
"print(\"Ohne Zurücklegen: P(X>=1) =\", simulation(zuruecklegen=False))"
]
}
],
"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