I\'m currently reading Eloquent Javascript and so far it\'s been a good read, but I\'m stuck on this one function that he\'s put up, for calculating the phi-coeffcient. This
Try to visualize the process on each step, and substitute, like an equation:
┌──────────────┬──────────────┐ │ label: n00 │ label: n01 │ │ count: 76 │ count: 9 │ │ no squirrel, │ no squirrel, │ │ no pizza │ pizza │ ├──────────────┼──────────────┤ │ label: n10 │ label: n11 │ │ count: 4 │ count: 1 │ │ squirrel, │ squirrel, │ │ no pizza │ pizza │ └──────────────┴──────────────┘
table = [n00, n01, n10, n11]
n00 = table[0] = 76
n10 = table[1] = 4
n01 = table[2] = 9
n11 = table[3] = 1
n1• = n10 + n11 = table[2] + table[3] = 9 + 1 = 10
n0• = n00 + n01 = table[0] + table[1] = 76 + 4 = 80
n•1 = n01 + n11 = table[1] + table[3] = 4 + 1 = 5
n•0 = n00 + n10 = table[0] + table[2] = 76 + 9 = 85
//pseudo code
phi = function(table) {
return (n11 * n00 - n10 * n01) /
Math.sqrt(n1• * n0• * n•1 * n•0)
}
//JavaScript code
phi = function(table) {
return (table[3] * table[0] - table[2] * table[1]) /
Math.sqrt((table[2] + table[3]) *
(table[0] + table[1]) *
(table[1] + table[3]) *
(table[0] + table[2]))
}