Exemplo n.º 1
0
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')