From 6b2f4f4cd3fbf1de30566e88a9bf84596b14d868 Mon Sep 17 00:00:00 2001 From: andyeisenberg Date: Wed, 25 Nov 2026 13:52:37 -0500 Subject: [PATCH] Correlation section --- requirements.txt | 4 +- source/ch-Linear-Regression.ptx | 261 +------------------------- source/main.ptx | 2 +- source/sec-Correlation.ptx | 302 +++++++++++++++++++++++++++++++ source/sec-Covariance.ptx | 2 +- source/sec-Linear-Regression.ptx | 168 +++++++++++++++++ 6 files changed, 476 insertions(+), 263 deletions(-) create mode 100644 source/sec-Correlation.ptx create mode 100644 source/sec-Linear-Regression.ptx diff --git a/requirements.txt b/requirements.txt index 7a4e3c4..5f29b31 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -# This file was automatically generated with PreTeXt 2.45.0. -pretext == 2.45.0 +# This file was automatically generated with PreTeXt 2.56.2. +pretext == 2.56.2 diff --git a/source/ch-Linear-Regression.ptx b/source/ch-Linear-Regression.ptx index fb3c9ba..300e4e4 100644 --- a/source/ch-Linear-Regression.ptx +++ b/source/ch-Linear-Regression.ptx @@ -9,263 +9,6 @@

-
- Correlation - -

- Text of section. -

- - - - -

- Calculate the correlation of X and Y: -

- - - - - - - - X = 1 - X = 2 - - - - Y = 0 - 0.12 - 0.24 - - - - Y = 1 - 0.3 - 34 - - -
-
-
- - - -

- Calculate the correlation of X and Y: -

- - - - - - - - X = 0 - X = 1 - X = 2 - - - - Y = 0 - 0.08 - 0.16 - 0.1 - - - - Y = 1 - 0.14 - 0.2 - 0.32 - - -
-
-
- - - -

- Suppose we roll a fair, 4-sided die two times. - Let X be the sum of the rolls, and let Y be the product of the rolls. - Find the correlation of X and Y. -

- -

- [Note: Somewhat tedious.] -

-
-
-
-
- -
- Linear Regression - -

- Text of section. -

- - - - -

- A survey of local companies collects information about marketing budgets X and revenue Y (each measures in thousands of dollars), shown below. - A linear regression gives the best linear fit as Y = 18.28 X + 29.69. - What is the coefficient of determination r^2? -

- - - - - - - x_i - y_i - (y_i - \text{avg})^2 - \text{pred } y_i - \text{res}^2 - - - - 200 - 4300 - 2073600 - 3686 - 377377 - - - - 420 - 7700 - 3841600 - 7707 - 53 - - - - 270 - 4500 - 1537600 - 4965 - 216495 - - - - 380 - 7000 - 1587600 - 6976 - 572 - - - - 300 - 5200 - 291600 - 5514 - 98401 - - - - sum: - 28700 - 9332000 - 28848 - 692898 - - -
-
-
- - - -

- A sample of 100 measurements are taken and a best fit line is calculated, resulting in the data below (the final line shows the sums for each column). - Find the coefficient of determination. -

- - - - - - - x_i - y_i - \text{pred } y_i - (y - \text{avg})^2 - \text{res}^2 - - - - 38.00 - 121.00 - 93.17 - 11.83 - 774.33 - - - - 87.00 - 241.00 - 238.95 - 13586.23 - 4.22 - - - - 30.00 - 61.00 - 69.37 - 4024.63 - 70.12 - - - - 35.00 - 85.00 - 84.25 - 1555.51 - 0.57 - - - - ⋮ - ⋮ - ⋮ - ⋮ - ⋮ - - - - 33.00 - 108.00 - 78.30 - 270.27 - 882.19 - - - - 26.00 - 32.00 - 57.47 - 8545.15 - 648.91 - - - - sum: - 12444.00 - 12444.00 - 515206.64 - 29789.80 - - -
-
-
-
-
+ + \ No newline at end of file diff --git a/source/main.ptx b/source/main.ptx index f74956f..71eaa52 100644 --- a/source/main.ptx +++ b/source/main.ptx @@ -1,4 +1,4 @@ - + diff --git a/source/sec-Correlation.ptx b/source/sec-Correlation.ptx new file mode 100644 index 0000000..da339b4 --- /dev/null +++ b/source/sec-Correlation.ptx @@ -0,0 +1,302 @@ +
+ Correlation + +

