uncertain-0.4.0.1: Manipulating numbers with inherent experimental/measurement uncertainty
Copyright(c) Justin Le 2016
LicenseBSD3
Maintainerjustin@jle.im
Stabilityexperimental
Portabilitynon-portable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Numeric.Uncertain.MonteCarlo

Description

Provides an interface for computing and propagating uncertainty by using Monte Carlo simulations. These may be more accurate than the normal propagators if the third-order taylor series expansion terms are non-negligible.

Basically simulates sampling from the distribution represented by the given Uncerts, applying the function of interest, and aggregating the mean and standard deviation of the results. x +/- dx is treated as a random variable whose probability density is the normal distribution with mean x and standard deviation dx.

This module attempts to duplicate the API offered by Numeric.Uncertain and is meant to be imported qualified alongside Numeric.Uncertain

import           Numeric.Uncertain
import qualified Numeric.Uncertain.MonteCarlo as MC

Actions are parameterized over all PrimMonad instances, so can be run under both ST and IO, making it suitable for exploratory purposes. All functions require a Gen from System.Random.MWC for random value generation purposes.

ghci> import qualified Numeric.Uncertain.MonteCarlo as MC
ghci> import System.Random.MWC
ghci> let x = 1.52 +/- 0.07
ghci> let y = 781.4 +/- 0.3
ghci> let z = 1.53e-1 `withPrecision' 3
ghci> g <- create
ghci> cosh x
2.4 +/- 0.2
ghci> MC.liftU cosh x g
2.4 +/- 0.2
ghci> exp x / z * sin (y ** z)
10.9 +/- 0.9
ghci> MC.liftU3 (\a b c -> exp a / c * sin (b**c)) x y z g
10.8 +/- 1.0
ghci> pi + 3 * logBase x y
52 +/- 5
ghci> MC.liftU2 (\a b -> pi + 3 * logBase a b) x y g
51 +/- 5
Synopsis

Sampling from an Uncert

sampleUncert :: PrimMonad m => Uncert Double -> Gen (PrimState m) -> m Double Source #

Sample a random Double from the distribution specified by an Uncert Double. x +/- dx is treated as a random variable whose probability density is the normal distribution with mean x and standard deviation dx.

Lifting functions via Monte Carlo simulation

Fixed iterations

liftU :: PrimMonad m => (Double -> Double) -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Lifts a numeric function over an Uncert using a Monte Carlo simulation with 1000 samples.

ghci> g <- create
ghci> MC.liftU (\x -> log x ^ 2) (12.2 +/- 0.5) g
6.3 +/- 0.2

liftU2 :: PrimMonad m => (Double -> Double -> Double) -> Uncert Double -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Lifts a two-argument (curried) function over two Uncerts using a Monte Carlo simulation with 1000 samples.

ghci> g <- create
ghci> MC.liftU2 (\x y -> x**y) (13.5 +- 0.1) (1.64 +- 0.08)
70 +/- 20

liftU3 :: PrimMonad m => (Double -> Double -> Double -> Double) -> Uncert Double -> Uncert Double -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Lifts a three-argument (curried) function over three Uncerts. See liftU2 and liftUF for more details.

liftU4 :: (PrimMonad m, Applicative m) => (Double -> Double -> Double -> Double -> Double) -> Uncert Double -> Uncert Double -> Uncert Double -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Lifts a four-argument (curried) function over four Uncerts. See liftU2 and liftUF for more details.

liftU5 :: PrimMonad m => (Double -> Double -> Double -> Double -> Double -> Double) -> Uncert Double -> Uncert Double -> Uncert Double -> Uncert Double -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Lifts a five-argument (curried) function over five Uncerts. See liftU2 and liftUF for more details.

liftUF :: (Traversable f, PrimMonad m) => (f Double -> Double) -> f (Uncert Double) -> Gen (PrimState m) -> m (Uncert Double) Source #

Lifts a multivariate numeric function on a container (given as an f a -> a) to work on a container of Uncerts using a Monte Carlo simulation with 1000 samples.

ghci> g <- create
ghci> M.liftUF (\[x,y,z] -> x*y+z) [12.2 +- 0.5, 56 +- 2, 0.12 +/- 0.08] g
680 +/- 40

Variable iterations

liftU' :: PrimMonad m => Int -> (Double -> Double) -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Like liftU, but allows you to specify the number of samples to run the Monte Carlo simulation with.

liftU2' :: PrimMonad m => Int -> (Double -> Double -> Double) -> Uncert Double -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Like liftU2, but allows you to specify the number of samples to run the Monte Carlo simulation with.

liftU3' :: PrimMonad m => Int -> (Double -> Double -> Double -> Double) -> Uncert Double -> Uncert Double -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Like liftU3, but allows you to specify the number of samples to run the Monte Carlo simulation with.

liftU4' :: PrimMonad m => Int -> (Double -> Double -> Double -> Double -> Double) -> Uncert Double -> Uncert Double -> Uncert Double -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Like liftU4, but allows you to specify the number of samples to run the Monte Carlo simulation with.

liftU5' :: PrimMonad m => Int -> (Double -> Double -> Double -> Double -> Double -> Double) -> Uncert Double -> Uncert Double -> Uncert Double -> Uncert Double -> Uncert Double -> Gen (PrimState m) -> m (Uncert Double) Source #

Like liftU5, but allows you to specify the number of samples to run the Monte Carlo simulation with.

liftUF' :: (Traversable f, PrimMonad m) => Int -> (f Double -> Double) -> f (Uncert Double) -> Gen (PrimState m) -> m (Uncert Double) Source #

Like liftUF, but allows you to specify the number of samples to run the Monte Carlo simulation with.