The lycanthrope’s log
🔍: Here the auther continues the discussion of objects in JavaScript with an application to a problem of correlating events with transformations into a squirrel.
👩🎓: Breaking Down the JavaScript Code and the Concept of Correlation:
Journal Array and addEntry Function:
let journal = [];initializes an empty array that will store the journal entries.addEntry(events, squirrel)is a function that creates a new entry in the journal. Each entry is an object with propertieseventsandsquirrel.- The shorthand
{events, squirrel}in the function is ES6 property shorthand for{events: events, squirrel: squirrel}, where the property name is the same as the variable name.
Data Recording:
- Jacques logs his daily events and whether he turned into a squirrel using the
addEntryfunction.
- Jacques logs his daily events and whether he turned into a squirrel using the
Correlation and the Phi Coefficient (ϕ):
- Correlation measures the relationship between two variables. It can range from -1 (perfect inverse correlation), through 0 (no correlation), to 1 (perfect correlation).
- The Phi coefficient is a specific measure used for binary variables (true/false, yes/no) to determine the degree of association between them.
- To compute ϕ, a frequency table is used to count the occurrences of the variables' combinations.
📚: Understanding the Frequency Table and Phi Coefficient Calculation:
Frequency Table:
- The table counts occurrences of four combinations: when there is neither a squirrel transformation nor pizza (n00), when there's pizza but no squirrel (n01), when there's a squirrel but no pizza (n10), and when both a squirrel transformation and pizza occur (n11).
- The formula for ϕ given in the book is:
- This formula calculates the correlation between two binary variables.
Phi Coefficient Formula:
Application to the Pizza Table:
- Using the frequency table, we calculate the dividend as , which equals 40.
- The divisor is the square root of , which equals the square root of 340000.
- The resulting ϕ is approximately 0.069, indicating a very weak correlation between eating pizza and transforming into a squirrel.

where:
- is the frequency of both events occurring together (squirrel and pizza).
- is the frequency of neither event occurring.
- is the frequency of the first event occurring without the second (squirrel, no pizza).
- is the frequency of the second event occurring without the first (pizza, no squirrel).
- is the total number of times the first event occurred (squirrel).
- is the total number of times the first event did not occur.
- is the total number of times the second event occurred (pizza).
- is the total number of times the second event did not occur.
Breaking Down the Formula:
Numerator: The numerator calculates the product of the frequency of both variables occurring together and the frequency of neither occurring, and then subtracts the product of the frequencies of each occurring alone. This part of the formula assesses the degree of association between the two events.
Denominator: The denominator is the square root of the product of the totals of each event occurring and not occurring. This part normalizes the numerator, accounting for the different frequencies of each event, and ensures that the Phi Coefficient stays within the -1 to 1 range.
Calculating ϕ for the Weresquirrel Example:
Let's assign the frequencies based on the table you've provided:
- (Squirrel and pizza)
- (No squirrel, no pizza)
- (Squirrel, no pizza)
- (No squirrel, pizza)
- (Total squirrel)
- (Total no squirrel)
- (Total pizza)
- (Total no pizza)
Plugging these into the formula gives us:
Calculating the numerator:
Calculating the denominator:
Finally, we find ϕ by dividing the numerator by the denominator:
🔬: Interpreting ϕ:
- A ϕ of 0.069 indicates a very weak positive correlation between eating pizza and turning into a squirrel. In practical terms, it suggests that the occurrence of one does not reliably predict the occurrence of the other.
💡: Conclusion:
- The Phi Coefficient is a concise way to represent the relationship between two binary variables. While the formula might look intimidating, it's essentially comparing the occurrence of events to what would be expected if there were no relationship at all. The closer the value of ϕ is to 0, the weaker the relationship. Values near -1 or 1 indicate strong relationships, with -1 being a perfect inverse relationship and 1 being a perfect direct relationship.
Link to the chapter of the Book Eloquent Javascript

Comments
Post a Comment