def formula(self, simulation, period): choice = simulation.calculate('choice', period) result = switch( choice, { 1: 80, 2: 90, }, ) return result # TaxBenefitSystem instance declared after formulas tax_benefit_system = CountryTaxBenefitSystem() tax_benefit_system.add_variables(choice, uses_multiplication, uses_switch) month = '2013-01' scenario = tax_benefit_system.new_scenario().init_from_attributes( period=month, input_variables={ # 'choice': [1, 1, 1, 2], 'choice': np.random.randint(2, size=1000) + 1, }, ) def test_switch(): simulation = scenario.new_simulation(debug=True) uses_switch = simulation.calculate('uses_switch', period=month) assert isinstance(uses_switch, np.ndarray)
def get_filled_tbs(): tax_benefit_system = CountryTaxBenefitSystem() tax_benefit_system.add_variables(input, intermediate, output) return tax_benefit_system
# december cotisation depending on november value class cotisation(Variable): value_type = int entity = Person definition_period = MONTH def formula(person, period): if period.start.month == 12: return 2 * person('cotisation', period.last_month) else: return person.empty_array() + 1 # TaxBenefitSystem instance declared after formulas tax_benefit_system = CountryTaxBenefitSystem() tax_benefit_system.add_variables(variable1, variable2, variable3, variable4, variable5, variable6, variable7, cotisation) def test_pure_cycle(simulation, reference_period): with raises(CycleError): simulation.calculate('variable1', period=reference_period) def test_spirals_result_in_default_value(simulation, reference_period): variable3 = simulation.calculate('variable3', period=reference_period) assert_near(variable3, [0]) def test_spiral_heuristic(simulation, reference_period): variable5 = simulation.calculate('variable5', period=reference_period) variable6 = simulation.calculate('variable6', period=reference_period)
return person.empty_array() + choice class formula_4(Variable): value_type = bool entity = Person base_function = requested_period_last_value definition_period = MONTH def formula(person, period, parameters, choice): return person.empty_array() + choice # TaxBenefitSystem instance declared after formulas tax_benefit_system = CountryTaxBenefitSystem() tax_benefit_system.add_variables(formula_1, formula_2, formula_3, formula_4) reference_period = periods.period('2013-01') def get_simulation(): return tax_benefit_system.new_scenario().init_from_attributes( period=reference_period.first_month, ).new_simulation() def test_cache(): simulation = get_simulation() formula_1_result = simulation.calculate('formula_1', period=reference_period) formula_2_result = simulation.calculate('formula_2', period=reference_period)
entity = Person definition_period = MONTH value_type = int calculate_output = calculate_output_add class variable_with_calculate_output_divide(Variable): entity = Person definition_period = YEAR value_type = int calculate_output = calculate_output_divide tax_benefit_system = CountryTaxBenefitSystem() tax_benefit_system.add_variables(simple_variable, variable_with_calculate_output_add, variable_with_calculate_output_divide) @raises(ValueError) def test_calculate_output_default(): simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=single) simulation.calculate_output('simple_variable', 2017) def test_calculate_output_add(): simulation = Simulation(tax_benefit_system=tax_benefit_system, simulation_json=single) simulation.set_input('variable_with_calculate_output_add', '2017-01', [10]) simulation.set_input('variable_with_calculate_output_add', '2017-05', [20])
definition_period = MONTH value_type = int calculate_output = calculate_output_add class variable_with_calculate_output_divide(Variable): entity = Person definition_period = YEAR value_type = int calculate_output = calculate_output_divide tax_benefit_system = CountryTaxBenefitSystem() tax_benefit_system.add_variables( simple_variable, variable_with_calculate_output_add, variable_with_calculate_output_divide ) def test_calculate_output_default(simulation): with raises(ValueError): simulation.calculate_output('simple_variable', 2017) def test_calculate_output_add(simulation): simulation.set_input('variable_with_calculate_output_add', '2017-01', [10]) simulation.set_input('variable_with_calculate_output_add', '2017-05', [20]) simulation.set_input('variable_with_calculate_output_add', '2017-12', [70]) assert_near(simulation.calculate_output('variable_with_calculate_output_add', 2017), 100)
def formula(person, period): choice = person('choice', period) result = switch( choice, { 1: 80, 2: 90, }, ) return result # TaxBenefitSystem instance declared after formulas our_tbs = CountryTaxBenefitSystem() our_tbs.add_variables(choice, uses_multiplication, uses_switch, returns_scalar) @fixture def month(): return '2013-01' @fixture def simulation(month): builder = SimulationBuilder() builder.default_period = month simulation = builder.build_from_variables( our_tbs, {'choice': np.random.randint(2, size=1000) + 1}) simulation.debug = True return simulation
# december cotisation depending on november value class cotisation(Variable): value_type = int entity = Person definition_period = MONTH def formula(person, period): if period.start.month == 12: return 2 * person('cotisation', period.last_month) else: return person.empty_array() + 1 # TaxBenefitSystem instance declared after formulas tax_benefit_system = CountryTaxBenefitSystem() tax_benefit_system.add_variables(variable1, variable2, variable3, variable4, variable5, variable6, variable7, cotisation) def test_pure_cycle(simulation, reference_period): with raises(CycleError): simulation.calculate('variable1', period = reference_period) def test_spirals_result_in_default_value(simulation, reference_period): variable3 = simulation.calculate('variable3', period = reference_period) assert_near(variable3, [0]) def test_spiral_heuristic(simulation, reference_period): variable5 = simulation.calculate('variable5', period = reference_period) variable6 = simulation.calculate('variable6', period = reference_period)