micropython-ulab icon indicating copy to clipboard operation
micropython-ulab copied to clipboard

add numpy.cov

Open v923z opened this issue 5 years ago • 3 comments

https://numpy.org/doc/stable/reference/generated/numpy.cov.html

Starting point based on https://github.com/v923z/micropython-ulab/issues/183

v923z avatar Oct 28 '20 18:10 v923z

@street-grease-coder If you look at the notes section of https://numpy.org/doc/stable/reference/generated/numpy.cov.html, isn't everything you need already implemented in ulab?

v923z avatar Feb 17 '21 09:02 v923z

if there's a covariance function this should work indeed, sorry if I missed this! I will check it out but a bit short on time at the moment... sorry

street-grease-coder avatar Feb 17 '21 10:02 street-grease-coder

@street-grease-coder What I meant is that you can calculate the covariance matrix in 5 numpy statements

m = np.arange(10, dtype=np.float64)
>>> f = np.arange(10) * 2
>>> a = np.arange(10) ** 2.
>>> ddof = 1
>>> w = f * a
>>> v1 = np.sum(w)
>>> v2 = np.sum(w * a)
>>> m -= np.sum(m * w, axis=None, keepdims=True) / v1
>>> cov = np.dot(m * w, m.T) * v1 / (v1**2 - ddof * v2)

We might have to implement the keepdims keyword, though. Since the implementation of this function is in python even in numpy, I don't see, why we should move to C.

Two similar issues have been raised in https://github.com/v923z/micropython-ulab/issues/320, and https://github.com/v923z/micropython-ulab/issues/278, and I think, we might have to add a snippets directory for numpy functions that are implemented in python. Do you want to try your hand at it?

v923z avatar Feb 17 '21 17:02 v923z