Skip to main content

5. 7 Summarizing with reduce

 Summarizing with reduce   Understanding Reduce Purpose of Reduce : reduce is a higher-order function used to compute a single value from an array. It's instrumental in operations like summing up numbers or finding an item in an array that meets a specific criterion (e.g., the script with the most characters). Operation : It works by repeatedly taking an element from the array and combining it with a current value until all elements have been processed. This process is akin to folding or reducing the array into a single value. 🎨: Think of reduce like making juice from oranges. You start with a bunch of oranges (the array), and then you squeeze them one by one into a jug (the single value). Your hands (the combining function) do the squeezing, and you might already have some juice in the jug to start with (the start value). 👩‍🎓: The Mechanics of reduce - The reduce function takes three arguments: the array to reduce, a combining function, and a starting value. The combin...

Chapter 4.9 The final analysis

 

The final analysis

 

 

🔍: The final section concludes the statistical analysis from "Eloquent JavaScript," where the goal is to determine which events correlate with Jacques' squirrel transformations.

👩‍🎓: Identifying Unique Events: The journalEvents function is designed to find every unique event that occurs in the journal:

  • It loops through each entry in the journal.
  • Then it loops through each event in an entry.
  • If the event is not already in the events array, it adds the event to this array.
  • The result is a list of all unique events in the journal.

🔬: Calculating Correlations: Once we have all the unique events, the script calculates the Phi Coefficient (ϕ) for each event to determine the correlation with the squirrel transformations:

  • It loops over each unique event and uses the phi function along with the tableFor function for each event.
  • The phi function returns a correlation value for each event, which is logged to the console.

🎨: Filtering Significant Correlations: Jacques filters the results to focus on significant correlations:

  • He is interested in correlations greater than 0.1 or less than -0.1, as these represent stronger relationships.
  • Events like "weekend" and "peanuts" show significant positive correlations, while "brushed teeth" shows a strong negative correlation.

💡: Investigating Strong Correlations: The strongest correlations are investigated further:

  • Jacques discovers that "peanut teeth," an event created when he eats peanuts and does not brush his teeth, has a perfect correlation (ϕ = 1) with his transformations.
  • This suggests a direct cause-and-effect relationship between this combined event and becoming a squirrel.

 

Code Explanation

Let's break down the journalEvents function step by step. This function is designed to find every unique type of event that occurs in the journal entries.


function journalEvents(journal) {
  let events = [];
  for (let entry of journal) {
    for (let event of entry.events) {
      if (!events.includes(event)) {
        events.push(event);
      }
    }
  }
  return events;
} 

Detailed Breakdown

  1. Function Declaration:

    • function journalEvents(journal) { ... }: Declares a function named journalEvents that takes one parameter, journal, which is expected to be an array of journal entries.
  2. Initializing an Array:

    • let events = [];: Creates an empty array named events. This array will store the unique events found in the journal.
  3. First Loop - Iterating Over Journal Entries:

    • for (let entry of journal) { ... }: A for...of loop that iterates over each entry in the journal. Each entry is expected to be an object that contains, among other things, an events array.
  4. Second Loop - Iterating Over Events in an Entry:

    • for (let event of entry.events) { ... }: Another for...of loop inside the first one. This loop iterates over each event in the current entry's events array.
  5. Checking for Unique Events:

    • if (!events.includes(event)) { ... }: This if statement checks whether the current event is already in the events array. The ! operator negates the result of events.includes(event), so the if block executes only if event is not found in events.
  6. Adding New Events:

    • events.push(event);: If the current event is not already in the events array, it gets added to events using the push method.
  7. Returning the Result:

    • return events;: After all journal entries have been processed, the events array, now containing all unique events, is returned.

Example Use Case

  • console.log(journalEvents(JOURNAL));: This line calls the journalEvents function with JOURNAL (an array of journal entries) and logs the result.
  • The output is an array of unique event strings, like ["carrot", "exercise", "weekend", "bread", …].

 Code Explanation


