{ "cells": [ { "cell_type": "markdown", "id": "20fce00f", "metadata": {}, "source": [ "### Data Analysis\n", "#### Probability: Independent events \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": [ "For indepedent events $A_1$, $A_2$,...,$A_m$ we have:
\n", "$P(A_1,A_2,...,A_m)=P(A_1)P(A_2)...P(A_m)$
\n", "**Special case:** for two independent events $A$ and $B$:
\n", "$P(A,B)=P(A)P(B)$" ] }, { "cell_type": "code", "execution_count": 1, "id": "93a7d14f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Experiment: rolling a die\n", "Probability of A={1, 3, 5} is 0.5\n", "Probability of B={2, 3, 5} is 0.5\n", "Probability of A and B: is 0.3333333333333333\n", "Two events A={1, 3, 5} and B={2, 3, 5} are not independent.\n" ] } ], "source": [ "#probability of event A with sample space SS\n", "def prob(A,SS):\n", " return len(A)/len(SS)\n", "\n", "# event of intersection A and B \n", "def intersection(A,B):\n", " return A&B\n", "\n", "# Experiment: rolling a die (dice)\n", "SS=set(range(1,7))\n", "A={1,3,5} # odd numbers in first roll\n", "B={2,3,5} #prime numbers in first roll\n", "pA=prob(A,SS)\n", "pB=prob(B,SS)\n", "p_AB=prob(intersection(A,B),SS)\n", "p_A,p_B=prob(A,SS),prob(B,SS)\n", "print('Experiment: rolling a die')\n", "print(f'Probability of A={A} is {p_A}')\n", "print(f'Probability of B={B} is {p_B}')\n", "print(f'Probability of A and B: is {p_AB}')\n", "if p_AB==p_A*p_B:\n", " print(f'Two events A={A} and B={B} are independent.')\n", "else:\n", " print(f'Two events A={A} and B={B} are not independent.')" ] }, { "cell_type": "markdown", "id": "cb243ee6", "metadata": {}, "source": [ "Let's examine the probability of systems to work if they are composed of several independent components:
\n", "- **Series system:** Components are connected in a series form\n", "- **Parallel system:** Components are connected in a parallel from" ] }, { "cell_type": "code", "execution_count": 2, "id": "d8c05267", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "We have 3 components with probs. [0.6, 0.3, 0.5] to work correctly.\n", "Probability of series network to work: 0.09\n", "Probability of parallel network to work: 0.86\n" ] } ], "source": [ "def prob_sys_series(ps):\n", " result=1\n", " for p in ps:\n", " result*=p\n", " return result\n", "\n", "def prob_sys_parallel(ps):\n", " result=1\n", " for p in ps:\n", " result*=(1-p)\n", " return 1-result\n", "\n", "# check with what probabilty a system works \n", "# if it has two components with the following probabilities to work\n", "ps=[.6,.3,.5] #prob. of components\n", "print(f'We have {len(ps)} components with probs. {ps} to work correctly.')\n", "print(f'Probability of series network to work: {prob_sys_series(ps)}')\n", "print(f'Probability of parallel network to work: {prob_sys_parallel(ps)}')" ] }, { "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.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }