{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Moderne Methoden der Datenanalyse SS2023\n", "# Practical Exercise 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.1 (Voluntary)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "“Should I carry an umbrella or should I risk to get wet?” A possible answer to this question\n", "is to look at the weather forecast. But as we all know it is not always reliable.\n", "Let’s assume that if it will rain, the forecast predicts this correctly in 80 % of the cases. If\n", "it will not rain, the forecast is assumed to be accurate in 90 % of the cases. In Sun-City\n", "the a-priori rain probability is only 5 %, in Equal-City it’s 50 % and in Rain-City it’s 95 %.\n", "Calculate (on a sheet of paper or with a short program) the four probabilities that it will\n", "(not) rain if (no) rain is predicted for the three cities.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two different risks of a wrong decision:\n", "- Carry an umbrella, but it does not rain.\n", "- Don’t carry an umbrella in case it rains." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which are the three possible strategies and which of them is the optimal one to minimize\n", "the risk of a wrong decision in each of the three cities? Calculate and compare the risk for\n", "each of the three possible strategies. Determine the optimal strategy when the second risk\n", "is considered 10 or 100 times more serious." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def weather(iCity):\n", " # A: it rains B: forecasts predict rain\n", " \n", " pRainPredicted_if_Rain = 0.8 # P(B|A)\n", " pRainNotPredicted_if_NotRain = 0.9 # P(not B| not A)\n", " \n", " pRain = [0.05, 0.5, 0.95] # prior P(A)\n", " cityName = [\"Sun-City\", \"Equal-City\", \"Rain-City\"]\n", " \n", " # P(not B|A) = 1 - P(B|A)\n", " pRainNotPredicted_if_Rain = 1. - pRainPredicted_if_Rain\n", " # P(B|not A) = 1 - P(not B|not A)\n", " pRainPredicted_if_NotRain = 1. - pRainNotPredicted_if_NotRain\n", " # P(not A) = 1 - P(A)\n", " pNotRain = 1. - pRain[iCity]\n", " \n", " \n", " # P(A and B) = P(A) * P(B|A)\n", " pRain_and_RainPredicted = pRain[iCity] * pRainPredicted_if_Rain\n", " # P(not A and B) = P(not A) * P(B|not A)\n", " pNotRain_and_RainPredicted = pNotRain * pRainPredicted_if_NotRain\n", " # P(A and not B) = P(A) * P(not B|A)\n", " pRain_and_RainNotPredicted = pRain[iCity] * pRainNotPredicted_if_Rain\n", " \n", " \n", " # P(B) = P(A and B) + P(not A and B)\n", " pRainPredicted = pRain_and_RainPredicted + pNotRain_and_RainPredicted\n", " # P(not B) = 1 - P(B)\n", " pRainNotPredicted = 1. - pRainPredicted\n", " \n", " # TODO : Find all the four probability combinations using Bayes Theorem and print the results for each city\n", " \n", " # TODO : Evaluate the risk assesment\n", " # Risk1: carry an umbrella but it does not rain\n", " # Risk2: not carry an umbrella and it rains\n", " # Strategies: A. always carry an umbrella , B. look at forecasts, C. never carry an umbrella\n", " \n", " print('\\n')\n", " print('Strategy A: always carry an umbrella with you')\n", " risk1_always = pNotRain;\n", " risk2_always = 0.\n", " print(' Risk 1: you carry an umbrella but it does not rain p = {0:.1f} %'.format(risk1_always * 100))\n", " print(' Risk 2: you don\\'t carry an umbrella and it rains p = {0:.1f} %'.format(risk2_always * 100))\n", " \n", " # TODO: Determine the optimal strategy when Risk2 is considered 10 or 100 times more serious.\n", " \n", " # TODO: Evaluate the results for each city\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.2 (Obligatory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test your calculations for the previous exercise ‘experimentally’ by writing a Monte Carlo\n", "simulation: Simulate N weather events, generate uniformly distributed random numbers\n", "between 0 and 1 for the rain forecast and compare them to the corresponding probability\n", "given in the above exercise. Make sure that you only use the values given in the text in\n", "your code. Then count the number of events in each category (rain and no rain predicted).\n", "Finally, use these numbers to determine the fraction of wrong weather forecasts and compare\n", "this number to the probability calculated before. Repeat the simulation for different values\n", "of N." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the any of the random number generators introduced in the first exercise sheet to setup this Monte Carlo experiment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "from ROOT import gRandom\n", "\n", "def weatherMC(iCity, nDays):\n", " \n", " # A: it rains B: forecasts predict rain\n", " \n", " pRainPredicted_if_Rain = 0.8 # P(B|A)\n", " pRainNotPredicted_if_NotRain = 0.9 # P(not B| not A)\n", " \n", " pRain = [0.05, 0.5, 0.95] # prior P(A)\n", " cityName = [\"Sun-City\", \"Equal-City\", \"Rain-City\"]\n", " \n", " # Counter\n", " countRain = 0\n", " countNotRain = 0\n", " countRainPredicted = 0\n", " countRainNotPredicted = 0\n", " countRain_RainPredicted = 0\n", " countRain_RainNotPredicted = 0\n", " countNotRain_RainPredicted = 0\n", " countNotRain_RainNotPredicted = 0\n", " \n", " gRandom.SetSeed()\n", " \n", " # TODO: Calculate for each days\n", " \n", " \n", " # Evaluate all the four combinations with Bayes Theorem\n", " \n", " # Print results\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.3 (Obligatory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A famous logical decision problem is the so-called ”Monty Hall Dilemma”, named after the\n", "host of an American television game show (”Let’s make a deal”). In German it is referred\n", "to as the ”Ziegenproblem”. Imagine you are contestant in a game show. There are three\n", "doors with an automobile behind one of them and goats behind the other two doors. The\n", "automobile and the two goats are assigned randomly to the three doors and you don’t have\n", "any prior knowledge about where the goats or the automobile are. ”First you point toward\n", "a door,” says the host of the show. ”Then I’ll open one of the other doors to reveal a goat.\n", "After I’ve shown you the goat, you make your final choice whether to stick with your initial\n", "choice of doors, or to switch to the remaining door. You win whatever is behind the door.”\n", "You begin by pointing to door number 1. The host shows you that door number 3 has a\n", "goat. What do you think - shall you stay with your initial choice (door 1) or switch to\n", "door number 2 to win the car? What are the probabilities to win the car? Calculate the\n", "probabilities by hand and write a programme that simulates a large number of such games\n", "to validate your calculated results." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Theoretical Calculation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### TODO: Make your calculation here using the latex syntax " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Python Approach" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "def MontyHall_calculation(): \n", " #When you first walk into the gameshow, the probability of the car being behind one of the 3 doors is:\n", " p_1 = 1./3\n", " p_2 = 1./3\n", " p_3 = 1./3\n", " \n", " #You choose door 1. \n", " #If the prize is behind door 1 too, find the probability that the host reveals door 3.\n", " \n", " #If the prize is behind door 2, find the probability that the host reveals door 3. And the same if prize is behind door 3.\n", " \n", " #The host reveals door 3 has a cute snow goat.\n", " # Find The probability of the car being behind door 2, given that door 3 has been revealed.\n", " \n", " # TODO: Calculate the probalitiy of winning the car after switching the door 2 and also the probability to win a car after sticking to door 1\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def MontyHall(Switch_door, Ngames):\n", " doors = [\"A\", \"B\", \"C\"]\n", " \n", " # TODO: Write a function that simulates a large number of such games to validate the results.\n", " " ] } ], "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.8.6" } }, "nbformat": 4, "nbformat_minor": 4 }