+ Recall , and our alternative formula + , which says that: + + \Cov(X, Y) = \E\left[(X - \E(X))(Y - \E(Y))\right] \E\left(XY\right) - \E(X)\E(Y). + + One observation we made about the covariance is that, if X and + Y tend to get bigger (than their respective averages) and smaller + (than their respective averages) together, then \Cov(X, Y) will + be positive. + If one variable tends to get big while the other gets small, + \Cov(X, Y) will be negative. +

+ +

+ However, it's not clear how to interpret the magnitude of + \Cov(X, Y). + One thought that we might investigagte is to compare \Cov(X, Y) + to \Cov(10X, 10Y) (or some other scaling factor). + In principal, if we want to measure the tendency of X and + Y to get big or small simultaneously, it would be nice if our + measure wasn't simply confused by the scale of the values themselves. +

+ + +

+ + \Cov(10X, 10Y) \amp = \E(10X \cdot 10Y) - \E(10X)\E(10Y) + \amp = 100 \E(XY) - 100 \E(X)\E(Y) + \amp = 100 \left(\E(XY) - \E(X)\E(Y)\right) + \amp = 100 \Cov(X, Y). + + So it seems that the magnitude of covariance is not necessarily + meaningful; it can be confused by the scale of the values taken by + the random variables. +

+ +

+ While we're at it, we might generalize the calculation above to see + exactly how covariance responds to scalar multiplication: + + \Cov(\alpha X, \beta Y) \amp = \E(\alpha X \cdot \beta Y) - \E(\alpha X)\E(\beta Y) + \amp = \alpha \beta \E(XY) - \alpha \beta \E(X)\E(Y) + \amp = \alpha \beta \left(\E(XY) - \E(X)\E(Y)\right) + \amp = \alpha \beta \Cov(X, Y). + +

+
+ + + +

+ Let X, Y be random variables with nonzero standard + deviations \sigma_X, \sigma_Y, respectively. + The correlation of X and Y is: + + \rho_{X, Y} \amp = \frac{\Cov(X, Y)}{\sigma_X \sigma_Y} = \frac{\Cov(X, Y)}{\sqrt{\sigma_X^2 \sigma_Y^2}} + +

+
+
+ +

+ We first observe that standard deviations are never negative, so the + sign of the correlation will always match the sign of the covariance. + It remains to be seen whether the magnitude of the correlation can be + usefully interpreted. +

+ + +

+ Let's see how the correlation responds to scaling the random + variables. + We know \Var(\alpha X) = \alpha^2 \Var(X) and + \Var(\beta Y) = \beta^2 \Var(Y), so: + + \rho_{\alpha X, \beta Y} \amp = \frac{\Cov(\alpha X, \beta Y)}{\sqrt{\Var(\alpha X)\Var(\beta Y)}} + \amp = \frac{\alpha \beta \Cov(X, Y)}{\sqrt{\alpha^2 \beta^2 \Var(X) \Var(Y)}} + \amp = \frac{\alpha \beta \Cov(X, Y)}{|\alpha \beta| \sqrt{\Var(X) \Var(Y)}} + \amp = \pm \rho_{X, Y}. + + Note the minus sign that could show up when you take the square root + of the square of a constant. + This makes perfect sense, conceptually: the sign information was + meant to tell us whether X and Y get bigger together, + or whether one gets bigger while the other gets smaller. + If we multiply only one of them by a negative constant, we flip its + values upside down, so it makes sense for the sign of both the + covariance and the correlation to switch in this case. + But the interesting thing here is the magnitude, which did + not change when the individual random variables were scaled. + This gives some hope that the magnitude of the correlation can be + meaningfully interpreted. +

+
+ + +

+ Consider the joint and marginal distributions below (from + ): +

