I'm reading through the third edition of Eloquent JavaScript by Marijn Haverbeke. Formula 4.1 in Chapter 4 Data Structures: Objects and Arrays kind of comes out of nowhere, and I wanted to see if I could figure out why it works. If other readers find this, I'm curious if you found a derivation that's less convoluted than the one that follows.
ϕ=n1∙n0∙n∙1n∙0n11n00−n10n01
We watch Jacques one day, waiting for Jacques to perform an activity like 'touched tree.' Define two Bernoulli random variables S and A. S records if Jacques transforms into a squirrel, and A records if Jacques performs the activity of interest. The two random variables are not independent. Jacques may be more likely to transform into a squirrel if they touch a tree. Next, we can mechanically apply formulas from the class we took on introductory probability.
The little p's come from the probability mass function of the random vector (S,A). The first subscript is a 1 in the event that Jacques turns into a squirrel, and the second subscript is a 1 in the event that Jacques performs the given activity.
The expected value of the random variable SA is easy, because it is a new Bernoulli variable that is 1 only in the event that Jacques does the activity and turns into a squirrel.
The little n's are just the number of times we observe the particular outcome associated with the given probability over n days. If we substitute those equations into the equation for Corr(S,A) and ask Mathematica to simplify the horror that results we get