{ "cells": [ { "cell_type": "markdown", "id": "20fce00f", "metadata": {}, "source": [ "### Data Analysis\n", "#### Inclusion-exclusion principle \n", "
\n", "https://github.com/ostad-ai/Machine-Learning\n", "
Explanation: https://www.pinterest.com/HamedShahHosseini/Machine-Learning/background-knowledge" ] }, { "cell_type": "markdown", "id": "6c88da5f", "metadata": {}, "source": [ "For set operaions in Python, see our repository for Python\n", "
https://github.com/ostad-ai/Python-Everything" ] }, { "cell_type": "markdown", "id": "9b279ce8", "metadata": {}, "source": [ "In set theory: \n", "- $|A \\cup B|=|A|+|B|-|A \\cap B|$\n", "
\n", "\n", "In probability:\n", "- $P(A\\cup B)=P(A)+P(B)-P(A \\cap B)$" ] }, { "cell_type": "code", "execution_count": 1, "id": "e761a498", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Checking inclusion-exclusion principle for sets:\n", "It is True\n" ] } ], "source": [ "# for a given set A\n", "def cardinality(A):\n", " return len(A)\n", "#-----for sets A and B\n", "def union(A,B):\n", " return A|B\n", "def intersection(A,B):\n", " return A&B\n", "#---example:\n", "A={2,4,6} # dice with even outcomes\n", "B={1,2,3} # dice less than 4\n", "S={1,2,3,4,5,6} #sampel space\n", "print('Checking inclusion-exclusion principle for sets:')\n", "result=cardinality(union(A,B))==cardinality(A)+cardinality(B)-cardinality(intersection(A,B))\n", "print(f'It is {result}')" ] }, { "cell_type": "code", "execution_count": 2, "id": "d3ee49f1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Checking inclusion-exclusion principle for probabilities:\n", "It is True\n" ] } ], "source": [ "#-----probability of event A in sample space S\n", "def prob(A,S):\n", " return cardinality(A)/cardinality(S)\n", "#--- example: \n", "print('Checking inclusion-exclusion principle for probabilities:')\n", "result= prob(union(A,B),S)==prob(A,S)+prob(B,S)-prob(intersection(A,B),S)\n", "print(f'It is {result}')" ] }, { "cell_type": "code", "execution_count": null, "id": "74ae0061", "metadata": {}, "outputs": [], "source": [] } ], "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }