numkit.observables — Observables as quantities with errors

Example showing how to use QuantityWithError:
>>> from numkit.observables import QuantityWithError
>>> a = QuantityWithError(2.0, 1.0)
>>> a2 = QuantityWithError(2.0, 1.0)  # 2nd independent measurement of a
>>> a3 = QuantityWithError(2.0, 1.0)  # 3rd independent measurement of a
>>> b = QuantityWithError(-1, 0.5)
>>> a+a
4 (2)
>>> a+a2
4 (1.41421)
>>> (a+a+a)/3
2 (1)
>>> (a+a2+a3)/3
2 (0.57735)
>>> a/b
-2 (1.41421)

Note that each quantity has an identity: it makes a difference to the error of a combined quantity such as a+a if the inputs are independent measurements of the same.

See also

Various packages that describe quantities with units, in particular quantities.

Classes

class numkit.observables.QuantityWithError(value, error=None, qid=None, **kwargs)

Number with error and basic error propagation arithmetic.

The quantity is assumed to be a mean of an observable (value) with an associated (Gaussian) error error (which is the sqrt of the variance variance of the data).

The covariance is not taken into account in the error propagation (i.e. all quantities are assumed to be uncorrelated) with the exception of the case of binary relations of the quantity with itself. For instance, a*a is correctly interpreted as a**2). However, this behaviour is not guaranteed to work for any complicated expression.

static asQuantityWithError(other)

Return a QuantityWithError.

If the input is already a QuantityWithError then it is returned itself. This is important because a new quantity x’ would be considered independent from the original one x and thus lead to different error estimates for quantities such as x*x versus x*x’.

astuple()

Return tuple (value,error).

copy()

Create a new quantity with the same value and error.

deepcopy()

Create an exact copy with the same identity.

property error

Error of a quantity as sqrt of the variance.

isSame(other)

Check if other is 100% correlated with self.

True if
`- other is the same observable (instance)
  • other was derived from self without using any other independent quantities with errors, e.g.

    >>> a = QuantityWithError(1.0, 0.5)
    >>> b = a**2 - a*2
    >>> a.isSame(b)
    True
    
False if
  • other is a scalar (without an error), or

  • other was computed from self without involvement of any other observables.

TODO

How should one treat the case when a quantity is used again in an operation, e.g.

c = a + b
d = c/a

How to compute the error on d? What should the result for c.isSame(a) be?

numkit.observables.QuantityWithError.value

Value of the observable \(A\), typically the mean of a number of observations, \(\langle A \rangle\).

numkit.observables.QuantityWithError.error

Error of the observable, typically the square root of the variance of the observations, \(\sqrt{\langle (A - \langle A \rangle)^2 \rangle}\).

Changing the error automatically changes the variance.

numkit.observables.QuantityWithError.variance

Variance \(\langle (A - \langle A \rangle)^2 \rangle\) of the observable. Changing the variance automatically changes the error.

class numkit.observables.QID(iterable=None)

Identity of a QuantityWithError.

QID(iterable) –> identity QID() –> None

The anonymous identity is None, anything else is a frozenset().

The QID can contain arbitray (but unique) identifiers in the iterable; however, strings are treated as individual objects and not as iterables.

The error arithmetic encapsulated by the operator-overloading of QuantityWithError builds new QIDs by accumulating QIDs of the terms of the expression. In a certain sense, the “history” of a quantity becomes its “identity”.

union(x)

Return the union of sets as a new set.

(i.e. all elements that are in either set.)

Functions

numkit.observables.iterable(obj)

Returns True if obj can be iterated over and is not a string.

numkit.observables.asiterable(obj)

Returns obj so that it can be iterated over; a string is not treated as iterable