Class: Benford
- Inherits:
-
Object
- Object
- Benford
- Defined in:
- lib/benford.rb
Instance Method Summary collapse
- #counts ⇒ Object
- #deviation ⇒ Object
- #distribution ⇒ Object
-
#law ⇒ Object
P(d) = log10(1 + 1/d).
- #load!(nums) ⇒ Object
- #numbers ⇒ Object
Instance Method Details
#counts ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/benford.rb', line 20 def counts return @digit_counts unless @digit_counts.nil? digit_counts = Hash.new(0) numbers.each do |v| digit_counts[v.first] += 1 end @digit_counts = digit_counts end |
#deviation ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/benford.rb', line 42 def deviation return @variants unless @variants.nil? variants = Hash.new(0.0) law.each do |digit, occurence| variants[digit] = distribution[digit] - occurence end @variants = variants end |
#distribution ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/benford.rb', line 29 def distribution return @dist unless @dist.nil? dist = Hash.new(0.0) counts.each do |k, v| dist[k] = v.to_f / numbers.count end @dist = dist end |
#law ⇒ Object
P(d) = log10(1 + 1/d)
13 14 15 16 17 18 |
# File 'lib/benford.rb', line 13 def law return @benford unless @benford.nil? benford = {} (1..9).each { |d| benford[d.to_s] = Math.log10( 1 + 1 / d.to_f) } @benford = benford end |
#load!(nums) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/benford.rb', line 3 def load!(nums) @numbers = [] nums.each do |num| num.gsub!(".", "") num.gsub!(",", "") @numbers << num.to_s if num.is_numeric? end end |
#numbers ⇒ Object
38 39 40 |
# File 'lib/benford.rb', line 38 def numbers @numbers end |