ENH: ratio function to mimic diff
![](https://secure.gravatar.com/avatar/6f1e0a1c41913d3a2cc3cfe6b7260e91.jpg?s=120&d=mm&r=g)
I have created PR#9481 to introduce a `ratio` function that behaves very similarly to `diff`, except that it divides successive elements instead of subtracting them. It has some handling built in for zero division, as well as the ability to select between `/` and `//` operators. There is currently no masked version. Perhaps someone could suggest a simple mechanism for hooking np.ma.true_divide and np.ma.floor_divide in as the operators instead of the regular np.* versions. Please let me know your thoughts. Regards, -Joe
![](https://secure.gravatar.com/avatar/93a76a800ef6c5919baa8ba91120ee98.jpg?s=120&d=mm&r=g)
This is an interesting idea, but I don't understand the use cases for this function. In particular, what would you use n-th order ratios for? One use case I can think of is estimating the slope of a log-scaled plot. But here exp(diff(log(x))) is an easy substitute. I guess ratio() would work in cases where values are both positive and negative, but again I don't know when that would be useful. If your signal crosses zero, ratios are likely to diverge. On Fri, Jul 28, 2017 at 3:25 PM Joseph Fox-Rabinovitz < jfoxrabinovitz@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/6f1e0a1c41913d3a2cc3cfe6b7260e91.jpg?s=120&d=mm&r=g)
On Jul 29, 2017 12:23, "Stephan Hoyer" <shoyer@gmail.com> wrote: This is an interesting idea, but I don't understand the use cases for this function. In particular, what would you use n-th order ratios for? There is no good use case for the nth order differences that I am aware of. I just added that to mimic the way diff works. One use case I can think of is estimating the slope of a log-scaled plot. But here exp(diff(log(x))) is an easy substitute. My original motivation was very similar to that. I was looking for the largest geometric gap in a sorted sequence of numbers. Taking logs and exponents seemed like a sledge hammer for that task. I guess ratio() would work in cases where values are both positive and negative, but again I don't know when that would be useful. If your signal crosses zero, ratios are likely to diverge. They would, but looking for sign changes is easy, and I added an argument to flag actual zeros. On Fri, Jul 28, 2017 at 3:25 PM Joseph Fox-Rabinovitz < jfoxrabinovitz@gmail.com> wrote:
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/97c543aca1ac7bbcfb5279d0300c8330.jpg?s=120&d=mm&r=g)
I'd also like to see a more detailed motivation for this. And, if it is useful, then that would make 3 operations that have special case pairwise moving window variants (subtract, floor_divide, true_divide). 3 is a lot of special cases. Should there instead be a generic mechanism for doing this for arbitrary binary operations? -n On Jul 28, 2017 3:25 PM, "Joseph Fox-Rabinovitz" <jfoxrabinovitz@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/6f1e0a1c41913d3a2cc3cfe6b7260e91.jpg?s=120&d=mm&r=g)
On Jul 29, 2017 12:55, "Nathaniel Smith" <njs@pobox.com> wrote: I'd also like to see a more detailed motivation for this. And, if it is useful, then that would make 3 operations that have special case pairwise moving window variants (subtract, floor_divide, true_divide). 3 is a lot of special cases. Should there instead be a generic mechanism for doing this for arbitrary binary operations? Perhaps another method for ufuncs of two arguments? I agree that there should be a generic mechanism since a lack of one is what is preventing me from applying this to masked arrays immediately. It would have to take in some domain filter, like many of the translated masked functions do. A ufunc could provide that transparently. -n On Jul 28, 2017 3:25 PM, "Joseph Fox-Rabinovitz" <jfoxrabinovitz@gmail.com> wrote:
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/93a76a800ef6c5919baa8ba91120ee98.jpg?s=120&d=mm&r=g)
This is an interesting idea, but I don't understand the use cases for this function. In particular, what would you use n-th order ratios for? One use case I can think of is estimating the slope of a log-scaled plot. But here exp(diff(log(x))) is an easy substitute. I guess ratio() would work in cases where values are both positive and negative, but again I don't know when that would be useful. If your signal crosses zero, ratios are likely to diverge. On Fri, Jul 28, 2017 at 3:25 PM Joseph Fox-Rabinovitz < jfoxrabinovitz@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/6f1e0a1c41913d3a2cc3cfe6b7260e91.jpg?s=120&d=mm&r=g)
On Jul 29, 2017 12:23, "Stephan Hoyer" <shoyer@gmail.com> wrote: This is an interesting idea, but I don't understand the use cases for this function. In particular, what would you use n-th order ratios for? There is no good use case for the nth order differences that I am aware of. I just added that to mimic the way diff works. One use case I can think of is estimating the slope of a log-scaled plot. But here exp(diff(log(x))) is an easy substitute. My original motivation was very similar to that. I was looking for the largest geometric gap in a sorted sequence of numbers. Taking logs and exponents seemed like a sledge hammer for that task. I guess ratio() would work in cases where values are both positive and negative, but again I don't know when that would be useful. If your signal crosses zero, ratios are likely to diverge. They would, but looking for sign changes is easy, and I added an argument to flag actual zeros. On Fri, Jul 28, 2017 at 3:25 PM Joseph Fox-Rabinovitz < jfoxrabinovitz@gmail.com> wrote:
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/97c543aca1ac7bbcfb5279d0300c8330.jpg?s=120&d=mm&r=g)
I'd also like to see a more detailed motivation for this. And, if it is useful, then that would make 3 operations that have special case pairwise moving window variants (subtract, floor_divide, true_divide). 3 is a lot of special cases. Should there instead be a generic mechanism for doing this for arbitrary binary operations? -n On Jul 28, 2017 3:25 PM, "Joseph Fox-Rabinovitz" <jfoxrabinovitz@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/6f1e0a1c41913d3a2cc3cfe6b7260e91.jpg?s=120&d=mm&r=g)
On Jul 29, 2017 12:55, "Nathaniel Smith" <njs@pobox.com> wrote: I'd also like to see a more detailed motivation for this. And, if it is useful, then that would make 3 operations that have special case pairwise moving window variants (subtract, floor_divide, true_divide). 3 is a lot of special cases. Should there instead be a generic mechanism for doing this for arbitrary binary operations? Perhaps another method for ufuncs of two arguments? I agree that there should be a generic mechanism since a lack of one is what is preventing me from applying this to masked arrays immediately. It would have to take in some domain filter, like many of the translated masked functions do. A ufunc could provide that transparently. -n On Jul 28, 2017 3:25 PM, "Joseph Fox-Rabinovitz" <jfoxrabinovitz@gmail.com> wrote:
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
participants (3)
-
Joseph Fox-Rabinovitz
-
Nathaniel Smith
-
Stephan Hoyer