-- |
-- 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 <- forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation forall a. Simulation (SignalSource a)
newSignalSource
     let loop :: Maybe Double -> Process b
loop Maybe Double
t0 =
           do (Double
delay, a
a) <- forall (m :: * -> *) a. ParameterLift m => Parameter a -> m a
liftParameter Parameter (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
$
                Double -> Process ()
holdProcess Double
delay
              Double
t2 <- 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 -> forall a. Maybe a
Nothing
                                          Just Double
t0 -> forall a. a -> Maybe a
Just Double
delay }
              forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$
                forall a. SignalSource a -> a -> Event ()
triggerSignal SignalSource (Arrival a)
source Arrival a
arrival
              Maybe Double -> Process b
loop (forall a. a -> Maybe a
Just Double
t2)
     ProcessId
pid <- forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation Simulation ProcessId
newProcessId
     forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent forall a b. (a -> b) -> a -> b
$
       ProcessId -> Process () -> Event ()
runProcessUsingId ProcessId
pid forall a b. (a -> b) -> a -> b
$
       forall {b}. Maybe Double -> Process b
loop forall a. Maybe a
Nothing
     DisposableEvent -> Composite ()
disposableComposite forall a b. (a -> b) -> a -> b
$
       Event () -> DisposableEvent
DisposableEvent forall a b. (a -> b) -> a -> b
$
       ProcessId -> Event ()
cancelProcessWithId ProcessId
pid
     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Int -> Int -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Int -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Int -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  Double -> Double -> Parameter 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 :: 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 =
  forall a. Parameter (Double, a) -> Composite (Signal (Arrival a))
newRandomSignal forall a b. (a -> b) -> a -> b
$
  forall a. DiscretePDF a -> Parameter 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)