From 344195779690316631058ac6c1ea0edf0c4bddcd Mon Sep 17 00:00:00 2001 From: AnaCarvalho84 <131803922+AnaCarvalho84@users.noreply.github.com> Date: Thu, 10 Aug 2023 22:35:15 +0100 Subject: [PATCH] lab done --- main.ipynb | 260 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 main.ipynb diff --git a/main.ipynb b/main.ipynb new file mode 100644 index 0000000..e302c46 --- /dev/null +++ b/main.ipynb @@ -0,0 +1,260 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introduction To Probability\n", + "## Challenge 1\n", + "\n", + "A and B are events of a probability space with $(\\omega, \\sigma, P)$ such that $P(A) = 0.3$, $P(B) = 0.6$ and $P(A \\cap B) = 0.1$\n", + "\n", + "Which of the following statements are false?\n", + "* $P(A \\cup B) = 0.6$\n", + "* $P(A \\cap B^{C}) = 0.2$\n", + "* $P(A \\cap (B \\cup B^{C})) = 0.4$\n", + "* $P(A^{C} \\cap B^{C}) = 0.3$\n", + "* $P((A \\cap B)^{C}) = 0.9$" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + "a) 𝑃(𝐴βˆͺ𝐡) = 𝑃(𝐴) + 𝑃(𝐡) - 𝑃(𝐴∩𝐡) \n", + " 𝑃(𝐴) = 0.3, 𝑃(𝐡) = 0.6 e 𝑃(𝐴∩𝐡) = 0.1\n", + " 𝑃(𝐴βˆͺ𝐡) = 0.3 + 0.6 - 0.1 = 0.8 - False\n", + " \n", + "b) 𝑃(𝐴∩𝐡') = 𝑃(𝐴) - 𝑃(𝐴∩𝐡)\n", + " 𝑃(𝐴) = 0.3 𝑃(𝐴∩𝐡) = 0.1\n", + " 𝑃(𝐴∩𝐡') = 0.3 - 0.1 = 0.2 - True\n", + " \n", + "c) 𝑃(𝐴∩(𝐡βˆͺ𝐡')) = 𝑃(𝐴) = 0.3 False\n", + "\n", + "d) 𝑃(𝐴''∩𝐡') = 1 - 𝑃(𝐴βˆͺ𝐡)\n", + " 𝑃(𝐴βˆͺ𝐡) = 0.8\n", + " 𝑃(𝐴'∩𝐡') = 1 - 0.8 = 0.2 - False\n", + " \n", + "e) 𝑃((𝐴∩𝐡)') = 1 - 𝑃(𝐴∩𝐡)\n", + " 𝑃((𝐴∩𝐡)') = 1 - 0.1 = 0.9 - True\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 2\n", + "There is a box with 10 white balls, 12 red balls and 8 black balls. Calculate the probability of:\n", + "* Taking a white ball out.\n", + "* Taking a white ball out after taking a black ball out.\n", + "* Taking a red ball out after taking a black and a red ball out.\n", + "* Taking a red ball out after taking a black and a red ball out with reposition.\n", + "\n", + "**Hint**: Reposition means putting back the ball into the box after taking it out." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + "a) Taking a white ball out - 10/30 = 0.33\n", + "b) Taking a white ball out after taking a black ball out = 10/29 = 0.34\n", + "c) Taking a red ball out after taking a black and a red ball out = 12/28 = 0,43\n", + "d) Taking a red ball out after taking a black and a red ball out with reposition = 12/30\n", + "\"\"\"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 3\n", + "\n", + "You are planning to go on a picnic today but the morning is cloudy. You hate rain so you don't know whether to go out or stay home! To help you make a decision, you gather the following data about rainy days:\n", + "\n", + "* Knowing that it is a rainy day, the probability of cloudy is 50%!\n", + "* The probability of any day (rainy or not) starting off cloudy is 40%. \n", + "* This month is usually dry so only 3 of 30 days (10%) tend to be rainy. \n", + "\n", + "What is the probability of rain, given the day started cloudy?" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nP(A|B) = (P(B|A)*P(A))/P(B)\\n((10)*(50))/40 = 12.5%\\n'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\"\n", + "\n", + "P(Rain|Cloudy) is the probability of rain given that the day started cloudy\n", + "P(Cloudy∣Rain) is the probability of the day starting cloudy given that it's a rainy day.this is 50%.\n", + "P(Rain) is the overall probability of rain. Given that only 10% of days tend to be rainy, P(Rain)=0.1\n", + "P(Cloudy) is the overall probability of the day starting cloudy. From the information provided, the probability of any day starting off cloudy is 40%\n", + "\n", + "P(Rain|Cloudy) = ((10)*(50))/40 = 12.5%\n", + "\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 4\n", + "\n", + "One thousand people were asked through a telephone survey whether they thought more street lighting is needed at night or not.\n", + "\n", + "Out of the 480 men that answered the survey, 324 said yes and 156 said no. On the other hand, out of the 520 women that answered, 351 said yes and 169 said no. \n", + "\n", + "We wonder if men and women have a different opinions about the street lighting matter. Is gender relevant or irrelevant to the question?\n", + "\n", + "Consider the following events:\n", + "- The answer is yes, so the person that answered thinks that more street lighting is needed.\n", + "- The person who answered is a man.\n", + "\n", + "We want to know if these events are independent, that is, if the fact of wanting more light depends on whether one is male or female. Are these events independent or not?\n", + "\n", + "**Hint**: To clearly compare the answers by gender, it is best to place the data in a table." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\USER\\AppData\\Local\\Temp\\ipykernel_16484\\2382365963.py:7: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n", + " street_lighting = street_lighting.append(data, ignore_index=True)\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
AnswerMenWoman
0Yes324351
1No156169
\n", + "
" + ], + "text/plain": [ + " Answer Men Woman\n", + "0 Yes 324 351\n", + "1 No 156 169" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "\n", + "street_lighting = pd.DataFrame(columns=['Answer', 'Men', 'Woman'])\n", + "data = [{'Answer': 'Yes', 'Men': 324, 'Woman': 351},\n", + " {'Answer': 'No', 'Men': 156, 'Woman': 169}]\n", + "\n", + "street_lighting = street_lighting.append(data, ignore_index=True)\n", + "\n", + "street_lighting \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + "Events A: The answer is yes (more street lighting is needed).\n", + "Events B: The person who answered is a man.\n", + "\n", + "𝑃(A|B) = 𝑃(A) imply that the occurrence of event B (being a man) does not affect the probability of event A (answering yes)\n", + "𝑃(A) = (Number of \"Yes\" answers) / (Total number of answers) = (324 + 351) / 1000 = 0.675\n", + "𝑃(B) = (Number of men) / (Total number of answers) = 480 / 1000 = 0.48\n", + "𝑃(A|B) = (Number of \"Yes\" answers by men) / (Total number of men) = 324 / 480 = 0.675\n", + "\n", + "Since 𝑃(A|B) = 𝑃(A), the events \"The answer is yes\" and \"The person who answered is a man\" are independent. \n", + "This implies that the opinion about street lighting (wanting more light) is not influenced by gender. In other words, being a man or a woman does not affect the likelihood of wanting more street lighting.\n", + "\"\"\"" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}