Wednesday, April 11, 2012

How to easily do error propagation with Python

You can easily do error propagation using the uncertainties package in Python, without having to estimate analytically the propagated error or doing Monte Carlo simulations.

Example: Suppose you have two arrays x and y which have associated uncertainties errx and erry. Using x and y, you calculate some function z = f(x, y) which can be arbitrarily complicated (but can be expressed in an analytical form) and you want to estimate the resulting uncertainty errz in z from errx and erry.

The script below

  • defines the arrays x, y, errx and erry using numpy
  • defines a function z=log10(x+y^2) for illustration purposes
  • demonstrates how to invoke 'uncertainties' in order to estimate the uncertainty in z from errx and erry (i.e., errz = f(errx, erry) )


Requirements:



 import uncertainties as unc  
 import uncertainties.unumpy as unumpy  
 import numpy  
 import nemmen  
   
 # Defines x and y  
 x=numpy.linspace(0,10,50)  
 y=numpy.linspace(15,20,50)  
   
 # Defines the error arrays, values follow a normal distribution  
 # (method random_normal defined in http://astropython.blogspot.com/2012/04/how-to-generate-array-of-random-numbers.html)  
 errx=nemmen.random_normal(0.1,0.2,50);     errx=numpy.abs(errx)  
 erry=nemmen.random_normal(0.3,0.2,50);     erry=numpy.abs(erry)  
   
 # Defines special arrays holding the values *and* errors  
 x=unumpy.uarray(( x, errx ))  
 y=unumpy.uarray(( y, erry ))  
   
 """  
 Now any operation that you carry on xerr and yerr will   
 automatically propagate the associated errors, as long  
 as you use the methods provided with uncertainties.unumpy  
 instead of using the numpy methods.  
   
 Let's for instance define z as   
 z = log10(x+y**2)  
 and estimate errz.  
 """  
 z=unumpy.log10(x+y**2)  
   
 # Print the propagated error errz  
 errz=unumpy.std_devs(z)  
 print errz  

Update Oct. 23rd 2014: code snippet available on github.

8 comments:

  1. Thank you for illustrating how to use my uncertainties package with arrays! The code looks good to me.

    I would like to mention that the final errors errz actually depend on x and y too: errz = f(x, errx, y, erry) would be more appropriate.

    One can also write the simpler "from uncertainties import unumpy". :)

    ReplyDelete
    Replies
    1. Dear Eric,

      Your package is very useful. I have been using it lately a lot in my work!

      Thanks for pointing that out.

      Cheers.

      Delete
  2. Great post.
    What if the errors are correlated ?

    ReplyDelete
  3. Great post.
    What if the errors are correlated ?

    ReplyDelete
  4. Correlations are automatically and transparently taken into account (see http://pythonhosted.org/uncertainties/ for more information). For example, x-x is strictly equal to zero, even if x has an uncertainty.

    ReplyDelete
  5. I just found out about the uncertainties package via this blog (via Google, of course) and could not be happier post-installation. As an astronomy PhD student, I feel that error propagation directs too much of my time towards the nitty gritty and away from doing the exciting parts of science. THANK YOU.

    ReplyDelete
  6. excellent blog and really useful for me to cross-check my monte-carlo error propagator, cheers!

    ReplyDelete