{-# LANGUAGE CPP #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ViewPatterns #-}
module Numeric.Uncertain.MonteCarlo
(
sampleUncert
, liftU, liftU2, liftU3, liftU4, liftU5, liftUF
, liftU', liftU2', liftU3', liftU4', liftU5', liftUF'
)
where
import Control.Monad
import Control.Monad.Primitive
import Data.Hople
import Numeric.Uncertain (Uncert, fromSamples, uMeanStd)
import Prelude.Compat
import System.Random.MWC
import System.Random.MWC.Distributions
sampleUncert
#if __GLASGOW_HASKELL__ < 710
:: (PrimMonad m, Functor m)
#else
:: PrimMonad m
#endif
=> Uncert Double
-> Gen (PrimState m)
-> m Double
sampleUncert (uMeanStd->(x, dx)) g = normal x dx g
{-# INLINABLE sampleUncert #-}
liftU
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Functor m)
#endif
=> (Double -> Double)
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU = liftU' 1000
{-# INLINE liftU #-}
liftUF
#if __GLASGOW_HASKELL__ >= 710
:: (Traversable f, PrimMonad m)
#else
:: (Traversable f, PrimMonad m, Applicative m)
#endif
=> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
liftUF = liftUF' 1000
{-# INLINE liftUF #-}
liftU2
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Applicative m)
#endif
=> (Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU2 = liftU2' 1000
{-# INLINE liftU2 #-}
liftU3
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Applicative m)
#endif
=> (Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU3 = liftU3' 1000
{-# INLINE liftU3 #-}
liftU4
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Applicative m)
#endif
=> (Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU4 = liftU4' 1000
{-# INLINE liftU4 #-}
liftU5
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Applicative m)
#endif
=> (Double -> Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU5 = liftU5' 1000
{-# INLINE liftU5 #-}
liftU'
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Functor m)
#endif
=> Int
-> (Double -> Double)
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU' n f u g = fromSamples <$> replicateM n samp
where
samp = f <$> sampleUncert u g
{-# INLINABLE liftU' #-}
liftUF'
#if __GLASGOW_HASKELL__ >= 710
:: (Traversable f, PrimMonad m)
#else
:: (Traversable f, PrimMonad m, Applicative m)
#endif
=> Int
-> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
liftUF' n f us g = fromSamples <$> replicateM n samp
where
samp = f <$> traverse (flip sampleUncert g) us
{-# INLINABLE liftUF' #-}
liftU2'
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Applicative m)
#endif
=> Int
-> (Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU2' n f x y = liftUF' n (uncurryH2 f) (H2 x y)
{-# INLINABLE liftU2' #-}
liftU3'
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Applicative m)
#endif
=> Int
-> (Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU3' n f x y z = liftUF' n (uncurryH3 f) (H3 x y z)
{-# INLINABLE liftU3' #-}
liftU4'
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Applicative m)
#endif
=> Int
-> (Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU4' n f x y z a = liftUF' n (uncurryH4 f) (H4 x y z a)
{-# INLINABLE liftU4' #-}
liftU5'
#if __GLASGOW_HASKELL__ >= 710
:: PrimMonad m
#else
:: (PrimMonad m, Applicative m)
#endif
=> Int
-> (Double -> Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU5' n f x y z a b = liftUF' n (uncurryH5 f) (H5 x y z a b)
{-# INLINEABLE liftU5' #-}