Class: Quandl::Operation::Collapse::Guess
- Inherits:
-
Object
- Object
- Quandl::Operation::Collapse::Guess
- Defined in:
- lib/quandl/operation/collapse/guess.rb
Class Method Summary collapse
- .ensure_positive_gap(gap) ⇒ Object
- .find_average_gap(data) ⇒ Object
- .find_smallest_gap(data) ⇒ Object
- .frequency(data) ⇒ Object
- .frequency_from_gap(gap) ⇒ Object
Class Method Details
.ensure_positive_gap(gap) ⇒ Object
79 80 81 82 83 |
# File 'lib/quandl/operation/collapse/guess.rb', line 79 def ensure_positive_gap(gap) gap = gap.to_i gap = gap * -1 if gap < 0 gap end |
.find_average_gap(data) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/quandl/operation/collapse/guess.rb', line 41 def find_average_gap(data) # init gap = 100_000 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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/quandl/operation/collapse/guess.rb', line 18 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
8 9 10 11 12 13 14 15 16 |
# File 'lib/quandl/operation/collapse/guess.rb', line 8 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
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/quandl/operation/collapse/guess.rb', line 68 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 |