Skip to content

Instantly share code, notes, and snippets.

View tdunning's full-sized avatar

Ted Dunning tdunning

View GitHub Profile
@henrygarner
henrygarner / llr.rb
Created February 1, 2011 12:04
A simple Ruby demonstration of Ted Dunning's log-likelihood statistical measure, via Paul Rayson http://ucrel.lancs.ac.uk/llwizard.html
require 'matrix'
module LLR
def self.calculate(m)
2 * (m.to_a.flatten.h - m.row_vectors.map(&:sum).h - m.column_vectors.map(&:sum).h)
end
def sum
to_a.inject(nil) { |sum, x| x = yield(x) if block_given?; sum ? sum + x : x }
end