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

generalized mean #19341

Closed
nschloe opened this issue Jun 25, 2021 · 5 comments
Closed

generalized mean #19341

nschloe opened this issue Jun 25, 2021 · 5 comments

Comments

@nschloe
Copy link
Contributor

nschloe commented Jun 25, 2021

np.mean computes the arithmetic mean, but there are many other interesting means, e.g, the root mean square, harmonic mean, or the geometric mean. They can all be parametrized with

$$M_p(x) = \left(\frac{1}{n} \sum_{i=1}^n x_i^p\right)^{1/p}$$

See Wikipedia on generalized means.

@bashtage
Copy link
Contributor

Already available in np.linalg.norm.

@nschloe
Copy link
Contributor Author

nschloe commented Jun 25, 2021

Yeah, except for the 1/n term and the fact that the absolute value is taken of all x. np.linalg.norm also doesn't work correctly for p=0 and +-np.inf. It also has problems for for p which are large in magnitude (due to overflow), but that possibly warrants a separate bug report.

Edit: cleaned up formatting (mattip)

@nschloe
Copy link
Contributor Author

nschloe commented Jun 25, 2021

For future reference, I added a mean implementation to npx which correctly handles all the cases mentioned above.

@qbarthelemy
Copy link
Contributor

qbarthelemy commented Jun 1, 2022

This mean is available in the latest Scipy:
http://scipy.github.io/devdocs/reference/generated/scipy.stats.pmean.html
added by scipy/scipy#15729
@nschloe

@mattip
Copy link
Member

mattip commented Jun 2, 2022

I will close this as there are alternatives available. @nschloe if you feel np.linalg.norm could be improved please open a separate issue.

@mattip mattip closed this as completed Jun 2, 2022
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

5 participants