Mathjs: Make std() and variance() Able to Handle Arrays of Values with Units

Created on 30 Aug 2020  路  4Comments  路  Source: josdejong/mathjs

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

feature help wanted

Most helpful comment

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

All 4 comments

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.

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

Really nice! Thanks @rnd-debug !

Published now in v7.3.0 馃帀

Was this page helpful?
0 / 5 - 0 ratings