JavaScript Data structures: Computing Correlation

We can represent a two-by-two table in JavaScript with a four-element array ([76, 9, 4, 1]). We could also use other representations, such as an array containing two two-element arrays ([[76, 9], [4, 1]]) or an object with property names like “11” and “01”, but the flat array is simple and makes the expressions that access the table pleasantly short. We’ll interpret the indices to the array as two-bit binary numbers, where the leftmost (most significant) digit refers to the squirrel variable and the rightmost (least significant) digit refers to the event variable. For example, the binary number 10 refers to the case where Jacques did turn into a squirrel, but the event (say, “pizza”) didn’t occur. This happened four times. And since binary 10 is 2 in decimal notation, we will store this number at index 2 of the array.

This is the function that computes the ø coefficient from such an array:

function phi(table) {

return (table[3] * table[0] – table[2] * table[l]) /

Math.sqrt((table[2] + table[3]) *

(table[0] + table[1]) *

(table[1] + table[3]) *

(table[0] + table[2]));


console.log(phi([76, 9, 4, 1]));

// → 0.068599434

This is a direct translation of the ø formula into JavaScript. Math.sqrt is the square root function, as provided by the Math object in a standard JavaScript environment. We have to add two fields from the table to get fields like «!• because the sums of rows or columns are not stored directly in our data structure.

Jacques kept his journal for three months. The resulting data set is avail­able in the coding sandbox for this chapter ( code#4), where it is stored in the JOURNAL binding and in a downloadable file.

To extract a two-by-two table for a specific event from the journal, we must loop over all the entries and tally how many times the event occurs in relation to squirrel transformations.

function tableFor(event, journal) {

let table = [0, 0, 0, 0]; for (let i = 0; i < journal.length; i++) {

let entry = journal[i], index = 0;

if ( index += 1;

if (entry.squirrel) index += 2; table[index] += 1;


return table;


console.log(tableFor(“pizza”, JOURNAL));

// → [76, 9, 4, 1]

Arrays have an includes method that checks whether a given value exists in the array. The function uses that to determine whether the event name it is interested in is part of the event list for a given day.

The body of the loop in tableFor figures out which box in the table each journal entry falls into by checking whether the entry contains the specific event it’s interested in and whether the event happens alongside a squirrel incident. The loop then adds one to the correct box in the table.

We now have the tools we need to compute individual correlations. The only step remaining is to find a correlation for every type of event that was recorded and see whether anything stands out.

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

No Starch Press; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *