From ce83b2841c1f1168047ebe325e02871d34ac4ade Mon Sep 17 00:00:00 2001 From: adris Date: Tue, 14 Nov 2023 14:22:18 +0000 Subject: [PATCH] Lab Done --- Lab_01_Introduction_To_Probability.ipynb | 427 +++++++++++++++++++++++ .gitignore => your-code/.gitignore | 0 README.md => your-code/README.md | 0 3 files changed, 427 insertions(+) create mode 100644 Lab_01_Introduction_To_Probability.ipynb rename .gitignore => your-code/.gitignore (100%) rename README.md => your-code/README.md (100%) diff --git a/Lab_01_Introduction_To_Probability.ipynb b/Lab_01_Introduction_To_Probability.ipynb new file mode 100644 index 0000000..712f510 --- /dev/null +++ b/Lab_01_Introduction_To_Probability.ipynb @@ -0,0 +1,427 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "PAOePPeF_fFP" + }, + "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", + "source": [ + "\"\"\"FORMULA: (A U B) = (A) + (B) - (A ∩ B)\n", + " (A U B) = 0.3 + 0.6 - 0.1\n", + " (A U B) = 0.8 \"\"\"\n", + "\n", + "# FIRST STATEMENT: P(A ∪ B) = 0.6 FALSE" + ], + "metadata": { + "id": "IMCmE1-TWzpY" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "\"\"\" From Venn diagram in class: A intersection when NOT B happens, so that is |A ∩ NOT WITH B's intersection half|\n", + "\n", + " P(A ∩ B') = P(A) − P (A ∩ B)\n", + " P(A ∩ B') = 0.3 - 0.1\n", + " P(A ∩ B') = 0.2 \"\"\"\n", + "\n", + "# SECOND STATEMENT: P(A ∩ B') = 0.2 TRUE" + ], + "metadata": { + "id": "FCs3OjSxXj5U" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "\"\"\" B ∪ B' for me is essentially covers all possible outcomes. It's the universe\n", + "P(A ∩ (B ∪ B')) = 0.4\n", + "P(A ∩ S) = 0.4 \"\"\"\n", + "\n", + "### THIRD STATEMENT: False? I believe that S is the universe so that is just the probability of A happening in the universe. it's A's probability (0.3) and not 0.4" + ], + "metadata": { + "id": "LswSdWUnK57P" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "\"\"\" From VENN diagram in class: P(A' ∩ B') = P(A U B)', so if P(A U B) is 0.8, P(A U B)' will assume 0.2\n", + "\n", + "So again, if P(A U B)'is == P(A' ∩ B'), the statement P(A' ∩ B' ) = 0.3 is False \"\"\"\n", + "\n", + "# FOURTH STATEMENT: P(A' ∩ B') = 0.3 FALSE" + ], + "metadata": { + "id": "T4jJpMNkK54-" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "\"\"\" Thank you Djisus. An easy one: so if P(A ∩ B) = 0.1, it's complementary - P((A ∩ B )') - will be 0.9. no sweat. Thank the lord!\"\"\"\n", + "\n", + "# FIFTH STATEMENT: P((A ∩ B )') = 0.9 TRUE" + ], + "metadata": { + "id": "b6m9txa0K5xo" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JrBM0nfO_fFR" + }, + "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", + "source": [ + "# 10 white balls\n", + "# 12 red balls\n", + "# 8 black balls" + ], + "metadata": { + "id": "G840AoCuZ1zW" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# 1.\n", + "white_b = 10/(10+12+8)\n", + "print(\"Probability of taking a white ball out is:\", white_b)\n", + "\n", + "# %\n", + "white_b1 = white_b*100\n", + "print(\"Probability of taking a white ball out is:\", round(white_b1,2),\"%\")" + ], + "metadata": { + "id": "y1cl4EMNar22", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "8176f780-d22f-446f-d9ac-5fef49275690" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of taking a white ball out is: 0.3333333333333333\n", + "Probability of taking a white ball out is: 33.33 %\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# 2.\n", + "black_ball = 8/(10+12+8)\n", + "white_b = 10/(10+12+7)\n", + "w_after_b = black_ball * white_b\n", + "print(\"Probability of taking a white ball after taking a black ball out is:\", w_after_b)\n", + "\n", + "# %\n", + "w_after_b1 = w_after_b*100\n", + "print(\"Probability of taking a white ball after taking a black ball out is:\", round(w_after_b1,2),\"%\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vNbZufWrar0Q", + "outputId": "9f0a9b9b-818a-42b4-a7a8-fec6260465fb" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of taking a white ball after taking a black ball out is: 0.09195402298850575\n", + "Probability of taking a white ball after taking a black ball out is: 9.2 %\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# 3.\n", + "first_red_ball = 12/(10+12+8)\n", + "black_ball = 8/(10+11+8)\n", + "second_red_ball = 11/(10+11+7)\n", + "r_after_b_r = first_red_ball * black_ball * second_red_ball\n", + "print(\"Probability of taking a red ball out after taking a black and a red ball out is:\", r_after_b_r)\n", + "\n", + "# %\n", + "r_after_b_r1 = r_after_b_r*100\n", + "print(\"Probability of taking a red ball out after taking a black and a red ball out is:\", round(r_after_b_r1,2),\"%\")" + ], + "metadata": { + "id": "ySrjw5Cjarxs", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "f3f25d2b-180a-45d8-c388-5db2a82ac534" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of taking a red ball out after taking a black and a red ball out is: 0.04334975369458128\n", + "Probability of taking a red ball out after taking a black and a red ball out is: 4.33 %\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "o0qdwwMt_fFR", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9be73222-7d9f-4083-9430-dc5cf4c63890" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of taking a red ball out after taking a black and a red ball out with reposition: 0.04266666666666667\n", + "Probability of taking a red ball out after taking a black and a red ball out with reposition: 4.27 %\n" + ] + } + ], + "source": [ + "4 # # Taking a red ball out after taking a black and a red ball out with reposition\n", + "first_red_ball = 12/(10+12+8)\n", + "black_ball = 8/(10+12+8)\n", + "second_red_ball = 12/(10+12+8)\n", + "r_after_b_r = first_red_ball * black_ball * second_red_ball\n", + "print(\"Probability of taking a red ball out after taking a black and a red ball out with reposition:\", r_after_b_r)\n", + "\n", + "# %\n", + "r_after_b_r1 = r_after_b_r*100\n", + "print(\"Probability of taking a red ball out after taking a black and a red ball out with reposition:\", round(r_after_b_r1,2),\"%\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s4-NoDQo_fFS" + }, + "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": null, + "metadata": { + "id": "MVm7Tr8J_fFS" + }, + "outputs": [], + "source": [ + "\"\"\"\n", + "cloudy 0.5\n", + "rainy or not is 0.4\n", + "0.10 dryness\n", + "\"\"\"\n", + "# What is the probability of rain, given the day started cloudy?\n", + "# the day started cloudy -> this is will be my \"sample\" so it will be the denominator: 0.4\n", + "p_rain = (0.5*0.1)/0.4\n", + "p_rain_p = (p_rain)*100\n", + "\n", + "print(\"The probability of raining, given that the day started cloudy is\",p_rain,\"or\",p_rain_p, \"%\" )" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "60SBPCSp_fFS" + }, + "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": null, + "metadata": { + "id": "K0txPtaM_fFS" + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "\n", + "light_data = {\"Gender\": [\"M\", \"F\", \"Total\"],\n", + " \"Yes\": [324, 351, 675],\n", + " \"No\": [156, 169, 325],\n", + " \"Total\": [480, 520, 1000]}\n", + "\n", + "df = pd.DataFrame(light_data)\n", + "print(df)" + ] + }, + { + "cell_type": "code", + "source": [ + "\"\"\" These events for me look independent, because the following answer (yes or no) will not be influenciated with the previous answer - just in context/pratical way -\n", + "In a practical way of seeing this that the P(YES|MAN) should be the same as P(MAN) [P (A | B) = P(A)] \"\"\"" + ], + "metadata": { + "id": "PhJWm4G3eyKu" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# P (YES|MAN) will be the total of YES MAN answers over the total number YES answers\n", + "p_y_man = (324/675)\n", + "print(\"Probability of a man answering yes is\", p_y_man)\n", + "# P (MAN) will be the total answers from men over the total number of answers\n", + "p_man = 480/1000\n", + "print(\"Probability of an answer being given from a man is\", p_man)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "C8AzpD_M9q-o", + "outputId": "0fd3562f-4791-44ce-f657-62d9914716fb" + }, + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Probability of a man answering yes is 0.48\n", + "Probability of an answer being given from a man is 0.48\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# So:\n", + "if p_man == p_y_man:\n", + " print(\"They are independent\")\n", + "else:\n", + " print(\"Not independente\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JQof24hf9FJQ", + "outputId": "ae1d0a53-7178-4de5-afca-c07b04771713" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "They are independent\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.7.2" + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/.gitignore b/your-code/.gitignore similarity index 100% rename from .gitignore rename to your-code/.gitignore diff --git a/README.md b/your-code/README.md similarity index 100% rename from README.md rename to your-code/README.md