--- created_at: '2015-11-03T10:10:23.000Z' title: Cosines and correlation (2010) url: http://www.johndcook.com/blog/2010/06/17/covariance-and-law-of-cosines/ author: ColinWright points: 49 story_text: comment_text: num_comments: 17 story_id: story_title: story_url: parent_id: created_at_i: 1446545423 _tags: - story - author_ColinWright - story_10498549 objectID: '10498549' year: 2010 --- [Source](https://www.johndcook.com/blog/2010/06/17/covariance-and-law-of-cosines/ "Permalink to Cosines and correlation") # Cosines and correlation [ ![John D. Cook][1] ][2] [Skip to content][3] * [ABOUT][4] * [SERVICES][5] * [APPLIED MATH][6] * [STATISTICS][7] * [COMPUTATION][8] * [WRITING][9] * [BLOG][2] * [TECHNICAL NOTES][10] * [JOURNAL ARTICLES][11] * [TWITTER][12] * [PRESENTATIONS][13] * [NEWSLETTER][14] * [CLIENTS][15] * [ENDORSEMENTS][16] [(832) 422-8646][17] [Contact][18] # Cosines and correlation Posted on [17 June 2010][19] by [John][20] ## Preview This post will explain a connection between probability and geometry. Standard deviations for independent random variables add according to the Pythagorean theorem. Standard deviations for correlated random variables add like the law of cosines. This is because correlation is a cosine. ## Independent variables First, let’s start with two independent random variables _X_ and _Y_. Then the standard deviations of _X_ and _Y_ add like sides of a right triangle. ![diagram][21] In the diagram above, “sd” stands for standard deviation, the square root of variance. The diagram is correct because the formula Var(_X_+_Y_) = Var(_X_) + Var(_Y_) is analogous to the Pythagorean theorem _c_2 = _a_2 \+ _b_2. ## Dependent variables Next we drop the assumption of independence. If _X_ and _Y_ are correlated, the variance formula is analogous to the law of cosines. ![diagram][22] The generalization of the previous variance formula to allow for dependent variables is Var(_X_+_Y_) = Var(_X_) + Var(_Y_) + 2 Cov(_X_, _Y_). Here Cov(_X_,_Y_) is the covariance of _X_ and _Y_. The analogous law of cosines is _c_2 = _a_2 \+ _b_2 – 2 _a b_ cos(θ). If we let _a_, _b_, and _c_ be the standard deviations of _X_, _Y_, and _X_+_Y_ respectively, then cos(θ) = -ρ where ρ is the correlation between _X_ and _Y_ defined by ρ(_X_, _Y_) = Cov(_X_, _Y_) / sd(_X_) sd(_Y_). When θ is π/2 (i.e. 90°) the random variables are independent. When θ is larger, the variables are positively correlated. When θ is smaller, the variables are negatively correlated. Said another way, as θ increases from 0 to π (i.e. 180°), the correlation increases from -1 to 1. The analogy above is a little awkward, however, because of the minus sign. Let’s rephrase it in terms of the supplementary angle φ = π – θ. Slide the line representing the standard deviation of Y over to the left end of the horizontal line representing the standard deviation of X. ![diagram][23] Now cos(φ) = ρ = correlation(_X_, _Y_). When φ is small, the two line segments are pointing in nearly the same direction and the random variables are highly positively correlated. If φ is large, near π, the two line segments are pointing in nearly opposite directions and the random variables are highly negatively correlated. ## Connection explained Now let’s see the source of the connection between correlation and the law of cosines. Suppose _X_ and _Y_ have mean 0. Think of _X_ and _Y_ as members of an inner product space where the inner product <_X_, _Y_> is E(_XY_). Then <_X_+_Y_, _X_+_Y_> = < _X_, _X_> \+ < _Y_, _Y_> \+ 2<_X_, _Y_ >. In an inner product space, <_X_, _Y_ > = || _X_ || || _Y_ || cos φ where the norm || _X_ || of a vector is the square root of the vector’s inner product with itself. The above equation _defines_ the angle φ between two vectors. You could justify this definition by seeing that it agrees with ordinary plane geometry in the plane containing the three vectors _X_, _Y_, and _X_+_Y_. * * * For daily posts on probability, follow [@ProbFact][24] on Twitter. ![ProbFact twitter icon][25] Categories : [Math][26] [Statistics][27] Tags : [Math][28] [Probability and Statistics][29] Bookmark the [permalink][30] # Post navigation Previous Post[Why computers have two zeros: +0 and -0][31] Next Post[Porting Python to C#][32] ## 19 thoughts on “Cosines and correlation” 1. [Mike Anderson][33] [ 17 June 2010 at 06:11 ][34] Nice write-up. I sneak this into my lectures occasionally to perk up the math majors, who don’t always notice the many interesting mathematical objects that appear in statistics. ( The denominator for correlation, sd(X)sd(Y), is the geometric mean of the two variances–what’s THAT all about, guys? ) 2. [Maria Droujkova][35] [ 17 June 2010 at 06:37 ][36] Nice! It would only take a current events example to make a cool enrichment activity. Thank you! 3. Ger Hobbelt [ 17 June 2010 at 07:30 ][37] Thank you for this; showing up right when I needed it! Alas, it would have been extra great if my teachers had pointed out this little bit of intel about 25 years ago, while they got me loathing those ever-resurfacing bloody dice even more. Meanwhile, I’ve shown to be dumb enough not to recognize this ‘correlation’ with lovely goniometrics on my own. Despite all that I’ve found the increasing need for understanding statistics (as you work on/with statistical classifiers and you feel the need to really ‘get’ those s.o.b.s for only then do you have a chance at reasoning why they fail on you the way they do) and your piece just made a bit of my brain drop a quarter — I’m Dutch; comprehension is so precious around here we are willing to part with a quarter instead of only a penny 😉 4. Pingback: Relación entre la ley de cosenos y correlación de variables « Bitácoras en Estadística 5. Harry Hendon [ 21 June 2010 at 19:55 ][38] Hi John, maybe you can help on a related problem, which I think uses the law of cosines as well (but I lost my derivation): If you know the two correlations of one time series with two other predictor time series, what does this tell you about the possible range of correlation between the two predictor time series. That is, given r(X,Y)=a and r(X,Z)=b, what is the possible range of r(Y,Z)=c in terms of a and b? Of course some examples are intuitively trivial (eg, if a=b=1, then c=1, and if a=0 but b=1 then c=0). But, consider if a=b=.7 (which are strong correlations), then I think the possible range of c is still enormous (0.