+ + + + + <tabular halign="center"> + <row bottom="minor"> + <cell right="minor"/> + <cell right="minor"><m>X = 0</m></cell> + <cell><m>X = 1</m></cell> + </row> + + <row bottom="minor"> + <cell right="minor"><m>Y = 0</m></cell> + <cell right="minor">0.2</cell> + <cell>0.1</cell> + </row> + + <row> + <cell right="minor"><m>Y = 1</m></cell> + <cell right="minor">0.2</cell> + <cell>0.5</cell> + </row> + </tabular> + </table> + + <table> + <title/> + <tabular halign="center"> + <row bottom="minor"> + <cell><m>x</m></cell> + <cell><m>\Pr(X = x)</m></cell> + </row> + + <row> + <cell>0</cell> + <cell>0.4</cell> + </row> + + <row> + <cell>1</cell> + <cell>0.6</cell> + </row> + </tabular> + </table> + + <table> + <title/> + <tabular halign="center"> + <row bottom="minor"> + <cell><m>y</m></cell> + <cell><m>\Pr(Y = y)</m></cell> + </row> + + <row> + <cell>0</cell> + <cell>0.3</cell> + </row> + + <row> + <cell>1</cell> + <cell>0.7</cell> + </row> + </tabular> + </table> + </sidebyside> + + <p> + We previously calculated + <m>\E(X) = 0.6, \E(Y) = 0.7, \Cov(X, Y) = 0.08</m>, so + <m>\Var(X) = (0.6)(0.4) = 0.24</m> and + <m>\Var(Y) = (0.7)(0.3) = 0.21</m>, and: + <md> + <mrow> \rho_{X, Y} = \frac{\Cov(X, Y)}{\sqrt{\Var(X) \Var(Y)}} = \frac{0.08}{\sqrt{(0.24)(0.21)}} \approx 0.356. </mrow> + </md> + </p> + </example> + + <example> + <p> + What can we say if <m>X</m> and <m>Y</m> are linearly related? + Suppose <m>\E(X) = \mu</m> and <m>\Var(X) = \sigma^2</m>, and let + <m>Y = aX + b</m>. + Then: + <md> + <mrow> \E(Y) \amp = \E(aX + b) = a \E(X) + b = a \mu + b </mrow> + <mrow> \Var(Y) \amp = \Var(aX + b) = a^2\Var(X) = a^2\sigma^2 </mrow> + <mrow> \E(XY) \amp = \E(X(aX + b)) = \E(aX^2 + bX) = a \E(X^2) + b \mu </mrow> + </md> + Since <m>\Var(X) = \E(X^2) - (\E(X))^2</m>, we have + <m>\E(X^2) = \Var(X) + (\E(X))^2</m>. + Therefore: + <md> + <mrow> \E(XY) \amp = a(\sigma^2 + \mu^2) + b \mu = a\sigma^2 + a \mu^2 + b\mu </mrow> + </md> + Now we can calculate the covariance and correlation: + <md> + <mrow> \Cov(X, Y) \amp = \E(XY) - \E(X)\E(Y) </mrow> + <mrow> \amp = a \sigma^2 + a\mu^2 + b\mu - \mu(a\mu + b) </mrow> + <mrow> \amp = a \sigma^2 + a\mu^2 + b\mu - a\mu^2 - b\mu </mrow> + <mrow> \amp = a \sigma^2 </mrow> + <mrow> \rho_{X, Y} \amp = \frac{a \sigma^2}{\sqrt{\sigma^2 \cdot a^2\sigma^2}} </mrow> + <mrow> \amp = \frac{a \sigma^2}{|a| \sigma^2} </mrow> + <mrow> \amp = \pm 1. </mrow> + </md> + It is a fact (not justified here) that + <m>-1 \leq \rho_{X, Y} \leq 1</m> always. + So, the above calculation shows that random variables in a linear + relationship to one another are maximally correlated. + </p> + </example> + + <p> + In the next section, we will investigate the ability of the correlation + to capture linear relationships. + </p> + + <exercises xml:id="exercises-Correlation"> + <exercise> + <statement> + <p> + Calculate the correlation of <m>X</m> and <m>Y</m>: + </p> + + <table> + <title/> + <tabular halign="center"> + <row header="yes" bottom="minor"> + <cell right="minor"/> + <cell right="minor"><m>X = 1</m></cell> + <cell><m>X = 2</m></cell> + </row> + + <row bottom="minor"> + <cell right="minor"><m>Y = 0</m></cell> + <cell right="minor">0.12</cell> + <cell>0.24</cell> + </row> + + <row> + <cell right="minor"><m>Y = 1</m></cell> + <cell right="minor">0.3</cell> + <cell>34</cell> + </row> + </tabular> + </table> + </statement> + </exercise> + + <exercise> + <statement> + <p> + Calculate the correlation of <m>X</m> and <m>Y</m>: + </p> + + <table> + <title/> + <tabular halign="center"> + <row header="yes" bottom="minor"> + <cell right="minor"/> + <cell right="minor"><m>X = 0</m></cell> + <cell right="minor"><m>X = 1</m></cell> + <cell><m>X = 2</m></cell> + </row> + + <row bottom="minor"> + <cell right="minor"><m>Y = 0</m></cell> + <cell right="minor">0.08</cell> + <cell right="minor">0.16</cell> + <cell>0.1</cell> + </row> + + <row> + <cell right="minor"><m>Y = 1</m></cell> + <cell right="minor">0.14</cell> + <cell right="minor">0.2</cell> + <cell>0.32</cell> + </row> + </tabular> + </table> + </statement> + </exercise> + + <exercise> + <statement> + <p> + Suppose we roll a fair, 4-sided die two times. + Let <m>X</m> be the sum of the rolls, and let <m>Y</m> be + the product of the rolls. + Find the correlation of <m>X</m> and <m>Y</m>. + </p> + + <p> + [Note: Somewhat tedious.] + </p> + </statement> + </exercise> + </exercises> +</section> \ No newline at end of file diff --git a/source/sec-Covariance.ptx b/source/sec-Covariance.ptx index 2843a09..b1b38d4 100644 --- a/source/sec-Covariance.ptx +++ b/source/sec-Covariance.ptx @@ -52,7 +52,7 @@ The joint distribution lets you find <m>\E(XY)</m>. </p> - <example> + <example xml:id="example-joint-distribution"> <statement> <p> Let <m>X</m> and <m>Y</m> be indicator random variables with joint and marginal distributions below. diff --git a/source/sec-Linear-Regression.ptx b/source/sec-Linear-Regression.ptx new file mode 100644 index 0000000..8a8585c --- /dev/null +++ b/source/sec-Linear-Regression.ptx @@ -0,0 +1,168 @@ +<section xml:id="sec-Linear-Regression" xmlns:xi="http://www.w3.org/2001/XInclude"> + <title>Linear Regression + +

+ Text of section. +

+ + + + +

+ A survey of local companies collects information about marketing budgets X and revenue Y (each measures in thousands of dollars), shown below. + A linear regression gives the best linear fit as Y = 18.28 X + 29.69. + What is the coefficient of determination r^2? +

+ +
+ + + + + x_i + y_i + (y_i - \text{avg})^2 + \text{pred } y_i + \text{res}^2 + + + + 200 + 4300 + 2073600 + 3686 + 377377 + + + + 420 + 7700 + 3841600 + 7707 + 53 + + + + 270 + 4500 + 1537600 + 4965 + 216495 + + + + 380 + 7000 + 1587600 + 6976 + 572 + + + + 300 + 5200 + 291600 + 5514 + 98401 + + + + sum: + 28700 + 9332000 + 28848 + 692898 + + +
+ + + + + +

+ A sample of 100 measurements are taken and a best fit line is calculated, resulting in the data below (the final line shows the sums for each column). + Find the coefficient of determination. +

+ + + + + + + x_i + y_i + \text{pred } y_i + (y - \text{avg})^2 + \text{res}^2 + + + + 38.00 + 121.00 + 93.17 + 11.83 + 774.33 + + + + 87.00 + 241.00 + 238.95 + 13586.23 + 4.22 + + + + 30.00 + 61.00 + 69.37 + 4024.63 + 70.12 + + + + 35.00 + 85.00 + 84.25 + 1555.51 + 0.57 + + + + ⋮ + ⋮ + ⋮ + ⋮ + ⋮ + + + + 33.00 + 108.00 + 78.30 + 270.27 + 882.19 + + + + 26.00 + 32.00 + 57.47 + 8545.15 + 648.91 + + + + sum: + 12444.00 + 12444.00 + 515206.64 + 29789.80 + + +
+
+
+ +
\ No newline at end of file