probabilistic-numerics / probnum

Probabilistic Numerics in Python.
http://probnum.org
MIT License
436 stars 56 forks source link

Implement functions like `np.sum` or `np.exp` on random variables #194

Open marvinpfoertner opened 4 years ago

marvinpfoertner commented 4 years ago

We should implement numpy-style functions on random variables. For instance, if rv1 is a Normal, then rv2 = probnum.exp(rv1) should be a LogNormal. These functions could reside in probnum.random_variables._functions and could be implemented in methods of the random variable subclasses, e.g. Normal.exp. Furthermore, we should provide generic fallback implementations of these methods which at least allow sampling from the random variable, e.g. as sample = lambda: np.exp(norm_rv.sample()).

marvinpfoertner commented 4 years ago

Any thoughts? @JonathanWenger @pnkraemer @nathanaelbosch

pnkraemer commented 4 years ago

Thanks for the initiative, I think that something like this is a good idea (even though it is not needed as of now, at least not in the parts I am working with) . I am not sure how self-explanatory rv.exp would be, but that is, of course, just a technicality...

marvinpfoertner commented 4 years ago

Offering rv.sum() would follow the numpy API, so I figured rv.exp() is maybe a good analogy. We could of course also offer the function probnum.exp(rv), but it should call rv.exp() internally or otherwise we would have a gigantic conditional switching between different random variables. @pnkraemer Do you agree with this?

pnkraemer commented 4 years ago

Fair enough, at least internal the implementation should be in the random variable (but I am still not sure about a "public" rv.exp()). No need to discuss this in all detail now, though (IMO).

JonathanWenger commented 4 years ago

In my opinion any function in NumPy we can support for random variables should share the exact same usage and interface. This would mean that both ~some_rv.exp()~ some_rv.sum() and ~pn.exp(some_rv)~ pn.sum(some_rv) are implemented.

pnkraemer commented 4 years ago

But exp() is not an ndarray method, just a numpy function (https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html). Wouldnt the most numpy-similar solution then be to not have it as a method but just as a function? (I understand that the actual implementation would make sense as method, but potentially as a private one?)

JonathanWenger commented 4 years ago

Good point. Yes i would be in favor of the implementation being in, e.g. Normal._exp(). For some_rv.sum(), some_rv.max() the above still goes.