{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 概率相关定理" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
speciesislandbill_length_mmbill_depth_mmflipper_length_mmbody_mass_gsex
0AdelieTorgersen39.118.7181.03750.0MALE
1AdelieTorgersen39.517.4186.03800.0FEMALE
2AdelieTorgersen40.318.0195.03250.0FEMALE
4AdelieTorgersen36.719.3193.03450.0FEMALE
5AdelieTorgersen39.320.6190.03650.0MALE
\n", "
" ], "text/plain": [ " species island bill_length_mm bill_depth_mm flipper_length_mm \\\n", "0 Adelie Torgersen 39.1 18.7 181.0 \n", "1 Adelie Torgersen 39.5 17.4 186.0 \n", "2 Adelie Torgersen 40.3 18.0 195.0 \n", "4 Adelie Torgersen 36.7 19.3 193.0 \n", "5 Adelie Torgersen 39.3 20.6 190.0 \n", "\n", " body_mass_g sex \n", "0 3750.0 MALE \n", "1 3800.0 FEMALE \n", "2 3250.0 FEMALE \n", "4 3450.0 FEMALE \n", "5 3650.0 MALE " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv\")\n", "df = df.dropna()\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(333, 7)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shape" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "({'Adelie', 'Chinstrap', 'Gentoo'},\n", " {'Biscoe', 'Dream', 'Torgersen'},\n", " {'FEMALE', 'MALE'})" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(df.species), set(df.island), set(df.sex)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 True\n", "1 True\n", "2 True\n", "4 True\n", "5 True\n", "Name: species, dtype: bool" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Fraction of Adelie species\n", "adelie = (df['species'] == 'Adelie')\n", "adelie.head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "146" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adelie.sum()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.43843843843843844" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This is the fraction of True values in the Series\n", "# Therefore, it is the fractionn of adelie species penguins\n", "adelie.mean()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def prob(A):\n", " '''probability of A'''\n", " '''Input: a series of True and False values'''\n", " return A.mean()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.43843843843843844" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(adelie)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conjunction\n", "\n", "A & B" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 True\n", "2 True\n", "4 True\n", "5 False\n", "Name: sex, dtype: bool" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Adelie and Female\n", "female = (df['sex'] == 'FEMALE')\n", "female.head()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.4954954954954955" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(female)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.21921921921921922" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(adelie & female)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.21921921921921922" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(female & adelie)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.21724427129832535" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The two results are different because being adelie and being female are not independent\n", "prob(adelie) * prob(female)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditional probability\n", "What is the probability that a penguins is female, given that it is of the species of Adelie?" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 True\n", "2 True\n", "4 True\n", "5 False\n", "Name: sex, dtype: bool" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "female[adelie].head()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1 True\n", "2 True\n", "4 True\n", "6 True\n", "12 True\n", "Name: species, dtype: bool" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adelie[female].head()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(146, 7)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# number of Adelie\n", "adelie_df = df[df.species == 'Adelie']\n", "adelie_df.shape" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(73, 7)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## female in adelie\n", "female_adelie = adelie_df[adelie_df.sex == 'FEMALE']\n", "female_adelie.shape" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "female_adelie.shape[0] / adelie_df.shape[0]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(female[adelie])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.44242424242424244" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(adelie[female])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we can know that probability of A, given B can be computated as `prob(A[B])`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some laws\n", "\n", "$$P(A|B) = \\frac{P(A \\& B)}{P(B)}$$" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(female[adelie])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.43843843843843844" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(adelie)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.21921921921921922" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(female & adelie)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(female[adelie]) == prob(female & adelie)/prob(adelie)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we know that \n", "\n", "$$P(A \\& B) = P(B) P(A|B)$$\n", "\n", "So\n", "\n", "$$P(B \\& A) = P(A) P(B|A)$$\n", "\n", "Because \n", "\n", "$$P(A \\& B) = P(B \\& A)$$\n", "\n", "So:\n", "\n", "$$\\begin{aligned} P(A|B) &= \\frac{P(A \\& B)}{P(B)} \\\\ &= \\frac{P(B \\& A)}{P(B)} \\\\ &= \\frac{P(A) P(B|A)}{P(B)}\\end{aligned}$$" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }