ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 26 Oct 2013 16:19:12 +0200Propagation of uncertaintyhttps://ask.sagemath.org/question/10660/propagation-of-uncertainty/Is there any simple method in sage to do a calculation with uncertainties? Something like this: $(10\pm2) * (3\pm1) = (30\pm18)$. Or with a more "sophisticated" formula: $(30\pm11.67)$ because of $(10 \times 3 \times \sqrt{(2/10)^2 + (1/3)^2} = 11.67)$. Both ways are taken from [here](http://www.rit.edu/~w-uphysi/uncertainties/Uncertaintiespart2.html), but the second way is also described on [wikipedia](https://en.wikipedia.org/wiki/Propagation_of_uncertainty).
---
To do such calculations I have already seen a method using [RIF](http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfi.html)'s like the following:
R=RIF(8,12)*RIF(2,4)
R.str(error_digits=2); R.lower(); R.upper(); R.center()
Which outputs:
32.?16
16.0000000000000
48.0000000000000
32.0000000000000
But with as one can see the *center* result is $32$ ($\frac{16+48}{2}$) instead of $30$ ($10*3$). Also I think it is a bit complex, because one has to calculate the upper an lower limits before, instead of a simple thing like:
uncertain(10,2)
(And as last point, I think result with the question mark is not very nice)
---
If one of my calculations is wrong please let me know, because I am new to error calculations.Sat, 26 Oct 2013 15:06:51 +0200https://ask.sagemath.org/question/10660/propagation-of-uncertainty/Answer by tmonteil for <p>Is there any simple method in sage to do a calculation with uncertainties? Something like this: $(10\pm2) * (3\pm1) = (30\pm18)$. Or with a more "sophisticated" formula: $(30\pm11.67)$ because of $(10 \times 3 \times \sqrt{(2/10)^2 + (1/3)^2} = 11.67)$. Both ways are taken from <a href="http://www.rit.edu/~w-uphysi/uncertainties/Uncertaintiespart2.html">here</a>, but the second way is also described on <a href="https://en.wikipedia.org/wiki/Propagation_of_uncertainty">wikipedia</a>.</p>
<hr/>
<p>To do such calculations I have already seen a method using <a href="http://www.sagemath.org/doc/reference/rings_numerical/sage/rings/real_mpfi.html">RIF</a>'s like the following:</p>
<pre><code>R=RIF(8,12)*RIF(2,4)
R.str(error_digits=2); R.lower(); R.upper(); R.center()
</code></pre>
<p>Which outputs:</p>
<pre><code>32.?16
16.0000000000000
48.0000000000000
32.0000000000000
</code></pre>
<p>But with as one can see the <em>center</em> result is $32$ ($\frac{16+48}{2}$) instead of $30$ ($10*3$). Also I think it is a bit complex, because one has to calculate the upper an lower limits before, instead of a simple thing like:</p>
<pre><code>uncertain(10,2)
</code></pre>
<p>(And as last point, I think result with the question mark is not very nice)</p>
<hr/>
<p>If one of my calculations is wrong please let me know, because I am new to error calculations.</p>
https://ask.sagemath.org/question/10660/propagation-of-uncertainty/?answer=15613#post-id-15613Your approach with `RIF` is the right one.
You should understand that the product of the middles is usually not equal to the middle of the products, not every map is flat! Actually, the result $32\pm 16$ given by `RIF` is more accurate than your $30\pm 18$, since the interval $[16,48]$ is strictly contained in the interval $[12, 48]$, and both are valid results.
The result using the "sophisticated" formula is wrong: the smallest product between $10\pm 2$ and $3\pm 1$ is $8\times 2 = 16$, and is smaller than $30-11.67 = 18.33$. It uses a truncated Taylor estimation, so it can only be used to have a quick rough estimate of the error, not a guaranteed upper bound, see the [Caveats and warnings](https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Caveats_and_warnings) section.
If you want to use custom functions instead of `RIF` defaults, you can define:
sage: uncertain = lambda value, error : RIF(value-error, value+error)
sage: value_error = lambda r : (r.center(), r.absolute_diameter()/2)
Then you can do:
sage: R = uncertain(10,2) * uncertain(3,1)
sage: value_error(R)
(32.0000000000000, 16.0000000000000)
Sat, 26 Oct 2013 15:45:15 +0200https://ask.sagemath.org/question/10660/propagation-of-uncertainty/?answer=15613#post-id-15613Comment by TobiMarg for <p>Your approach with <code>RIF</code> is the right one. </p>
<p>You should understand that the product of the middles is usually not equal to the middle of the products, not every map is flat! Actually, the result $32\pm 16$ given by <code>RIF</code> is more accurate than your $30\pm 18$, since the interval $[16,48]$ is strictly contained in the interval $[12, 48]$, and both are valid results.</p>
<p>The result using the "sophisticated" formula is wrong: the smallest product between $10\pm 2$ and $3\pm 1$ is $8\times 2 = 16$, and is smaller than $30-11.67 = 18.33$. It uses a truncated Taylor estimation, so it can only be used to have a quick rough estimate of the error, not a guaranteed upper bound, see the <a href="https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Caveats_and_warnings">Caveats and warnings</a> section.</p>
<p>If you want to use custom functions instead of <code>RIF</code> defaults, you can define:</p>
<pre><code>sage: uncertain = lambda value, error : RIF(value-error, value+error)
sage: value_error = lambda r : (r.center(), r.absolute_diameter()/2)
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: R = uncertain(10,2) * uncertain(3,1)
sage: value_error(R)
(32.0000000000000, 16.0000000000000)
</code></pre>
https://ask.sagemath.org/question/10660/propagation-of-uncertainty/?comment=16848#post-id-16848Thank you for your answer. To the second point (wrong method/calculation): I haven't read the whole Wikipedia article, because I got it first from the other site. Thanks for pointing that out. Also your "custom functions" are really nice.Sat, 26 Oct 2013 16:19:12 +0200https://ask.sagemath.org/question/10660/propagation-of-uncertainty/?comment=16848#post-id-16848