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

Implement more types of measurable elemwise transformations #6631

Closed
16 tasks done
ricardoV94 opened this issue Mar 29, 2023 · 13 comments
Closed
16 tasks done

Implement more types of measurable elemwise transformations #6631

ricardoV94 opened this issue Mar 29, 2023 · 13 comments

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Mar 29, 2023

We already have support for

  • add (and subtraction via addition)
  • mul (and division and neg via multiplication)
  • pow (inludes reciprocal, sqrt, sqr, etc...)
  • exp
  • log
  • abs

Other operations we could support

valid_scalar_types = (Exp, Log, Add, Mul, Pow, Abs)

Other operations we can support via rewrites to already supported forms

  • exp2, log2, log10
  • log1p
  • expm1
  • log1mexp
  • log1pexp (softplus)
  • sigmoid

Example of such rewrites:

@node_rewriter([neg])
def measurable_neg_to_product(fgraph, node):
"""Convert negation of `MeasurableVariable`s to product with `-1`."""
inp = node.inputs[0]
if not (inp.owner and isinstance(inp.owner.op, MeasurableVariable)):
return None
rv_map_feature: Optional[PreserveRVMappings] = getattr(fgraph, "preserve_rv_mappings", None)
if rv_map_feature is None:
return None # pragma: no cover
# Only apply this rewrite if the variable is unvalued
if inp in rv_map_feature.rv_values:
return None # pragma: no cover
return [pt.mul(inp, -1.0)]

@LukeLB
Copy link
Contributor

LukeLB commented Apr 1, 2023

@ricardoV94 is this just a continuation of the work done here https://github.com/pymc-devs/pymc/pull/6414/files? If so I can follow what I did there and knock a few of these off

@ricardoV94
Copy link
Member Author

Yes, 👍

@LukeLB
Copy link
Contributor

LukeLB commented Apr 4, 2023

I've created Transform classes for cosh, sinh, tanh, erf, erfc however there doesn't appear to be an inverse function available for erfcx in pytensor or pymc.math. Any suggestions on what to do here? It feels like writing a new function in pytensor for erfcxinv may be overkill for this issue...

@ricardoV94
Copy link
Member Author

ricardoV94 commented Apr 5, 2023

You can exploit the equivalence between erfcx and erfc https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.erfcx.html

So you don't need a specific transform, just to rewrite it into that form.

@LukeLB
Copy link
Contributor

LukeLB commented Apr 5, 2023

So if I rewrite it into that form I can get the foward function, but I don't understand how to get the backward function from that? This discussion on the inverse of erfcx in Matlab seems to suggest writing a specific functon https://www.mathworks.com/matlabcentral/answers/302915-inverse-of-erfcx-scaled-complementary-error-function

@ricardoV94
Copy link
Member Author

ricardoV94 commented Apr 5, 2023

@LukeLB my bad, I didn't notice that x shows in both terms (of course it does). So the forward form can't be easily inverted as you said.

Writing a specific function seems like a nice (but perhaps low impact) challenge. Up to you if you want to pursue it ;)

@LukeLB
Copy link
Contributor

LukeLB commented Apr 5, 2023

Not to worry, it did have me confused for a bit there!

This is all a learning experience for me so happy to add value (even if small) wherever I can. What do you reccomend I do?

Presumably open a new issue in PyTensor and link to this issue? Or can I just directly put in a PR on PyTensor for the function?

@ricardoV94
Copy link
Member Author

I think you can implement it directly in pymc using Scan and Switch statements as building blocks.

@ricardoV94
Copy link
Member Author

@LukeLB any interest in picking some of the new ones I added: arcsinh and arctanh?

A user on discourse was asking how to implement the sinh-arcsinh normal distribution: https://discourse.pymc.io/t/the-sinh-arcsinh-normal-distribution/12136

The only ingredient missing is the arcsinh I think

@LukeLB
Copy link
Contributor

LukeLB commented Jun 12, 2023

@ricardoV94 really sorry I seem to have messed up by notifications when switching email addresses and didn't get an email through for this. Yes, I'd be interested in implementing these additional transformations! I'll have a look this week.

@ricardoV94
Copy link
Member Author

No problem @LukeLB, still very much welcome and timely :)

@LukeLB
Copy link
Contributor

LukeLB commented Jun 12, 2023

Fab, I'll let you know how I get on :)

@ricardoV94
Copy link
Member Author

Closed via #6826

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

No branches or pull requests

2 participants