Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

policy parameters w/ vanilla Python expressions instead of Numpy functions #1102

Closed
MattHJensen opened this issue Dec 12, 2016 · 6 comments
Closed

Comments

@MattHJensen
Copy link
Contributor

In #1081, @martinholmer created the capability to create a policy parameter that is a Python expression rather than a number. This is a major innovation that implements an idea originally raised by @feenberg in #429.

The implementation in #1081 allows the user to interact with whitelisted records variables using Numpy array functions (maximum, minimum, and where).

It would be valuable if the user could use vanilla python expressions (max, min, if, and, or) in addition to or instead of Numpy array functions. The most important of these is to allow if statements whether than where functions.

One reason for preferring vanilla Python syntax is that experience from Tax-Calculator's early days shows that using where statements for tax logic can get very messy. See https://github.com/talumbau/Tax-Calculator/blob/4cdc5841fefc5d175c9e5e538c7fd9c670fe35d1/translation.py#L1243-L1367s. (We also learned that Python's and and or are much nicer for tax logic than Numpy's alternatives.)

A second reason for preferring vanilla Python syntax is that we use vanilla Python syntax wherever possible in functions.py, and using Numpy syntax for these "hooks" will require users to familiarize themselves with reading tax logic in two syntaxes.

A third reason for preferring vanilla Python syntax is that I expect it will be generally more familiar to TaxBrain users from non-Python backgrounds.

@talumbau, I'm wondering if some of the @iterate_jit technology that you developed for Tax-Calculator functions.py could also be applied to these hooks.

@martinholmer
Copy link
Collaborator

@MattHJensen said:

The implementation in #1081 allows the user to interact with whitelisted records variables using Numpy array functions (maximum, minimum, and where).

Actually, that is not precisely true. The functions available for use in parameter expressions at the moment are "max", "min", and "where". This is an important point because issue #1102 is largely about syntatic sugar. The "max" function is an alias for numpy.maximum and the "min" function is an alias for the numpy.minimum function. So, "max" and "min" in policy parameter expressions are identical to the "vanilla python" functions.

It would be valuable if the user could use vanilla python expressions (max, min, if, and, or) in addition to or instead of Numpy array functions. The most important of these is to allow if statements rather than where functions.

Perhaps this syntactic sugar would make expression writing somewhat sweeter for some users, but there is a cost in development complexity and increased potential for errors that will be very difficult to detect. I think it is way too early in the history of policy parameter expressions to know exactly how big a problem it will be to ask users to use a "where" function rather than an "if" statement. Why not wait to discuss this when we have some experience with using the parameter expression capability? How many of those on the development team have used the capabilities on the master branch to write parameter expressions? Can those who have used this feature provide us examples of the expressions you have used? And can you share with the rest of the development team any expressions that you couldn't write with the current "max", "min", and "where" functions? And can you share expressions that became difficult to read/understand because of the nature of the "where" function?

@feenberg @Amy-Xu @zrisher @GoFroggyRun @andersonfrailey @codykallen

@feenberg
Copy link
Contributor

feenberg commented Dec 12, 2016 via email

@talumbau
Copy link
Member

just one clarifying question: is the desire to use the "ternary operator" form (or rather, Python's equivalent) which looks like this:

   x = y if cond else z

or do you want to use the full Python if/else construct:

    if cond:
        statement1
    elif cond2:
        statement2
    else:
        statement 3

@MattHJensen
Copy link
Contributor Author

@martinholmer said:

Why not wait to discuss this when we have some experience with using the parameter expression capability?

You're absolutely right. I've had an example in my head for a long time now but realize I haven't communicated that to anyone else. Here it is:

#1103 (comment)

@talumbau, the example above also answers your question.

@feenberg
Copy link
Contributor

feenberg commented Dec 12, 2016 via email

@martinholmer
Copy link
Collaborator

The parameter-code capability was developed and then removed from Tax-Calculator as described in pull request #1221.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants