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

Wrong handling of term ordering leads to incorrect differentiation #235

Closed
e-dorigatti opened this issue Dec 19, 2024 · 4 comments · Fixed by #236
Closed

Wrong handling of term ordering leads to incorrect differentiation #235

e-dorigatti opened this issue Dec 19, 2024 · 4 comments · Fixed by #236
Assignees
Labels
bug Something isn't working

Comments

@e-dorigatti
Copy link

Hello, differentiation in the latest version sometimes gives incorrect results compared to the earlier version:

import formulaic
print(formulaic.__version__)

f = formulaic.Formula("x1 + x2 + x3 + x1:x2 + {x3**2}")
print(f)

for var in ['x1', 'x2', 'x3']:
    print(var, f.differentiate(var, use_sympy=True))

Here are the outputs:

1.1.0
1 + x1 + x2 + x3 + x3 ** 2 + x1:x2
x1 0 + 1 + 0 + 0 + 0 + x2
x2 0 + 0 + 1 + 0 + 0 + x1
x3 0 + 0 + 0 + 1 + 0 + (2*x3)

and

1.0.2
1 + x1 + x2 + x3 + x3 ** 2 + x1:x2
x1 root:
    [0, 1, 0, 0, 0, x2]
x2 root:
    [0, 0, 1, 0, 0, x1]
x3 root:
    [0, 0, 0, 1, (2*x3), 0]

Seems like the issue is related to the default ordering of the terms, as the correct results can be obtained by explicitly disabling it:

f = formulaic.Formula("x1 + x2 + x3 + x1:x2 + {x3**2}")
f.ordering = formulaic.formula.OrderingMethod.NONE
@matthewwardrop
Copy link
Owner

Hi @e-dorigatti ! Thanks for surfacing this! I'll make sure it gets fixed soon.

I didn't realise anyone other than myself had ever shown an interest in the differentiation features. How do you use them in practice?

@matthewwardrop matthewwardrop self-assigned this Dec 19, 2024
@matthewwardrop matthewwardrop added the bug Something isn't working label Dec 19, 2024
@e-dorigatti
Copy link
Author

Hi @matthewwardrop thank you for your prompt answer! We are using formulas in our bofire package to express the model for an experimental design, and use the differentiation features to optimize the design with respect to some optimality criteria. In practice we use this to plan experiments :)

@matthewwardrop
Copy link
Owner

Nice! Maybe I should upgrade them from experimental status then :).

@matthewwardrop
Copy link
Owner

This has been fixed and released as 1.1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants