Tilman Tilman - 2 months ago 4x
Python Question

Sum with Pythons uncertainties giving a diferent result than expected

A friend of mine is evaluating data with Pythons package

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

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
handle this? And how can I reproduce in any way what uncertainties does!?


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

for i in range(0,len(array)):
    for j in range(0,len(array)):


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!)