-- |
-- Module     : Simulation.Aivika.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.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.Generator
import Simulation.Aivika.Parameter
import Simulation.Aivika.Parameter.Random
import Simulation.Aivika.Simulation
import Simulation.Aivika.Dynamics
import Simulation.Aivika.Event
import Simulation.Aivika.Composite
import Simulation.Aivika.Process
import Simulation.Aivika.Signal
import Simulation.Aivika.Statistics
import Simulation.Aivika.Arrival

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

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

-- | Create a new signal with integer random delays distributed uniformly.
newRandomUniformIntSignal :: Int
                             -- ^ the minimum delay
                             -> Int
                             -- ^ the maximum delay
                             -> Composite (Signal (Arrival Int))
                             -- ^ the computation of signal emitting random events with the delays generated
newRandomUniformIntSignal :: Int -> Int -> Composite (Signal (Arrival Int))
newRandomUniformIntSignal Int
min Int
max =
  Parameter (Double, Int) -> Composite (Signal (Arrival Int))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Int) -> Composite (Signal (Arrival Int)))
-> Parameter (Double, Int) -> Composite (Signal (Arrival Int))
forall a b. (a -> b) -> a -> b
$
  Int -> Int -> Parameter Int
randomUniformInt Int
min Int
max Parameter Int
-> (Int -> Parameter (Double, Int)) -> Parameter (Double, Int)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
  (Double, Int) -> Parameter (Double, Int)
forall a. a -> Parameter a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Double
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 :: Double
                             -- ^ the minimum delay
                             -> Double
                             -- ^ the median of the delay
                             -> Double
                             -- ^ the maximum delay
                             -> Composite (Signal (Arrival Double))
                             -- ^ the computation of signal emitting random events with the delays generated
newRandomTriangularSignal :: Double -> Double -> Double -> Composite (Signal (Arrival Double))
newRandomTriangularSignal Double
min Double
median Double
max =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Double -> Parameter Double
randomTriangular Double
min Double
median Double
max Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)

-- | Create a new signal with random delays distributed normally.
newRandomNormalSignal :: Double
                         -- ^ the mean delay
                         -> Double
                         -- ^ the delay deviation
                         -> Composite (Signal (Arrival Double))
                         -- ^ the computation of signal emitting random events with the delays generated
newRandomNormalSignal :: Double -> Double -> Composite (Signal (Arrival Double))
newRandomNormalSignal Double
mu Double
nu =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter Double
randomNormal Double
mu Double
nu Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
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 :: 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 (Signal (Arrival Double))
                            -- ^ the computation of signal emitting random events with the delays generated
newRandomLogNormalSignal :: Double -> Double -> Composite (Signal (Arrival Double))
newRandomLogNormalSignal Double
mu Double
nu =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter Double
randomLogNormal Double
mu Double
nu Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
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 :: Double
                              -- ^ the mean delay (the reciprocal of the rate)
                              -> Composite (Signal (Arrival Double))
                              -- ^ the computation of signal emitting random events with the delays generated
newRandomExponentialSignal :: Double -> Composite (Signal (Arrival Double))
newRandomExponentialSignal Double
mu =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  Double -> Parameter Double
randomExponential Double
mu Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
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 :: Double
                         -- ^ the scale (the reciprocal of the rate)
                         -> Int
                         -- ^ the shape
                         -> Composite (Signal (Arrival Double))
                         -- ^ the computation of signal emitting random events with the delays generated
newRandomErlangSignal :: Double -> Int -> Composite (Signal (Arrival Double))
newRandomErlangSignal Double
beta Int
m =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  Double -> Int -> Parameter Double
randomErlang Double
beta Int
m Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
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 :: Double
                          -- ^ the mean delay
                          -> Composite (Signal (Arrival Int))
                          -- ^ the computation of signal emitting random events with the delays generated
newRandomPoissonSignal :: Double -> Composite (Signal (Arrival Int))
newRandomPoissonSignal Double
mu =
  Parameter (Double, Int) -> Composite (Signal (Arrival Int))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Int) -> Composite (Signal (Arrival Int)))
-> Parameter (Double, Int) -> Composite (Signal (Arrival Int))
forall a b. (a -> b) -> a -> b
$
  Double -> Parameter Int
randomPoisson Double
mu Parameter Int
-> (Int -> Parameter (Double, Int)) -> Parameter (Double, Int)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
  (Double, Int) -> Parameter (Double, Int)
forall a. a -> Parameter a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Double
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 :: Double
                           -- ^ the probability
                           -> Int
                           -- ^ the number of trials
                           -> Composite (Signal (Arrival Int))
                           -- ^ the computation of signal emitting random events with the delays generated
newRandomBinomialSignal :: Double -> Int -> Composite (Signal (Arrival Int))
newRandomBinomialSignal Double
prob Int
trials =
  Parameter (Double, Int) -> Composite (Signal (Arrival Int))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Int) -> Composite (Signal (Arrival Int)))
-> Parameter (Double, Int) -> Composite (Signal (Arrival Int))
forall a b. (a -> b) -> a -> b
$
  Double -> Int -> Parameter Int
randomBinomial Double
prob Int
trials Parameter Int
-> (Int -> Parameter (Double, Int)) -> Parameter (Double, Int)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
  (Double, Int) -> Parameter (Double, Int)
forall a. a -> Parameter a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Double
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 :: Double
                        -- ^ the shape
                        -> Double
                        -- ^ the scale (a reciprocal of the rate)
                        -> Composite (Signal (Arrival Double))
                        -- ^ the computation of signal emitting random events with the delays generated
newRandomGammaSignal :: Double -> Double -> Composite (Signal (Arrival Double))
newRandomGammaSignal Double
kappa Double
theta =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter Double
randomGamma Double
kappa Double
theta Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
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 :: Double
                       -- ^ the shape (alpha)
                       -> Double
                       -- ^ the shape (beta)
                       -> Composite (Signal (Arrival Double))
                       -- ^ the computation of signal emitting random events with the delays generated
newRandomBetaSignal :: Double -> Double -> Composite (Signal (Arrival Double))
newRandomBetaSignal Double
alpha Double
beta =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter Double
randomBeta Double
alpha Double
beta Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
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 :: Double
                          -- ^ shape
                          -> Double
                          -- ^ scale
                          -> Composite (Signal (Arrival Double))
                          -- ^ the computation of signal emitting random events with the delays generated
newRandomWeibullSignal :: Double -> Double -> Composite (Signal (Arrival Double))
newRandomWeibullSignal Double
alpha Double
beta =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter Double
randomWeibull Double
alpha Double
beta Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
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 :: DiscretePDF Double
                           -- ^ the discrete probability density function
                           -> Composite (Signal (Arrival Double))
                           -- ^ the computation of signal emitting random events with the delays generated
newRandomDiscreteSignal :: DiscretePDF Double -> Composite (Signal (Arrival Double))
newRandomDiscreteSignal DiscretePDF Double
dpdf =
  Parameter (Double, Double) -> Composite (Signal (Arrival Double))
forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal (Parameter (Double, Double) -> Composite (Signal (Arrival Double)))
-> Parameter (Double, Double)
-> Composite (Signal (Arrival Double))
forall a b. (a -> b) -> a -> b
$
  DiscretePDF Double -> Parameter Double
forall a. DiscretePDF a -> Parameter a
randomDiscrete DiscretePDF Double
dpdf Parameter Double
-> (Double -> Parameter (Double, Double))
-> Parameter (Double, Double)
forall a b. Parameter a -> (a -> Parameter b) -> Parameter b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
  (Double, Double) -> Parameter (Double, Double)
forall a. a -> Parameter a
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)