Module: StudFinder::Normalizer

Defined in:
lib/stud_finder/normalizer.rb

Class Method Summary collapse

Class Method Details

.lower_bound(sorted_values, raw) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stud_finder/normalizer.rb', line 22

def lower_bound(sorted_values, raw)
  low = 0
  high = sorted_values.length

  while low < high
    mid = (low + high) / 2
    if sorted_values[mid] < raw
      low = mid + 1
    else
      high = mid
    end
  end

  low
end

.percentile_rank(raw_counts, files) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stud_finder/normalizer.rb', line 7

def percentile_rank(raw_counts, files)
  return {} if files.empty?

  values = files.map { |file| raw_counts.fetch(file, 0).to_f }
  return files.to_h { |file| [file, 0.0] } if files.length == 1 || values.uniq.length == 1

  denominator = files.length - 1
  sorted_values = values.sort

  files.to_h do |file|
    raw = raw_counts.fetch(file, 0).to_f
    [file, lower_bound(sorted_values, raw).to_f / denominator]
  end
end