{-# LANGUAGE CPP #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoImplicitPrelude #-}
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 ::
PrimMonad m =>
Uncert Double ->
Gen (PrimState m) ->
m Double
sampleUncert :: forall (m :: * -> *).
PrimMonad m =>
Uncert Double -> Gen (PrimState m) -> m Double
sampleUncert (Uncert Double -> (Double, Double)
forall a. Floating a => Uncert a -> (a, a)
uMeanStd -> (Double
x, Double
dx)) = Double -> Double -> Gen (PrimState m) -> m Double
forall g (m :: * -> *).
StatefulGen g m =>
Double -> Double -> g -> m Double
normal Double
x Double
dx
{-# INLINEABLE sampleUncert #-}
liftU ::
PrimMonad m =>
(Double -> Double) ->
Uncert Double ->
Gen (PrimState m) ->
m (Uncert Double)
liftU :: forall (m :: * -> *).
PrimMonad m =>
(Double -> Double)
-> Uncert Double -> Gen (PrimState m) -> m (Uncert Double)
liftU = Int
-> (Double -> Double)
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
forall (m :: * -> *).
PrimMonad m =>
Int
-> (Double -> Double)
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU' Int
1000
{-# INLINE liftU #-}
liftUF ::
(Traversable f, PrimMonad m) =>
(f Double -> Double) ->
f (Uncert Double) ->
Gen (PrimState m) ->
m (Uncert Double)
liftUF :: forall (f :: * -> *) (m :: * -> *).
(Traversable f, PrimMonad m) =>
(f Double -> Double)
-> f (Uncert Double) -> Gen (PrimState m) -> m (Uncert Double)
liftUF = Int
-> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
forall (f :: * -> *) (m :: * -> *).
(Traversable f, PrimMonad m) =>
Int
-> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
liftUF' Int
1000
{-# INLINE liftUF #-}
liftU2 ::
PrimMonad m =>
(Double -> Double -> Double) ->
Uncert Double ->
Uncert Double ->
Gen (PrimState m) ->
m (Uncert Double)
liftU2 :: forall (m :: * -> *).
PrimMonad m =>
(Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU2 = Int
-> (Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
forall (m :: * -> *).
PrimMonad m =>
Int
-> (Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU2' Int
1000
{-# INLINE liftU2 #-}
liftU3 ::
PrimMonad m =>
(Double -> Double -> Double -> Double) ->
Uncert Double ->
Uncert Double ->
Uncert Double ->
Gen (PrimState m) ->
m (Uncert Double)
liftU3 :: forall (m :: * -> *).
PrimMonad m =>
(Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU3 = Int
-> (Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
forall (m :: * -> *).
PrimMonad m =>
Int
-> (Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU3' Int
1000
{-# INLINE liftU3 #-}
liftU4 ::
(PrimMonad m, Applicative m) =>
(Double -> Double -> Double -> Double -> Double) ->
Uncert Double ->
Uncert Double ->
Uncert Double ->
Uncert Double ->
Gen (PrimState m) ->
m (Uncert Double)
liftU4 :: forall (m :: * -> *).
(PrimMonad m, Applicative m) =>
(Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU4 = Int
-> (Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
forall (m :: * -> *).
PrimMonad m =>
Int
-> (Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU4' Int
1000
{-# INLINE liftU4 #-}
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)
liftU5 :: forall (m :: * -> *).
PrimMonad m =>
(Double -> Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU5 = Int
-> (Double -> Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
forall (m :: * -> *).
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)
liftU5' Int
1000
{-# INLINE liftU5 #-}
liftU' ::
PrimMonad m =>
Int ->
(Double -> Double) ->
Uncert Double ->
Gen (PrimState m) ->
m (Uncert Double)
liftU' :: forall (m :: * -> *).
PrimMonad m =>
Int
-> (Double -> Double)
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU' Int
n Double -> Double
f Uncert Double
u Gen (PrimState m)
g = [Double] -> Uncert Double
forall a. Fractional a => [a] -> Uncert a
fromSamples ([Double] -> Uncert Double) -> m [Double] -> m (Uncert Double)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m Double -> m [Double]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n m Double
samp
where
samp :: m Double
samp = Double -> Double
f (Double -> Double) -> m Double -> m Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Uncert Double -> Gen (PrimState m) -> m Double
forall (m :: * -> *).
PrimMonad m =>
Uncert Double -> Gen (PrimState m) -> m Double
sampleUncert Uncert Double
u Gen (PrimState m)
g
{-# INLINEABLE liftU' #-}
liftUF' ::
(Traversable f, PrimMonad m) =>
Int ->
(f Double -> Double) ->
f (Uncert Double) ->
Gen (PrimState m) ->
m (Uncert Double)
liftUF' :: forall (f :: * -> *) (m :: * -> *).
(Traversable f, PrimMonad m) =>
Int
-> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
liftUF' Int
n f Double -> Double
f f (Uncert Double)
us Gen (PrimState m)
g = [Double] -> Uncert Double
forall a. Fractional a => [a] -> Uncert a
fromSamples ([Double] -> Uncert Double) -> m [Double] -> m (Uncert Double)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m Double -> m [Double]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n m Double
samp
where
samp :: m Double
samp = f Double -> Double
f (f Double -> Double) -> m (f Double) -> m Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Uncert Double -> m Double) -> f (Uncert Double) -> m (f Double)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> f a -> f (f b)
traverse (Uncert Double -> Gen (PrimState m) -> m Double
forall (m :: * -> *).
PrimMonad m =>
Uncert Double -> Gen (PrimState m) -> m Double
`sampleUncert` Gen (PrimState m)
g) f (Uncert Double)
us
{-# INLINEABLE liftUF' #-}
liftU2' ::
PrimMonad m =>
Int ->
(Double -> Double -> Double) ->
Uncert Double ->
Uncert Double ->
Gen (PrimState m) ->
m (Uncert Double)
liftU2' :: forall (m :: * -> *).
PrimMonad m =>
Int
-> (Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU2' Int
n Double -> Double -> Double
f Uncert Double
x Uncert Double
y = Int
-> (H2 Double -> Double)
-> H2 (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
forall (f :: * -> *) (m :: * -> *).
(Traversable f, PrimMonad m) =>
Int
-> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
liftUF' Int
n ((Double -> Double -> Double) -> H2 Double -> Double
forall a. (a -> a -> a) -> H2 a -> a
uncurryH2 Double -> Double -> Double
f) (Uncert Double -> Uncert Double -> H2 (Uncert Double)
forall a. a -> a -> H2 a
H2 Uncert Double
x Uncert Double
y)
{-# INLINEABLE liftU2' #-}
liftU3' ::
PrimMonad m =>
Int ->
(Double -> Double -> Double -> Double) ->
Uncert Double ->
Uncert Double ->
Uncert Double ->
Gen (PrimState m) ->
m (Uncert Double)
liftU3' :: forall (m :: * -> *).
PrimMonad m =>
Int
-> (Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU3' Int
n Double -> Double -> Double -> Double
f Uncert Double
x Uncert Double
y Uncert Double
z = Int
-> (H3 Double -> Double)
-> H3 (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
forall (f :: * -> *) (m :: * -> *).
(Traversable f, PrimMonad m) =>
Int
-> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
liftUF' Int
n ((Double -> Double -> Double -> Double) -> H3 Double -> Double
forall a. (a -> a -> a -> a) -> H3 a -> a
uncurryH3 Double -> Double -> Double -> Double
f) (Uncert Double
-> Uncert Double -> Uncert Double -> H3 (Uncert Double)
forall a. a -> a -> a -> H3 a
H3 Uncert Double
x Uncert Double
y Uncert Double
z)
{-# INLINEABLE liftU3' #-}
liftU4' ::
PrimMonad m =>
Int ->
(Double -> Double -> Double -> Double -> Double) ->
Uncert Double ->
Uncert Double ->
Uncert Double ->
Uncert Double ->
Gen (PrimState m) ->
m (Uncert Double)
liftU4' :: forall (m :: * -> *).
PrimMonad m =>
Int
-> (Double -> Double -> Double -> Double -> Double)
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Gen (PrimState m)
-> m (Uncert Double)
liftU4' Int
n Double -> Double -> Double -> Double -> Double
f Uncert Double
x Uncert Double
y Uncert Double
z Uncert Double
a = Int
-> (H4 Double -> Double)
-> H4 (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
forall (f :: * -> *) (m :: * -> *).
(Traversable f, PrimMonad m) =>
Int
-> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
liftUF' Int
n ((Double -> Double -> Double -> Double -> Double)
-> H4 Double -> Double
forall a. (a -> a -> a -> a -> a) -> H4 a -> a
uncurryH4 Double -> Double -> Double -> Double -> Double
f) (Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> H4 (Uncert Double)
forall a. a -> a -> a -> a -> H4 a
H4 Uncert Double
x Uncert Double
y Uncert Double
z Uncert Double
a)
{-# INLINEABLE liftU4' #-}
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)
liftU5' :: forall (m :: * -> *).
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)
liftU5' Int
n Double -> Double -> Double -> Double -> Double -> Double
f Uncert Double
x Uncert Double
y Uncert Double
z Uncert Double
a Uncert Double
b = Int
-> (H5 Double -> Double)
-> H5 (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
forall (f :: * -> *) (m :: * -> *).
(Traversable f, PrimMonad m) =>
Int
-> (f Double -> Double)
-> f (Uncert Double)
-> Gen (PrimState m)
-> m (Uncert Double)
liftUF' Int
n ((Double -> Double -> Double -> Double -> Double -> Double)
-> H5 Double -> Double
forall a. (a -> a -> a -> a -> a -> a) -> H5 a -> a
uncurryH5 Double -> Double -> Double -> Double -> Double -> Double
f) (Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> Uncert Double
-> H5 (Uncert Double)
forall a. a -> a -> a -> a -> a -> H5 a
H5 Uncert Double
x Uncert Double
y Uncert Double
z Uncert Double
a Uncert Double
b)
{-# INLINEABLE liftU5' #-}