{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Probability" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "remove-cell" ] }, "source": [ "Think Bayes, Second Edition\n", "\n", "Copyright 2020 Allen B. Downey\n", "\n", "License: [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](https://creativecommons.org/licenses/by-nc-sa/4.0/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The foundation of Bayesian statistics is Bayes's Theorem, and the foundation of Bayes's Theorem is conditional probability. \n", "\n", "In this chapter, we'll start with conditional probability, derive Bayes's Theorem, and demonstrate it using a real dataset. In the next chapter, we'll use Bayes's Theorem to solve problems related to conditional probability. In the chapters that follow, we'll make the transition from Bayes's Theorem to Bayesian statistics, and I'll explain the difference." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linda the Banker\n", "\n", "To introduce conditional probability, I'll use an example from a [famous experiment by Tversky and Kahneman](https://en.wikipedia.org/wiki/Conjunction_fallacy), who posed the following question:\n", "\n", "> Linda is 31 years old, single, outspoken, and very bright. She majored in philosophy. As a student, she was deeply concerned with issues of discrimination and social justice, and also participated in anti-nuclear demonstrations. Which is more probable?\n", "> 1. Linda is a bank teller.\n", "> 2. Linda is a bank teller and is active in the feminist movement.\n", "\n", "Many people choose the second answer, presumably because it seems more consistent with the description. It seems uncharacteristic if Linda is *just* a bank teller; it seems more consistent if she is also a feminist." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But the second answer cannot be \"more probable\", as the question asks. Suppose we find 1000 people who fit Linda's description and 10 of them work as bank tellers. How many of them are also feminists? At most, all 10 of them are; in that case, the two options are *equally* probable. If fewer than 10 are, the second option is *less* probable. But there is no way the second option can be *more* probable.\n", "\n", "If you were inclined to choose the second option, you are in good company. The biologist [Stephen J. Gould wrote](https://doi.org/10.1080/09332480.1989.10554932) :\n", "\n", "> I am particularly fond of this example because I know that the [second] statement is least probable, yet a little [homunculus](https://en.wikipedia.org/wiki/Homunculus_argument) in my head continues to jump up and down, shouting at me, \"but she can't just be a bank teller; read the description.\"\n", "\n", "If the little person in your head is still unhappy, maybe this chapter will help." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Probability\n", "\n", "At this point I should provide a definition of \"probability\", but that [turns out to be surprisingly difficult](https://en.wikipedia.org/wiki/Probability_interpretations). To avoid getting stuck before we start, we will use a simple definition for now and refine it later: A **probability** is a fraction of a finite set.\n", "\n", "For example, if we survey 1000 people, and 20 of them are bank tellers, the fraction that work as bank tellers is 0.02 or 2\\%. If we choose a person from this population at random, the probability that they are a bank teller is 2\\%.\n", "By \"at random\" I mean that every person in the dataset has the same chance of being chosen.\n", "\n", "With this definition and an appropriate dataset, we can compute probabilities by counting.\n", "To demonstrate, I'll use data from the [General Social Survey](http://gss.norc.org/) (GSS). " ] }, { "cell_type": "markdown", "metadata": { "tags": [ "remove-cell" ] }, "source": [ "The following cell downloads the data." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:08.628591Z", "iopub.status.busy": "2021-04-16T19:35:08.627766Z", "iopub.status.idle": "2021-04-16T19:35:08.630508Z", "shell.execute_reply": "2021-04-16T19:35:08.629988Z" }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "# Load the data file\n", "\n", "from os.path import basename, exists\n", "\n", "def download(url):\n", " filename = basename(url)\n", " if not exists(filename):\n", " from urllib.request import urlretrieve\n", " local, _ = urlretrieve(url, filename)\n", " print('Downloaded ' + local)\n", " \n", "download('https://github.com/AllenDowney/ThinkBayes2/raw/master/data/gss_bayes.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I'll use Pandas to read the data and store it in a `DataFrame`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:08.634488Z", "iopub.status.busy": "2021-04-16T19:35:08.633720Z", "iopub.status.idle": "2021-04-16T19:35:09.081049Z", "shell.execute_reply": "2021-04-16T19:35:09.081454Z" } }, "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", "
caseidyearagesexpolviewspartyidindus10
01197421.014.02.04970.0
12197441.015.00.09160.0
25197458.026.01.02670.0
36197430.015.04.06870.0
47197448.015.04.07860.0
\n", "
" ], "text/plain": [ " caseid year age sex polviews partyid indus10\n", "0 1 1974 21.0 1 4.0 2.0 4970.0\n", "1 2 1974 41.0 1 5.0 0.0 9160.0\n", "2 5 1974 58.0 2 6.0 1.0 2670.0\n", "3 6 1974 30.0 1 5.0 4.0 6870.0\n", "4 7 1974 48.0 1 5.0 4.0 7860.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "gss = pd.read_csv('gss_bayes.csv')\n", "gss.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `DataFrame` has one row for each person surveyed and one column for each variable I selected.\n", "\n", "The columns are\n", "\n", "* `caseid`: Respondent identifier.\n", "\n", "* `year`: Year when the respondent was surveyed.\n", "\n", "* `age`: Respondent's age when surveyed.\n", "\n", "* `sex`: Male or female.\n", "\n", "* `polviews`: Political views on a range from liberal to conservative.\n", "\n", "* `partyid`: Political party affiliation, Democrat, Independent, or Republican.\n", "\n", "* `indus10`: [Code](https://www.census.gov/cgi-bin/sssd/naics/naicsrch?chart=2007) for the industry the respondent works in.\n", "\n", "Let's look at these variables in more detail, starting with `indus10`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fraction of Bankers\n", "\n", "The code for \"Banking and related activities\" is 6870, so we can select bankers like this:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.085579Z", "iopub.status.busy": "2021-04-16T19:35:09.084990Z", "iopub.status.idle": "2021-04-16T19:35:09.096470Z", "shell.execute_reply": "2021-04-16T19:35:09.095986Z" } }, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 False\n", "2 False\n", "3 True\n", "4 False\n", "Name: indus10, dtype: bool" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "banker = (gss['indus10'] == 6870)\n", "banker.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is a Pandas `Series` that contains the Boolean values `True` and `False`.\n", "\n", "If we use the `sum` function on this `Series`, it treats `True` as 1 and `False` as 0, so the total is the number of bankers." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.100573Z", "iopub.status.busy": "2021-04-16T19:35:09.100000Z", "iopub.status.idle": "2021-04-16T19:35:09.103808Z", "shell.execute_reply": "2021-04-16T19:35:09.103159Z" } }, "outputs": [ { "data": { "text/plain": [ "728" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "banker.sum()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this dataset, there are 728 bankers.\n", "\n", "To compute the *fraction* of bankers, we can use the `mean` function, which computes the fraction of `True` values in the `Series`:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.108607Z", "iopub.status.busy": "2021-04-16T19:35:09.107935Z", "iopub.status.idle": "2021-04-16T19:35:09.110861Z", "shell.execute_reply": "2021-04-16T19:35:09.111310Z" } }, "outputs": [ { "data": { "text/plain": [ "0.014769730168391155" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "banker.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "About 1.5% of the respondents work in banking, so if we choose a random person from the dataset, the probability they are a banker is about 1.5%." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Probability Function\n", "\n", "I'll put the code from the previous section in a function that takes a Boolean series and returns a probability:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.115436Z", "iopub.status.busy": "2021-04-16T19:35:09.114857Z", "iopub.status.idle": "2021-04-16T19:35:09.116926Z", "shell.execute_reply": "2021-04-16T19:35:09.117430Z" } }, "outputs": [], "source": [ "def prob(A):\n", " \"\"\"Computes the probability of a proposition, A.\"\"\" \n", " return A.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we can compute the fraction of bankers like this:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.121943Z", "iopub.status.busy": "2021-04-16T19:35:09.121067Z", "iopub.status.idle": "2021-04-16T19:35:09.124766Z", "shell.execute_reply": "2021-04-16T19:35:09.125312Z" } }, "outputs": [ { "data": { "text/plain": [ "0.014769730168391155" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's look at another variable in this dataset.\n", "The values of the column `sex` are encoded like this:\n", "\n", "```\n", "1 Male\n", "2 Female\n", "```\n", "\n", "So we can make a Boolean series that is `True` for female respondents and `False` otherwise." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.133453Z", "iopub.status.busy": "2021-04-16T19:35:09.132833Z", "iopub.status.idle": "2021-04-16T19:35:09.135227Z", "shell.execute_reply": "2021-04-16T19:35:09.134787Z" } }, "outputs": [], "source": [ "female = (gss['sex'] == 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And use it to compute the fraction of respondents who are women." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.138777Z", "iopub.status.busy": "2021-04-16T19:35:09.138234Z", "iopub.status.idle": "2021-04-16T19:35:09.140987Z", "shell.execute_reply": "2021-04-16T19:35:09.140543Z" } }, "outputs": [ { "data": { "text/plain": [ "0.5378575776019476" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(female)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The fraction of women in this dataset is higher than in the adult U.S. population because [the GSS does not include people living in institutions](https://gss.norc.org/faq) like prisons and military housing, and those populations are more likely to be male." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Political Views and Parties\n", "\n", "The other variables we'll consider are `polviews`, which describes the political views of the respondents, and `partyid`, which describes their affiliation with a political party.\n", "\n", "The values of `polviews` are on a seven-point scale:\n", "\n", "```\n", "1\tExtremely liberal\n", "2\tLiberal\n", "3\tSlightly liberal\n", "4\tModerate\n", "5\tSlightly conservative\n", "6\tConservative\n", "7\tExtremely conservative\n", "```\n", "\n", "I'll define `liberal` to be `True` for anyone whose response is \"Extremely liberal\", \"Liberal\", or \"Slightly liberal\"." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.144820Z", "iopub.status.busy": "2021-04-16T19:35:09.144315Z", "iopub.status.idle": "2021-04-16T19:35:09.146158Z", "shell.execute_reply": "2021-04-16T19:35:09.146493Z" } }, "outputs": [], "source": [ "liberal = (gss['polviews'] <= 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's the fraction of respondents who are liberal by this definition." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.150191Z", "iopub.status.busy": "2021-04-16T19:35:09.149628Z", "iopub.status.idle": "2021-04-16T19:35:09.152382Z", "shell.execute_reply": "2021-04-16T19:35:09.152013Z" } }, "outputs": [ { "data": { "text/plain": [ "0.27374721038750255" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(liberal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we choose a random person in this dataset, the probability they are liberal is about 27%." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The values of `partyid` are encoded like this:\n", "\n", "```\n", "0\tStrong democrat\n", "1\tNot strong democrat\n", "2\tIndependent, near democrat\n", "3\tIndependent\n", "4\tIndependent, near republican\n", "5\tNot strong republican\n", "6\tStrong republican\n", "7\tOther party\n", "```\n", "\n", "I'll define `democrat` to include respondents who chose \"Strong democrat\" or \"Not strong democrat\":" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.156357Z", "iopub.status.busy": "2021-04-16T19:35:09.155780Z", "iopub.status.idle": "2021-04-16T19:35:09.158246Z", "shell.execute_reply": "2021-04-16T19:35:09.157752Z" } }, "outputs": [], "source": [ "democrat = (gss['partyid'] <= 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here's the fraction of respondents who are Democrats, by this definition." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.162885Z", "iopub.status.busy": "2021-04-16T19:35:09.162111Z", "iopub.status.idle": "2021-04-16T19:35:09.164826Z", "shell.execute_reply": "2021-04-16T19:35:09.165317Z" } }, "outputs": [ { "data": { "text/plain": [ "0.3662609048488537" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(democrat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conjunction\n", "\n", "Now that we have a definition of probability and a function that computes it, let's move on to conjunction.\n", "\n", "\"Conjunction\" is another name for the logical `and` operation. If you have two [propositions](https://en.wikipedia.org/wiki/Proposition), `A` and `B`, the conjunction `A and B` is `True` if both `A` and `B` are `True`, and `False` otherwise.\n", "\n", "If we have two Boolean series, we can use the `&` operator to compute their conjunction.\n", "For example, we have already computed the probability that a respondent is a banker." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.169647Z", "iopub.status.busy": "2021-04-16T19:35:09.168929Z", "iopub.status.idle": "2021-04-16T19:35:09.171685Z", "shell.execute_reply": "2021-04-16T19:35:09.171252Z" } }, "outputs": [ { "data": { "text/plain": [ "0.014769730168391155" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the probability that they are a Democrat:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.175131Z", "iopub.status.busy": "2021-04-16T19:35:09.174679Z", "iopub.status.idle": "2021-04-16T19:35:09.176933Z", "shell.execute_reply": "2021-04-16T19:35:09.177292Z" } }, "outputs": [ { "data": { "text/plain": [ "0.3662609048488537" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(democrat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can compute the probability that a respondent is a banker *and* a Democrat:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.180713Z", "iopub.status.busy": "2021-04-16T19:35:09.180011Z", "iopub.status.idle": "2021-04-16T19:35:09.183970Z", "shell.execute_reply": "2021-04-16T19:35:09.183516Z" } }, "outputs": [ { "data": { "text/plain": [ "0.004686548995739501" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(banker & democrat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we should expect, `prob(banker & democrat)` is less than `prob(banker)`, because not all bankers are Democrats.\n", "\n", "We expect conjunction to be commutative; that is, `A & B` should be the same as `B & A`. To check, we can also compute `prob(democrat & banker)`:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.187308Z", "iopub.status.busy": "2021-04-16T19:35:09.186747Z", "iopub.status.idle": "2021-04-16T19:35:09.189804Z", "shell.execute_reply": "2021-04-16T19:35:09.190185Z" } }, "outputs": [ { "data": { "text/plain": [ "0.004686548995739501" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(democrat & banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As expected, they are the same." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditional Probability\n", "\n", "Conditional probability is a probability that depends on a condition, but that might not be the most helpful definition. Here are some examples:\n", "\n", "* What is the probability that a respondent is a Democrat, given that they are liberal?\n", "\n", "* What is the probability that a respondent is female, given that they are a banker?\n", "\n", "* What is the probability that a respondent is liberal, given that they are female?\n", "\n", "Let's start with the first one, which we can interpret like this: \"Of all the respondents who are liberal, what fraction are Democrats?\"\n", "\n", "We can compute this probability in two steps:\n", "\n", "1. Select all respondents who are liberal.\n", "\n", "2. Compute the fraction of the selected respondents who are Democrats.\n", "\n", "To select liberal respondents, we can use the bracket operator, `[]`, like this:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.193582Z", "iopub.status.busy": "2021-04-16T19:35:09.192986Z", "iopub.status.idle": "2021-04-16T19:35:09.196312Z", "shell.execute_reply": "2021-04-16T19:35:09.195843Z" } }, "outputs": [], "source": [ "selected = democrat[liberal]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`selected` contains the values of `democrat` for liberal respondents, so `prob(selected)` is the fraction of liberals who are Democrats:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.200734Z", "iopub.status.busy": "2021-04-16T19:35:09.199974Z", "iopub.status.idle": "2021-04-16T19:35:09.202760Z", "shell.execute_reply": "2021-04-16T19:35:09.203214Z" } }, "outputs": [ { "data": { "text/plain": [ "0.5206403320240125" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(selected)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A little more than half of liberals are Democrats. If that result is lower than you expected, keep in mind:\n", "\n", "1. We used a somewhat strict definition of \"Democrat\", excluding Independents who \"lean\" democratic.\n", "\n", "2. The dataset includes respondents as far back as 1974; in the early part of this interval, there was less alignment between political views and party affiliation, compared to the present.\n", "\n", "Let's try the second example, \"What is the probability that a respondent is female, given that they are a banker?\"\n", "We can interpret that to mean, \"Of all respondents who are bankers, what fraction are female?\"\n", "\n", "Again, we'll use the bracket operator to select only the bankers and `prob` to compute the fraction that are female." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.206724Z", "iopub.status.busy": "2021-04-16T19:35:09.206015Z", "iopub.status.idle": "2021-04-16T19:35:09.209768Z", "shell.execute_reply": "2021-04-16T19:35:09.210120Z" } }, "outputs": [ { "data": { "text/plain": [ "0.7706043956043956" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "selected = female[banker]\n", "prob(selected)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "About 77% of the bankers in this dataset are female.\n", "\n", "Let's wrap this computation in a function.\n", "I'll define `conditional` to take two Boolean series, `proposition` and `given`, and compute the conditional probability of `proposition` conditioned on `given`:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.213058Z", "iopub.status.busy": "2021-04-16T19:35:09.212636Z", "iopub.status.idle": "2021-04-16T19:35:09.214860Z", "shell.execute_reply": "2021-04-16T19:35:09.214485Z" } }, "outputs": [], "source": [ "def conditional(proposition, given):\n", " \"\"\"Probability of A conditioned on given.\"\"\"\n", " return prob(proposition[given])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use `conditional` to compute the probability that a respondent is liberal given that they are female." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.218614Z", "iopub.status.busy": "2021-04-16T19:35:09.218019Z", "iopub.status.idle": "2021-04-16T19:35:09.222351Z", "shell.execute_reply": "2021-04-16T19:35:09.221981Z" } }, "outputs": [ { "data": { "text/plain": [ "0.27581004111500884" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conditional(liberal, given=female)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "About 28% of female respondents are liberal.\n", "\n", "I included the keyword, `given`, along with the parameter, `female`, to make this expression more readable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditional Probability Is Not Commutative\n", "\n", "We have seen that conjunction is commutative; that is, `prob(A & B)` is always equal to `prob(B & A)`.\n", "\n", "But conditional probability is *not* commutative; that is, `conditional(A, B)` is not the same as `conditional(B, A)`.\n", "\n", "That should be clear if we look at an example. Previously, we computed the probability a respondent is female, given that they are banker." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.225222Z", "iopub.status.busy": "2021-04-16T19:35:09.224726Z", "iopub.status.idle": "2021-04-16T19:35:09.228199Z", "shell.execute_reply": "2021-04-16T19:35:09.227808Z" } }, "outputs": [ { "data": { "text/plain": [ "0.7706043956043956" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conditional(female, given=banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result shows that the majority of bankers are female. That is not the same as the probability that a respondent is a banker, given that they are female:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.231432Z", "iopub.status.busy": "2021-04-16T19:35:09.230897Z", "iopub.status.idle": "2021-04-16T19:35:09.235411Z", "shell.execute_reply": "2021-04-16T19:35:09.235854Z" } }, "outputs": [ { "data": { "text/plain": [ "0.02116102749801969" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conditional(banker, given=female)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Only about 2% of female respondents are bankers.\n", "\n", "I hope this example makes it clear that conditional probability is not commutative, and maybe it was already clear to you. Nevertheless, it is a common error to confuse `conditional(A, B)` and `conditional(B, A)`. We'll see some examples later." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Condition and Conjunction\n", "\n", "We can combine conditional probability and conjunction. For example, here's the probability a respondent is female, given that they are a liberal Democrat." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.239595Z", "iopub.status.busy": "2021-04-16T19:35:09.239062Z", "iopub.status.idle": "2021-04-16T19:35:09.244206Z", "shell.execute_reply": "2021-04-16T19:35:09.244621Z" } }, "outputs": [ { "data": { "text/plain": [ "0.576085409252669" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conditional(female, given=liberal & democrat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "About 57% of liberal Democrats are female.\n", "\n", "And here's the probability they are a liberal female, given that they are a banker:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.247749Z", "iopub.status.busy": "2021-04-16T19:35:09.247310Z", "iopub.status.idle": "2021-04-16T19:35:09.251132Z", "shell.execute_reply": "2021-04-16T19:35:09.250686Z" } }, "outputs": [ { "data": { "text/plain": [ "0.17307692307692307" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conditional(liberal & female, given=banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "About 17% of bankers are liberal women." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Laws of Probability\n", "\n", "In the next few sections, we'll derive three relationships between conjunction and conditional probability:\n", "\n", "* Theorem 1: Using a conjunction to compute a conditional probability.\n", "\n", "* Theorem 2: Using a conditional probability to compute a conjunction.\n", "\n", "* Theorem 3: Using `conditional(A, B)` to compute `conditional(B, A)`.\n", "\n", "Theorem 3 is also known as Bayes's Theorem.\n", "\n", "I'll write these theorems using mathematical notation for probability:\n", "\n", "* $P(A)$ is the probability of proposition $A$.\n", "\n", "* $P(A~\\mathrm{and}~B)$ is the probability of the conjunction of $A$ and $B$, that is, the probability that both are true.\n", "\n", "* $P(A | B)$ is the conditional probability of $A$ given that $B$ is true. The vertical line between $A$ and $B$ is pronounced \"given\". \n", "\n", "With that, we are ready for Theorem 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Theorem 1\n", "\n", "What fraction of bankers are female? We have already seen one way to compute the answer:\n", "\n", "1. Use the bracket operator to select the bankers, then\n", "\n", "2. Use `mean` to compute the fraction of bankers who are female.\n", "\n", "We can write these steps like this:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.254022Z", "iopub.status.busy": "2021-04-16T19:35:09.253618Z", "iopub.status.idle": "2021-04-16T19:35:09.257836Z", "shell.execute_reply": "2021-04-16T19:35:09.258250Z" } }, "outputs": [ { "data": { "text/plain": [ "0.7706043956043956" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "female[banker].mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or we can use the `conditional` function, which does the same thing:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.261661Z", "iopub.status.busy": "2021-04-16T19:35:09.260776Z", "iopub.status.idle": "2021-04-16T19:35:09.264587Z", "shell.execute_reply": "2021-04-16T19:35:09.264191Z" } }, "outputs": [ { "data": { "text/plain": [ "0.7706043956043956" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conditional(female, given=banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But there is another way to compute this conditional probability, by computing the ratio of two probabilities:\n", "\n", "1. The fraction of respondents who are female bankers, and\n", "\n", "2. The fraction of respondents who are bankers.\n", "\n", "In other words: of all the bankers, what fraction are female bankers?\n", "Here's how we compute this ratio." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.268221Z", "iopub.status.busy": "2021-04-16T19:35:09.267591Z", "iopub.status.idle": "2021-04-16T19:35:09.271822Z", "shell.execute_reply": "2021-04-16T19:35:09.271337Z" } }, "outputs": [ { "data": { "text/plain": [ "0.7706043956043956" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(female & banker) / prob(banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is the same. This example demonstrates a general rule that relates conditional probability and conjunction. Here's what it looks like in math notation:\n", "\n", "$$P(A|B) = \\frac{P(A~\\mathrm{and}~B)}{P(B)}$$\n", "\n", "And that's Theorem 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Theorem 2\n", "\n", "If we start with Theorem 1 and multiply both sides by $P(B)$, we get Theorem 2.\n", "\n", "$$P(A~\\mathrm{and}~B) = P(B) ~ P(A|B)$$\n", "\n", "This formula suggests a second way to compute a conjunction: instead of using the `&` operator, we can compute the product of two probabilities.\n", "\n", "Let's see if it works for `liberal` and `democrat`. Here's the result using `&`:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.275335Z", "iopub.status.busy": "2021-04-16T19:35:09.274699Z", "iopub.status.idle": "2021-04-16T19:35:09.278134Z", "shell.execute_reply": "2021-04-16T19:35:09.277739Z" } }, "outputs": [ { "data": { "text/plain": [ "0.1425238385067965" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(liberal & democrat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here's the result using Theorem 2:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.282010Z", "iopub.status.busy": "2021-04-16T19:35:09.281020Z", "iopub.status.idle": "2021-04-16T19:35:09.285790Z", "shell.execute_reply": "2021-04-16T19:35:09.286268Z" } }, "outputs": [ { "data": { "text/plain": [ "0.1425238385067965" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(democrat) * conditional(liberal, democrat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "They are the same." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Theorem 3\n", "\n", "We have established that conjunction is commutative. In math notation, that means:\n", "\n", "$$P(A~\\mathrm{and}~B) = P(B~\\mathrm{and}~A)$$\n", "\n", "If we apply Theorem 2 to both sides, we have\n", "\n", "$$P(B) P(A|B) = P(A) P(B|A)$$\n", "\n", "Here's one way to interpret that: if you want to check $A$ and $B$, you can do it in either order:\n", "\n", "1. You can check $B$ first, then $A$ conditioned on $B$, or\n", "\n", "2. You can check $A$ first, then $B$ conditioned on $A$.\n", "\n", "If we divide through by $P(B)$, we get Theorem 3:\n", "\n", "$$P(A|B) = \\frac{P(A) P(B|A)}{P(B)}$$\n", "\n", "And that, my friends, is Bayes's Theorem.\n", "\n", "To see how it works, let's compute the fraction of bankers who are liberal, first using `conditional`:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.289892Z", "iopub.status.busy": "2021-04-16T19:35:09.289327Z", "iopub.status.idle": "2021-04-16T19:35:09.292846Z", "shell.execute_reply": "2021-04-16T19:35:09.293307Z" } }, "outputs": [ { "data": { "text/plain": [ "0.2239010989010989" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conditional(liberal, given=banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now using Bayes's Theorem:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.296917Z", "iopub.status.busy": "2021-04-16T19:35:09.296411Z", "iopub.status.idle": "2021-04-16T19:35:09.302183Z", "shell.execute_reply": "2021-04-16T19:35:09.301729Z" } }, "outputs": [ { "data": { "text/plain": [ "0.2239010989010989" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(liberal) * conditional(banker, liberal) / prob(banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "They are the same." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Law of Total Probability\n", "\n", "In addition to these three theorems, there's one more thing we'll need to do Bayesian statistics: the law of total probability.\n", "Here's one form of the law, expressed in mathematical notation:\n", "\n", "$$P(A) = P(B_1 \\mathrm{and} A) + P(B_2 \\mathrm{and} A)$$\n", "\n", "In words, the total probability of $A$ is the sum of two possibilities: either $B_1$ and $A$ are true or $B_2$ and $A$ are true.\n", "But this law applies only if $B_1$ and $B_2$ are:\n", "\n", "* Mutually exclusive, which means that only one of them can be true, and\n", "\n", "* Collectively exhaustive, which means that one of them must be true.\n", "\n", "As an example, let's use this law to compute the probability that a respondent is a banker.\n", "We can compute it directly like this:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.306120Z", "iopub.status.busy": "2021-04-16T19:35:09.305480Z", "iopub.status.idle": "2021-04-16T19:35:09.308555Z", "shell.execute_reply": "2021-04-16T19:35:09.309041Z" } }, "outputs": [ { "data": { "text/plain": [ "0.014769730168391155" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So let's confirm that we get the same thing if we compute male and female bankers separately.\n", "\n", "In this dataset all respondents are designated male or female. Recently, the GSS Board of Overseers announced that they will add more inclusive gender questions to the survey (you can read more about this issue, and their decision, at .\n", "\n", "\n", "We already have a Boolean `Series` that is `True` for female respondents.\n", "Here's the complementary `Series` for male respondents." ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.313714Z", "iopub.status.busy": "2021-04-16T19:35:09.312851Z", "iopub.status.idle": "2021-04-16T19:35:09.315579Z", "shell.execute_reply": "2021-04-16T19:35:09.315077Z" } }, "outputs": [], "source": [ "male = (gss['sex'] == 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can compute the total probability of `banker` like this. " ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.320293Z", "iopub.status.busy": "2021-04-16T19:35:09.319613Z", "iopub.status.idle": "2021-04-16T19:35:09.323814Z", "shell.execute_reply": "2021-04-16T19:35:09.324375Z" } }, "outputs": [ { "data": { "text/plain": [ "0.014769730168391155" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prob(male & banker) + prob(female & banker)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because `male` and `female` are mutually exclusive and collectively exhaustive (MECE), we get the same result we got by computing the probability of `banker` directly.\n", "\n", "Applying Theorem 2, we can also write the law of total probability like this:\n", "\n", "$$P(A) = P(B_1) P(A|B_1) + P(B_2) P(A|B_2)$$\n", "\n", "And we can test it with the same example:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.330125Z", "iopub.status.busy": "2021-04-16T19:35:09.328968Z", "iopub.status.idle": "2021-04-16T19:35:09.336156Z", "shell.execute_reply": "2021-04-16T19:35:09.335726Z" } }, "outputs": [ { "data": { "text/plain": [ "0.014769730168391153" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(prob(male) * conditional(banker, given=male) +\n", "prob(female) * conditional(banker, given=female))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When there are more than two conditions, it is more concise to write the law of total probability as a summation:\n", "\n", "$$P(A) = \\sum_i P(B_i) P(A|B_i)$$\n", "\n", "Again, this holds as long as the conditions, $B_i$ are mutually exclusive and collectively exhaustive.\n", "As an example, let's consider `polviews`, which has seven different values." ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.340326Z", "iopub.status.busy": "2021-04-16T19:35:09.339575Z", "iopub.status.idle": "2021-04-16T19:35:09.345569Z", "shell.execute_reply": "2021-04-16T19:35:09.345920Z" } }, "outputs": [ { "data": { "text/plain": [ "polviews\n", "1.0 1442\n", "2.0 5808\n", "3.0 6243\n", "4.0 18943\n", "5.0 7940\n", "6.0 7319\n", "7.0 1595\n", "Name: count, dtype: int64" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = gss['polviews']\n", "B.value_counts().sort_index()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On this scale, `4.0` represents \"Moderate\".\n", "So we can compute the probability of a moderate banker like this:" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.349523Z", "iopub.status.busy": "2021-04-16T19:35:09.348794Z", "iopub.status.idle": "2021-04-16T19:35:09.354627Z", "shell.execute_reply": "2021-04-16T19:35:09.354093Z" } }, "outputs": [ { "data": { "text/plain": [ "0.005822682085615744" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 4\n", "prob(B==i) * conditional(banker, B==i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we can use `sum` and a [generator expression](https://www.johndcook.com/blog/2020/01/15/generator-expression/) to compute the summation." ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.358978Z", "iopub.status.busy": "2021-04-16T19:35:09.358491Z", "iopub.status.idle": "2021-04-16T19:35:09.371125Z", "shell.execute_reply": "2021-04-16T19:35:09.370597Z" } }, "outputs": [ { "data": { "text/plain": [ "0.014769730168391157" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(prob(B==i) * conditional(banker, B==i)\n", " for i in range(1, 8))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is the same.\n", "\n", "In this example, using the law of total probability is a lot more work than computing the probability directly, but it will turn out to be useful, I promise." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "Here's what we have so far:\n", "\n", "**Theorem 1** gives us a way to compute a conditional probability using a conjunction:\n", "\n", "$$P(A|B) = \\frac{P(A~\\mathrm{and}~B)}{P(B)}$$\n", "\n", "**Theorem 2** gives us a way to compute a conjunction using a conditional probability:\n", "\n", "$$P(A~\\mathrm{and}~B) = P(B) P(A|B)$$\n", "\n", "**Theorem 3**, also known as Bayes's Theorem, gives us a way to get from $P(A|B)$ to $P(B|A)$, or the other way around:\n", "\n", "$$P(A|B) = \\frac{P(A) P(B|A)}{P(B)}$$\n", "\n", "**The Law of Total Probability** provides a way to compute probabilities by adding up the pieces:\n", "\n", "$$P(A) = \\sum_i P(B_i) P(A|B_i)$$\n", "\n", "At this point you might ask, \"So what?\" If we have all of the data, we can compute any probability we want, any conjunction, or any conditional probability, just by counting. We don't have to use these formulas.\n", "\n", "And you are right, *if* we have all of the data. But often we don't, and in that case, these formulas can be pretty useful -- especially Bayes's Theorem.\n", "In the next chapter, we'll see how." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Use `conditional` to compute the following probabilities:\n", "\n", "* What is the probability that a respondent is liberal, given that they are a Democrat?\n", "\n", "* What is the probability that a respondent is a Democrat, given that they are liberal?\n", "\n", "Think carefully about the order of the arguments you pass to `conditional`." ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.399461Z", "iopub.status.busy": "2021-04-16T19:35:09.398872Z", "iopub.status.idle": "2021-04-16T19:35:09.403442Z", "shell.execute_reply": "2021-04-16T19:35:09.403943Z" } }, "outputs": [ { "data": { "text/plain": [ "0.3891320002215698" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "\n", "conditional(liberal, given=democrat)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.407668Z", "iopub.status.busy": "2021-04-16T19:35:09.407123Z", "iopub.status.idle": "2021-04-16T19:35:09.411160Z", "shell.execute_reply": "2021-04-16T19:35:09.411604Z" } }, "outputs": [ { "data": { "text/plain": [ "0.5206403320240125" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "\n", "conditional(democrat, given=liberal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Let's use the tools in this chapter to solve a variation of the Linda problem.\n", "\n", "> Linda is 31 years old, single, outspoken, and very bright. She majored in philosophy. As a student, she was deeply concerned with issues of discrimination and social justice, and also participated in anti-nuclear demonstrations. Which is more probable?\n", "> 1. Linda is a banker.\n", "> 2. Linda is a banker and considers herself a liberal Democrat.\n", "\n", "To answer this question, compute \n", "\n", "* The probability that Linda is a banker, given that she is female,\n", "\n", "* The probability that Linda is a banker and a liberal Democrat, given that she is female." ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.375687Z", "iopub.status.busy": "2021-04-16T19:35:09.374854Z", "iopub.status.idle": "2021-04-16T19:35:09.379273Z", "shell.execute_reply": "2021-04-16T19:35:09.378643Z" } }, "outputs": [ { "data": { "text/plain": [ "0.02116102749801969" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "\n", "conditional(banker, given=female)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.383986Z", "iopub.status.busy": "2021-04-16T19:35:09.383142Z", "iopub.status.idle": "2021-04-16T19:35:09.386942Z", "shell.execute_reply": "2021-04-16T19:35:09.387425Z" } }, "outputs": [ { "data": { "text/plain": [ "0.0023009316887329786" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "\n", "conditional(banker & liberal & democrat, given=female)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** There's a [famous quote](https://quoteinvestigator.com/2014/02/24/heart-head/) about young people, old people, liberals, and conservatives that goes something like:\n", "\n", "> If you are not a liberal at 25, you have no heart. If you are not a conservative at 35, you have no brain.\n", "\n", "Whether you agree with this proposition or not, it suggests some probabilities we can compute as an exercise.\n", "Rather than use the specific ages 25 and 35, let's define `young` and `old` as under 30 or over 65:" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.415701Z", "iopub.status.busy": "2021-04-16T19:35:09.415139Z", "iopub.status.idle": "2021-04-16T19:35:09.418451Z", "shell.execute_reply": "2021-04-16T19:35:09.418810Z" } }, "outputs": [ { "data": { "text/plain": [ "0.19435991073240008" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "young = (gss['age'] < 30)\n", "prob(young)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.423028Z", "iopub.status.busy": "2021-04-16T19:35:09.422417Z", "iopub.status.idle": "2021-04-16T19:35:09.425586Z", "shell.execute_reply": "2021-04-16T19:35:09.425960Z" } }, "outputs": [ { "data": { "text/plain": [ "0.17328058429701765" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "old = (gss['age'] >= 65)\n", "prob(old)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For these thresholds, I chose round numbers near the 20th and 80th percentiles. Depending on your age, you may or may not agree with these definitions of \"young\" and \"old\".\n", "\n", "I'll define `conservative` as someone whose political views are \"Conservative\", \"Slightly Conservative\", or \"Extremely Conservative\"." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.429448Z", "iopub.status.busy": "2021-04-16T19:35:09.428975Z", "iopub.status.idle": "2021-04-16T19:35:09.432387Z", "shell.execute_reply": "2021-04-16T19:35:09.432875Z" } }, "outputs": [ { "data": { "text/plain": [ "0.3419354838709677" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conservative = (gss['polviews'] >= 5)\n", "prob(conservative)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `prob` and `conditional` to compute the following probabilities.\n", "\n", "* What is the probability that a randomly chosen respondent is a young liberal?\n", "\n", "* What is the probability that a young person is liberal?\n", "\n", "* What fraction of respondents are old conservatives?\n", "\n", "* What fraction of conservatives are old?\n", "\n", "For each statement, think about whether it is expressing a conjunction, a conditional probability, or both.\n", "\n", "For the conditional probabilities, be careful about the order of the arguments.\n", "If your answer to the last question is greater than 30%, you have it backwards!" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.436838Z", "iopub.status.busy": "2021-04-16T19:35:09.436245Z", "iopub.status.idle": "2021-04-16T19:35:09.439984Z", "shell.execute_reply": "2021-04-16T19:35:09.440468Z" } }, "outputs": [ { "data": { "text/plain": [ "0.06579427875836884" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "\n", "prob(young & liberal)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.443827Z", "iopub.status.busy": "2021-04-16T19:35:09.443300Z", "iopub.status.idle": "2021-04-16T19:35:09.447774Z", "shell.execute_reply": "2021-04-16T19:35:09.447317Z" } }, "outputs": [ { "data": { "text/plain": [ "0.338517745302714" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "\n", "conditional(liberal, given=young)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.451326Z", "iopub.status.busy": "2021-04-16T19:35:09.450505Z", "iopub.status.idle": "2021-04-16T19:35:09.455557Z", "shell.execute_reply": "2021-04-16T19:35:09.455085Z" } }, "outputs": [ { "data": { "text/plain": [ "0.06701156421180766" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "\n", "prob(old & conservative)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "execution": { "iopub.execute_input": "2021-04-16T19:35:09.458973Z", "iopub.status.busy": "2021-04-16T19:35:09.458445Z", "iopub.status.idle": "2021-04-16T19:35:09.462616Z", "shell.execute_reply": "2021-04-16T19:35:09.463061Z" } }, "outputs": [ { "data": { "text/plain": [ "0.19597721609113564" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Solution\n", "\n", "conditional(old, given=conservative)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "celltoolbar": "Tags", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }