Skip to content

Commit 7f837cf

Browse files
committed
improved precision and calculation methods, and modified readme
1 parent 152f6fc commit 7f837cf

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

README.md

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,63 @@
11
# labfis.py
2-
Small library (currently only one class) for uncertainty calculations. Made by and for Physics Laboratory students in IFSC, who can't use uncertainties.py because mean absolute deviation.
2+
3+
## Description
4+
5+
Small library (currently only one class) for uncertainty calculations and error propagation.
6+
7+
The uncertainty calculations are in accordance with gaussian’s propagation, as calculated by an analytical method:
8+
9+
$$\Delta_f = \sqrt{\left(\frac{\partial f}{\partial x}\right)^2{\Delta_x}^2 + \left(\frac{\partial f}{\partial y}\right)^2{\Delta_y}^2 + \left(\frac{\partial f}{\partial z}\right)^2{\Delta_z}^2 + ...}$$
10+
11+
Made by and for Physics Laboratory students in IFSC, who can't use uncertainties.py because of mean’s absolute deviation used in its calculation.
312

413
To get this library on google colaboratory:
5-
14+
615
```
716
!curl --remote-name \
8-
-H 'Accept: application/vnd.github.v3.raw' \
9-
--location https://raw.githubusercontent.com/phisgroup/labfis.py/development/labfis/main.py
17+
18+
-H 'Accept: application/vnd.github.v3.raw' \
19+
20+
--location https://raw.githubusercontent.com/phisgroup/labfis.py/development/labfis/main.py
21+
```
22+
23+
## Usage
24+
25+
Just import with `from labfis import labfloat` and create an *labfloat* object as:
26+
1027
```
28+
>>> from labfis import labfloat
29+
>>> a = labfloat(1,3)
30+
>>> b = labfloat(2,4)
31+
>>> a*b
32+
(2 ± 7)
33+
```
34+
35+
## Instalation
36+
37+
Intstall master releases with:
38+
39+
```
40+
pip install labfis
41+
```
42+
43+
Install development releases with:
44+
45+
```
46+
pip install git+https://github.com/phisgroup/labfis.py/tree/development
47+
```
48+
49+
## References
50+
51+
1. Kirchner, James. ["Data Analysis Toolkit #5: Uncertainty Analysis and Error Propagation"](http://seismo.berkeley.edu/~kirchner/eps_120/Toolkits/Toolkit_05.pdf) (PDF). _Berkeley Seismology Laboratory_. University of California. Retrieved 22 April 2016.
52+
2. [Goodman, Leo](https://en.wikipedia.org/wiki/Leo_Goodman "Leo Goodman") (1960). "On the Exact Variance of Products". _Journal of the American Statistical Association_. **55** (292): 708–713. [doi](https://en.wikipedia.org/wiki/Doi_(identifier) "Doi (identifier)"):[10.2307/2281592](https://doi.org/10.2307%2F2281592). [JSTOR](https://en.wikipedia.org/wiki/JSTOR_(identifier) "JSTOR (identifier)") [2281592](https://www.jstor.org/stable/2281592).
53+
3. Ochoa1,Benjamin; Belongie, Serge ["Covariance Propagation for Guided Matching"](http://vision.ucsd.edu/sites/default/files/ochoa06.pdf)
54+
4. Ku, H. H. (October 1966). ["Notes on the use of propagation of error formulas"](http://nistdigitalarchives.contentdm.oclc.org/cdm/compoundobject/collection/p16009coll6/id/99848/rec/1). _Journal of Research of the National Bureau of Standards_. **70C** (4): 262. [doi](https://en.wikipedia.org/wiki/Doi_(identifier) "Doi (identifier)"):[10.6028/jres.070c.025](https://doi.org/10.6028%2Fjres.070c.025). [ISSN](https://en.wikipedia.org/wiki/ISSN_(identifier) "ISSN (identifier)") [0022-4316](https://www.worldcat.org/issn/0022-4316). Retrieved 3 October 2012.
55+
5. Clifford, A. A. (1973). _Multivariate error analysis: a handbook of error propagation and calculation in many-parameter systems_. John Wiley & Sons. [ISBN](https://en.wikipedia.org/wiki/ISBN_(identifier) "ISBN (identifier)") [978-0470160558](https://en.wikipedia.org/wiki/Special:BookSources/978-0470160558 "Special:BookSources/978-0470160558").
56+
6. Lee, S. H.; Chen, W. (2009). "A comparative study of uncertainty propagation methods for black-box-type problems". _Structural and Multidisciplinary Optimization_. **37** (3): 239–253. [doi](https://en.wikipedia.org/wiki/Doi_(identifier) "Doi (identifier)"):[10.1007/s00158-008-0234-7](https://doi.org/10.1007%2Fs00158-008-0234-7).
57+
7. Johnson, Norman L.; Kotz, Samuel; Balakrishnan, Narayanaswamy (1994). _Continuous Univariate Distributions, Volume 1_. Wiley. p. 171. [ISBN](https://en.wikipedia.org/wiki/ISBN_(identifier) "ISBN (identifier)") [0-471-58495-9](https://en.wikipedia.org/wiki/Special:BookSources/0-471-58495-9 "Special:BookSources/0-471-58495-9").
58+
8. Lecomte, Christophe (May 2013). "Exact statistics of systems with uncertainties: an analytical theory of rank-one stochastic dynamic systems". _Journal of Sound and Vibrations_. **332** (11): 2750–2776. [doi](https://en.wikipedia.org/wiki/Doi_(identifier) "Doi (identifier)"):[10.1016/j.jsv.2012.12.009](https://doi.org/10.1016%2Fj.jsv.2012.12.009).
59+
9. ["A Summary of Error Propagation"](http://ipl.physics.harvard.edu/wp-uploads/2013/03/PS3_Error_Propagation_sp13.pdf) (PDF). p. 2. Retrieved 2016-04-04.
60+
10. ["Propagation of Uncertainty through Mathematical Operations"](http://web.mit.edu/fluids-modules/www/exper_techniques/2.Propagation_of_Uncertaint.pdf) (PDF). p. 5. Retrieved 2016-04-04.
61+
11. ["Strategies for Variance Estimation"](http://www.sagepub.com/upm-data/6427_Chapter_4__Lee_%28Analyzing%29_I_PDF_6.pdf) (PDF). p. 37. Retrieved 2013-01-18.
62+
12. Harris, Daniel C. (2003), [_Quantitative chemical analysis_](https://books.google.com/books?id=csTsQr-v0d0C&pg=PA56)(6th ed.), Macmillan, p. 56, [ISBN](https://en.wikipedia.org/wiki/ISBN_(identifier) "ISBN (identifier)") [978-0-7167-4464-1](https://en.wikipedia.org/wiki/Special:BookSources/978-0-7167-4464-1 "Special:BookSources/978-0-7167-4464-1")
63+
13. ["Error Propagation tutorial"](http://www.foothill.edu/psme/daley/tutorials_files/10.%20Error%20Propagation.pdf) (PDF). _Foothill College_. October 9, 2009. Retrieved 2012-03-01.

labfis/main.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from math import floor, ceil, trunc, log, log10
1+
from math import floor, ceil, trunc, log, log10, sqrt
22
from numbers import Number
33

44
class LabFloatError(Exception):
@@ -193,7 +193,7 @@ def __ge__(self,other):
193193

194194
def __add__(self, other):
195195
if isinstance(other, labfloat):
196-
return labfloat(self.mean + other.mean, self.uncertainty + other.uncertainty)
196+
return labfloat(self.mean + other.mean, sqrt(self.uncertainty ** 2 + other.uncertainty ** 2))
197197
if isinstance(other, Number):
198198
return labfloat(self.mean + other, self.uncertainty)
199199

@@ -205,13 +205,13 @@ def __iadd__(self, other):
205205

206206
def __sub__(self, other):
207207
if isinstance(other, labfloat):
208-
return labfloat(self.mean - other.mean, self.uncertainty + other.uncertainty)
208+
return labfloat(self.mean - other.mean, sqrt(self.uncertainty ** 2 + other.uncertainty ** 2))
209209
if isinstance(other, Number):
210210
return labfloat(self.mean - other, self.uncertainty)
211211

212212
def __rsub__(self, other):
213213
if isinstance(other, labfloat):
214-
pass
214+
return labfloat(other.mean - self.mean, sqrt(other.uncertainty ** 2 + self.uncertainty ** 2))
215215
if isinstance(other, Number):
216216
return labfloat(other - self.mean, self.uncertainty)
217217

@@ -220,9 +220,9 @@ def __isub__(self, other):
220220

221221
def __mul__(self, other):
222222
if isinstance(other, labfloat):
223-
return labfloat(self.mean * other.mean, self.mean * other.uncertainty + other.mean * self.uncertainty)
223+
return labfloat(self.mean * other.mean, sqrt((other.mean * self.uncertainty) ** 2 + (self.mean * other.uncertainty) ** 2))
224224
if isinstance(other, Number):
225-
return labfloat(self.mean * other, other * self.uncertainty)
225+
return labfloat(self.mean * other, abs(other * self.uncertainty))
226226

227227
def __rmul__(self, other):
228228
return self.__mul__(other)
@@ -232,9 +232,9 @@ def __imul__(self, other):
232232

233233
def __div__(self, other):
234234
if isinstance(other, labfloat):
235-
return labfloat(self.mean / other.mean, (self.mean * other.uncertainty + other.mean * self.uncertainty)/other.mean**2)
235+
return labfloat(self.mean / other.mean, sqrt((self.uncertainty / other.mean) ** 2 + (self.mean * other.uncertainty / (other.mean ** 2)) ** 2 ))
236236
if isinstance(other, Number):
237-
return labfloat(self.mean / other, self.uncertainty / other)
237+
return labfloat(self.mean / other, abs(self.uncertainty / other))
238238

239239
def __truediv__(self, other):
240240
return self.__div__(other)
@@ -247,24 +247,24 @@ def __itruediv__(self, other):
247247

248248
def __rdiv__(self, other):
249249
if isinstance(other, labfloat):
250-
return labfloat(other.mean / self.mean, (other.mean * self.uncertainty + self.mean * other.uncertainty)/self.mean**2)
250+
return labfloat(other.mean / self.mean, sqrt((other.uncertainty / self.mean) ** 2 + (other.mean * self.uncertainty / (self.mean ** 2)) ** 2 ))
251251
if isinstance(other, Number):
252-
return labfloat(other / self.mean, other * self.uncertainty / self.mean ** 2)
252+
return labfloat(other / self.mean, abs(other * self.uncertainty / self.mean ** 2))
253253

254254
def __rtruediv__(self, other):
255255
return self.__rdiv__(other)
256256

257257
def __pow__(self, other):
258258
if isinstance(other, labfloat):
259-
raise LabFloatError(0)
259+
return labfloat(self.mean ** other.mean, sqrt((other.mean * self.mean ** (other.mean - 1) * self.uncertainty) ** 2 + (self.mean ** other.mean * log(abs(self.mean)) * other.uncertainty) ** 2))
260260
if isinstance(other, Number):
261-
return labfloat(self.mean ** other, other * self.mean ** (other-1) * self.uncertainty)
261+
return labfloat(self.mean ** other, abs(other * self.mean ** (other - 1) * self.uncertainty))
262262

263263
def __rpow__(self, other):
264264
if isinstance(other, labfloat):
265-
raise LabFloatError(0)
266-
if isinstance(other, (float, int, hex, oct, complex)):
267-
return labfloat(other ** self.mean, other ** self.mean * log(other) * self.uncertainty)
265+
raise labfloat(other.mean ** self.mean, sqrt((self.mean * other.mean ** (self.mean - 1) * other.uncertainty) ** 2 + (other.mean ** self.mean * log(abs(other.mean)) * self.uncertainty) ** 2))
266+
if isinstance(other, Number):
267+
return labfloat(other ** self.mean, abs(other ** self.mean * log(abs(other)) * self.uncertainty))
268268

269269
def __ipow__(self, other):
270270
return self.__pow__(other)

0 commit comments

Comments
 (0)