def recalc_M(S, D_cba, Y, nr_sectors): """ Calculate Multipliers based on footprints. Parameters ---------- D_cba : pandas.DataFrame or numpy array Footprint per sector and country Y : pandas.DataFrame or numpy array Final demand: aggregated across categories or just one category, one column per country. This will be diagonalized per country block. The diagonolized form must be invertable for this method to work. nr_sectors : int Number of sectors in the MRIO Returns ------- pandas.DataFrame or numpy.array Multipliers M The type is determined by the type of D_cba. If DataFrame index/columns as D_cba """ Y_diag = ioutil.diagonalize_blocks(Y.values, blocksize=nr_sectors) Y_inv = np.linalg.inv(Y_diag) M = D_cba.dot(Y_inv) if type(D_cba) is pd.DataFrame: M.columns = D_cba.columns M.index = D_cba.index return M
def recalc_N(S, D_iba, V, nr_sectors): """Calculate Nultipliers based on footprints. Parameters ---------- D_iba : pandas.DataFrame or numpy array Footprint per sector and country V : pandas.DataFrame or numpy array Final demand: aggregated across categories or just one category, one column per country. This will be diagonalized per country block. The diagonolized form must be invertable for this method to work. nr_sectors : int Number of sectors in the NRIO Returns ------- pandas.DataFrame or numpy.array Nultipliers N The type is determined by the type of D_iba. If DataFrame index/columns as D_iba """ V_diag = ioutil.diagonalize_blocks(V.values, blocksize=nr_sectors) #V_inv = np.linalg.inv(V_diag) #problem there are zeros in V_diag: this prohibits the inversion diag = V_diag.diagonal() #masking where 0 mdiag = np.ma.masked_array(diag, mask=(diag == 0), fill_value=0) inv_diag = 1 / mdiag inv_diag[inv_diag.mask] = 0 inv_diag.data V_inv = np.diag(inv_diag) N = D_iba.dot(V_inv) if type(D_iba) is pd.DataFrame: N.columns = D_iba.columns N.index = D_iba.index return N
def calc_D_iba(S, G, V, nr_sectors): """Calculate sector specific cba and pba based accounts, imp and exp accounts The total industry output x for the calculation is recalculated from L and y Parameters ---------- G : pandas.DataFrame Ghosh input output table L S : pandas.DataFrame Direct impact coefficients V : pandas.DataFrame Value added: aggregated across categories or just one category, one column per country nr_sectors : int Number of sectors in the MRIO Returns ------- Value D_iba Format: D_row x L_col (=nr_countries*nr_sectors) - D_iba Income-based footprints per sector and country """ # diagonalize each sector block per country # this results in a disaggregated y with final demand per country per # sector in one column V_diag = ioutil.diagonalize_blocks(V.values, blocksize=nr_sectors) x_diag = G.dot(V_diag) x_tot = x_diag.values.sum(1) del V_diag D_iba = pd.DataFrame(S.values.dot(x_diag), index=S.index, columns=S.columns) return D_iba
def calc_accounts(S, L, Y, nr_sectors): """ Calculate sector specific cba and pba based accounts, imp and exp accounts The total industry output x for the calculation is recalculated from L and y Parameters ---------- L : pandas.DataFrame Leontief input output table L S : pandas.DataFrame Direct impact coefficients Y : pandas.DataFrame Final demand: aggregated across categories or just one category, one column per country nr_sectors : int Number of sectors in the MRIO Returns ------- Tuple (D_cba, D_pba, D_imp, D_exp) Format: D_row x L_col (=nr_countries*nr_sectors) - D_cba Footprint per sector and country - D_pba Total factur use per sector and country - D_imp Total global factor use to satisfy total final demand in the country per sector - D_exp Total factor use in one country to satisfy final demand in all other countries (per sector) """ # diagonalize each sector block per country # this results in a disaggregated y with final demand per country per # sector in one column Y_diag = ioutil.diagonalize_blocks(Y.values, blocksize=nr_sectors) x_diag = L.dot(Y_diag) x_tot = x_diag.values.sum(1) del Y_diag D_cba = pd.DataFrame(S.values.dot(x_diag), index=S.index, columns=S.columns) # D_pba = S.dot(np.diagflat(x_tot)) # faster broadcasted calculation: D_pba = pd.DataFrame(S.values * x_tot.reshape((1, -1)), index=S.index, columns=S.columns) # for the traded accounts set the domestic industry output to zero dom_block = np.zeros((nr_sectors, nr_sectors)) x_trade = ioutil.set_block(x_diag.values, dom_block) D_imp = pd.DataFrame(S.values.dot(x_trade), index=S.index, columns=S.columns) x_exp = x_trade.sum(1) # D_exp = S.dot(np.diagflat(x_exp)) # faster broadcasted version: D_exp = pd.DataFrame(S.values * x_exp.reshape((1, -1)), index=S.index, columns=S.columns) return (D_cba, D_pba, D_imp, D_exp)
def calc_accounts(S, L, Y, nr_sectors): """ Calculate sector specific cba and pba based accounts, imp and exp accounts The total industry output x for the calculation is recalculated from L and y Parameters ---------- L : pandas.DataFrame Leontief input output table L S : pandas.DataFrame Direct impact coefficients Y : pandas.DataFrame Final demand: aggregated across categories or just one category, one column per country nr_sectors : int Number of sectors in the MRIO Returns ------- Tuple (D_cba, D_pba, D_imp, D_exp) Format: D_row x L_col (=nr_countries*nr_sectors) - D_cba Footprint per sector and country - D_pba Total factur use per sector and country - D_imp Total global factor use to satisfy total final demand in the country per sector - D_exp Total factor use in one country to satisfy final demand in all other countries (per sector) """ # diagonalize each sector block per country # this results in a disaggregated y with final demand per country per # sector in one column Y_diag = ioutil.diagonalize_blocks(Y.values, blocksize=nr_sectors) x_diag = L.dot(Y_diag) x_tot = x_diag.values.sum(1) del Y_diag D_cba = pd.DataFrame(S.values.dot(x_diag), index=S.index, columns=S.columns) # D_pba = S.dot(np.diagflat(x_tot)) # faster broadcasted calculation: D_pba = pd.DataFrame(S.values*x_tot.reshape((1, -1)), index=S.index, columns=S.columns) # for the traded accounts set the domestic industry output to zero dom_block = np.zeros((nr_sectors, nr_sectors)) x_trade = ioutil.set_block(x_diag.values, dom_block) D_imp = pd.DataFrame(S.values.dot(x_trade), index=S.index, columns=S.columns) x_exp = x_trade.sum(1) # D_exp = S.dot(np.diagflat(x_exp)) # faster broadcasted version: D_exp = pd.DataFrame(S.values * x_exp.reshape((1, -1)), index=S.index, columns=S.columns) return (D_cba, D_pba, D_imp, D_exp)