module Simulation.Aivika.Stream.Random
(
randomStream,
randomUniformStream,
randomUniformIntStream,
randomTriangularStream,
randomNormalStream,
randomLogNormalStream,
randomExponentialStream,
randomErlangStream,
randomPoissonStream,
randomBinomialStream,
randomGammaStream,
randomBetaStream,
randomWeibullStream,
randomDiscreteStream) 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.Process
import Simulation.Aivika.Processor
import Simulation.Aivika.Stream
import Simulation.Aivika.Statistics
import Simulation.Aivika.Ref
import Simulation.Aivika.Arrival
randomStream :: Parameter (Double, a)
-> Stream (Arrival a)
randomStream delay = Cons $ loop Nothing where
loop t0 =
do t1 <- liftDynamics time
case t0 of
Nothing -> return ()
Just t0 ->
when (t1 /= t0) $
error $
"The time of requesting for a new random event is different from " ++
"the time when the previous event has arrived. Probably, your model " ++
"contains a logical error. The random events should be requested permanently. " ++
"At least, they can be lost, for example, when trying to enqueue them, but " ++
"the random stream itself must always work: randomStream."
(delay, a) <- liftParameter delay
when (delay > 0) $
holdProcess delay
t2 <- liftDynamics time
let arrival = Arrival { arrivalValue = a,
arrivalTime = t2,
arrivalDelay =
case t0 of
Nothing -> Nothing
Just t0 -> Just delay }
return (arrival, Cons $ loop (Just t2))
randomUniformStream :: Double
-> Double
-> Stream (Arrival Double)
randomUniformStream min max =
randomStream $
randomUniform min max >>= \x ->
return (x, x)
randomUniformIntStream :: Int
-> Int
-> Stream (Arrival Int)
randomUniformIntStream min max =
randomStream $
randomUniformInt min max >>= \x ->
return (fromIntegral x, x)
randomTriangularStream :: Double
-> Double
-> Double
-> Stream (Arrival Double)
randomTriangularStream min median max =
randomStream $
randomTriangular min median max >>= \x ->
return (x, x)
randomNormalStream :: Double
-> Double
-> Stream (Arrival Double)
randomNormalStream mu nu =
randomStream $
randomNormal mu nu >>= \x ->
return (x, x)
randomLogNormalStream :: Double
-> Double
-> Stream (Arrival Double)
randomLogNormalStream mu nu =
randomStream $
randomLogNormal mu nu >>= \x ->
return (x, x)
randomExponentialStream :: Double
-> Stream (Arrival Double)
randomExponentialStream mu =
randomStream $
randomExponential mu >>= \x ->
return (x, x)
randomErlangStream :: Double
-> Int
-> Stream (Arrival Double)
randomErlangStream beta m =
randomStream $
randomErlang beta m >>= \x ->
return (x, x)
randomPoissonStream :: Double
-> Stream (Arrival Int)
randomPoissonStream mu =
randomStream $
randomPoisson mu >>= \x ->
return (fromIntegral x, x)
randomBinomialStream :: Double
-> Int
-> Stream (Arrival Int)
randomBinomialStream prob trials =
randomStream $
randomBinomial prob trials >>= \x ->
return (fromIntegral x, x)
randomGammaStream :: Double
-> Double
-> Stream (Arrival Double)
randomGammaStream kappa theta =
randomStream $
randomGamma kappa theta >>= \x ->
return (x, x)
randomBetaStream :: Double
-> Double
-> Stream (Arrival Double)
randomBetaStream alpha beta =
randomStream $
randomBeta alpha beta >>= \x ->
return (x, x)
randomWeibullStream :: Double
-> Double
-> Stream (Arrival Double)
randomWeibullStream alpha beta =
randomStream $
randomWeibull alpha beta >>= \x ->
return (x, x)
randomDiscreteStream :: DiscretePDF Double
-> Stream (Arrival Double)
randomDiscreteStream dpdf =
randomStream $
randomDiscrete dpdf >>= \x ->
return (x, x)