Writing OpenFisca code

Modelling legislation

(Rules as Code + OpenFisca) training series, session three.

Overview

  1. Variables
  2. Parameters
  3. Testing

1. Variables

Simple variable example showing 'fulltime employment'

Simple variable boolean example, note:

  • The reference url
  • The entity the variable belongs to
  • The definition period
  • The lack of a formula

github.com/BetterRules/openfisca-aotearoa/.../demographics/work.py

Variables cont. Parameter example from OpenFisca Aotearoa showing 'Age of majority'

Variable with formula example​

github.com/BetterRules/openfisca-aotearoa/.../demographics/work.py

Variables cont.

Working with vectors

  • OpenFisca formulas should always return vectors
  • The formulas need to preserve the incoming vector 'shape'
  • This means if/else statements aren't possible

2. Parameters

Parameter example from OpenFisca Aotearoa showing 'Age of majority'

Simple parameter example​

github.com/BetterRules/openfisca-aotearoa/.../age_of_majority.yaml

Parameters cont. Parameter example from OpenFisca Aotearoa showing References to data point sources

Referencing: note ref. urls for source data​

github.com/BetterRules/openfisca-aotearoa/.../asset_limits_1.yml

Parameters cont. Parameter example showing value changes over time

Time based values​

github.com/BetterRules/openfisca-aotearoa/.../asset_limits_1.yml

Parameters cont.
Parameter example showing value changes over time

Scale example with use case

Utilising the parameter in a formula

github.com/BetterRules/openfisca-aotearoa/.../income_test_1.yaml

Parameters cont.
Parameter example showing value changes over time

Complex example and use case

Utilising the parameter in a formula

github.com/BetterRules/openfisca-aotearoa/.../jobseeker_support/base.yaml

3. Tests

Simple test example

Simple test with multiple inputs and one output

github.com/BetterRules/openfisca-aotearoa/.../entitled.yaml

Tests cont.
Simple test example with multiple scenarios

Testing a single formula from multiple scenarios

github.com/BetterRules/openfisca-aotearoa/.../child.yaml

Tests cont.
Testing with entities

Testing example that utilises entities (families)

github.com/BetterRules/openfisca-aotearoa/.../community_services_card.yaml

Tests cont.
Testing with entities

Testing example that utilises entities (families)

github.com/BetterRules/openfisca-aotearoa/.../community_services_card.yaml

Tests cont.
Testing with entities and multiple people

Testing example that utilises multiple family members

github.com/BetterRules/openfisca-aotearoa/.../community_services_card.yaml

Tests cont.
Testing multiple scenarios over multiple periods

Testing multiple scenarios over multiple periods

github.com/BetterRules/openfisca-aotearoa/.../jobseeker_support/base.yaml


Hotkeys

  • f: full screen

  • s: speaker notes

  • b: pause

  • / / space: navigate through slides

  • cltr / alt + click to zoom