module Simulation.Aivika.Signal.Random
(
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
newRandomSignal :: Parameter (Double, a)
-> Composite (Signal (Arrival a))
newRandomSignal :: 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 (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 (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 (m :: * -> *) a. DynamicsLift m => Dynamics a -> m a
liftDynamics Dynamics Double
time
let arrival :: Arrival a
arrival = Arrival :: forall a. a -> Double -> Maybe Double -> Arrival a
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 t0 -> Double -> Maybe Double
forall a. a -> Maybe a
Just Double
delay }
Event () -> Process ()
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 (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation Simulation ProcessId
newProcessId
Event () -> Composite ()
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 (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
newRandomUniformSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomUniformIntSignal :: Int
-> Int
-> Composite (Signal (Arrival Int))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
(Double, Int) -> Parameter (Double, Int)
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)
newRandomTriangularSignal :: Double
-> Double
-> Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomNormalSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomLogNormalSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomExponentialSignal :: Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomErlangSignal :: Double
-> Int
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomPoissonSignal :: Double
-> Composite (Signal (Arrival Int))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
(Double, Int) -> Parameter (Double, Int)
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)
newRandomBinomialSignal :: Double
-> Int
-> Composite (Signal (Arrival Int))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
x ->
(Double, Int) -> Parameter (Double, Int)
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)
newRandomGammaSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomBetaSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomWeibullSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)
newRandomDiscreteSignal :: DiscretePDF Double
-> Composite (Signal (Arrival Double))
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 (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Double
x ->
(Double, Double) -> Parameter (Double, Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x, Double
x)