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

Derivatives of transformations #341

Merged
merged 7 commits into from
Nov 6, 2023
Merged

Derivatives of transformations #341

merged 7 commits into from
Nov 6, 2023

Conversation

mjskay
Copy link
Contributor

@mjskay mjskay commented Apr 3, 2022

This PR closes #322 by supplying derivatives of transformation functions and inverses where possible.

Specifically, it:

  • Adds two optional, may-be-NULL fields to transform objects: $d_transform and $d_inverse. These functions are the derivatives of $transform and $inverse.
  • Supplies derivatives for all existing numeric transformations
  • Applies the chain rule to give derivatives for compose_trans() (unless any derivatives in the transformation are missing, in which case the corresponding derivative function is NULL).
  • Includes tests for all of the above.

Some possible discussion points:

  • I initially used $transform_deriv and $inverse_deriv, but found that existing tests used partial matching on $trans and $inv, so these names caused failures. I wasn't sure if other legacy code relies on this partial matching, so I conservatively went with the names $d_transform and $d_inverse. I am not sure these are the best names and am happy to change them.
  • I did not supply derivatives for the date transform. Arguably those derivatives could be a constant like identity_trans(), at least for applications like I am imagining these will be used for (plotting densities), but I went with the more conservative choice of not including it.

@thomasp85
Copy link
Member

@mjskay would you update the PR to remove conflicts?

@thomasp85 thomasp85 added this to the scales 1.3.0 milestone Nov 2, 2023
@teunbrand
Copy link
Contributor

I'd like to point out a small interaction with #360 here.
That PR adds the inverse hyperbolic sine transformation.
I believe those derivatives should be 1 / sqrt(x^2 + 1) for the transformation and cosh() for the inverse.
I'm not sure what gets merged first or which PR should implement this (if any), but it might be neat to include.

@pearsonca
Copy link
Contributor

if this goes in first, happy to update #360 to use this feature (and double check derivates etc)

@mjskay
Copy link
Contributor Author

mjskay commented Nov 2, 2023

Conflicts resolved here.

Thanks @teunbrand @pearsonca. Similarly, if #360 goes first I'd be happy to add those derivatives here; either way works for me.

@thomasp85
Copy link
Member

We'll do #360 first. Will let you know once merged

@pearsonca
Copy link
Contributor

We'll do #360 first. Will let you know once merged

oops, saw this after writing derivates into 360 - i can deal with rebasing that once this is merged?

@mjskay
Copy link
Contributor Author

mjskay commented Nov 2, 2023

@pearsonca I think @thomasp85 was suggesting to merge #360 without the derivatives and then deal with derivatives here, which probably makes sense in case any feedback on this PR causes changes to the derivative stuff (e.g., name changes or what have you).

@thomasp85
Copy link
Member

#360 has been merged - can you update this PR with derivatives for that and then we can get this in

@mjskay
Copy link
Contributor Author

mjskay commented Nov 3, 2023

Done!

Also thanks for all the hard work shepherding through all these issues and PRs :).

@thomasp85 thomasp85 merged commit 483a82a into r-lib:main Nov 6, 2023
11 of 12 checks passed
@thomasp85
Copy link
Member

Thank you for all the work

@mjskay mjskay deleted the deriv branch November 6, 2023 15:28
plietar added a commit to plietar/bench that referenced this pull request Feb 19, 2024
The latest release of the scales package has added a pair of optional
arguments, `d_transform` and `d_inverse`, in the middle of the parameter
list for `trans_new` (see r-lib/scales#341). As
a consequence, this has shifted the position of the `breaks` parameter
from 4th to 6th.

Because the `bench_time_trans` and `bench_bytes_trans` functions from
this package were passing in the breaks object positionally, this change
in the scales package means that the breaks object is now matched with
the new `d_transform` formal parameter and it ends up being ignored.

This commit changes the arguments to `trans_new` from being positional
to being named, which should be more robust to future changes to the
function.
plietar added a commit to plietar/bench that referenced this pull request May 21, 2024
The latest release of the scales package has added a pair of optional
arguments, `d_transform` and `d_inverse`, in the middle of the parameter
list for `trans_new` (see r-lib/scales#341). As
a consequence, this has shifted the position of the `breaks` parameter
from 4th to 6th.

Because the `bench_time_trans` and `bench_bytes_trans` functions from
this package were passing in the breaks object positionally, this change
in the scales package means that the breaks object is now matched with
the new `d_transform` formal parameter and it ends up being ignored.

This commit changes the arguments to `trans_new` from being positional
to being named, which should be more robust to future changes to the
function.
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

Successfully merging this pull request may close these issues.

Feature request (and offer for help): optionally supply derivatives of transforms and inverses
4 participants