Skip to content

Commit

Permalink
added census interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
bentut committed Oct 5, 2012
1 parent e16e144 commit 8545ff3
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/series_interpolation.rb
@@ -1,4 +1,5 @@
module SeriesInterpolation

def interpolate_to (frequency, operation, series_to_store_name)
series_to_store_name.ts= interpolate frequency,operation
end
Expand All @@ -13,6 +14,32 @@ def fill_days_interpolation
new_series.frequency = "day"
new_series
end
def census_interpolate(frequency)
raise AggregationError if frequency != :quarter and self.frequency != "year"
quarterly_data = {}
last = nil
started_interpolation = false
data.sort.each do |key, value|
unless last.nil?
year = key.to_date.year
step = (value - last) / 4
quarterly_data["#{year-1}-10-01"] = value - 3 * step
quarterly_data["#{year}-01-01"] = value - 2 * step
quarterly_data["#{year}-04-01"] = value - 1 * step
quarterly_data["#{year}-07-01"] = value
unless started_interpolation
quarterly_data["#{year-1}-01-01"] = last - 2 * step
quarterly_data["#{year-1}-04-01"] = last - 1 * step
quarterly_data["#{year-1}-07-01"] = last
started_interpolation = true
end
end
last = value
end
new_series = new_transformation("Interpolated with Census method from #{self.name}", quarterly_data)
new_series.frequency = frequency
new_series
end

def interpolate(frequency, operation)
# puts "FREQUENCY: #{frequency} - #{frequency.class}"
Expand Down Expand Up @@ -43,7 +70,7 @@ def interpolate(frequency, operation)
quarterly_data[(Date.parse(last_date) >> 3).to_s] = last + interval/4
#quarterly_data
new_series = new_transformation("Interpolated from #{self.name}", quarterly_data)
new_series.frequency = frequency #may not want to do this... probably saving series unintentionally
new_series.frequency = frequency
new_series
end

Expand Down

0 comments on commit 8545ff3

Please sign in to comment.