-- |
-- Module     : Simulation.Aivika.Process.Random
-- 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 module defines helper functions, which are useful to hold 
-- the 'Process' computation for a time interval according to some
-- random distribution.
--

module Simulation.Aivika.Process.Random
       (randomUniformProcess,
        randomUniformProcess_,
        randomUniformIntProcess,
        randomUniformIntProcess_,
        randomTriangularProcess,
        randomTriangularProcess_,
        randomNormalProcess,
        randomNormalProcess_,
        randomLogNormalProcess,
        randomLogNormalProcess_,
        randomExponentialProcess,
        randomExponentialProcess_,
        randomErlangProcess,
        randomErlangProcess_,
        randomPoissonProcess,
        randomPoissonProcess_,
        randomBinomialProcess,
        randomBinomialProcess_,
        randomGammaProcess,
        randomGammaProcess_,
        randomBetaProcess,
        randomBetaProcess_,
        randomWeibullProcess,
        randomWeibullProcess_,
        randomDiscreteProcess,
        randomDiscreteProcess_) where

import Control.Monad
import Control.Monad.Trans

import Simulation.Aivika.Generator
import Simulation.Aivika.Parameter
import Simulation.Aivika.Parameter.Random
import Simulation.Aivika.Process

-- | Hold the process for a random time interval distributed uniformly.
randomUniformProcess :: Double
                        -- ^ the minimum time interval
                        -> Double
                        -- ^ the maximum time interval
                        -> Process Double
                        -- ^ a computation of the time interval
                        -- for which the process was actually held
randomUniformProcess :: Double -> Double -> Process Double
randomUniformProcess Double
min Double
max =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomUniform Double
min Double
max
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t

-- | Hold the process for a random time interval distributed uniformly.
randomUniformProcess_ :: Double
                         -- ^ the minimum time interval
                         -> Double
                         -- ^ the maximum time interval
                         -> Process ()
randomUniformProcess_ :: Double -> Double -> Process ()
randomUniformProcess_ Double
min Double
max =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomUniform Double
min Double
max
     Double -> Process ()
holdProcess Double
t

-- | Hold the process for a random time interval distributed uniformly.
randomUniformIntProcess :: Int
                           -- ^ the minimum time interval
                           -> Int
                           -- ^ the maximum time interval
                           -> Process Int
                           -- ^ a computation of the time interval
                           -- for which the process was actually held
randomUniformIntProcess :: Int -> Int -> Process Int
randomUniformIntProcess Int
min Int
max =
  do Int
t <- Parameter Int -> Process Int
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Int -> Process Int) -> Parameter Int -> Process Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Parameter Int
randomUniformInt Int
min Int
max
     Double -> Process ()
holdProcess (Double -> Process ()) -> Double -> Process ()
forall a b. (a -> b) -> a -> b
$ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
t
     Int -> Process Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
t

-- | Hold the process for a random time interval distributed uniformly.
randomUniformIntProcess_ :: Int
                            -- ^ the minimum time interval
                            -> Int
                            -- ^ the maximum time interval
                            -> Process ()
randomUniformIntProcess_ :: Int -> Int -> Process ()
randomUniformIntProcess_ Int
min Int
max =
  do Int
t <- Parameter Int -> Process Int
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Int -> Process Int) -> Parameter Int -> Process Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Parameter Int
randomUniformInt Int
min Int
max
     Double -> Process ()
holdProcess (Double -> Process ()) -> Double -> Process ()
forall a b. (a -> b) -> a -> b
$ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
t

-- | Hold the process for a random time interval having the triangular distribution.
randomTriangularProcess :: Double
                           -- ^ the minimum time interval
                           -> Double
                           -- ^ a median of the time interval
                           -> Double
                           -- ^ the maximum time interval
                           -> Process Double
                           -- ^ a computation of the time interval
                           -- for which the process was actually held
randomTriangularProcess :: Double -> Double -> Double -> Process Double
randomTriangularProcess Double
min Double
median Double
max =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double -> Parameter Double
randomTriangular Double
min Double
median Double
max
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t

