fho
fho

Reputation: 6798

Is there a value with error library in Haskell?

I am looking for a library that provides a 'value with error' (eg x ± y). But searching for "Haskell xyz Error" only gives error handling libraries.

I would expect that such a library would provide common math operations (Num, Floating) where appropriate. The use case would be to get a error estimate from a calculation based on noisy sensor readings.

Update

I did some research and the term "propagation of uncertainty" came up. I found uncertainly-haskell which I'll try out soon. Are there other packages like this?

Upvotes: 5

Views: 182

Answers (3)

Wizek
Wizek

Reputation: 4993

The uncertain package seems to provide what you are looking for:

Some highlights from the readme:

Provides tools to manipulate numbers with inherent experimental/measurement uncertainty, and propagates them through functions based on principles from statistics.

 

Manipulate with error propagation

ghci> let x = 1.52 +/- 0.07
ghci> let y = 781.4 +/- 0.3
ghci> let z = 1.53e-1 `withPrecision` 3

ghci> cosh x
2.4 +/- 0.2

ghci> exp x / z * sin (y ** z)
10.9 +/- 0.9

ghci> pi + 3 * logBase x y
52 +/- 5

 

Create numbers

ghci> 1.52 +/- 0.07
1.52 +/- 7.0e-2

ghci> fromSamples [12.5, 12.7, 12.6, 12.6, 12.5]
12.58 +/- 7.0e-2

 

Comparisons

Note that this is very different from other libraries with similar data types (like from intervals and rounding); these do not attempt to maintain intervals or simply digit precisions; they instead are intended to model actual experimental and measurement data with their uncertainties, and apply functions to the data with the uncertainties and properly propagating the errors with sound statistical principles.

For a clear example, take

> (52 +/- 6) + (39 +/- 4)
91.0 +/- 7.0

In a library like intervals, this would result in 91 +/- 10 (that is, a lower bound of 46 + 35 and an upper bound of 58 + 43). However, with experimental data, errors in two independent samples tend to "cancel out", and result in an overall aggregate uncertainty in the sum of approximately 7.

Upvotes: 0

Alain O'Dea
Alain O'Dea

Reputation: 21686

The Data.Eq.Approximate module seems to be a fit for getting approximate equality.

Data.Eq.Approximate

Contents Type wrappers Classes for tolerance type annotations Absolute tolerance Relative tolerance Zero tolerance Tolerance annotations using Digits The purpose of this module is to provide newtype wrapper that allows one to effectively override the equality operator of a value so that it > is approximate rather than exact. For example, the type

type ApproximateDouble = AbsolutelyApproximateValue (Digits Five) Double defines an alias for a wrapper containing Doubles such that two doubles are equal if they are equal to within five decimals of accuracy; for > example, we have that

1 == (1+10^^(-6) :: ApproximateDouble) evaluates to True. Note that we did not need to wrap the value 1+10^^(-6) since AbsolutelyApproximateValue is an instance of Num. For > convenience, Num as well as many other of the numerical classes such as Real and Floating have all been derived for the wrappers defined in > this package so that one can conveniently use the wrapped values in the same way as one would use the values themselves.

Two kinds of wrappers are provided by this package.

Upvotes: 2

Tobias Brandt
Tobias Brandt

Reputation: 3413

Have a look at the intervals package.

Upvotes: 5

Related Questions