The Lycanthrope’s Log

The Lycanthrope’s Log

This is a guide to understanding the lycanthrope exercise on eloquent javascript chapter 4

Table of contents

No heading

No headings in the article.

Jacques turns into a squirrel on certain days and he needed to keep track of his activity to help me better understand which activity or activities triggers his squirrel transformation.
To better understand this guide, you need to read it along with the eloquent javascript textbook. You can find it here

1. The dynamic data entry
To start off, Jacques needs to state all the activities he does in a day and record them for some time (say a month or two)

NOTE: if you declare an object property name without a value, it takes its value from the parameter that shares the same name. (ie. The squirrel object property will be matched with the value of the function parameter “squirrel”)

// console.log(journal) will give us something like this which is an array of objects
 [ 
    {
      squirrel: false,
      events: ["pizza","brushed teeth","work"]
    },
    {
      squirrel: true,
      events: ["pizza","peanuts","candy","work"]
    }
]

2. The Correlation

To get the events correlation, we need to follow the steps below.

  • the Phi coefficient

    Basically, we are using the phi coefficient where (you can find the formula from eloquent javascript chapter 4)
    For the numerator

    n00 = no squirrel, no event

    n01 = no squirrel, yes event

    n10 = yes squirrel, no event

    n11 = yes squirrel, yes event

    We will be using an array index for this and here’s how we do it

    [0, 1, 2, 3] ⇒ convert to binary, and if the number is not up to two values we will add 0 to the left of it.

    [00, 01, 10, 11] ⇒ this gives us a replica of what we have from n00,n11,n01,n00 etc.

    For the denominator

    n1. = sum of all places where the squirrel is true

    n0. = sum of all places where the squirrel is false

    n.1 = sum of all places where the event occurred

    n.0 = sum of all places where the event did not occur

  • So let’s say we have the correlation table for work is ⇒[76, 9, 4, 1 ]
    Analyzing it:

    n00 = 76

    n01 = 9

    n10 = 4

    n11 = 1
    n1. = 5 ⇒ (4 + 1)

    n0. = 85 ⇒(76 + 9)

    n.1 = 10 ⇒ (9 + 1)

    n.0 = 80 ⇒(76 + 4)

  • the dynamic correlation table

    Now we have a clue of what each array index denotes, we can now go ahead to find the number of occurrences for a particular event (say “work” will look like).
    Basically, I want to loop through the journal array and search for the number of times a particular event happened and if the squirrel happened or not alongside the event.

      console.log(tableFor("work", journal)) ⇒ [4, 3, 1, 2]
      // which means:-
      // The event “work” has no squirrel and no work occurrence ⇒ 4
      // has  no squirrel and a work occurrence ⇒ 3
      // has  a squirrel and no work occurrence ⇒ 1
      // has  a squirrel and a work occurrence ⇒ 2
    
  • The phi function

    Using the correlation array we got in the previous step, we can now write a function using the phi formula to help us get the phi coefficient.

      console.log(phi([4, 3, 1, 2]))
      =>  0.2182178902359924
      // this means the chance of turning to a werewolf on "work" activity is low.
    
  • Get all the activities done

    To get the correlation table for all the activities done, we need to know all the different activities in the journal.

      // logging to the console we will get a list of all the activities done
      console.log(eventsInJournal(journal))
      [
          'work', 'touched tree', 'pizza', 'running','television', 
          'ice cream', 'cauliflower', 'lasagna','weekend'......
      ]
    
  • dynamic correlation table for all events.
    Now that we have a list of all activities/events in the journal, we can now go ahead to get a correlation table for each event.

      bread :  -0.21821789023599236
      pudding :  -0.21821789023599236
      brushed teeth :  -0.8017837257372732
      weekend :  -0.2182178902359924
       touched tree :  -0.42857142857142855
       lasagna :  0.2182178902359924
       peanuts :  1
       work :  0.2182178902359924
       carrot :  -0.42857142857142855
       nachos :  -0.21821789023599236
       cycling :  -0.3273268353539886
       pizza :  0.2182178902359924
       candy :  0.5091750772173156
       brussel sprouts :  -0.21821789023599236
       ice cream :  -0.42857142857142855
      ......... and others.
    

    From here we can see the particular activity that actually turns him into a squirrel.

  • filtering the result to get the least and the highest correlation value

      brushed teeth :  -0.8017837257372732
      peanuts :  1
      candy :  0.5091750772173156
      spaghetti :  0.5091750772173156
    

    This shows that eating peanuts causes him to transform into a squirrel while brushing his teeth has the least chance of turning him into a squirrel.

I hope this guide helps you in understanding the lycanthrope's exercise, for more questions, do use the comment session.