Skip to content

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

@brookshsmith

Description

@brookshsmith

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions