So, Jacques starts up his JavaScript interpreter and sets up the environment he needs to keep his journal.

let journal = [];

function addEntry(events, squirrel) {

journal.push({events, squirrel});

^{}}

Note that the object added to the journal looks a little odd. Instead of declaring properties like events: events, it just gives a property name. This is shorthand that means the same thing—if a property name in brace notation isn’t followed by a value, its value is taken from the binding with the same name.

So then, every evening at 10 pm—or sometimes the next morning, after climbing down from the top shelf of his bookcase—Jacques records the day.

addEntry([“work”, “touched tree”, “pizza”, “running”, “television”], false);

addEntry([“work”, “ice cream”, “cauliflower”, “lasagna”, “touched tree”, “brushed teeth”], false);

addEntry([“weekend”, “cycling”, “break”, “peanuts”, “beer”], true);

Once he has enough data points, he intends to use statistics to find out which of these events may be related to the squirrelifications.

*Correlation* is a measure of dependence between statistical variables. A statistical variable is not quite the same as a programming variable. In statistics you typically have a set of *measurements*, and each variable is measured for every measurement. Correlation between variables is usually expressed as a value that ranges from -1 to 1. Zero correlation means the variables are not related. A correlation of 1 indicates that the two are perfectly related—if you know one, you also know the other. A correlation of -1 also means that the variables are perfectly related but that they are opposites—when one is true, the other is false.

To compute the measure of correlation between two Boolean variables, we can use the *phi coefficient* (ø). This is a formula whose input is a frequency table containing the number of times the different combinations of the variables were observed. The output of the formula is a number between -1 and 1 that describes the correlation.

We could take the event of eating pizza and put that in a frequency table like this, where each number indicates the amount of times that combination occurred in our measurements:

If we call that table n, we can compute 0 using the following formula:

(If at this point you’re putting the book down to focus on a terrible flashback to 10th-grade math class—hold on! I do not intend to torture you with endless pages of cryptic notation—it’s just this one formula for now. And even with this one, all we do is turn it into JavaScript.)

The notation n_{01} indicates the number of measurements where the first variable (squirrelness) is false (0) and the second variable (pizza) is true (1). In the pizza table, n_{01} is 9.

The value n_{1}^ refers to the sum of all measurements where the first variable is true, which is 5 in the example table. Likewise, n^_{0} refers to the sum of the measurements where the second variable is false.

So for the pizza table, the part above the division line (the dividend) would be 1 x 76 – 4 x 9 = 40, and the part below it (the divisor) would be the square root of 5 x 85 x 10 x 80, or √340000. This comes out to 0 ~ 0.069, which is tiny. Eating pizza does not appear to have influence on the transformations.

Source: Haverbeke Marijn (2018), *Eloquent JavaScript: A Modern Introduction to Programming*,

No Starch Press; 3rd edition.