{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Programmieraufgabe 1 (Geburtstagsparadoxon)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wichtig: Damit alle benötigten Pakete richtig eingebunden werden, führen Sie die nächste Zelle einmal aus." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# some setup\n", "import numpy as np # makes numpy routines and data types available as np.[name ouf routine or data type]\n", "import matplotlib.pyplot as plt # makes plotting command available as plt.[name of command]\n", "from numpy import random\n", "\n", "random.seed(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "a) Die Funktion calcchance(size, k, samples) soll die relative Häufigkeit zurückgeben, dass unter \"size\" vielen Leuten \"k\" Leute am gleichen Tag Geburtstag haben. Das Zufallsexperiment wird dabei \"samples\" oft wiederholt. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def calcchance(size=32,k=2,samples=10000):\n", " # if you set parameter=value in the declaration, this becomes the default value \n", " # and the parameter can be omitted on call.\n", " # Everything in the block has to be indented the same amount of spaces (this is\n", " # python's way of { } )\n", " \n", " # loop over the number of samples\n", " for i in range(samples):\n", " # create random birthdays for every person in the group (array of size 'size')\n", " a = random.randint(0,365,size)\n", " # Insert code here ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = calcchance(34,2,10000)\n", "print(c)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b) Die folgende Funktion soll die relative Häufigkeit zurückgeben, dass es unter \"size\" vielen Leuten noch einen weiteren gibt, der am gleichen Tag wie \"num\" Geburtstag hat. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def calcchance2(size=32,num=0,samples=10000):\n", " for i in range(samples):\n", " a = random.randint(0,365,size)\n", " # Insert code here\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "c) Stellen Sie mittels \"pyplot\" die relativen Häufigkeiten (10000 Wiederholungen) aus Aufgabe a) graphisch dar (k=2, size = 2,...,90) und geben Sie aus, wie gross die Gruppe sein muss, so dass in \n", "\n", "(i) 50% \n", "(ii) 90%\n", "(iii) 95%\n", "\n", "aller Fälle 2 Leute am selben Tag Geburtstag haben. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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" }, "widgets": { "state": { "dc3a3f1d23314758bf356f952c03d594": { "views": [ { "cell_index": 4 } ] } }, "version": "1.2.0" } }, "nbformat": 4, "nbformat_minor": 1 }