{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "1adba5ee",
      "metadata": {
        "id": "1adba5ee"
      },
      "source": [
        "# Application Session 5 (Unit 2)\n",
        "## Conditional Probability, Independence, and Bayes’ Theorem (using real SMS Dataset)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "676d6b55",
      "metadata": {
        "id": "676d6b55"
      },
      "source": [
        "## Session Goals\n",
        "By the end of this session, students should be able to:\n",
        "\n",
        "- Define the random experiment, sample space, and events using real data.\n",
        "- Compute probabilities using empirical counting (relative frequency).\n",
        "- Compute **conditional probability** from data\n",
        "- Check **independence** using probabilities\n",
        "- Apply **Bayes’ Theorem** and compare with the direct result\n",
        "\n",
        "---\n",
        "\n",
        "## Connecting Theory to Practice\n",
        "\n",
        "In Unit 2, we studied:\n",
        "\n",
        "- Random experiments and sample spaces  \n",
        "- Events, unions (∪), intersections (∩), complements  \n",
        "- Addition rule  \n",
        "- Conditional probability  \n",
        "- Multiplication rule  \n",
        "- Independence  \n",
        "- Bayes’ theorem  \n",
        "\n",
        "In this session, we will apply these same ideas using real SMS message data.\n",
        "\n",
        "\n",
        "## What We Will Do\n",
        "\n",
        "We will use Python to compute probabilities from the **SMSSpamCollection** dataset and answer practical questions like:\n",
        "\n",
        "\n",
        "1. What is the probability that a randomly selected message is spam or contains a link??\n",
        "\n",
        "$$\n",
        "P(\\text{S}), P(\\text{L})\n",
        "$$\n",
        "\n",
        "2. If a message contains a weblink, how likely is it to be spam?\n",
        "\n",
        "$$\n",
        "P(\\text{S} \\mid \\text{L})\n",
        "$$\n",
        "\n",
        "3. Are the events “Spam” and “Contains Link” independent?\n",
        "\n",
        "$$\n",
        "P(\\text{S} \\cap \\text{L}) = P(\\text{S})\\,P(\\text{L})\n",
        "$$\n",
        "\n",
        "4. If we already know a message is spam, how likely is it to contain a weblink? (using Bayes’ Theorem)\n",
        "\n",
        "$$\n",
        "P(\\text{L} \\mid \\text{S})\n",
        "=\n",
        "\\frac{P(\\text{S} \\mid \\text{L})\\,P(\\text{L})}{P(\\text{S})}\n",
        "$$\n",
        "\n",
        "\n",
        "## Dataset: SMSSpamCollection\n",
        "\n",
        "The SMSSpamCollection dataset contains over 5,500 real SMS messages labeled as **spam** or **ham** (not spam messages).  \n",
        "\n",
        "We use this dataset to apply probability concepts similar to how spam filters work.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "635f3493",
      "metadata": {
        "id": "635f3493"
      },
      "source": [
        "## Step 1: Load the dataset\n",
        "We have a CSV file named `SMSSpamCollection.csv` with two columns:\n",
        "1. `label`: `spam` or `ham` (ham = not spam)\n",
        "2. `text`: the message content\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0cc8e0df",
      "metadata": {
        "id": "0cc8e0df"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "\n",
        "# Read the CSV file (already provided)\n",
        "df = pd.read_csv(\"SMSSpamCollection.csv\")   # columns: label, text\n",
        "\n",
        "df.head()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3850bfd9",
      "metadata": {
        "id": "3850bfd9"
      },
      "source": [
        "## Step 2: Define events (as True/False columns)\n",
        "We will define two events:\n",
        "- **S**: the message is spam\n",
        "- **L**: the message contains a weblink (has `http` or `www`)\n",
        "\n",
        "These events become True/False columns, which makes probability easy:\n",
        "- `True` counts as 1\n",
        "- `False` counts as 0\n",
        "So the **mean** is the probability.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "64ebbed5",
      "metadata": {
        "id": "64ebbed5"
      },
      "outputs": [],
      "source": [
        "# Event 1: Spam\n",
        "df[\"spam\"] = (df[\"label\"] == \"spam\")\n",
        "\n",
        "# Event 2: Link exists in the message text\n",
        "df[\"link\"] = df[\"text\"].str.contains(\"http|www\", case=False)\n",
        "\n",
        "# Check the first 5 rows to verify that spam and link columns were created correctly\n",
        "df[[\"spam\", \"link\"]].head()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2a71024f",
      "metadata": {
        "id": "2a71024f"
      },
      "source": [
        "## Step 3 — Basic Probabilities from Data\n",
        "\n",
        "Now we answer the question:\n",
        "1. What are the probabilities that a randomly selected message is spam or contains a link?\n",
        "\n",
        "We compute:\n",
        "\n",
        "$$\n",
        "P(S) \\quad \\text ,  \\quad P(L)\n",
        "$$"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ea81c959",
      "metadata": {
        "id": "ea81c959"
      },
      "outputs": [],
      "source": [
        "# function P(event) returns the proportion of True values (relative frequency)\n",
        "def P(event):\n",
        "    return event.mean()\n",
        "\n",
        "P_S = P(df[\"spam\"])\n",
        "P_L = P(df[\"link\"])\n",
        "\n",
        "# Compute P(S) and P(L) using empirical (relative frequency) probability\n",
        "print(\"P(S) =\", P_S)\n",
        "print(\"P(L) =\", P_L)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "47831a02",
      "metadata": {
        "id": "47831a02"
      },
      "source": [
        "## Step 4: Conditional probability\n",
        "Now we answer the question:\n",
        "\n",
        "2. If a message contains a link, how likely is it to be spam?\n",
        "\n",
        "We compute:\n",
        "$$P(S\\mid L) = \\frac{P(S \\cap L)}{P(L)}$$\n",
        "\n",
        "using empirical (relative frequency) probability.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7796486b",
      "metadata": {
        "id": "7796486b"
      },
      "outputs": [],
      "source": [
        "# Computes conditional probability P(A | B)\n",
        "# = probability that both A and B occur divided by probability of B\n",
        "def cond_prob(A, B):\n",
        "    return P(A & B) / P(B)\n",
        "\n",
        "P_S_given_L = cond_prob(df[\"spam\"], df[\"link\"])\n",
        "\n",
        "print(\"P(S|L) =\", P_S_given_L)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "9219b412",
      "metadata": {
        "id": "9219b412"
      },
      "source": [
        "## Step 5: Independence\n",
        "\n",
        "Now we answer the question:\n",
        "\n",
        "3. Are the events \"Spam\" (S) and \"Contains Link\" (L) **independent**?\n",
        "\n",
        "We test:\n",
        "$$P(S \\cap L) = P(S)\\,P(L)$$\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b2e52ed8",
      "metadata": {
        "id": "b2e52ed8"
      },
      "outputs": [],
      "source": [
        "intersection = P(df[\"spam\"] & df[\"link\"])\n",
        "product = P(df[\"spam\"]) * P(df[\"link\"])\n",
        "\n",
        "print(\"P(S and L) =\", intersection)\n",
        "print(\"P(S) x P(L) =\", product)\n",
        "print(\"Difference =\", abs(intersection - product))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5bdb6d7c",
      "metadata": {
        "id": "5bdb6d7c"
      },
      "source": [
        "## Step 6: Bayes’ Theorem\n",
        "\n",
        "Now we answer the question:\n",
        "\n",
        "4. Can we compute $P(L \\mid S)$  (if we already know a message is spam, how likely is it to contain a weblink) using Bayes’ Theorem?\n",
        "\n",
        "We use:\n",
        "\n",
        "$$P(L\\mid S) = \\frac{P(S\\mid L)\\,P(L)}{P(S)}$$\n",
        "\n",
        "then compare it with the **direct** conditional probability from Step 4.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c1c473e3",
      "metadata": {
        "id": "c1c473e3"
      },
      "outputs": [],
      "source": [
        "direct = cond_prob(df[\"link\"], df[\"spam\"])\n",
        "bayes = (P_S_given_L * P_L) / P_S\n",
        "\n",
        "print(\"Bayes  P(link | spam)  =\", bayes)\n",
        "print(\"Direct P(link | spam)  =\", direct)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d2c2fdca",
      "metadata": {
        "id": "d2c2fdca"
      },
      "source": [
        "###**Task**:\n",
        "Lets have one more event:\n",
        "- **N**: message contains any number\n",
        "\n",
        "Then answer:\n",
        "1) Find $P(S \\mid N)$\n",
        "2) Are **S** and **N** events independent?\n",
        "3) Use Bayes to compute $P(N \\mid S)$ and compare with the direct method.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "78a0c0ed",
      "metadata": {
        "id": "78a0c0ed"
      },
      "source": [
        "##**Summary**\n",
        "In this session, we applied Unit 2 probability concepts to a real SMS dataset.\n",
        "\n",
        "We answered the following questions:\n",
        "\n",
        "- What is the probability that a message is spam?\n",
        "- How does this probability change if we know the message contains a link?\n",
        "- Are the events \"Spam\" and \"Contains Link\" independent?\n",
        "- Can Bayes’ Theorem reproduce the same conditional probability?\n",
        "\n",
        "Key ideas reinforced:\n",
        "\n",
        "- Empirical probability (relative frequency)\n",
        "- Conditional probability from data\n",
        "- Testing independence using  $P(A \\cap B) = P(A)\\,P(B)$\n",
        "- Using Bayes’ Theorem to reverse conditional probability\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.x"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}