for (let event of journalEvents(JOURNAL)) {
  console.log(event + ":", phi(tableFor(event, JOURNAL)));
}
  1. Looping Over Events:

    • for (let event of journalEvents(JOURNAL)) { ... }
    • This is a for...of loop that iterates over each unique event returned by the journalEvents(JOURNAL) function.
    • journalEvents(JOURNAL) is a function call that returns an array of unique events from the JOURNAL.
    • let event creates a new variable event in each iteration of the loop, which holds the current event from the array.
  2. Calculating Correlation:

    • Inside the loop, phi(tableFor(event, JOURNAL)) is called for each event.
    • tableFor(event, JOURNAL) is a function call that creates a frequency table for the current event in relation to squirrel transformations (as recorded in JOURNAL). The table is structured in a way that it can be used to calculate the phi coefficient.
    • phi(...) is a function that calculates the phi coefficient (a measure of correlation) for the given frequency table.
  3. Logging the Results:

    • console.log(event + ":", phi(tableFor(event, JOURNAL)));
    • This statement logs the result to the console.
    • It prints the name of the event followed by the calculated phi coefficient, showing the correlation of each event with the transformations into a squirrel.

 Code Explanation

for (let event of journalEvents(JOURNAL)) {
  let correlation = phi(tableFor(event, JOURNAL));
  if (correlation > 0.1 || correlation < -0.1) {
    console.log(event + ":", correlation);
  }
} 
  1. Loop Over Each Event:

    • for (let event of journalEvents(JOURNAL)) { ... }
    • This loop iterates over each unique event found in the JOURNAL dataset.
    • journalEvents(JOURNAL) returns an array of all unique events in the JOURNAL.
  2. Calculate Correlation for Each Event:

    • let correlation = phi(tableFor(event, JOURNAL));
    • For each event, a frequency table is generated using tableFor(event, JOURNAL). This table outlines how often the event coincides with the transformation into a squirrel.
    • The phi function then calculates the correlation coefficient (phi coefficient) for the event based on this frequency table.
  3. Check for Significant Correlation:

    • if (correlation > 0.1 || correlation < -0.1) { ... }
    • The if statement checks if the absolute value of the correlation is greater than 0.1.
    • A correlation coefficient larger than 0.1 or smaller than -0.1 is considered significant in this context.
  4. Log Significant Events:

    • console.log(event + ":", correlation);
    • If the correlation is significant, the event name and its correlation coefficient are logged to the console.
    • This output helps identify which events have a strong positive or negative correlation with turning into a squirrel.

Example of What This Does

If JOURNAL is an array of journal entries where each entry records daily activities and whether or not the person turned into a squirrel, this code will find and display those activities that have a strong correlation with the transformations. For instance, if eating "pizza" has a correlation of 0.12 with becoming a squirrel, it will be logged to the console.

Conclusion

This code is a practical application of statistical analysis in JavaScript. It's used to analyze complex datasets to find meaningful patterns — in this case, identifying which events might have a significant impact on a specific outcome (squirrel transformations). This kind of analysis is crucial in fields like data science and behavioral studies.

 

Code Explanation

for (let entry of JOURNAL) {
  if (entry.events.includes("peanuts") &&
     !entry.events.includes("brushed teeth")) {
    entry.events.push("peanut teeth");
  }
}
console.log(phi(tableFor("peanut teeth", JOURNAL)));
// → 1 

ooping Over Each Entry

  • for (let entry of JOURNAL) { ... }
    • This loop iterates through each entry in the JOURNAL. Each entry is an object that represents a day's journal entry, including a list of events and whether or not the transformation into a squirrel occurred that day.

Conditional Check and Modifying Entries

  • if (entry.events.includes("peanuts") && !entry.events.includes("brushed teeth")) { ... }

    • This if statement checks two conditions for each journal entry:
      1. entry.events.includes("peanuts"): It checks whether the list of events for the day includes "peanuts".
      2. !entry.events.includes("brushed teeth"): It checks that the list of events does not include "brushed teeth". The ! operator negates the result, so this condition is true if "brushed teeth" is not in the list.
  • entry.events.push("peanut teeth");

    • If both conditions are true, "peanut teeth" is added to the events list for that day using the push method. This effectively creates a new event that signifies days when peanuts were eaten without brushing teeth.

Calculating Correlation

  • console.log(phi(tableFor("peanut teeth", JOURNAL)));
    • After modifying the journal entries, the script calculates the phi coefficient for the event "peanut teeth".
    • tableFor("peanut teeth", JOURNAL) generates a frequency table for the occurrence of "peanut teeth" relative to the squirrel transformations.
    • phi(...) calculates the phi coefficient, a statistical measure of correlation, using this frequency table.

Result and Interpretation

  • The result of console.log(phi(tableFor("peanut teeth", JOURNAL))); is 1.
  • A phi coefficient of 1 indicates a perfect positive correlation. This means that every time the event "peanut teeth" occurred (peanuts were eaten without brushing teeth), the transformation into a squirrel also occurred, without any exceptions. 


Link to the chapter in the book:
 

 

Comments