openfisca / openfisca-core

OpenFisca core engine. See other repositories for countries-specific code & data.
https://openfisca.org
GNU Affero General Public License v3.0
168 stars 75 forks source link

Conditional formulas #1135

Open nikhilwoodruff opened 2 years ago

nikhilwoodruff commented 2 years ago

Often variables have a formula applying to a population, but will only ever be calculated for a subset of that population. For example, in the US, we have State tax variables for every State (relevant only to tax units in that State). Or, in all systems, we have benefit variables relevant only to entities which meet qualification criteria.

Right now, because of how OpenFisca is implemented using vectorised functions, we carry out all computations on the entire population for which a variable is defined. This creates unnecessary computation, and I think there's a way to avoid that without changing anything fundamental about the OpenFisca interface, and without stopping to use vectorised functions.

I'm envisioning something like:

class some_variable(Variable):
  entity = Person
  label = "Some variable"
  definition_period = YEAR
  value_type = float
  default_value = 1
  calculate_if = "meets_qualification_criteria"

  def formula(person, period, parameters):
    return 2

This is of importance to us in the US system, so I'm going to begin developing a solution using monkey-patching in OpenFisca-Tools. But it'd be much cleaner (and more accessible to other countries) to implement in OpenFisca-Core. Would Core be open to this feature?

cc @benjello @MattiSG @sandcha