def _cs_cs_flows(self, country_names=None, sectors=None): """ Calculate country-sector to country-sector flows using country-country flows, the import ratios and the technical coefficients y_ijrs = p_ijr * d_jr * a_jrs * x_js """ x = self.total_production() a = self.technical_coefficients() d = self.import_ratios() p = self.import_propensities() # Rename various index levels x = dataframe.rename_index_level(x, {'country':'to_country', 'sector':'to_sector'}) a = dataframe.rename_index_level(a, {'country':'to_country'}) d = dataframe.rename_index_level(d, {'country':'to_country', 'sector':'from_sector'}) p = dataframe.rename_index_level(p, {'sector':'from_sector'}) # d * a * x is easy to deal with since it's all within country j broadcast = dataframe.broadcast dax = broadcast(broadcast(a, x), d) dax.name = 'dax' # pdax: this is a little more complicated since p doesn't know about s, # and dax doesn't know about i p.name = 'p' pdax = pd.merge(dax.reset_index(), p.reset_index()) pdax = pdax.set_index(['from_country', 'to_country', 'from_sector', 'to_sector']).sortlevel() y_ijrs = pdax.dax * pdax.p y_ijrs.name = 'cs_cs_flow' return y_ijrs
def _split_flows_by_import_propensities(self, flows, country_field='country', country_names=None, with_RoW=False): """ Calculate country to country flows using the given flows vector and the import propensities """ ip = self._import_propensities.astype(float) flows = dataframe.rename_index_level(flows, {country_field:'to_country'}) return dataframe.broadcast(ip, flows)