Permalink
Browse files

added some intermediate calculations for famsize

  • Loading branch information...
1 parent bf7cf72 commit 9ff9fb572c5c476803930e5037d7099ac3a6a269 @bentut committed Nov 16, 2012
@@ -3,8 +3,16 @@ def first_value_date
self.data.sort.each do |datestring, value|
return datestring unless value.nil?
end
+ return nil
end
-
+
+ def last_value_date
+ self.data.sort.reverse.each do |datestring, value|
+ return datestring unless value.nil?
+ end
+ return nil
+ end
+
def trim(start_date = get_last_incomplete_january_datestring, end_date = Time.now.to_date.to_s)
new_series_data = get_values_after_including(start_date, end_date)
new_transformation("Trimmed #{name} starting at #{start_date}", new_series_data)
@@ -4,6 +4,48 @@ def interpolate_to (frequency, operation, series_to_store_name)
series_to_store_name.ts= interpolate frequency,operation
end
+ def extend_first_data_point_back_to(date)
+ new_data = {}
+ first_data_point_date = self.first_value_date
+ first_data_point_val = data[first_data_point_date]
+
+ offset = 1 if self.frequency == "month"
+ offset = 3 if self.frequency == "quarter"
+ offset = 6 if self.frequency == "semi"
+ offset = 12 if self.frequency == "year"
+ new_date = Date.parse(first_data_point_date) << offset
+
+ while new_date.to_s >= date
+ new_data[new_date.to_s] = first_data_point_val
+ new_date = new_date << offset
+ end
+ new_series = new_transformation("Extended the first value back to #{date}", new_data)
+ new_series.frequency = self.frequency
+ new_series
+ end
+
+ def extend_last_date_to_match(series_name)
+ new_data = {}
+ last_data_point_date = series_name.ts.last_value_date
+ current_last_data_point = self.last_value_date
+
+ last_data_point_val = data[current_last_data_point]
+
+ offset = 1 if self.frequency == "month"
+ offset = 3 if self.frequency == "quarter"
+ offset = 6 if self.frequency == "semi"
+ offset = 12 if self.frequency == "year"
+ new_date = Date.parse(current_last_data_point) >> offset
+
+ while new_date.to_s <= last_data_point_date
+ new_data[new_date.to_s] = last_data_point_val
+ new_date = new_date >> offset
+ end
+ new_series = new_transformation("Extended the value value out to the last date of #{series_name}", new_data)
+ new_series.frequency = self.frequency
+ new_series
+ end
+
def fill_interpolate_to(target_frequency)
freq = self.frequency.to_s
new_series_data = {}
@@ -81,8 +123,6 @@ def pseudo_centered_spline_interpolation(frequency)
temp_series.frequency = self.frequency
series_data = temp_series.linear_interpolate(frequency).data
- temp_series.print
-
new_series = new_transformation("Pseudo Centered Spline Interpolation of #{self.name}", series_data)
new_series.frequency = frequency
new_series
View
@@ -1773,11 +1773,12 @@ task :bea_identities => :environment do
"YCE_R@US.M".ts_eval= %Q|"YCE_R@US.M".tsn.load_from("/Volumes/UHEROwork/data/rawdata/History/us_upd_m.xls").trim("1900-01-01","1994-12-01")|
"GDP_CN_R@US.A".ts_eval= %Q|"GDP_CN_R@US.A".tsn.load_from("/Volumes/UHEROwork/data/rawdata/History/us_upd_a.xls").trim("1900-01-01","1994-12-01")|
- #these are all a little wrong... history file?
["HI", "HON", "MAU", "KAU", "HAW"].each do |cnty|
- "YPC@#{cnty}.A".ts_eval = %Q|"YPCBEA@#{cnty}.A".ts|
+ "YPC@#{cnty}.A".ts_eval = %Q|"Y@#{cnty}.A".ts / "NR@#{cnty}.A".ts |
end
+ "YPC@HI.Q".ts_eval = %Q|"Y@HI.Q".ts / "NR@HI.Q".ts |
+
#wrong
# "YPC@HI.Q".ts_eval = %Q|"YPCBEA@HI.Q".ts|
"YPC_R@HI.Q".ts_eval = %Q|"YPC@HI.Q".ts / "CPI@HON.Q".ts|
@@ -1885,7 +1886,67 @@ task :bea_identities => :environment do
# # ("#{pre}#{type}_R&@#{cnty}.A".ts.find_units) rescue puts "_R ERROR FORM #{pre}#{type}_R&@#{cnty}"
# # end
# end
+ # @HON
+ #
+ # calculate famsize and sh_ypc
+
+
+ "FAMSIZE_TEMP@HI.A".ts_eval= %Q|"YMED@HI.A".ts / "YPC@HI.A".ts|
+ "FAMSIZE@HI.Q".ts_eval = %Q|"FAMSIZE_TEMP@HI.A".ts.pseudo_centered_spline_interpolation(:quarter)|
+
+ "FAMSIZE_TEMP@HON.A".ts_eval= %Q|"YMED@HON.A".ts / "YPC@HON.A".ts|
+ "SH_YPC_TEMP@HON.A".ts_eval= %Q|"YPC@HON.A".ts/"YPC@HI.A".ts|
+
+ "FAMSIZE@HON.Q".ts_eval = %Q|"FAMSIZE_TEMP@HON.A".ts.pseudo_centered_spline_interpolation(:quarter)|
+ "SH_YPC@HON.Q".ts_eval = %Q|"SH_YPC_TEMP@HON.A".ts.pseudo_centered_spline_interpolation(:quarter)|
+
+ "FAMSIZE2@HON.Q".ts_eval = %Q|"FAMSIZE@HON.Q".ts|
+ #may also want to change this to extend... or just extend on both sides...
+ "FAMSIZE2@HON.Q".ts_eval = %Q|"FAMSIZE2@HON.Q".ts.extend_first_data_point_back_to("1970-01-01")|
+ "FAMSIZE2@HON.Q".ts_eval = %Q|"FAMSIZE2@HON.Q".ts.extend_last_date_to_match("YPC@HI.Q")|
+ "SH_YPC@HON.Q".ts_eval = %Q|"SH_YPC@HON.Q".ts.extend_last_date_to_match("YPC@HI.Q")|
+ "PAFSGF@HON.Q".ts_eval = %Q| ("FAMSIZE2@HON.Q".ts * "SH_YPC@HON.Q".ts * "YPC@HI.Q".ts / "RMORT@US.Q".ts)*(("RMORT@US.Q".ts / 1200 + 1)**-360 * -1 + 1) * (300/8) |
+ # series HPMT@HON.Q = PMKRSGF@HON.Q*0.8*(RMORT@US.Q/1200)/(1-(1/(1+RMORT@US.Q/1200)**360))
+ # series HYQUAL@HON.Q = HPMT@HON.Q*10/3*12
+ # series HAI@HON.Q = (FAMSFAMSIZE2@HON.Q*SH_YPC@HON.Q*YPC@HI.Q)/HYQUAL@HON.Q*100
+ # series HPMTCON@HON.Q = PMKRCON@HON.Q*0.8*(RMORT@US.Q/1200)/(1-(1/(1+RMORT@US.Q/1200)**360))
+ # series HYQUALCON@HON.Q = HPMTCON@HON.Q*10/3*12
+ # series HAICON@HON.Q = (FAMSFAMSIZE2@HON.Q*SH_YPC@HON.Q*YPC@HI.Q)/HYQUALCON@HON.Q*100
+
+ #DELETE original famsize defs for circular reference
+
+
+ # Aggregate FAMSIZE@HON.A and SH_YPC@HON.A
+ # FAMSIZE@HON.A = average FAMSIZE@HON.Q
+ # aggregate using famsize, not famsize2!
+ # SH_YPC@HON.A = average SH_YPC@HON.Q
+ #
+ # @HI
+ #
+ # Calculate famsize
+ # FAMSIZE@HI.A = YMED@HI.A / YPC@HI.A
+ # Extend FAMSIZE@HI.A backwards
+ # We have YMED@HON.A going back to 1990 but YMED@HI.A only goes back to 1996, so we can only calculate FAMSIZE@HI.A after 1996
+ # The basic idea to take the ratio of FAMSIZE@HI.A/FAMSIZE@HON.A and apply that ratio to FAMSIZE@HON.A for 1990-1995 to make FAMSIZE@HI.A
+ # Calculate intermediate series DIFF.A = FAMSIZE@HI.A/FAMSIZE@HON.A
+ # Calculate the mean of DIFF.A = AVGDIFF (I think this is something like 1.017)
+ # Between 1990-1995 FAMSIZE@HI.A = FAMSIZE@HON.A * AVGDIFF
+ # Interpolate to quarterly
+ # FAMSIZE@HI.Q = FAMSIZE@HI.A interpolated with new method
+ # Extend FAMSIZE@HI.Q if necessary
+ # There might be a case where YPC@HI.A ends before YPC@HI.Q, so FAMSIZE@HI.Q will end before YPC@HI.Q
+ # If FAMSIZE@HI.Q ends before YPC@HI.Q, repeat the last value of FAMSIZE@HI.Q until YPC@HI.Q and FAMSIZE@HI.Q end in the same period
+ # Intermediate calculations finished! Calculate the following series by identity
+ # series PAFSGF@HI.A = (300/8)*(FAMSIZE@HI.A*YPC@HI.A/RMORT@US.Q)*(1-1/(1+RMORT@US.Q/1200)**360)
+ # series HPMT@HI.A = PMKRSGF@HI.A*0.8*(RMORT@US.Q/1200)/(1-(1/(1+RMORT@US.Q/1200)**360))
+ # series HYQUAL@HI.A = HPMT@HI.A*10/3*12
+ # series HAI@HI.A = (FAMSFAMSIZE@HI.A*YPC@HI.A)/HYQUAL@HI.A*100
+ # series HPMTCON@HI.A = PMKRCON@HI.A*0.8*(RMORT@US.Q/1200)/(1-(1/(1+RMORT@US.Q/1200)**360))
+ # series HYQUALCON@HI.A = HPMTCON@HI.A*10/3*12
+ # series HAICON@HI.A = (FAMSFAMSIZE@HI.A*YPC@HI.A)/HYQUALCON@HI.A*100
+ # Aggregate FAMSIZE@HI.Q
+ # FAMSIZE@HI.A = average FAMSIZE@HI.Q
CSV.open("public/rake_time.csv", "a") {|csv| csv << ["bea_identities", "%.2f" % (Time.now - t) , t.to_s, Time.now.to_s] }
View
@@ -542,5 +542,7 @@ task :const_identities => :environment do
end
+
+
CSV.open("public/rake_time.csv", "a") {|csv| csv << ["const_identities", "%.2f" % (Time.now - t) , t.to_s, Time.now.to_s] }
end
Oops, something went wrong.

0 comments on commit 9ff9fb5

Please sign in to comment.