Class: Quandl::Operation::Collapse::Guess

Inherits:
Object
  • Object
show all
Defined in:
lib/quandl/operation/collapse/guess.rb

Class Method Summary collapse

Class Method Details

.ensure_positive_gap(gap) ⇒ Object



75
76
77
78
79
# File 'lib/quandl/operation/collapse/guess.rb', line 75

def ensure_positive_gap(gap)
  gap = gap.to_i
  gap *= -1 if gap < 0
  gap
end

.find_average_gap(data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/quandl/operation/collapse/guess.rb', line 39

def find_average_gap(data)
  # init
  pdate = nil
  row_count = data.count
  majority_count = (row_count * 0.55).to_i
  gaps = {}
  # find the smallest gap
  data.each do |row|
    # this row's date
    date = row[0]
    # only if pdate is present
    if pdate
      # calculate the gap
      diff = (pdate - date).to_i
      # increment the gap counter
      gaps[diff] ||= 0
      gaps[diff] += 1
      # if the diff count is greater than majority_count, we have a consensus
      return diff if gaps[diff] > majority_count
    end
    # previous row's date
    pdate = date
  end
  gaps.to_a.sort_by { |r| r[1] }.try(:last).try(:first)
end

.find_smallest_gap(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/quandl/operation/collapse/guess.rb', line 16

def find_smallest_gap(data)
  # init
  gap = 100_000
  pdate = nil
  # find the smallest gap
  data.each do |row|
    # if the gap is 1, we're done
    break if gap <= 1
    # this row's date
    date = row[0]
    # only if pdate is present
    if pdate
      # calculate the gap
      diff = (pdate - date).to_i
      # replace the previous gap if it is smaller
      gap = diff if diff < gap
    end
    # previous row's date
    pdate = date
  end
  gap
end

.frequency(data) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/quandl/operation/collapse/guess.rb', line 6

def frequency(data)
  return :annual unless data && data[0] && data[0][0]
  # find the smallest point of difference between dates
  gap = find_average_gap(data)
  # ensure gap is not negative
  gap = ensure_positive_gap(gap)
  # determine the freq from the size of the smallest gap
  frequency_from_gap(gap)
end

.frequency_from_gap(gap) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/quandl/operation/collapse/guess.rb', line 65

def frequency_from_gap(gap)
  case
  when gap <= 1 then :daily
  when gap <= 10 then :weekly
  when gap <= 31 then :monthly
  when gap <= 93 then :quarterly
  else :annual
  end
end