-- | Hold the process for a random time interval having the triangular distribution.
randomTriangularProcess_ :: Double
                            -- ^ the minimum time interval
                            -> Double
                            -- ^ a median of the time interval
                            -> Double
                            -- ^ the maximum time interval
                            -> Process ()
randomTriangularProcess_ :: Double -> Double -> Double -> Process ()
randomTriangularProcess_ Double
min Double
median Double
max =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double -> Parameter Double
randomTriangular Double
min Double
median Double
max
     Double -> Process ()
holdProcess Double
t

-- | Hold the process for a random time interval distributed normally.
randomNormalProcess :: Double
                       -- ^ the mean time interval
                       -> Double
                       -- ^ the time interval deviation
                       -> Process Double
                       -- ^ a computation of the time interval
                       -- for which the process was actually held
randomNormalProcess :: Double -> Double -> Process Double
randomNormalProcess Double
mu Double
nu =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomNormal Double
mu Double
nu
     Bool -> Process () -> Process ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
t Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0) (Process () -> Process ()) -> Process () -> Process ()
forall a b. (a -> b) -> a -> b
$
       Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t
         
-- | Hold the process for a random time interval distributed normally.
randomNormalProcess_ :: Double
                        -- ^ the mean time interval
                        -> Double
                        -- ^ the time interval deviation
                        -> Process ()
randomNormalProcess_ :: Double -> Double -> Process ()
randomNormalProcess_ Double
mu Double
nu =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomNormal Double
mu Double
nu
     Bool -> Process () -> Process ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
t Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0) (Process () -> Process ()) -> Process () -> Process ()
forall a b. (a -> b) -> a -> b
$
       Double -> Process ()
holdProcess Double
t

-- | Hold the process for a random time interval having the lognormal distribution.
randomLogNormalProcess :: Double
                          -- ^ the mean for a normal distribution
                          -- which this distribution is derived from
                          -> Double
                          -- ^ the deviation for a normal distribution
                          -- which this distribution is derived from
                          -> Process Double
                          -- ^ a computation of the time interval
                          -- for which the process was actually held
randomLogNormalProcess :: Double -> Double -> Process Double
randomLogNormalProcess Double
mu Double
nu =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomLogNormal Double
mu Double
nu
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t

-- | Hold the process for a random time interval having the lognormal distribution.
randomLogNormalProcess_ :: Double
                           -- ^ the mean for a normal distribution
                           -- which this distribution is derived from
                           -> Double
                           -- ^ the deviation for a normal distribution
                           -- which this distribution is derived from
                           -> Process ()
randomLogNormalProcess_ :: Double -> Double -> Process ()
randomLogNormalProcess_ Double
mu Double
nu =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomLogNormal Double
mu Double
nu
     Double -> Process ()
holdProcess Double
t

-- | Hold the process for a random time interval distributed exponentially
-- with the specified mean (the reciprocal of the rate).
randomExponentialProcess :: Double
                            -- ^ the mean time interval (the reciprocal of the rate)
                            -> Process Double
                            -- ^ a computation of the time interval
                            -- for which the process was actually held
randomExponentialProcess :: Double -> Process Double
randomExponentialProcess Double
mu =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Parameter Double
randomExponential Double
mu
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t
         
-- | Hold the process for a random time interval distributed exponentially
-- with the specified mean (the reciprocal of the rate).
randomExponentialProcess_ :: Double
                             -- ^ the mean time interval (the reciprocal of the rate)
                             -> Process ()
randomExponentialProcess_ :: Double -> Process ()
randomExponentialProcess_ Double
mu =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Parameter Double
randomExponential Double
mu
     Double -> Process ()
holdProcess Double
t
         
-- | Hold the process for a random time interval having the Erlang distribution with
-- the specified scale (the reciprocal of the rate) and shape parameters.
randomErlangProcess :: Double
                       -- ^ the scale (the reciprocal of the rate)
                       -> Int
                       -- ^ the shape
                       -> Process Double
                       -- ^ a computation of the time interval
                       -- for which the process was actually held
randomErlangProcess :: Double -> Int -> Process Double
randomErlangProcess Double
beta Int
m =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Int -> Parameter Double
randomErlang Double
beta Int
m
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t

-- | Hold the process for a random time interval having the Erlang distribution with
-- the specified scale (the reciprocal of the rate) and shape parameters.
randomErlangProcess_ :: Double
                        -- ^ the scale (the reciprocal of the rate)
                        -> Int
                        -- ^ the shape
                        -> Process ()
randomErlangProcess_ :: Double -> Int -> Process ()
randomErlangProcess_ Double
beta Int
m =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Int -> Parameter Double
randomErlang Double
beta Int
m
     Double -> Process ()
holdProcess Double
t

-- | Hold the process for a random time interval having the Poisson distribution with
-- the specified mean.
randomPoissonProcess :: Double
                        -- ^ the mean time interval
                        -> Process Int
                        -- ^ a computation of the time interval
                        -- for which the process was actually held
randomPoissonProcess :: Double -> Process Int
randomPoissonProcess Double
mu =
  do Int
t <- Parameter Int -> Process Int
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Int -> Process Int) -> Parameter Int -> Process Int
forall a b. (a -> b) -> a -> b
$ Double -> Parameter Int
randomPoisson Double
mu
     Double -> Process ()
holdProcess (Double -> Process ()) -> Double -> Process ()
forall a b. (a -> b) -> a -> b
$ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
t
     Int -> Process Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
t

-- | Hold the process for a random time interval having the Poisson distribution with
-- the specified mean.
randomPoissonProcess_ :: Double
                         -- ^ the mean time interval
                         -> Process ()
randomPoissonProcess_ :: Double -> Process ()
randomPoissonProcess_ Double
mu =
  do Int
t <- Parameter Int -> Process Int
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Int -> Process Int) -> Parameter Int -> Process Int
forall a b. (a -> b) -> a -> b
$ Double -> Parameter Int
randomPoisson Double
mu
     Double -> Process ()
holdProcess (Double -> Process ()) -> Double -> Process ()
forall a b. (a -> b) -> a -> b
$ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
t

-- | Hold the process for a random time interval having the binomial distribution
-- with the specified probability and trials.
randomBinomialProcess :: Double
                         -- ^ the probability
                         -> Int
                         -- ^ the number of trials
                         -> Process Int
                         -- ^ a computation of the time interval
                         -- for which the process was actually held
randomBinomialProcess :: Double -> Int -> Process Int
randomBinomialProcess Double
prob Int
trials =
  do Int
t <- Parameter Int -> Process Int
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Int -> Process Int) -> Parameter Int -> Process Int
forall a b. (a -> b) -> a -> b
$ Double -> Int -> Parameter Int
randomBinomial Double
prob Int
trials
     Double -> Process ()
holdProcess (Double -> Process ()) -> Double -> Process ()
forall a b. (a -> b) -> a -> b
$ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
t
     Int -> Process Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
t

-- | Hold the process for a random time interval having the binomial distribution
-- with the specified probability and trials.
randomBinomialProcess_ :: Double
                         -- ^ the probability
                         -> Int
                         -- ^ the number of trials
                         -> Process ()
randomBinomialProcess_ :: Double -> Int -> Process ()
randomBinomialProcess_ Double
prob Int
trials =
  do Int
t <- Parameter Int -> Process Int
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Int -> Process Int) -> Parameter Int -> Process Int
forall a b. (a -> b) -> a -> b
$ Double -> Int -> Parameter Int
randomBinomial Double
prob Int
trials
     Double -> Process ()
holdProcess (Double -> Process ()) -> Double -> Process ()
forall a b. (a -> b) -> a -> b
$ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
t

-- | Hold the process for a random time interval having the Gamma distribution
-- with the specified shape and scale.
randomGammaProcess :: Double
                      -- ^ the shape
                      -> Double
                      -- ^ the scale (a reciprocal of the rate)
                      -> Process Double
                      -- ^ a computation of the time interval
                      -- for which the process was actually held
