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

ENH: Add scipy.stats.rms() #16179

Open
endolith opened this issue May 14, 2022 · 3 comments · May be fixed by #16189
Open

ENH: Add scipy.stats.rms() #16179

endolith opened this issue May 14, 2022 · 3 comments · May be fixed by #16189
Labels
enhancement A new feature or improvement scipy.signal scipy.stats

Comments

@endolith
Copy link
Member

endolith commented May 14, 2022

Is your feature request related to a problem? Please describe.

Long ago there was a matplotlib.mlab.rms_flat() that I used frequently to measure signals, but it no longer exists, so I copy and paste the function whenever needed:

def rms_flat(a):
    """
    Return the root mean square of all the elements of *a*, flattened out.
    """
    return np.sqrt(np.mean(np.abs(a) ** 2))

But this is a mild pain, and there are similar functions like np.mean() that don't require any copying and pasting.

Ironically, when this was requested in numpy, someone responded with "no that's too simple, just implement it inline" and then suggested an implementation that is incorrect...

Describe the solution you'd like.

So I think it should be in scipy.stats.rms(), alongside similar things like scipy.stats.gmean() and scipy.stats.mode().

Also we then have the convenience of axis=, etc.

Describe alternatives you've considered.

  • Copying and pasting the same function into each file.
  • Maybe there's something equivalent in scipy.stats already and creating an alias or more-googleable documentation would be enough

Additional context (e.g. screenshots, GIFs)

Also called "quadratic mean" and is a special case of generalized mean, requested in #12456

@endolith endolith added the enhancement A new feature or improvement label May 14, 2022
@tupui
Copy link
Member

tupui commented May 14, 2022

Totally agree. Nothing is trivial and it's not because it can be written in a single line of code that everyone can write such a line. Such functions are really helpful and everyone uses them when they exists. E.g. in scikit-learn they have a lot of simple metrics.

Maybe this would fit better in a more general place than stats.

@endolith
Copy link
Member Author

OK I started it.

Hmm... Going back to 2004 the code always had abs() in it, which I assume is for complex numbers.

I think this would make it consistent with the definition of power, which RMS is proportional to? But I don't have a great reference saying that it should be there.

  • Wolfram Alpha, given rms([1, 2i, −3, −4i]) returns ≈1.58114 i
  • Qalculate, given rms([1  (2 × i)  −3  −(4 × i)]) returns ≈ 1.581 138 830
  • matplotlib, given rms_flat([1, 2j, -3, -4j]) returns 2.7386127875258306

@endolith endolith linked a pull request May 15, 2022 that will close this issue
@tupui
Copy link
Member

tupui commented May 15, 2022

@nschloe I think you play a lot with complex numbers. Can you help here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.signal scipy.stats
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants