Skip to content

Instantly share code, notes, and snippets.

@rsnemmen
Created October 23, 2014 21:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsnemmen/2faa974ef0d51e22a4c8 to your computer and use it in GitHub Desktop.
Save rsnemmen/2faa974ef0d51e22a4c8 to your computer and use it in GitHub Desktop.
Doing statistical error propagation with Python
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment