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

Make std() and variance() Able to Handle Arrays of Values with Units #1950

Closed
smith120bh opened this issue Aug 30, 2020 · 4 comments
Closed

Make std() and variance() Able to Handle Arrays of Values with Units #1950

smith120bh opened this issue Aug 30, 2020 · 4 comments

Comments

@smith120bh
Copy link

@smith120bh smith120bh commented Aug 30, 2020

Using the std() or variance() functions on an array of values with units assigned results in an error. This seems to be in contrast to every other function in the Statistics category (mean(), median(), mad(), etc), which seem to operate happily on arrays of unit'ed values.

I'm not sure if this is a bug or a feature request, but I'd like to request that support for unit'ed values be added to the std() and variance() functions.

Example of error

vals = [2 mm, 5 mm, 6 mm]
math.mean(vals)  
  // 4.3333333333333 mm
math.std(vals)
  // TypeError: Cannot calculate std, unexpected type of argument (type: Unit, value: {"mathjs":"Unit","value":2,"unit":"in","fixPrefix":false})

I note, however, that I can write a simple function with map() that calculates the standard deviation of this array without difficulty:

math.sqrt(math.sum(math.map(vals, f(x) = (x-mean(vals))^2))/math.size(vals)[1])
  // 1.6996731711976 mm

Or in the expression parser on the homepage:
image

@josdejong
Copy link
Owner

@josdejong josdejong commented Aug 30, 2020

Thanks for your suggestion Brooks, would be nice if we can extend std and variance with support for units. It's probably not too complicated to do.

Anyone interested in implementing this? Help would be welcome.

@rnd-debug
Copy link
Contributor

@rnd-debug rnd-debug commented Sep 5, 2020

@josdejong @smith120bh check this #1959
Largely inspired by #1644 : kudos to @clintonc

@josdejong
Copy link
Owner

@josdejong josdejong commented Sep 6, 2020

Really nice! Thanks @rnd-debug !

@josdejong
Copy link
Owner

@josdejong josdejong commented Sep 26, 2020

Published now in v7.3.0 🎉

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

3 participants