Tilman Tilman - 3 months ago 8
Python Question

Sum with Pythons uncertainties giving a diferent result than expected

A friend of mine is evaluating data with Pythons package

uncertainties
. I am her statistics consulter, and I have come up with a weird result in her code.

sum(array)
and
sqrt(sum(unumpy.std_devs(array)**2))
yield different results, with the second one being the variance method as usually used in engineering.

Now, I know that the variance approach is only suited for when the error is small compared to the partial derivate (because of the Taylor series) which isn't given in this case, but how does
uncertainties
handle this? And how can I reproduce in any way what uncertainties does!?

Answer

This results due to my array being an AffineScalarFunc (as opposed to a Variable), and thus they not only store the value but also all the variables that the value depends on [1].

Now, my values are not fully independent (which wasn't clear at all at first sight*), and thus sum(array) also considers the off-diagonal elements of my covariance matrix in accordance to this formula (sorry that the article is in German, but English Wikipedias formula isn't as intuitive), whereas sqrt(sum(unumpy.std_devs(array)**2)) obviously doesn't and just adds up the diagonal elements.

A way to reproduce what uncertainties does is:

from uncertainties import covariance_matrix

sum=0
for i in range(0,len(array)):
    for j in range(0,len(array)):
        sum+=covariancematrix(array)[i][j]

print(sqrt(sum))

And then unumpy.std_devs(sum(array))==sqrt(sum) is True.

*Correlation due to the use of data taken from the same interpolation (of measurements) and because the length of a measurement was calculated as the difference of two times (and meassurement were consecutive, so the times are now correlated!)

Comments