Skip to content

mean value of arrays w/ UncertainQuantities #77

Open

Description

@mankoff

I have a 2D array and there is one standard deviation that describes this array, as calculated by np.std(arr). However, UncertainQuantity requires the uncertainty to be the same size as the array. I guess this makes sense since the uncertainty applies to every element, and some arrays may have different uncertainties with different elements. Still it would be convenient to be able to code like this:

x = np.array([1,2,3]) * pq.m
y = pq.UncertainQuantity( x, pq.m, np.std(x))
print y

That doesn't work because np.std(x) isn't the same size as x. My solution is to code this:

y = pq.UncertainQuantity( x, x.units, np.zeros_like(x)+np.std(x))

Now I perform some operations on the array y:

x = np.array([1,3,1,3,1,3]) * pq.m
y = pq.UncertainQuantity( x, x.units, np.zeros_like(x)+np.std(x))
print "\ny: ", y
print "\nsum(y): ", np.sum(y)
print "\nmean(y): ", np.mean(y)
print "\nnanmean(y): ", np.nanmean(y)
print "\nmymean(y): ", np.sum(y)/np.size(y)
#+END_SRC

y:  [ 1.  3.  1.  3.  1.  3.] m
+/-[ 1.  1.  1.  1.  1.  1.] m (1 sigma)

sum(y):  12.0 m
+/-2.44948974278 m (1 sigma)

mean(y):  2.0 m

nanmean(y):  2.0 m
+/-2.44948974278 m (1 sigma)

mymean(y):  2.0 m
+/-0.408248290464 m (1 sigma)

Issues seem to be

  • np.mean drops the uncertainty (but np.nanmean does not)
  • np.nanmean returns an incorrect result. The result from np.sum(y)/np.size(y) is correct.

Now I'm going to look into the code to see if I can easily fix this...

Activity

  1. mankoff commented on Mar 23, 2014

    @mankoff
    ContributorAuthor

    Patch for np.mean in #79 but I'm not sure how to handle np.nanmean. Although this would be very useful. Hints appreciated. I'll send another pull if I can code it up.

  2. mankoff commented on Mar 24, 2014

    @mankoff
    ContributorAuthor

    I've implemented nanmean for uncertain quantities, and nanstd for regular quantities. They are included in the pull.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions