-- |
-- Module     : Simulation.Aivika.Trans.Signal.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 random signals of events, which are useful
-- for describing the input of the model.
--

module Simulation.Aivika.Trans.Signal.Random
       (-- * Signal of Random Events
        newRandomSignal,
        newRandomUniformSignal,
        newRandomUniformIntSignal,
        newRandomTriangularSignal,
        newRandomNormalSignal,
        newRandomLogNormalSignal,
        newRandomExponentialSignal,
        newRandomErlangSignal,
        newRandomPoissonSignal,
        newRandomBinomialSignal,
        newRandomGammaSignal,
        newRandomBetaSignal,
        newRandomWeibullSignal,
        newRandomDiscreteSignal) where

import Control.Monad
import Control.Monad.Trans

import Simulation.Aivika.Trans.DES
import Simulation.Aivika.Trans.Generator
import Simulation.Aivika.Trans.Parameter
import Simulation.Aivika.Trans.Parameter.Random
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Dynamics
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Composite
import Simulation.Aivika.Trans.Process
import Simulation.Aivika.Trans.Signal
import Simulation.Aivika.Trans.Statistics
import Simulation.Aivika.Trans.Arrival

-- | Return a signal of random events that arrive with the specified delay.
newRandomSignal :: MonadDES m
                   => Parameter m (Double, a)
                   -- ^ compute a pair of the delay and event of type @a@
                   -> Composite m (Signal m (Arrival a))
                   -- ^ the computation that returns a signal emitting the delayed events
{-# INLINABLE newRandomSignal #-}
newRandomSignal :: forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal Parameter m (Double, a)
delay =
  do SignalSource m (Arrival a)
source <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall (m :: * -> *) a.
MonadDES m =>
Simulation m (SignalSource m a)
newSignalSource
     let loop :: Maybe Double -> Process m b
loop Maybe Double
t0 =
           do (Double
delay, a
a) <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
ParameterLift t m =>
Parameter m a -> t m a
liftParameter Parameter m (Double, a)
delay
              forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
delay forall a. Ord a => a -> a -> Bool
> Double
0) forall a b. (a -> b) -> a -> b
$
                forall (m :: * -> *). MonadDES m => Double -> Process m ()
holdProcess Double
delay
              Double
t2 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
DynamicsLift t m =>
Dynamics m a -> t m a
liftDynamics forall (m :: * -> *). Monad m => Dynamics m Double
time
              let arrival :: Arrival a
arrival = Arrival { arrivalValue :: a
arrivalValue = a
a,
                                      arrivalTime :: Double
arrivalTime  = Double
t2,
                                      arrivalDelay :: Maybe Double
arrivalDelay =
                                        case Maybe Double
t0 of
                                          Maybe Double
Nothing -> forall a. Maybe a
Nothing
                                          Just Double
t0 -> forall a. a -> Maybe a
Just Double
delay }
              forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
                forall (m :: * -> *) a. SignalSource m a -> a -> Event m ()
triggerSignal SignalSource m (Arrival a)
source Arrival a
arrival
              Maybe Double -> Process m b
loop (forall a. a -> Maybe a
Just Double
t2)
     ProcessId m
pid <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation forall (m :: * -> *). MonadDES m => Simulation m (ProcessId m)
newProcessId
     forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
EventLift t m =>
Event m a -> t m a
liftEvent forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *).
MonadDES m =>
ProcessId m -> Process m () -> Event m ()
runProcessUsingId ProcessId m
pid forall a b. (a -> b) -> a -> b
$
       forall {b}. Maybe Double -> Process m b
loop forall a. Maybe a
Nothing
     forall (m :: * -> *).
Monad m =>
DisposableEvent m -> Composite m ()
disposableComposite forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *). Event m () -> DisposableEvent m
DisposableEvent forall a b. (a -> b) -> a -> b
$
       forall (m :: * -> *). MonadDES m => ProcessId m -> Event m ()
cancelProcessWithId ProcessId m
pid
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. SignalSource m a -> Signal m a
publishSignal SignalSource m (Arrival a)
source

-- | Create a new signal with random delays distributed uniformly.
newRandomUniformSignal :: MonadDES m
                          => Double
                          -- ^ the minimum delay
                          -> Double
                          -- ^ the maximum delay
                          -> Composite m (Signal m (Arrival Double))
                          -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomUniformSignal #-}
newRandomUniformSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Composite m (Signal m (Arrival Double))
newRandomUniformSignal Double
min Double
max =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomUniform Double
min Double
max forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Create a new signal with integer random delays distributed uniformly.
newRandomUniformIntSignal :: MonadDES m
                             => Int
                             -- ^ the minimum delay
                             -> Int
                             -- ^ the maximum delay
                             -> Composite m (Signal m (Arrival Int))
                             -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomUniformIntSignal #-}
newRandomUniformIntSignal :: forall (m :: * -> *).
MonadDES m =>
Int -> Int -> Composite m (Signal m (Arrival Int))
newRandomUniformIntSignal Int
min Int
max =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *). MonadComp m => Int -> Int -> Parameter m Int
randomUniformInt Int
min Int
max forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x, Int
x)

-- | Create a new signal with random delays having the triangular distribution.
newRandomTriangularSignal :: MonadDES m
                             => Double
                             -- ^ the minimum delay
                             -> Double
                             -- ^ the median of the delay
                             -> Double
                             -- ^ the maximum delay
                             -> Composite m (Signal m (Arrival Double))
                             -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomTriangularSignal #-}
newRandomTriangularSignal :: forall (m :: * -> *).
MonadDES m =>
Double
-> Double -> Double -> Composite m (Signal m (Arrival Double))
newRandomTriangularSignal Double
min Double
median Double
max =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Double -> Parameter m Double
randomTriangular Double
min Double
median Double
max forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Create a new signal with random delays distributed normally.
newRandomNormalSignal :: MonadDES m
                         => Double
                         -- ^ the mean delay
                         -> Double
                         -- ^ the delay deviation
                         -> Composite m (Signal m (Arrival Double))
                         -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomNormalSignal #-}
newRandomNormalSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Composite m (Signal m (Arrival Double))
newRandomNormalSignal Double
mu Double
nu =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomNormal Double
mu Double
nu forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Create a new signal with random delays having the lognormal distribution.
newRandomLogNormalSignal :: MonadDES m
                            => Double
                            -- ^ the mean of a normal distribution which
                            -- this distribution is derived from
                            -> Double
                            -- ^ the deviation of a normal distribution which
                            -- this distribution is derived from
                            -> Composite m (Signal m (Arrival Double))
                            -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomLogNormalSignal #-}
newRandomLogNormalSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Composite m (Signal m (Arrival Double))
newRandomLogNormalSignal Double
mu Double
nu =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomLogNormal Double
mu Double
nu forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Return a new signal with random delays distibuted exponentially with the specified mean
-- (the reciprocal of the rate).
newRandomExponentialSignal :: MonadDES m
                              => Double
                              -- ^ the mean delay (the reciprocal of the rate)
                              -> Composite m (Signal m (Arrival Double))
                              -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomExponentialSignal #-}
newRandomExponentialSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Composite m (Signal m (Arrival Double))
newRandomExponentialSignal Double
mu =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *). MonadComp m => Double -> Parameter m Double
randomExponential Double
mu forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
         
-- | Return a new signal with random delays having the Erlang distribution with the specified
-- scale (the reciprocal of the rate) and shape parameters.
newRandomErlangSignal :: MonadDES m
                         => Double
                         -- ^ the scale (the reciprocal of the rate)
                         -> Int
                         -- ^ the shape
                         -> Composite m (Signal m (Arrival Double))
                         -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomErlangSignal #-}
newRandomErlangSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Int -> Composite m (Signal m (Arrival Double))
newRandomErlangSignal Double
beta Int
m =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Int -> Parameter m Double
randomErlang Double
beta Int
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Return a new signal with random delays having the Poisson distribution with
-- the specified mean.
newRandomPoissonSignal :: MonadDES m
                          => Double
                          -- ^ the mean delay
                          -> Composite m (Signal m (Arrival Int))
                          -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomPoissonSignal #-}
newRandomPoissonSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Composite m (Signal m (Arrival Int))
newRandomPoissonSignal Double
mu =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *). MonadComp m => Double -> Parameter m Int
randomPoisson Double
mu forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x, Int
x)

-- | Return a new signal with random delays having the binomial distribution with the specified
-- probability and trials.
newRandomBinomialSignal :: MonadDES m
                           => Double
                           -- ^ the probability
                           -> Int
                           -- ^ the number of trials
                           -> Composite m (Signal m (Arrival Int))
                           -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomBinomialSignal #-}
newRandomBinomialSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Int -> Composite m (Signal m (Arrival Int))
newRandomBinomialSignal Double
prob Int
trials =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Int -> Parameter m Int
randomBinomial Double
prob Int
trials forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x, Int
x)

-- | Return a new signal with random delays having the Gamma distribution by the specified
-- shape and scale.
newRandomGammaSignal :: MonadDES m
                        => Double
                        -- ^ the shape
                        -> Double
                        -- ^ the scale (a reciprocal of the rate)
                        -> Composite m (Signal m (Arrival Double))
                        -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomGammaSignal #-}
newRandomGammaSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Composite m (Signal m (Arrival Double))
newRandomGammaSignal Double
kappa Double
theta =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomGamma Double
kappa Double
theta forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Return a new signal with random delays having the Beta distribution by the specified
-- shape parameters (alpha and beta).
newRandomBetaSignal :: MonadDES m
                       => Double
                       -- ^ the shape (alpha)
                       -> Double
                       -- ^ the shape (beta)
                       -> Composite m (Signal m (Arrival Double))
                       -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomBetaSignal #-}
newRandomBetaSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Composite m (Signal m (Arrival Double))
newRandomBetaSignal Double
alpha Double
beta =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomBeta Double
alpha Double
beta forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Return a new signal with random delays having the Weibull distribution by the specified
-- shape and scale.
newRandomWeibullSignal :: MonadDES m
                          => Double
                          -- ^ shape
                          -> Double
                          -- ^ scale
                          -> Composite m (Signal m (Arrival Double))
                          -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomWeibullSignal #-}
newRandomWeibullSignal :: forall (m :: * -> *).
MonadDES m =>
Double -> Double -> Composite m (Signal m (Arrival Double))
newRandomWeibullSignal Double
alpha Double
beta =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *).
MonadComp m =>
Double -> Double -> Parameter m Double
randomWeibull Double
alpha Double
beta forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Return a new signal with random delays having the specified discrete distribution.
newRandomDiscreteSignal :: MonadDES m
                           => DiscretePDF Double
                           -- ^ the discrete probability density function
                           -> Composite m (Signal m (Arrival Double))
                           -- ^ the computation of signal emitting random events with the delays generated
{-# INLINABLE newRandomDiscreteSignal #-}
newRandomDiscreteSignal :: forall (m :: * -> *).
MonadDES m =>
DiscretePDF Double -> Composite m (Signal m (Arrival Double))
newRandomDiscreteSignal DiscretePDF Double
dpdf =
  forall (m :: * -> *) a.
MonadDES m =>
Parameter m (Double, a) -> Composite m (Signal m (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall (m :: * -> *) a.
MonadComp m =>
DiscretePDF a -> Parameter m a
randomDiscrete DiscretePDF Double
dpdf forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)