{ "cells": [ { "cell_type": "markdown", "id": "20fce00f", "metadata": {}, "source": [ "### Data Analysis\n", "#### Conditional probability, sampling with replacement and without replacement \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": [ "Using **multiplication rule:**
\n", "$P(A\\cap B)=P(A)P(B|A)$" ] }, { "cell_type": "code", "execution_count": 39, "id": "a755964d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "prob. of being defective: 0.3333333333333333\n", "prob. of being non-defective 0.6666666666666666\n", "A and B both defective (with replacement) 0.1111111111111111\n", "A and B both defective (without replacement) 0.08333333333333333\n" ] } ], "source": [ "#assuming outcomes are equally likely\n", "#box contains non-defective products with positive IDs\n", "# defective products with negative IDs\n", "box={1,2,3,4,5,6,-1,-2,-3}\n", "def prob_defective(sample_space,event='defective'):\n", " Ndefective=0\n", " for el in sample_space:\n", " if el<0:\n", " Ndefective+=1 \n", " if event=='defective': \n", " return Ndefective/len(sample_space)\n", " else:\n", " return (len(sample_space)-Ndefective)/len(sample_space)\n", "print('prob. of being defective: ',prob_defective(box,'defective'))\n", "print('prob. of being non-defective', prob_defective(box,'nondefective'))\n", "print('A and B both defective (with replacement)',\n", "prob_defective(box,'defective')*prob_defective(box,'defective'))\n", "#assuming first sampling is a defective object\n", "# we modify sample_space\n", "box2=box.copy()\n", "for el in box2:\n", " if el<0:\n", " box2.remove(el)\n", " break\n", "print('A and B both defective (without replacement)',\n", "prob_defective(box,'defective')*prob_defective(box2,'defective'))" ] }, { "cell_type": "code", "execution_count": null, "id": "20acd6ae", "metadata": {}, "outputs": [], "source": [] }, { "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 }