def get_A(self): A = NamedMatrix(square=True, rows=self.get_sectors()) if self.harmonized_rows is not None: A.set_harmonized_rows(self.harmonized_rows) stmt = self.generate_A_stmt() result = stmt(*self.condition_args) for row in result: A.set_element(row[0], row[1], row[2]) # from_sec, to_sec, value return A
def get_env_vector(self, series): if type(series) is str: series = [series] colname = "+".join(series) E = NamedMatrix(square=False, rows=self.get_sectors(), cols=[colname]) stmt = self.generate_env_stmt(series) result = stmt(*self.condition_args) for row in result: E.set_element(row[0], colname, row[1]) return E
def get_env_selector(self, maptable, env_col, other_col, columns, conditions=[]): sel = NamedMatrix(square=False) sel.set_columns(columns) sel.set_rows(self.get_sectors()) stmt = self.generate_map_stmt(maptable, env_col, other_col, conditions) result = stmt() for row in result: if sel.has_row(row[0]) and sel.has_column(row[1]): sel.set_element(row[0], row[1], 1) return sel
def get_wiod_trade_vector(from_country, to_country): yearstrings = [str(year) for year in config.STUDY_YEARS] vector = NamedMatrix(rows=yearstrings, cols=["value"]) strings = {"schema": config.WIOD_SCHEMA} for year in yearstrings: strings["year"] = year stmt = db.prepare("""SELECT sum(value) FROM %(schema)s.int_use_%(year)s WHERE from_country = $1 and to_country = $2""" % strings) result = stmt(from_country, to_country) if len(result) and len(result[0]): vector.set_element(year, "value", result[0][0]) return vector
def pce_bridge_vector(year, pcegroup=None): view = "%s.nipa_pce_%d" % (config.IO_SCHEMA, year) iogen = iogen_for_year(year, True, True, True) # just to get sectors vector = NamedMatrix(False, rows=iogen.get_sectors(), cols=["pce"]) if pcegroup is None: # get total stmt = db.prepare( "SELECT commodity, sum(value) FROM %s GROUP BY commodity" % view) result = stmt() else: stmt = db.prepare( "SELECT commodity, value FROM %s WHERE pcegroup = $1" % view) result = stmt(pcegroup) for row in result: vector.set_element(row[0], "pce", row[1]) return vector
def get_wiod_env_vector(country, year, env_series): strings = { "year": year, "schema": config.WIOD_SCHEMA, "blacklist": sqlhelper.set_repr(config.env_sector_blacklist_hh), "measurements": sqlhelper.set_repr(env_series), } vector = NamedMatrix(rows=common.env_sectors_with_hh, cols=["value"]) stmt = db.prepare("""SELECT industry, sum(value) FROM %(schema)s.env_%(year)d WHERE country = $1 AND measurement IN %(measurements)s AND industry NOT IN %(blacklist)s GROUP BY industry""" % strings) result = stmt(base_country) for row in result: if row[1] is not None: vector.set_element(row[0], "value", row[1]) return vector
##### pce category breakdown xn_cons = xn.add(import_n).subtract(export_n) imp_to_cons = import_n.divide(xn_cons) # remove scrap and used/secondhand goods colname = imp_to_cons.get_columns()[0] for sector in bea.scrap_used_codes[year]: imp_to_cons.set_element(sector, colname, 0) # create matrix to collapse split petroleum sectors when multiplying # by the import ratio rows = iogen_standard.get_sectors() cols = iogen.get_sectors() pa_collapser = NamedMatrix(False, None, rows, cols) for col in cols: if col in rows: pa_collapser.set_element(col, col, 1) elif col == eia.source_naics_map["PA-trans"][year] or \ col == eia.source_naics_map["PA-nontrans"][year]: pa_collapser.set_element(eia.source_naics_map["PA"][year], col, 1) conven_pce = common.pce_bridge_vector(year) # all in dollars conversions = {} # get btu per dollar which we will use to convert subvectors for code in energy_codes: conversions[code] = hybrid_pce.get_element(code) / \ conven_pce.get_element(code) for group in bea.nipa_groups: Y = common.pce_bridge_vector(year, group)
def trade_ratios(base_country, env_key): env_series = config.env_series_names[env_key] other_countries = sorted(config.countries.keys()) other_countries.remove(base_country) yearstrings = [str(y) for y in config.STUDY_YEARS] exported_y = NamedMatrix(rows=other_countries, cols=yearstrings) exported_E = NamedMatrix(rows=other_countries, cols=yearstrings) def describe_exporters(): print("Exports by country - " + env_key) print("dollars") for country in other_countries: formatted = [("%.0f" % exported_y.get_element(country, year)).rjust(6) for year in yearstrings] print(country, " ".join(formatted)) print("emissions") for country in other_countries: formatted = [("%.0f" % exported_E.get_element(country, year)).rjust(6) for year in yearstrings] print(country, " ".join(formatted)) print("intensities") intensities = exported_E.divide(exported_y) for country in other_countries: formatted = [("%.2f" % intensities.get_element(country, year)).rjust(6) for year in yearstrings] print(country, " ".join(formatted)) ### prepare plots env_title = env_key.replace(" ", "-") plot_group = "import-%s-%s" % (env_title, base_country) plots = {} for sector in common.default_env_sectors: sector_title = common.get_industry_title(sector) plots[sector] = GNUPlot("%s_%s" % (env_title, sector), "", #"Imports of %s - %s" % (sector_title, env_key), plot_group) plots["import"] = GNUPlot("%s_import" % env_title, "", #"All imports - %s" % env_key, plot_group) plots["export"] = GNUPlot("%s_export" % env_title, "", #"All imports - %s" % env_key, plot_group) def create_plots(): for (sector, plot) in plots.items(): plot.set_series_style( import_dollar_series, "pointtype 5 linetype rgb '#88BBFF'") # hollow circle plot.set_series_style( emission_series, "pointtype 7 linetype rgb '#1144FF'") # solid circle plot.set_series_style( self_emission_series, "pointtype 12 linetype rgb '#0000FF'") plot.set_series_style( export_dollar_series, "pointtype 4 linetype rgb '#CC9933'") plot.set_series_style( export_emissions_series, "pointtype 7 linetype rgb '#996600'") #plot.legend("width -8") plot.width = 480 plot.height = 300 plot.write_tables() plot.generate_plot() ### trade balance matrices export_balance = NamedMatrix(rows=common.default_env_sectors, cols=yearstrings) import_balance = NamedMatrix(rows=common.default_env_sectors, cols=yearstrings) all_E = NamedMatrix(rows=common.default_env_sectors, cols=yearstrings) def describe_balance(ratios=False): export_total = export_balance.sum(0) import_total = import_balance.sum(0) all_total = all_E.sum(0) balance = export_total.subtract(import_total) country_name = config.countries[base_country] oddyears = [str(y) for y in filter(lambda x: x % 2 == 1, config.STUDY_YEARS)] if ratios: balance_ratio = balance.divide(all_total) print(country_name.ljust(15) + " & " + " & ".join( [("%.2f" % balance_ratio.get_element("sum", y)).rjust(6) for y in oddyears]) + " \\\\") else: print(country_name.ljust(15) + " & " + " & ".join( [utils.add_commas(balance.get_element("sum", y)).rjust(9) for y in oddyears]) + " \\\\") def describe_balance_intensity(): export_total = export_balance.sum(0) import_total = import_balance.sum(0) all_total = all_E.sum(0) balance = export_total.subtract(import_total) balance_ratio = balance.divide(all_total) country_name = config.countries[base_country] years = [str(minyear), str(maxyear)] fields = [] for y in years: balance_val = balance.get_element("sum", y) ratio_val = balance_ratio.get_element("sum", y) (gdp_val, env_val, intensity) = \ common.get_efficiency(base_country, int(y)) if int(y) in balance_plots: plot = balance_plots[int(y)] # ratio = exports to imports plot.set_value("2 ratio", base_country, ratio_val) plot.set_value("1 intensity", base_country, intensity) if int(y) in worldmap: worldmap[int(y)].set_country_value(base_country, balance_val) fields.append(utils.add_commas(balance_val)) fields.append("%.2f" % ratio_val) fields.append("%.2f" % intensity) print(country_name.ljust(15) + " & " + " & ".join(fields) + " \\NN") ### iogen = common.iogen_for_year(config.STUDY_YEARS[0]) envgen = common.envgen_for_year(config.STUDY_YEARS[0]) strings = {"schema": config.WIOD_SCHEMA} for year in config.STUDY_YEARS: strings["year"] = year base_E = get_wiod_env_vector(base_country, year, env_series) all_E.set_column(str(year), base_E) base_E_sum = base_E.sum() iogen.set_table("%(schema)s.indbyind_%(year)d" % strings) iogen.set_condition_args(base_country) envgen.set_table("%(schema)s.env_%(year)d" % strings) envgen.set_condition_args(base_country) envgen.set_sectors(common.default_env_sectors) # prepare base country values y = iogen.get_Y() import_column = common.get_import_vector(iogen) base_imports = import_column.sum() base_gdp = y.sum() harmonizer = common.get_io_harmonizer(iogen) base_y = harmonizer.matrix_mult(y.get_total()) x = iogen.get_x() E = envgen.get_env_vector(env_series) L = iogen.get_L() J = E.divide(harmonizer.matrix_mult(x), ignore_zero_denom=True) base_JsL = J.square_matrix_from_diag()\ .matrix_mult(harmonizer).matrix_mult(L) exported = base_JsL.matrix_mult(y.get_exports()) export_balance.set_column(str(year), exported) plots["export"].set_value( export_emissions_series, year, exported.sum() / base_E_sum) plots["export"].set_value( export_dollar_series, year, y.get_exports().sum() / base_gdp) # prepare other country values stmt = db.prepare("""SELECT from_sector, sum(value) FROM wiod_plus.%s_io_import_%d WHERE country = $1 AND from_sector NOT IN ('ITM', 'IMP', 'Rex') GROUP BY from_sector""" % (base_country.lower(), year)) imported_E = None imported_y = None domestic_E = None # emissions under domestic technology assumption for country in other_countries: envgen.set_condition_args(country) envgen.set_sectors(common.default_env_sectors) iogen.set_condition_args(country) sectors = iogen.get_sectors() # number of sectors varies by country E = envgen.get_env_vector(env_series) if E.mat() is None: # this country has no data for env series continue imports = NamedMatrix(rows=sectors, cols=["imports"]) db_result = stmt(country) if not len(db_result): # we can't do any of the following with a blank import vector continue for row in db_result: imports.set_element(row[0], "imports", row[1]) sel = common.get_io_harmonizer(iogen) x = iogen.get_x() L = iogen.get_L() J = E.divide(sel.matrix_mult(x), ignore_zero_denom=True) JsL = J.square_matrix_from_diag().matrix_mult(sel).matrix_mult(L) current_E = JsL.matrix_mult(imports) current_y = sel.matrix_mult(imports) # temporary dumb way to deal with sector mismatch compat_imports = NamedMatrix(rows=base_JsL.get_columns(), cols=["imports"]) for col in base_JsL.get_columns(): if col in sectors: compat_imports.set_element( col, "imports", imports.get_element(col)) current_domestic_E = base_JsL.matrix_mult(compat_imports) imported_E = sum_if_not_none(imported_E, current_E) imported_y = sum_if_not_none(imported_y, current_y) domestic_E = sum_if_not_none(domestic_E, current_domestic_E) exported_y.set_element(country, str(year), imports.sum()) exported_E.set_element(country, str(year), current_E.sum()) # populate results table TradeResultsTable.insert_exports(year, base_country, exported) TradeResultsTable.insert_imports(year, base_country, imported_E) # generate import plots for sector in common.default_env_sectors: base_y_val = base_y.get_element(sector) if base_y_val > 0: plots[sector].set_value( import_dollar_series, year, imported_y.get_element(sector) / base_y_val) base_E_val = base_E.get_element(sector) if base_E_val > 0: plots[sector].set_value( emission_series, year, imported_E.get_element(sector) / base_E_val) plots[sector].set_value( self_emission_series, year, domestic_E.get_element(sector) / base_E_val) plots["import"].set_value(import_dollar_series, year, imported_y.sum() / base_y.sum()) plots["import"].set_value(emission_series, year, imported_E.sum() / base_E_sum) plots["import"].set_value(self_emission_series, year, domestic_E.sum() / base_E_sum) if year in dta_plots and base_country in dta_countries: dta_plots[year].set_value("DTA", config.countries[base_country], imported_E.sum() / base_E_sum) dta_plots[year].set_value("No DTA", config.countries[base_country], domestic_E.sum() / base_E_sum) # this is for DTA vs non-DTA table #print(base_country, year, # imported_E.sum(), # domestic_E.sum(), # imported_E.sum() / base_E_sum, # domestic_E.sum() / base_E_sum) plots["export"].set_value(emission_series, year, imported_E.sum() / base_E_sum) import_balance.set_column(str(year), imported_E) #describe_exporters() #create_plots() #describe_balance(True) describe_balance_intensity()
def trade_ratios(base_country, env_key): env_series = config.env_series_names[env_key] other_countries = sorted(config.countries.keys()) other_countries.remove(base_country) yearstrings = [str(y) for y in config.STUDY_YEARS] exported_y = NamedMatrix(rows=other_countries, cols=yearstrings) exported_E = NamedMatrix(rows=other_countries, cols=yearstrings) def describe_exporters(): print("Exports by country - " + env_key) print("dollars") for country in other_countries: formatted = [ ("%.0f" % exported_y.get_element(country, year)).rjust(6) for year in yearstrings ] print(country, " ".join(formatted)) print("emissions") for country in other_countries: formatted = [ ("%.0f" % exported_E.get_element(country, year)).rjust(6) for year in yearstrings ] print(country, " ".join(formatted)) print("intensities") intensities = exported_E.divide(exported_y) for country in other_countries: formatted = [ ("%.2f" % intensities.get_element(country, year)).rjust(6) for year in yearstrings ] print(country, " ".join(formatted)) ### prepare plots env_title = env_key.replace(" ", "-") plot_group = "import-%s-%s" % (env_title, base_country) plots = {} for sector in common.default_env_sectors: sector_title = common.get_industry_title(sector) plots[sector] = GNUPlot( "%s_%s" % (env_title, sector), "", #"Imports of %s - %s" % (sector_title, env_key), plot_group) plots["import"] = GNUPlot( "%s_import" % env_title, "", #"All imports - %s" % env_key, plot_group) plots["export"] = GNUPlot( "%s_export" % env_title, "", #"All imports - %s" % env_key, plot_group) def create_plots(): for (sector, plot) in plots.items(): plot.set_series_style( import_dollar_series, "pointtype 5 linetype rgb '#88BBFF'") # hollow circle plot.set_series_style( emission_series, "pointtype 7 linetype rgb '#1144FF'") # solid circle plot.set_series_style(self_emission_series, "pointtype 12 linetype rgb '#0000FF'") plot.set_series_style(export_dollar_series, "pointtype 4 linetype rgb '#CC9933'") plot.set_series_style(export_emissions_series, "pointtype 7 linetype rgb '#996600'") #plot.legend("width -8") plot.width = 480 plot.height = 300 plot.write_tables() plot.generate_plot() ### trade balance matrices export_balance = NamedMatrix(rows=common.default_env_sectors, cols=yearstrings) import_balance = NamedMatrix(rows=common.default_env_sectors, cols=yearstrings) all_E = NamedMatrix(rows=common.default_env_sectors, cols=yearstrings) def describe_balance(ratios=False): export_total = export_balance.sum(0) import_total = import_balance.sum(0) all_total = all_E.sum(0) balance = export_total.subtract(import_total) country_name = config.countries[base_country] oddyears = [ str(y) for y in filter(lambda x: x % 2 == 1, config.STUDY_YEARS) ] if ratios: balance_ratio = balance.divide(all_total) print( country_name.ljust(15) + " & " + " & ".join([("%.2f" % balance_ratio.get_element("sum", y)).rjust(6) for y in oddyears]) + " \\\\") else: print( country_name.ljust(15) + " & " + " & ".join([ utils.add_commas(balance.get_element("sum", y)).rjust(9) for y in oddyears ]) + " \\\\") def describe_balance_intensity(): export_total = export_balance.sum(0) import_total = import_balance.sum(0) all_total = all_E.sum(0) balance = export_total.subtract(import_total) balance_ratio = balance.divide(all_total) country_name = config.countries[base_country] years = [str(minyear), str(maxyear)] fields = [] for y in years: balance_val = balance.get_element("sum", y) ratio_val = balance_ratio.get_element("sum", y) (gdp_val, env_val, intensity) = \ common.get_efficiency(base_country, int(y)) if int(y) in balance_plots: plot = balance_plots[int(y)] # ratio = exports to imports plot.set_value("2 ratio", base_country, ratio_val) plot.set_value("1 intensity", base_country, intensity) if int(y) in worldmap: worldmap[int(y)].set_country_value(base_country, balance_val) fields.append(utils.add_commas(balance_val)) fields.append("%.2f" % ratio_val) fields.append("%.2f" % intensity) print(country_name.ljust(15) + " & " + " & ".join(fields) + " \\NN") ### iogen = common.iogen_for_year(config.STUDY_YEARS[0]) envgen = common.envgen_for_year(config.STUDY_YEARS[0]) strings = {"schema": config.WIOD_SCHEMA} for year in config.STUDY_YEARS: strings["year"] = year base_E = get_wiod_env_vector(base_country, year, env_series) all_E.set_column(str(year), base_E) base_E_sum = base_E.sum() iogen.set_table("%(schema)s.indbyind_%(year)d" % strings) iogen.set_condition_args(base_country) envgen.set_table("%(schema)s.env_%(year)d" % strings) envgen.set_condition_args(base_country) envgen.set_sectors(common.default_env_sectors) # prepare base country values y = iogen.get_Y() import_column = common.get_import_vector(iogen) base_imports = import_column.sum() base_gdp = y.sum() harmonizer = common.get_io_harmonizer(iogen) base_y = harmonizer.matrix_mult(y.get_total()) x = iogen.get_x() E = envgen.get_env_vector(env_series) L = iogen.get_L() J = E.divide(harmonizer.matrix_mult(x), ignore_zero_denom=True) base_JsL = J.square_matrix_from_diag()\ .matrix_mult(harmonizer).matrix_mult(L) exported = base_JsL.matrix_mult(y.get_exports()) export_balance.set_column(str(year), exported) plots["export"].set_value(export_emissions_series, year, exported.sum() / base_E_sum) plots["export"].set_value(export_dollar_series, year, y.get_exports().sum() / base_gdp) # prepare other country values stmt = db.prepare("""SELECT from_sector, sum(value) FROM wiod_plus.%s_io_import_%d WHERE country = $1 AND from_sector NOT IN ('ITM', 'IMP', 'Rex') GROUP BY from_sector""" % (base_country.lower(), year)) imported_E = None imported_y = None domestic_E = None # emissions under domestic technology assumption for country in other_countries: envgen.set_condition_args(country) envgen.set_sectors(common.default_env_sectors) iogen.set_condition_args(country) sectors = iogen.get_sectors( ) # number of sectors varies by country E = envgen.get_env_vector(env_series) if E.mat() is None: # this country has no data for env series continue imports = NamedMatrix(rows=sectors, cols=["imports"]) db_result = stmt(country) if not len(db_result): # we can't do any of the following with a blank import vector continue for row in db_result: imports.set_element(row[0], "imports", row[1]) sel = common.get_io_harmonizer(iogen) x = iogen.get_x() L = iogen.get_L() J = E.divide(sel.matrix_mult(x), ignore_zero_denom=True) JsL = J.square_matrix_from_diag().matrix_mult(sel).matrix_mult(L) current_E = JsL.matrix_mult(imports) current_y = sel.matrix_mult(imports) # temporary dumb way to deal with sector mismatch compat_imports = NamedMatrix(rows=base_JsL.get_columns(), cols=["imports"]) for col in base_JsL.get_columns(): if col in sectors: compat_imports.set_element(col, "imports", imports.get_element(col)) current_domestic_E = base_JsL.matrix_mult(compat_imports) imported_E = sum_if_not_none(imported_E, current_E) imported_y = sum_if_not_none(imported_y, current_y) domestic_E = sum_if_not_none(domestic_E, current_domestic_E) exported_y.set_element(country, str(year), imports.sum()) exported_E.set_element(country, str(year), current_E.sum()) # populate results table TradeResultsTable.insert_exports(year, base_country, exported) TradeResultsTable.insert_imports(year, base_country, imported_E) # generate import plots for sector in common.default_env_sectors: base_y_val = base_y.get_element(sector) if base_y_val > 0: plots[sector].set_value( import_dollar_series, year, imported_y.get_element(sector) / base_y_val) base_E_val = base_E.get_element(sector) if base_E_val > 0: plots[sector].set_value( emission_series, year, imported_E.get_element(sector) / base_E_val) plots[sector].set_value( self_emission_series, year, domestic_E.get_element(sector) / base_E_val) plots["import"].set_value(import_dollar_series, year, imported_y.sum() / base_y.sum()) plots["import"].set_value(emission_series, year, imported_E.sum() / base_E_sum) plots["import"].set_value(self_emission_series, year, domestic_E.sum() / base_E_sum) if year in dta_plots and base_country in dta_countries: dta_plots[year].set_value("DTA", config.countries[base_country], imported_E.sum() / base_E_sum) dta_plots[year].set_value("No DTA", config.countries[base_country], domestic_E.sum() / base_E_sum) # this is for DTA vs non-DTA table #print(base_country, year, # imported_E.sum(), # domestic_E.sum(), # imported_E.sum() / base_E_sum, # domestic_E.sum() / base_E_sum) plots["export"].set_value(emission_series, year, imported_E.sum() / base_E_sum) import_balance.set_column(str(year), imported_E) #describe_exporters() #create_plots() #describe_balance(True) describe_balance_intensity()