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 :: 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
newRandomUniformSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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)
newRandomUniformIntSignal :: Int
-> Int
-> Composite (Signal (Arrival Int))
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)
newRandomTriangularSignal :: Double
-> Double
-> Double
-> Composite (Signal (Arrival Double))
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)
newRandomNormalSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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)
newRandomLogNormalSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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)
newRandomExponentialSignal :: Double
-> Composite (Signal (Arrival Double))
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)
newRandomErlangSignal :: Double
-> Int
-> Composite (Signal (Arrival Double))
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)
newRandomPoissonSignal :: Double
-> Composite (Signal (Arrival Int))
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)
newRandomBinomialSignal :: Double
-> Int
-> Composite (Signal (Arrival Int))
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)
newRandomGammaSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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)
newRandomBetaSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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)
newRandomWeibullSignal :: Double
-> Double
-> Composite (Signal (Arrival Double))
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)
newRandomDiscreteSignal :: DiscretePDF Double
-> Composite (Signal (Arrival Double))
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)