def produce_agg_transfert_flux(simulation=simulation, year_list = range(1996, 2050, 10), year_min=1996): print 'entering generation of aggregated flux of payments ' tmp = simulation.cohorts.loc[:, ['net_transfers', 'pop', 'dsct']] tmp['running_transfers'] = tmp['net_transfers'] tmp['net_transfers'] *= tmp['dsct']*tmp['pop'] tmp_2 = simulation.cohorts_alt.loc[:, ['net_transfers', 'pop', 'dsct']] tmp_2['running_transfers'] = tmp_2['net_transfers'] tmp_2['net_transfers'] *= tmp_2['dsct']*tmp_2['pop'] print type(str(str(xls)+'\agre_agg.xlsx')) for year in year_list: flux_df = AccountingCohorts(tmp).extract_generation(year=year, typ='net_transfers', age=0) flux_df = flux_df.xs(0, level='sex') flux_df.columns = [year] print year flux_df_alt = AccountingCohorts(tmp_2).extract_generation(year=year, typ='net_transfers', age=0) flux_df_alt = flux_df_alt.xs(0, level='sex') flux_df_alt.columns = [str(year)+'_alt'] flux_df = concat([flux_df, flux_df_alt], axis=1)#, ignore_index=True) flux_df[year] *= ((1+simulation.discount_rate)/(1+simulation.growth_rate))**(year - year_min) flux_df[str(year)+'_alt'] *= ((1+simulation.discount_rate_alt)/(1+simulation.growth_rate_alt))**(year - year_min) print flux_df.head() flux_df.to_excel(xls+'\_combine_agg_'+str(year)+'.xlsx', 'flux') gc.collect()
def test_generation_extraction(): # Creating a fake cohort n = 0.00 r = 0.00 g = 0.05 population = create_testing_population_dataframe(year_start=2001, year_end=2061, rate=n) profile = create_constant_profiles_dataframe(population, tax=-1, sub=0.5) cohort = DataCohorts(population) # Applying projection methods year_length = 199 method = 'stable' cohort.population_project(year_length, method=method) cohort._fill(profile) typ = None cohort.proj_tax(g, r, typ, method='per_capita') cohort = AccountingCohorts(cohort) cohort._types = ['tax'] #Extracting generations start = 2030 age = 0 generation = cohort.extract_generation(start, typ='tax', age=age) count = age while count <= 100 & start + count <= array( list(generation.index_sets['year'])).max(): assert abs((1 + g)**(count + (start - 2001)) + generation.get_value((count, 1, start + count), 'tax')) == 0.0 count += 1
def new_per_capita_generation_present_value(self, typ, discount_rate=None): """ Returns present net value per capita of the data typ Parameters ---------- typ : str Column name discount_rate : float Returns ------- pv_percapita : an AccountingCohorts with column 'typ' containing the per capita present value of typ """ if typ not in self._types: raise Exception('cohort: variable %s is not in self._types' % typ) return if discount_rate is None: discount_rate = 0.0 if 'dsct' not in self._types: self.gen_dsct(discount_rate) tmp = self['dsct'] * self[typ] tmp = tmp.unstack(level='year') # untack year indices to columns pvm = tmp.xs(0, level='sex') pvf = tmp.xs( 1, level='sex') #Assuming 1 is the index for females resp. 0 is male. yr_min = array(list(self.index_sets['year'])).min() yr_max = array(list(self.index_sets['year'])).max() for yr in arange(yr_min, yr_max)[::-1]: pvm[yr] += hstack([pvm[yr + 1].values[1:], 0]) pvf[yr] += hstack([pvf[yr + 1].values[1:], 0]) pieces = [pvm, pvf] res = concat(pieces, keys=[0, 1], names=["sex"]) res = res.stack() res = res.reset_index() res = res.set_index(['age', 'sex', 'year']) res.columns = [typ] res = DataFrame(res) return AccountingCohorts(res)
def aggregate_generation_present_value(self, typ, discount_rate=None): """ Computes the present value of one column for the whole generation Parameters ---------- typ : str Name of the column of the per capita profile of tax or transfer discount_rate : float Rate used to calculate the present value Returns ------- res : an AccountingCohorts with column 'typ' containing the aggregat present value of typ """ if typ not in self._types: raise Exception('cohort: variable %s is not in self._types' % typ) return if discount_rate is None: discount_rate = 0.0 if 'dsct' not in self._types: self.gen_dsct(discount_rate) tmp = self['dsct'] * self[typ] * self['pop'] tmp = tmp.unstack(level='year') # untack year indices to columns pvm = tmp.xs(0, level='sex') pvf = tmp.xs( 1, level='sex') #Assuming 1 is the index for females resp. 0 is male. yr_min = array(list(self.index_sets['year'])).min() yr_max = array(list(self.index_sets['year'])).max() for yr in arange(yr_min, yr_max)[::-1]: pvm[yr] += hstack([pvm[yr + 1].values[1:], 0]) pvf[yr] += hstack([pvf[yr + 1].values[1:], 0]) pieces = [pvm, pvf] res = concat(pieces, keys=[0, 1], names=["sex"]) res = res.stack() res = res.reset_index() res = res.set_index(['age', 'sex', 'year']) res.columns = [typ] res = DataFrame(res) return AccountingCohorts(res)
def per_capita_generation_present_value(self, typ, discount_rate=None): """ Returns present net value per capita of the data typ Parameters ---------- typ : str Column name discount_rate : float Returns ------- pv_percapita : an AccountingCohorts with column 'typ' containing the per capita present value of typ """ if typ not in self._types: raise Exception('cohort: variable %s is not in self._types' % typ) pv_gen = self.aggregate_generation_present_value(typ, discount_rate) pop = DataFrame({'pop': self['pop']}) pv_percapita = DataFrame(pv_gen[typ] / pop['pop']) pv_percapita['pop'] = pop['pop'] pv_percapita.columns = [typ, 'pop'] return AccountingCohorts(pv_percapita)
def produce_agg_transfert_flux(simulation=simulation, year_list=range(1996, 2050, 10), year_min=1996): tmp = simulation.cohorts.loc[:, ['net_transfers', 'pop', 'dsct']] tmp['running_transfers'] = tmp['net_transfers'] tmp['net_transfers'] *= tmp['dsct'] * tmp['pop'] tmp_2 = simulation.cohorts_alt.loc[:, ['net_transfers', 'pop', 'dsct']] tmp_2['running_transfers'] = tmp_2['net_transfers'] tmp_2['net_transfers'] *= tmp_2['dsct'] * tmp_2['pop'] for year in year_list: flux_df = AccountingCohorts(tmp).extract_generation( year=year, typ='net_transfers', age=0) flux_df = flux_df.xs(0, level='sex') flux_df.columns = [year] print year flux_df_alt = AccountingCohorts(tmp_2).extract_generation( year=year, typ='net_transfers', age=0) flux_df_alt = flux_df_alt.xs(0, level='sex') flux_df_alt.columns = [str(year) + '_alt'] flux_df = concat([flux_df, flux_df_alt], axis=1) #, ignore_index=True) flux_df[year] *= ((1 + simulation.discount_rate) / (1 + simulation.growth_rate))**(year - year_min) flux_df[str(year) + '_alt'] *= ((1 + simulation.discount_rate_alt) / (1 + simulation.growth_rate_alt))**(year - year_min) print flux_df.head() flux_df.to_excel(str(xls) + str(year) + '_ESP_agg.xlsx', 'flux') gc.collect()
def transition(): levels = ['haut', 'bas'] taxes_list = ['tva', 'tipp', 'cot', 'irpp', 'impot', 'property'] payments_list = ['chomage', 'retraite', 'revsoc', 'maladie', 'educ'] year_length = 250 year_min = 1996 year_max = year_min+year_length-1 arrays=arange(year_min, year_min+60) record = DataFrame(index=arrays) simulation = Simulation() for param1 in levels: for param2 in levels: population_scenario = "projpop0760_FEC"+param1+"ESP"+param2+"MIG"+param1 simulation.load_population(population_filename, population_scenario) # Adding missing population data between 1996 and 2007 : store_pop = HDFStore(os.path.join(SRC_PATH, 'countries', country, 'sources', 'Carole_Bonnet', 'pop_1996_2006.h5')) corrected_pop = store_pop['population'] simulation.population = concat([corrected_pop, simulation.population]) simulation.load_profiles(profiles_filename) simulation.year_length = year_length r = 0.03 g = 0.01 n = 0.00 net_gov_wealth = -3217.7e+09 year_gov_spending = (1094)*1e+09 # Loading simulation's parameters : simulation.set_population_projection(year_length=year_length, method="stable") simulation.set_tax_projection(method="per_capita", rate=g) simulation.set_growth_rate(g) simulation.set_discount_rate(r) simulation.set_population_growth_rate(n) simulation.set_gov_wealth(net_gov_wealth) simulation.set_gov_spendings(year_gov_spending, default=True, compute=True) record[population_scenario] = NaN col_name2 = population_scenario+'_precision' record[col_name2] = NaN simulation.create_cohorts() simulation.cohorts.compute_net_transfers(name = 'net_transfers', taxes_list = taxes_list, payments_list = payments_list) simulation.create_present_values(typ='net_transfers') for year in range(year_min, year_min+60): #On tente de tronquer la df au fil du temps try: simulation.aggregate_pv = simulation.aggregate_pv.drop(labels=year-1, level='year') except: print 'except path' pass simulation.aggregate_pv = AccountingCohorts(simulation.aggregate_pv) # imbalance = simulation.compute_gen_imbalance(typ='net_transfers') ipl = simulation.compute_ipl(typ='net_transfers') # Calcul du résidut de l'IPL pour vérifier la convergence #(on se place tard dans la projection) precision_df = simulation.aggregate_pv print precision_df.head().to_string() year_min_ = array(list(precision_df.index.get_level_values(2))).min() year_max_ = array(list(precision_df.index.get_level_values(2))).max() - 1 # age_min = array(list(self.index.get_level_values(0))).min() age_max_ = array(list(precision_df.index.get_level_values(0))).max() print 'CALIBRATION CHECK : ', year_min_, year_max_ past_gen_dataframe = precision_df.xs(year_min_, level = 'year') past_gen_dataframe = past_gen_dataframe.cumsum() past_gen_transfer = past_gen_dataframe.get_value((age_max_, 1), 'net_transfers') # print ' past_gen_transfer = ', past_gen_transfer future_gen_dataframe = precision_df.xs(0, level = 'age') future_gen_dataframe = future_gen_dataframe.cumsum() future_gen_transfer = future_gen_dataframe.get_value((1, year_max_), 'net_transfers') # print ' future_gen_transfer =', future_gen_transfer #Note : do not forget to eliminate values counted twice last_ipl = past_gen_transfer + future_gen_transfer + net_gov_wealth - simulation.net_gov_spendings - past_gen_dataframe.get_value((0, 0), 'net_transfers') last_ipl = -last_ipl print last_ipl, ipl precision = (ipl - last_ipl)/ipl print 'precision = ', precision record.loc[year, population_scenario] = ipl record.loc[year, col_name2] = precision print record.head().to_string() xls = "C:/Users/Utilisateur/Documents/GitHub/ga/src/countries/france/sources/Carole_Bonnet/"+'ipl_evolution'+'.xlsx' print record.head(30).to_string() record.to_excel(xls, 'ipl')
def simple_scenario(): #Initialisation et entrée des paramètres de base : simulation = Simulation() year_length = 300 simulation.set_year_length(nb_year=year_length) net_gov_wealth = -3217.7e+09 year_gov_spending = (1094)*1e+09 net_gov_wealth_alt = -3217.7e+09 year_gov_spending_alt = (1094)*1e+09 taxes_list = ['tva', 'tipp', 'cot', 'irpp', 'impot', 'property'] payments_list = ['chomage', 'retraite', 'revsoc', 'maladie', 'educ'] simulation.set_population_projection(year_length=simulation.year_length, method="exp_growth") #On charge la population: population_scenario = "projpop0760_FECbasESPbasMIGbas" store_pop = HDFStore(os.path.join(SRC_PATH, 'countries', country, 'sources', 'Carole_Bonnet', 'pop_1996_2006.h5')) corrected_pop = store_pop['population'] simulation.population = concat([corrected_pop.iloc[0:101, :], corrected_pop.iloc[1111:1212,:]]) #<- la première année TODO: c'est moche simulation.load_population(population_filename, population_scenario, default=False) simulation.population_alt = concat([corrected_pop, simulation.population_alt]) simulation.load_profiles(profiles_filename) r = 0.03 g = 0.01 n = 0.00 r_alt = r g_alt = g n_alt = 0.00 #On crée le témoin : simulation.set_tax_projection(method="aggregate", rate=g) simulation.set_growth_rate(g) simulation.set_discount_rate(r) simulation.set_population_growth_rate(n) simulation.create_cohorts() simulation.set_gov_wealth(net_gov_wealth) simulation.set_gov_spendings(year_gov_spending, default=True, compute=True) simulation.cohorts.compute_net_transfers(name = 'net_transfers', taxes_list = taxes_list, payments_list = payments_list) simulation.create_present_values('net_transfers', default=True) #On crée le groupe test : simulation.set_growth_rate(g_alt, default=False) simulation.set_discount_rate(r_alt, default=False) simulation.set_population_growth_rate(n_alt, default=False) simulation.create_cohorts(default=False) simulation.cohorts_alt.loc[[x>=2015 for x in simulation.cohorts_alt.index.get_level_values(2)], 'retraite'] *= (1/2) simulation.set_gov_wealth(net_gov_wealth_alt, default=False) simulation.set_gov_spendings(year_gov_spending_alt, default=False, compute=True) simulation.cohorts_alt.compute_net_transfers(name = 'net_transfers', taxes_list = taxes_list, payments_list = payments_list) simulation.create_present_values('net_transfers', default=False) #Calcul de l'IPL et de sa décomposition ipl_base = simulation.compute_ipl(typ='net_transfers') ipl_alt = simulation.compute_ipl(typ='net_transfers', default=False, precision=False) tmp = simulation.cohorts.loc[:, ['net_transfers', 'pop', 'dsct']] tmp['running_transfers'] = tmp['net_transfers'] tmp['net_transfers'] *= tmp['dsct'] tmp_2 = simulation.cohorts_alt.loc[:, ['net_transfers', 'pop', 'dsct']] tmp_2['running_transfers'] = tmp_2['net_transfers'] tmp_2['net_transfers'] *= tmp_2['dsct'] xls = "C:/Users/Utilisateur/Documents/GitHub/ga/src/countries/france/sources/Output_folder/" for year in range(1996, 2007): flux_df = AccountingCohorts(tmp).extract_generation(year=year, typ='net_transfers', age=0) flux_df = flux_df.xs(0, level='sex') print year flux_df_alt = AccountingCohorts(tmp_2).extract_generation(year=year, typ='net_transfers', age=0) flux_df_alt = flux_df_alt.xs(0, level='sex') flux_df[year] = flux_df_alt['net_transfers'] flux_df.to_excel(str(xls)+str(year)+'_.xlsx', 'flux') print ipl_base, ipl_alt