<jupyter_start><jupyter_text>Dependence and Independence  Conditional distributions help us formalize our intuitive ideas about whether two random variables are independent of each other. Let $X$ and $Y$ be two random variables, and suppose we are given the value of $X$. Does that change our opinion about $Y$? If the answer is yes, then we will say that $X$ and $Y$ are dependent. If the answer is no, no matter what the given value of $X$ is, then we will say that $X$ and $Y$ are independent.Let's start with some examples and then move to precise definitions and results.  Dependence Here is the joint distribution of two random variables $X$ and $Y$. From this, what can we say about whether $X$ and $Y$ are dependent or independent?<jupyter_code>dist1<jupyter_output><empty_output><jupyter_text>You can see at once that if $X = 3$ then $Y$ can only be 0, whereas if $X = 2$ then $Y$ can be either 0 or 1. Knowing the value of $X$ changes the distribution of $Y$. That's dependence.  Here is an example in which you can't quickly determine dependence or independence by just looking at the possible values.<jupyter_code>dist2<jupyter_output><empty_output><jupyter_text>But you can tell by looking at the conditional distributions of $Y$ given $X$. They are different.<jupyter_code>dist2.conditional_dist('Y', 'X')<jupyter_output>/Users/dominiccroce/anaconda3/envs/textbook/lib/python3.6/site-packages/prob140/multi_variable.py:208: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  matrix = y.as_matrix()[:-1, :]<jupyter_text>It follows (and you should try to prove this), that at least some of the conditional distributions of $X$ given the different values of $Y$ will also be different from each other and from the marginal of $X$.Notice that not all the conditional distributions are different. The conditional distribution of $X$ given $Y=5$ is the same as the conditional distribution of $X$ given $Y=6$. But given $Y=7$, the conditional distribution changes. $X$ and $Y$ are dependent.<jupyter_code>dist2.conditional_dist('X', 'Y')<jupyter_output>/Users/dominiccroce/anaconda3/envs/textbook/lib/python3.6/site-packages/prob140/multi_variable.py:232: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  matrix = x.as_matrix()[:, :-1]<jupyter_text>Independence  Here is a joint distribution table in which you can't immediately tell whether there is dependence.<jupyter_code>dist3<jupyter_output><empty_output><jupyter_text>But look what happens when you condition $X$ on $Y$.<jupyter_code>dist3.conditional_dist('X', 'Y')<jupyter_output>/Users/dominiccroce/anaconda3/envs/textbook/lib/python3.6/site-packages/prob140/multi_variable.py:232: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  matrix = x.as_matrix()[:, :-1]<jupyter_text>All the rows are the same. That is, all the conditional distributions of $X$ given different values of $Y$ are the same, and hence are the same as the marginal of $X$ too. Given the value of $Y$, the probabilities for $X$ don't change at all. That's independence.You could have drawn the same conclusion by conditioning $Y$ on $X$:<jupyter_code>dist3.conditional_dist('Y', 'X')<jupyter_output>/Users/dominiccroce/anaconda3/envs/textbook/lib/python3.6/site-packages/prob140/multi_variable.py:208: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  matrix = y.as_matrix()[:-1, :]
{'fraction_non_alphanumeric': 0.09507445589919816, 'fraction_numerical': 0.01575028636884307, 'mean_word_length': 5.409174311926606, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '165320', 'license': 'MIT', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1033, 'n_words': 423, 'path': 'content/Chapter_04/05_Dependence_and_Independence.ipynb', 'repo_name': 'dcroce/jupyter-book', 'size': '3492'}