-- |
-- Module     : Simulation.Aivika.Trans.Generator.Primitive
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- This helper module defines primitives for generating random numbers.
--
module Simulation.Aivika.Trans.Generator.Primitive where

import Control.Monad
import Control.Monad.Trans

import Simulation.Aivika.Generator (DiscretePDF)

-- | Generate an uniform random number with the specified minimum and maximum.
generateUniform01 :: Monad m
                     => m Double
                     -- ^ the uniform random number ~ U (0, 1)
                     -> Double
                     -- ^ minimum
                     -> Double
                     -- ^ maximum
                     -> m Double
{-# INLINE generateUniform01 #-}
generateUniform01 :: forall (m :: * -> *).
Monad m =>
m Double -> Double -> Double -> m Double
generateUniform01 m Double
g Double
min Double
max =
  do Double
x <- m Double
g
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double
min forall a. Num a => a -> a -> a
+ Double
x forall a. Num a => a -> a -> a
* (Double
max forall a. Num a => a -> a -> a
- Double
min)

-- | Generate an uniform random number with the specified minimum and maximum.
generateUniformInt01 :: Monad m
                        => m Double
                        -- ^ the uniform random number ~ U (0, 1)
                        -> Int
                        -- ^ minimum
                        -> Int
                        -- ^ maximum
                        -> m Int
{-# INLINE generateUniformInt01 #-}
generateUniformInt01 :: forall (m :: * -> *). Monad m => m Double -> Int -> Int -> m Int
generateUniformInt01 m Double
g Int
min Int
max =
  do Double
x <- m Double
g
     let min' :: Double
min' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
min forall a. Num a => a -> a -> a
- Double
0.5
         max' :: Double
max' = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
max forall a. Num a => a -> a -> a
+ Double
0.5
         z :: Int
z    = forall a b. (RealFrac a, Integral b) => a -> b
round (Double
min' forall a. Num a => a -> a -> a
+ Double
x forall a. Num a => a -> a -> a
* (Double
max' forall a. Num a => a -> a -> a
- Double
min'))
         z' :: Int
z'   = if Int
z forall a. Ord a => a -> a -> Bool
< Int
min
                then Int
min
                else if Int
z forall a. Ord a => a -> a -> Bool
> Int
max
                     then Int
max
                     else Int
z
     forall (m :: * -> *) a. Monad m => a -> m a
return Int
z'

-- | Generate the triangular random number by the specified minimum, median and maximum.
generateTriangular01 :: Monad m
                        => m Double
                        -- ^ the uniform random number ~ U (0, 1)
                        -> Double
                        -- ^ minimum
                        -> Double
                        -- ^ median
                        -> Double
                        -- ^ maximum
                        -> m Double
{-# INLINE generateTriangular01 #-}
generateTriangular01 :: forall (m :: * -> *).
Monad m =>
m Double -> Double -> Double -> Double -> m Double
generateTriangular01 m Double
g Double
min Double
median Double
max =
  do Double
x <- m Double
g
     if Double
x forall a. Ord a => a -> a -> Bool
<= (Double
median forall a. Num a => a -> a -> a
- Double
min) forall a. Fractional a => a -> a -> a
/ (Double
max forall a. Num a => a -> a -> a
- Double
min)
       then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double
min forall a. Num a => a -> a -> a
+ forall a. Floating a => a -> a
sqrt ((Double
median forall a. Num a => a -> a -> a
- Double
min) forall a. Num a => a -> a -> a
* (Double
max forall a. Num a => a -> a -> a
- Double
min) forall a. Num a => a -> a -> a
* Double
x)
       else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double
max forall a. Num a => a -> a -> a
- forall a. Floating a => a -> a
sqrt ((Double
max forall a. Num a => a -> a -> a
- Double
median) forall a. Num a => a -> a -> a
* (Double
max forall a. Num a => a -> a -> a
- Double
min) forall a. Num a => a -> a -> a
* (Double
1 forall a. Num a => a -> a -> a
- Double
x))

-- | Generate a normal random number by the specified generator, mean and variance.
generateNormal01 :: Monad m
                    => m Double
                    -- ^ the normal random number ~ N (0, 1)
                    -> Double
                    -- ^ mean
                    -> Double
                    -- ^ variance
                    -> m Double
{-# INLINE generateNormal01 #-}
generateNormal01 :: forall (m :: * -> *).
Monad m =>
m Double -> Double -> Double -> m Double
generateNormal01 m Double
g Double
mu Double
nu =
  do Double
x <- m Double
g
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double
mu forall a. Num a => a -> a -> a
+ Double
nu forall a. Num a => a -> a -> a
* Double
x

-- | Generate the lognormal random number derived from a normal distribution with
-- the specified generator, mean and variance.
generateLogNormal01 :: Monad m
                       => m Double
                       -- ^ the normal random number ~ N (0, 1)
                       -> Double
                       -- ^ mean
                       -> Double
                       -- ^ variance
                       -> m Double
{-# INLINE generateLogNormal01 #-}
generateLogNormal01 :: forall (m :: * -> *).
Monad m =>
m Double -> Double -> Double -> m Double
generateLogNormal01 m Double
g Double
mu Double
nu =
  do Double
x <- m Double
g
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Floating a => a -> a
exp (Double
mu forall a. Num a => a -> a -> a
+ Double
nu forall a. Num a => a -> a -> a
* Double
x)

-- | Return the exponential random number with the specified mean.
generateExponential01 :: Monad m
                         => m Double
                         -- ^ the uniform random number ~ U (0, 1)
                         -> Double
                         -- ^ the mean
                         -> m Double
{-# INLINE generateExponential01 #-}
generateExponential01 :: forall (m :: * -> *). Monad m => m Double -> Double -> m Double
generateExponential01 m Double
g Double
mu =
  do Double
x <- m Double
g
     forall (m :: * -> *) a. Monad m => a -> m a
return (- forall a. Floating a => a -> a
log Double
x forall a. Num a => a -> a -> a
* Double
mu)

-- | Return the Erlang random number.
generateErlang01 :: Monad m
                    => m Double
                    -- ^ the uniform random number ~ U (0, 1)
                    -> Double
                    -- ^ the scale
                    -> Int
                    -- ^ the shape
                    -> m Double
{-# INLINABLE generateErlang01 #-}
generateErlang01 :: forall (m :: * -> *).
Monad m =>
m Double -> Double -> Int -> m Double
generateErlang01 m Double
g Double
beta Int
m =
  do Double
x <- forall {t}. (Ord t, Num t) => t -> Double -> m Double
loop Int
m Double
1
     forall (m :: * -> *) a. Monad m => a -> m a
return (- forall a. Floating a => a -> a
log Double
x forall a. Num a => a -> a -> a
* Double
beta)
       where loop :: t -> Double -> m Double
loop t
m Double
acc
               | t
m forall a. Ord a => a -> a -> Bool
< t
0     = forall a. HasCallStack => [Char] -> a
error [Char]
"Negative shape: generateErlang."
               | t
m forall a. Eq a => a -> a -> Bool
== t
0    = forall (m :: * -> *) a. Monad m => a -> m a
return Double
acc
               | Bool
otherwise = do Double
x <- m Double
g
                                t -> Double -> m Double
loop (t
m forall a. Num a => a -> a -> a
- t
1) (Double
x forall a. Num a => a -> a -> a
* Double
acc)

-- | Generate the Poisson random number with the specified mean.
generatePoisson01 :: Monad m
                     => m Double
                     -- ^ the uniform random number ~ U (0, 1)
                     -> Double
                     -- ^ the mean
                     -> m Int
{-# INLINABLE generatePoisson01 #-}
generatePoisson01 :: forall (m :: * -> *). Monad m => m Double -> Double -> m Int
generatePoisson01 m Double
g Double
mu =
  do Double
prob0 <- m Double
g
     let loop :: Double -> Double -> t -> m t
loop Double
prob Double
prod t
acc
           | Double
prob forall a. Ord a => a -> a -> Bool
<= Double
prod = forall (m :: * -> *) a. Monad m => a -> m a
return t
acc
           | Bool
otherwise    = Double -> Double -> t -> m t
loop
                            (Double
prob forall a. Num a => a -> a -> a
- Double
prod)
                            (Double
prod forall a. Num a => a -> a -> a
* Double
mu forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral (t
acc forall a. Num a => a -> a -> a
+ t
1))
                            (t
acc forall a. Num a => a -> a -> a
+ t
1)
     forall {m :: * -> *} {t}.
(Monad m, Integral t) =>
Double -> Double -> t -> m t
loop Double
prob0 (forall a. Floating a => a -> a
exp (- Double
mu)) Int
0

-- | Generate a binomial random number with the specified probability and number of trials. 
generateBinomial01 :: Monad m
                      => m Double
                      -- ^ the uniform random number ~ U (0, 1)
                      -> Double 
                      -- ^ the probability
                      -> Int
                      -- ^ the number of trials
                      -> m Int
{-# INLINABLE generateBinomial01 #-}
generateBinomial01 :: forall (m :: * -> *). Monad m => m Double -> Double -> Int -> m Int
generateBinomial01 m Double
g Double
prob Int
trials = forall {t} {t}. (Ord t, Num t, Num t) => t -> t -> m t
loop Int
trials Int
0 where
  loop :: t -> t -> m t
loop t
n t
acc
    | t
n forall a. Ord a => a -> a -> Bool
< t
0     = forall a. HasCallStack => [Char] -> a
error [Char]
"Negative number of trials: generateBinomial."
    | t
n forall a. Eq a => a -> a -> Bool
== t
0    = forall (m :: * -> *) a. Monad m => a -> m a
return t
acc
    | Bool
otherwise = do Double
x <- m Double
g
                     if Double
x forall a. Ord a => a -> a -> Bool
<= Double
prob
                       then t -> t -> m t
loop (t
n forall a. Num a => a -> a -> a
- t
1) (t
acc forall a. Num a => a -> a -> a
+ t
1)
                       else t -> t -> m t
loop (t
n forall a. Num a => a -> a -> a
- t
1) t
acc

-- | Generate a random number from the Gamma distribution using Marsaglia and Tsang method.
generateGamma01 :: Monad m
                   => m Double
                   -- ^ the normal random number ~ N (0,1)
                   -> m Double
                   -- ^ the uniform random number ~ U (0, 1)
                   -> Double
                   -- ^ the shape parameter (kappa) 
                   -> Double
                   -- ^ the scale parameter (theta)
                   -> m Double
{-# INLINABLE generateGamma01 #-}
generateGamma01 :: forall (m :: * -> *).
Monad m =>
m Double -> m Double -> Double -> Double -> m Double
generateGamma01 m Double
gn m Double
gu Double
kappa Double
theta
  | Double
kappa forall a. Ord a => a -> a -> Bool
<= Double
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"The shape parameter (kappa) must be positive: generateGamma01"
  | Double
kappa forall a. Ord a => a -> a -> Bool
> Double
1  =
    let d :: Double
d = Double
kappa forall a. Num a => a -> a -> a
- Double
1 forall a. Fractional a => a -> a -> a
/ Double
3
        c :: Double
c = Double
1 forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
sqrt (Double
9 forall a. Num a => a -> a -> a
* Double
d)
        loop :: m Double
loop =
          do Double
z <- m Double
gn
             if Double
z forall a. Ord a => a -> a -> Bool
<= - (Double
1 forall a. Fractional a => a -> a -> a
/ Double
c)
               then m Double
loop
               else do let v :: Double
v = (Double
1 forall a. Num a => a -> a -> a
+ Double
c forall a. Num a => a -> a -> a
* Double
z) forall a. Floating a => a -> a -> a
** Double
3
                       Double
u <- m Double
gu
                       if forall a. Floating a => a -> a
log Double
u forall a. Ord a => a -> a -> Bool
> Double
0.5 forall a. Num a => a -> a -> a
* Double
z forall a. Num a => a -> a -> a
* Double
z forall a. Num a => a -> a -> a
+ Double
d forall a. Num a => a -> a -> a
- Double
d forall a. Num a => a -> a -> a
* Double
v forall a. Num a => a -> a -> a
+ Double
d forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
log Double
v
                         then m Double
loop
                         else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double
d forall a. Num a => a -> a -> a
* Double
v forall a. Num a => a -> a -> a
* Double
theta
    in m Double
loop
  | Bool
otherwise  =
    do Double
x <- forall (m :: * -> *).
Monad m =>
m Double -> m Double -> Double -> Double -> m Double
generateGamma01 m Double
gn m Double
gu (Double
1 forall a. Num a => a -> a -> a
+ Double
kappa) Double
theta
       Double
u <- m Double
gu
       forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double
x forall a. Num a => a -> a -> a
* Double
u forall a. Floating a => a -> a -> a
** (Double
1 forall a. Fractional a => a -> a -> a
/ Double
kappa)

-- | Generate a random number from the Beta distribution.
generateBeta01 :: Monad m
                  => m Double
                  -- ^ the normal random number ~ N (0, 1)
                  -> m Double
                  -- ^ the uniform random number ~ U (0, 1)
                  -> Double
                  -- ^ the shape parameter alpha
                  -> Double
                  -- ^ the shape parameter beta
                  -> m Double
{-# INLINABLE generateBeta01 #-}
generateBeta01 :: forall (m :: * -> *).
Monad m =>
m Double -> m Double -> Double -> Double -> m Double
generateBeta01 m Double
gn m Double
gu Double
alpha Double
beta =
  do Double
g1 <- forall (m :: * -> *).
Monad m =>
m Double -> m Double -> Double -> Double -> m Double
generateGamma01 m Double
gn m Double
gu Double
alpha Double
1
     Double
g2 <- forall (m :: * -> *).
Monad m =>
m Double -> m Double -> Double -> Double -> m Double
generateGamma01 m Double
gn m Double
gu Double
beta Double
1
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double
g1 forall a. Fractional a => a -> a -> a
/ (Double
g1 forall a. Num a => a -> a -> a
+ Double
g2)

-- | Generate a random number from the Weibull distribution.
generateWeibull01 :: Monad m
                     => m Double
                     -- ^ the uniform random number ~ U (0, 1)
                     -> Double
                     -- ^ shape
                     -> Double
                     -- ^ scale
                     -> m Double
{-# INLINE generateWeibull01 #-}
generateWeibull01 :: forall (m :: * -> *).
Monad m =>
m Double -> Double -> Double -> m Double
generateWeibull01 m Double
g Double
alpha Double
beta =
  do Double
x <- m Double
g
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Double
beta forall a. Num a => a -> a -> a
* (- forall a. Floating a => a -> a
log Double
x) forall a. Floating a => a -> a -> a
** (Double
1 forall a. Fractional a => a -> a -> a
/ Double
alpha)

-- | Generate a random value from the specified discrete distribution.
generateDiscrete01 :: Monad m
                      => m Double
                      -- ^ the uniform random number ~ U (0, 1)
                      -> DiscretePDF a
                      -- ^ a discrete probability density function
                      -> m a
{-# INLINABLE generateDiscrete01 #-}
generateDiscrete01 :: forall (m :: * -> *) a. Monad m => m Double -> DiscretePDF a -> m a
generateDiscrete01 m Double
g []   = forall a. HasCallStack => [Char] -> a
error [Char]
"Empty PDF: generateDiscrete01"
generateDiscrete01 m Double
g [(a, Double)]
dpdf =
  do Double
x <- m Double
g
     let loop :: Double -> [(a, Double)] -> a
loop Double
acc [(a
a, Double
p)] = a
a
         loop Double
acc ((a
a, Double
p) : [(a, Double)]
dpdf) =
           if Double
x forall a. Ord a => a -> a -> Bool
<= Double
acc forall a. Num a => a -> a -> a
+ Double
p
           then a
a
           else Double -> [(a, Double)] -> a
loop (Double
acc forall a. Num a => a -> a -> a
+ Double
p) [(a, Double)]
dpdf
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall {a}. Double -> [(a, Double)] -> a
loop Double
0 [(a, Double)]
dpdf