def build(self): self.sim_builder = SimulationBuilder() self.system = openfisca_uk.CountryTaxBenefitSystem() self.apply_reforms(self.reforms) self.sim = self.sim_builder.build_from_entities( self.system, self.situation_data )
def __init__(self, *reforms, year=2020): self.year = year self.reforms = reforms self.system = openfisca_uk.CountryTaxBenefitSystem() self.entities = {var.key: var for var in self.system.entities} self.apply_reforms(self.reforms) self.situation_data = {"people": {}, "benunits": {}, "households": {}} self.varying = False self.num_points = None
def __init__(self, *reforms): self.reforms = reforms self.tax_benefit_system = openfisca_uk.CountryTaxBenefitSystem() self.entities = { var.key: var for var in self.tax_benefit_system.entities } for reform in reforms: self.tax_benefit_system = reform(self.tax_benefit_system) self.situation_data = {"people": {}, "benunits": {}, "households": {}} self.varying = False self.num_points = None
def load_dataset(self, dataset, year: int) -> None: data = dataset.load(year) year = str(year) self.system = openfisca_uk.CountryTaxBenefitSystem() self.apply_reforms(self.reforms) builder = SimulationBuilder() builder.create_entities(self.system) self.relations = { "person": np.array(data["P_person_id"][year]), "benunit": np.array(data["B_benunit_id"][year]), "household": np.array(data["H_household_id"][year]), "person-benunit": np.array(data["P_benunit_id"][year]), "person-household": np.array(data["P_household_id"][year]), } builder.declare_person_entity("person", np.array(data["P_person_id"][year])) benunits = builder.declare_entity("benunit", np.array(data["B_benunit_id"][year])) households = builder.declare_entity( "household", np.array(data["H_household_id"][year])) person_roles = np.array(np.array(data["P_role"][year])).astype(str) builder.join_with_persons( benunits, np.array(data["P_benunit_id"][year]), person_roles) # define person-benunit memberships builder.join_with_persons( households, np.array(data["P_household_id"][year]), person_roles) # define person-household memberships model = builder.build(self.system) skipped = [] for variable in data.keys(): if variable in self.system.variables: for period in data[variable].keys(): values = np.array(data[variable][period]) target_dtype = self.system.variables[variable].value_type if target_dtype in (Enum, str): values = values.astype(str) else: values = values.astype(target_dtype) try: model.set_input(variable, period, values) except: skipped += [variable] if skipped: warnings.warn( f"Incomplete initialisation: skipped {len(skipped)} variables:" ) self.simulation = model data.close()
def load_dataset(self, entity_dfs: Tuple[pd.DataFrame], verbose: bool = False) -> None: person, benunit, household = entity_dfs self.system = openfisca_uk.CountryTaxBenefitSystem() self.apply_reforms(self.reforms) builder = SimulationBuilder() builder.create_entities(self.system) person.sort_values("P_person_id", inplace=True) benunit.sort_values("B_benunit_id", inplace=True) household.sort_values("H_household_id", inplace=True) person["id"] = person["P_person_id"] benunit["id"] = benunit["B_benunit_id"] household["id"] = household["H_household_id"] person.sort_values("id", inplace=True) person.reset_index(inplace=True, drop=True) benunit.sort_values("id", inplace=True) benunit.reset_index(inplace=True, drop=True) household.sort_values("id", inplace=True) household.reset_index(inplace=True, drop=True) self.relations = { "person": np.array(person["P_person_id"]), "benunit": np.array(benunit["B_benunit_id"]), "household": np.array(household["H_household_id"]), "person-benunit": np.array(person["P_benunit_id"]), "person-household": np.array(person["P_household_id"]), } person_ids = np.array(person["P_person_id"]) benunit_ids = np.array(benunit["B_benunit_id"]) household_ids = np.array(household["H_household_id"]) builder.declare_person_entity("person", person_ids) benunits = builder.declare_entity("benunit", benunit_ids) households = builder.declare_entity("household", household_ids) person_roles = np.array(person["P_role"]) builder.join_with_persons( benunits, person["P_benunit_id"], person_roles) # define person-benunit memberships builder.join_with_persons( households, np.array(person["P_household_id"]), person_roles) # define person-household memberships model = builder.build(self.system) skipped = [] for input_file in [person, benunit, household]: for column in input_file.columns: if column != "P_role": try: def_period = self.system.get_variable( column).definition_period if def_period in ["eternity", "year"]: input_periods = [self.input_year] else: input_periods = period( self.input_year).get_subperiods(def_period) for subperiod in input_periods: model.set_input(column, subperiod, np.array(input_file[column])) except Exception: skipped += [column] if skipped and verbose: print( f"Incomplete initialisation: skipped {len(skipped)} variables:" ) for var in skipped: print(f"{var}") return model
def build(self): self.sim_builder = SimulationBuilder() self.sim = self.sim_builder.build_from_entities( openfisca_uk.CountryTaxBenefitSystem(), self.situation_data)
def load_frs(self, frs_data=None, verbose=False, change={}): """ Create and populate a tax-benefit simulation model from OpenFisca. Arguments: reforms: any reforms to apply, in order. data: any data to use instead of the loaded Family Resources Survey. input_period: the period in which to enter all data (at the moment, all data is entered under this period). Returns: A Simulation object. """ system = openfisca_uk.CountryTaxBenefitSystem() for reform in self.reforms: system = reform(system) # apply each reform in order builder = SimulationBuilder() builder.create_entities( system) # create the entities (person, benunit, etc.) if not frs_data: person_file, benunit_file, household_file = frs.load() else: person_file, benunit_file, household_file = frs_data person_file.sort_values("person_id", inplace=True) benunit_file.sort_values("benunit_id", inplace=True) household_file.sort_values("household_id", inplace=True) person_file["id"] = person_file["person_id"] benunit_file["id"] = benunit_file["benunit_id"] household_file["id"] = household_file["household_id"] person_file.sort_values("id", inplace=True) person_file.reset_index(inplace=True, drop=True) benunit_file.sort_values("id", inplace=True) benunit_file.reset_index(inplace=True, drop=True) household_file.sort_values("id", inplace=True) household_file.reset_index(inplace=True, drop=True) self.relations = { "person": np.array(person_file["person_id"]), "benunit": np.array(benunit_file["benunit_id"]), "household": np.array(household_file["household_id"]), "person-benunit": np.array(person_file["benunit_id"]), "person-household": np.array(person_file["household_id"]), } person_ids = np.array(person_file["person_id"]) benunit_ids = np.array(benunit_file["benunit_id"]) household_ids = np.array(household_file["household_id"]) builder.declare_person_entity("person", person_ids) benunits = builder.declare_entity("benunit", benunit_ids) households = builder.declare_entity("household", household_ids) person_roles = person_file["role"] builder.join_with_persons( benunits, np.array(person_file["benunit_id"]), person_roles) # define person-benunit memberships builder.join_with_persons(households, np.array(person_file["household_id"]), person_roles) model = builder.build(system) skipped = [] for input_file in [person_file, benunit_file, household_file]: for column in input_file.columns: if column in change: input_file[column] += change[column] if column != "role": try: def_period = system.get_variable( column).definition_period if def_period in ["eternity", "year"]: input_periods = [self.input_period] else: input_periods = period( self.input_period).get_subperiods(def_period) for subperiod in input_periods: model.set_input(column, subperiod, np.array(input_file[column])) except Exception as e: skipped += [column] if skipped and verbose: print( f"Incomplete initialisation: skipped {len(skipped)} variables:" ) for var in skipped: print(f"{var}") return model
from openfisca_core import periods from openfisca_core.model_api import * from openfisca_uk.entities import * from openfisca_uk.tools.general import * from flask_cors import CORS from openfisca_uk.microdata.simulation import Microsimulation from rdbl import gbp import plotly.express as px import logging import json app = Flask(__name__) CORS(app) SYSTEM = openfisca_uk.CountryTaxBenefitSystem() baseline = openfisca_uk.Microsimulation() baseline.calc("household_net_income") def abolish_PA(): class PA_reform(Reform): def apply(self): self.neutralize_variable("personal_allowance") return PA_reform def change_basic_rate(value): def change_BR_param(parameters): parameters.tax.income_tax.rates.uk.brackets[0].rate.update(periods.period("year:2015:10"), value=value / 100) return parameters class basic_rate_reform(Reform):