PolicyEngine / policyengine.dev

Information for PolicyEngine developers on how to contribute and how it's structured.
1 stars 3 forks source link

Provide guidance on parameters and variables with the same name #12

Open MaxGhenis opened 2 years ago

MaxGhenis commented 2 years ago

For example, consider the case of identifying whether a household is in poverty, where poverty line is indexed by household size. The formula could include Python variables that both represent the poverty line parameter (which is indexed by household size) and the household's poverty line based on their specific household size. I see three ways to address this:

A: Add a qualifier to the entity's value, e.g. household_

class is_in_poverty(Variable):
    def formula(household, period, parameters):
        poverty_line = parameters(period).poverty.poverty_line
        household_size = household.nb_persons()
        household_poverty_line = poverty_line[household_size]
        return household("household_income") < household_poverty_line

B: Add a qualifier to the parameter, e.g. param_ or p_

class is_in_poverty(Variable):
    def formula(household, period, parameters):
        param_poverty_line = parameters(period).poverty.poverty_line
        household_size = household.nb_persons()
        poverty_line = poverty_line[household_size]
        return household("household_income") < poverty_line

C: Add a separate OpenFisca variable for any parameter indexed by household features

class household_poverty_line(Variable):
    def formula(household, period, parameters):
        poverty_line = parameters(period).poverty.poverty_line
        household_size = household.nb_persons()
        return poverty_line[household_size]

class is_in_poverty(Variable):
    def formula(household, period, parameters):
        return household("household_income") < household("household_poverty_line")

Option C creates a lot of variables, but it seems cleanest to me. I think it would avoid the issue of potentially overlapping variables to represent parameters and Variables, but there might be some cases I'm not thinking of.

MaxGhenis commented 2 years ago

Per discussion with @nikhilwoodruff we'll go with Option C, and also add a request for automatic indexing to openfisca-core