randomGammaProcess :: Double -> Double -> Process Double
randomGammaProcess Double
kappa Double
theta =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomGamma Double
kappa Double
theta
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t

-- | Hold the process for a random time interval having the Gamma distribution
-- with the specified shape and scale.
randomGammaProcess_ :: Double
                       -- ^ the shape
                       -> Double
                       -- ^ the scale (a reciprocal of the rate)
                       -> Process ()
randomGammaProcess_ :: Double -> Double -> Process ()
randomGammaProcess_ Double
kappa Double
theta =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomGamma Double
kappa Double
theta
     Double -> Process ()
holdProcess Double
t

-- | Hold the process for a random time interval having the Beta distribution
-- with the specified shape parameters (alpha and beta).
randomBetaProcess :: Double
                     -- ^ the shape (alpha)
                     -> Double
                     -- ^ the shape (beta)
                     -> Process Double
                     -- ^ a computation of the time interval
                     -- for which the process was actually held
randomBetaProcess :: Double -> Double -> Process Double
randomBetaProcess Double
alpha Double
beta =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomBeta Double
alpha Double
beta
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t

-- | Hold the process for a random time interval having the Beta distribution
-- with the specified shape parameters (alpha and beta).
randomBetaProcess_ :: Double
                      -- ^ the shape (alpha)
                      -> Double
                      -- ^ the shape (beta)
                      -> Process ()
randomBetaProcess_ :: Double -> Double -> Process ()
randomBetaProcess_ Double
alpha Double
beta =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomBeta Double
alpha Double
beta
     Double -> Process ()
holdProcess Double
t

-- | Hold the process for a random time interval having the Weibull distribution
-- with the specified shape and scale.
randomWeibullProcess :: Double
                        -- ^ the shape
                        -> Double
                        -- ^ the scale
                        -> Process Double
                        -- ^ a computation of the time interval
                        -- for which the process was actually held
randomWeibullProcess :: Double -> Double -> Process Double
randomWeibullProcess Double
alpha Double
beta =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomWeibull Double
alpha Double
beta
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t

-- | Hold the process for a random time interval having the Weibull distribution
-- with the specified shape and scale.
randomWeibullProcess_ :: Double
                         -- ^ the shape
                         -> Double
                         -- ^ the scale
                         -> Process ()
randomWeibullProcess_ :: Double -> Double -> Process ()
randomWeibullProcess_ Double
alpha Double
beta =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Parameter Double
randomWeibull Double
alpha Double
beta
     Double -> Process ()
holdProcess Double
t

-- | Hold the process for a random time interval having the specified discrete distribution.
randomDiscreteProcess :: DiscretePDF Double
                         -- ^ the discrete probability density function
                         -> Process Double
                         -- ^ a computation of the time interval
                         -- for which the process was actually held
randomDiscreteProcess :: DiscretePDF Double -> Process Double
randomDiscreteProcess DiscretePDF Double
dpdf =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ DiscretePDF Double -> Parameter Double
forall a. DiscretePDF a -> Parameter a
randomDiscrete DiscretePDF Double
dpdf
     Double -> Process ()
holdProcess Double
t
     Double -> Process Double
forall (m :: * -> *) a. Monad m => a -> m a
return Double
t

-- | Hold the process for a random time interval having the specified discrete distribution.
randomDiscreteProcess_ :: DiscretePDF Double
                          -- ^ the discrete probability density function
                          -> Process ()
randomDiscreteProcess_ :: DiscretePDF Double -> Process ()
randomDiscreteProcess_ DiscretePDF Double
dpdf =
  do Double
t <- Parameter Double -> Process Double
forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter (Parameter Double -> Process Double)
-> Parameter Double -> Process Double
forall a b. (a -> b) -> a -> b
$ DiscretePDF Double -> Parameter Double
forall a. DiscretePDF a -> Parameter a
randomDiscrete DiscretePDF Double
dpdf
     Double -> Process ()
holdProcess Double
t