Created
September 11, 2015 06:54
-
-
Save jamiecook/b923e1457339a0339c7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AverageWageImporter | |
def setup_demog_cube | |
zonal_data = ZonalData.new | |
zonal_data.append_field(:avg_income) unless zonal_data.has_field?(:avg_income) | |
end | |
def load_avg_income_by_category | |
params = ZenithYamlParameters.hash_from_file('economics.yaml') | |
params.fetch('labour_supply_parameters'). | |
map_values { |_ind_cat, params| params.fetch('avg_weekly_wage').to_f }. | |
map_keys { |ind_cat| ind_cat.gsub(/emp_/, '').to_sym } | |
end | |
def import | |
avg_income_by_category = load_avg_income_by_category | |
industry_categories = [:agric, :ming, :manuf, :egw, :cons, :wh, :ret, :recps, :trst, :commc, :finbus, :pubad, :cserv] | |
industry_emp_by_zone = load_employment_by_zone([:total] + industry_categories) | |
avg_income_by_zone = calculate_avg_wage_by_zone(avg_income_by_category, industry_categories, industry_emp_by_zone) | |
save_avg_income_to_demog_cube(avg_income_by_zone) | |
end | |
def save_avg_income_to_demog_cube(avg_income_by_zone) | |
zonal_data = ZonalData.new | |
zonal_data['avg_income'] = OtVector.new(avg_income_by_zone) | |
end | |
def calculate_avg_wage_by_zone(avg_income_by_category, industry_categories, industry_emp_by_zone) | |
industry_emp_by_zone.map { |emp_by_industry_for_zone| | |
total_emp = emp_by_industry_for_zone.fetch(:total) | |
industry_weights = industry_categories.as_index_for { |ind_cat| | |
emp_by_industry_for_zone.fetch(ind_cat).fdiv(total_emp) | |
} | |
industry_weights.sum { |ind_cat, weight| weight * avg_income_by_category.fetch(ind_cat) } | |
} | |
end | |
def load_employment_by_zone(industry_categories) | |
demog_cube = ZonalData.new | |
emp_by_cat = industry_categories.as_index_for { |cat| demog_cube.zonalData("emp_#{cat}") } | |
(1..$Ot.network.num_centroids).map { |zone_nr| | |
industry_categories.as_index_for { |ind_cat| | |
emp_by_cat.fetch(ind_cat)[zone_nr] | |
} | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment