-- |
-- Module     : Simulation.Aivika.Trans.Dynamics.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 the random functions that always return the same values
-- in the integration time points within a single simulation run. The values
-- for another simulation run will be regenerated anew.
--
-- For example, the computations returned by these functions can be used in
-- the equations of System Dynamics.
--
-- Also it is worth noting that the values are generated in a strong order starting
-- from 'starttime' with step 'dt'. This is how the 'memo0Dynamics' function
-- actually works.
--

module Simulation.Aivika.Trans.Dynamics.Random
       (memoRandomUniformDynamics,
        memoRandomUniformIntDynamics,
        memoRandomTriangularDynamics,
        memoRandomNormalDynamics,
        memoRandomLogNormalDynamics,
        memoRandomExponentialDynamics,
        memoRandomErlangDynamics,
        memoRandomPoissonDynamics,
        memoRandomBinomialDynamics,
        memoRandomGammaDynamics,
        memoRandomBetaDynamics,
        memoRandomWeibullDynamics,
        memoRandomDiscreteDynamics) where

import Simulation.Aivika.Trans.Generator
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Parameter
import Simulation.Aivika.Trans.Internal.Simulation
import Simulation.Aivika.Trans.Internal.Dynamics
import Simulation.Aivika.Trans.Dynamics.Memo.Unboxed
import Simulation.Aivika.Trans.SD

-- | Computation that generates random numbers distributed uniformly and
-- memoizes them in the integration time points.
memoRandomUniformDynamics :: MonadSD m
                             => Dynamics m Double     -- ^ minimum
                             -> Dynamics m Double     -- ^ maximum
                             -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomUniformDynamics #-}
memoRandomUniformDynamics :: Dynamics m Double
-> Dynamics m Double -> Simulation m (Dynamics m Double)
memoRandomUniformDynamics Dynamics m Double
min Dynamics m Double
max =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
min' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
min
     Double
max' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
max
     Generator m -> Double -> Double -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Double -> m Double
generateUniform Generator m
g Double
min' Double
max'

-- | Computation that generates random integer numbers distributed uniformly and
-- memoizes them in the integration time points.
memoRandomUniformIntDynamics :: MonadSD m
                                => Dynamics m Int     -- ^ minimum
                                -> Dynamics m Int     -- ^ maximum
                                -> Simulation m (Dynamics m Int)
{-# INLINABLE memoRandomUniformIntDynamics #-}
memoRandomUniformIntDynamics :: Dynamics m Int -> Dynamics m Int -> Simulation m (Dynamics m Int)
memoRandomUniformIntDynamics Dynamics m Int
min Dynamics m Int
max =
  Dynamics m Int -> Simulation m (Dynamics m Int)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Int -> Simulation m (Dynamics m Int))
-> Dynamics m Int -> Simulation m (Dynamics m Int)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Int) -> Dynamics m Int
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Int) -> Dynamics m Int)
-> (Point m -> m Int) -> Dynamics m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Int
min' <- Point m -> Dynamics m Int -> m Int
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Int
min
     Int
max' <- Point m -> Dynamics m Int -> m Int
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Int
max
     Generator m -> Int -> Int -> m Int
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Int -> Int -> m Int
generateUniformInt Generator m
g Int
min' Int
max'

-- | Computation that generates random numbers from the triangular distribution
-- and memoizes the numbers in the integration time points.
memoRandomTriangularDynamics :: MonadSD m
                                => Dynamics m Double  -- ^ minimum
                                -> Dynamics m Double  -- ^ median
                                -> Dynamics m Double  -- ^ maximum
                                -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomTriangularDynamics #-}
memoRandomTriangularDynamics :: Dynamics m Double
-> Dynamics m Double
-> Dynamics m Double
-> Simulation m (Dynamics m Double)
memoRandomTriangularDynamics Dynamics m Double
min Dynamics m Double
median Dynamics m Double
max =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
min' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
min
     Double
median' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
median
     Double
max' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
max
     Generator m -> Double -> Double -> Double -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Double -> Double -> m Double
generateTriangular Generator m
g Double
min' Double
median' Double
max'

-- | Computation that generates random numbers distributed normally and
-- memoizes them in the integration time points.
memoRandomNormalDynamics :: MonadSD m
                            => Dynamics m Double     -- ^ mean
                            -> Dynamics m Double     -- ^ deviation
                            -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomNormalDynamics #-}
memoRandomNormalDynamics :: Dynamics m Double
-> Dynamics m Double -> Simulation m (Dynamics m Double)
memoRandomNormalDynamics Dynamics m Double
mu Dynamics m Double
nu =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
mu' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
mu
     Double
nu' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
nu
     Generator m -> Double -> Double -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Double -> m Double
generateNormal Generator m
g Double
mu' Double
nu'

-- | Computation that generates random numbers from the lognormal distribution
-- and memoizes the numbers in the integration time points.
memoRandomLogNormalDynamics :: MonadSD m
                               => Dynamics m Double
                               -- ^ the mean of a normal distribution which
                               -- this distribution is derived from
                               -> Dynamics m Double
                               -- ^ the deviation of a normal distribution which
                               -- this distribution is derived from
                               -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomLogNormalDynamics #-}
memoRandomLogNormalDynamics :: Dynamics m Double
-> Dynamics m Double -> Simulation m (Dynamics m Double)
memoRandomLogNormalDynamics Dynamics m Double
mu Dynamics m Double
nu =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
mu' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
mu
     Double
nu' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
nu
     Generator m -> Double -> Double -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Double -> m Double
generateLogNormal Generator m
g Double
mu' Double
nu'

-- | Computation that generates exponential random numbers with the specified mean
-- (the reciprocal of the rate) and memoizes them in the integration time points.
memoRandomExponentialDynamics :: MonadSD m
                                 => Dynamics m Double
                                 -- ^ the mean (the reciprocal of the rate)
                                 -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomExponentialDynamics #-}
memoRandomExponentialDynamics :: Dynamics m Double -> Simulation m (Dynamics m Double)
memoRandomExponentialDynamics Dynamics m Double
mu =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
mu' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
mu
     Generator m -> Double -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> m Double
generateExponential Generator m
g Double
mu'

-- | Computation that generates the Erlang random numbers with the specified scale
-- (the reciprocal of the rate) and integer shape but memoizes them in the integration
-- time points.
memoRandomErlangDynamics :: MonadSD m
                            => Dynamics m Double
                            -- ^ the scale (the reciprocal of the rate)
                            -> Dynamics m Int
                            -- ^ the shape
                            -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomErlangDynamics #-}
memoRandomErlangDynamics :: Dynamics m Double
-> Dynamics m Int -> Simulation m (Dynamics m Double)
memoRandomErlangDynamics Dynamics m Double
beta Dynamics m Int
m =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
beta' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
beta
     Int
m' <- Point m -> Dynamics m Int -> m Int
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Int
m
     Generator m -> Double -> Int -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Int -> m Double
generateErlang Generator m
g Double
beta' Int
m'

-- | Computation that generats the Poisson random numbers with the specified mean
-- and memoizes them in the integration time points.
memoRandomPoissonDynamics :: MonadSD m
                             => Dynamics m Double
                             -- ^ the mean
                             -> Simulation m (Dynamics m Int)
{-# INLINABLE memoRandomPoissonDynamics #-}
memoRandomPoissonDynamics :: Dynamics m Double -> Simulation m (Dynamics m Int)
memoRandomPoissonDynamics Dynamics m Double
mu =
  Dynamics m Int -> Simulation m (Dynamics m Int)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Int -> Simulation m (Dynamics m Int))
-> Dynamics m Int -> Simulation m (Dynamics m Int)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Int) -> Dynamics m Int
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Int) -> Dynamics m Int)
-> (Point m -> m Int) -> Dynamics m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
mu' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
mu
     Generator m -> Double -> m Int
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> m Int
generatePoisson Generator m
g Double
mu'

-- | Computation that generates binomial random numbers with the specified
-- probability and trials but memoizes them in the integration time points.
memoRandomBinomialDynamics :: MonadSD m
                              => Dynamics m Double  -- ^ the probability
                              -> Dynamics m Int  -- ^ the number of trials
                              -> Simulation m (Dynamics m Int)
{-# INLINABLE memoRandomBinomialDynamics #-}
memoRandomBinomialDynamics :: Dynamics m Double
-> Dynamics m Int -> Simulation m (Dynamics m Int)
memoRandomBinomialDynamics Dynamics m Double
prob Dynamics m Int
trials =
  Dynamics m Int -> Simulation m (Dynamics m Int)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Int -> Simulation m (Dynamics m Int))
-> Dynamics m Int -> Simulation m (Dynamics m Int)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Int) -> Dynamics m Int
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Int) -> Dynamics m Int)
-> (Point m -> m Int) -> Dynamics m Int
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
prob' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
prob
     Int
trials' <- Point m -> Dynamics m Int -> m Int
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Int
trials
     Generator m -> Double -> Int -> m Int
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Int -> m Int
generateBinomial Generator m
g Double
prob' Int
trials'

-- | Computation that generates random numbers from the Gamma distribution
-- with the specified shape and scale but memoizes the numbers in
-- the integration time points.
memoRandomGammaDynamics :: MonadSD m
                           => Dynamics m Double  -- ^ shape
                           -> Dynamics m Double  -- ^ scale (a reciprocal of the rate)
                           -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomGammaDynamics #-}
memoRandomGammaDynamics :: Dynamics m Double
-> Dynamics m Double -> Simulation m (Dynamics m Double)
memoRandomGammaDynamics Dynamics m Double
kappa Dynamics m Double
theta =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
kappa' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
kappa
     Double
theta' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
theta
     Generator m -> Double -> Double -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Double -> m Double
generateGamma Generator m
g Double
kappa' Double
theta'

-- | Computation that generates random numbers from the Beta distribution
-- by the specified shape parameters and memoizes the numbers in
-- the integration time points.
memoRandomBetaDynamics :: MonadSD m
                          => Dynamics m Double  -- ^ shape (alpha)
                          -> Dynamics m Double  -- ^ shape (beta)
                          -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomBetaDynamics #-}
memoRandomBetaDynamics :: Dynamics m Double
-> Dynamics m Double -> Simulation m (Dynamics m Double)
memoRandomBetaDynamics Dynamics m Double
alpha Dynamics m Double
beta =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
alpha' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
alpha
     Double
beta'  <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
beta
     Generator m -> Double -> Double -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Double -> m Double
generateBeta Generator m
g Double
alpha' Double
beta'

-- | Computation that generates random numbers from the Weibull distribution
-- with the specified shape and scale but memoizes the numbers in
-- the integration time points.
memoRandomWeibullDynamics :: MonadSD m
                             => Dynamics m Double  -- ^ shape
                             -> Dynamics m Double  -- ^ scale
                             -> Simulation m (Dynamics m Double)
{-# INLINABLE memoRandomWeibullDynamics #-}
memoRandomWeibullDynamics :: Dynamics m Double
-> Dynamics m Double -> Simulation m (Dynamics m Double)
memoRandomWeibullDynamics Dynamics m Double
alpha Dynamics m Double
beta =
  Dynamics m Double -> Simulation m (Dynamics m Double)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m Double -> Simulation m (Dynamics m Double))
-> Dynamics m Double -> Simulation m (Dynamics m Double)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m Double) -> Dynamics m Double
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m Double) -> Dynamics m Double)
-> (Point m -> m Double) -> Dynamics m Double
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     Double
alpha' <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
alpha
     Double
beta'  <- Point m -> Dynamics m Double -> m Double
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m Double
beta
     Generator m -> Double -> Double -> m Double
forall (m :: * -> *).
MonadGenerator m =>
Generator m -> Double -> Double -> m Double
generateWeibull Generator m
g Double
alpha' Double
beta'

-- | Computation that generates random values from the specified discrete
-- distribution and memoizes the values in the integration time points.
memoRandomDiscreteDynamics :: (MonadSD m, MonadMemo m a) => Dynamics m (DiscretePDF a) -> Simulation m (Dynamics m a)
{-# INLINABLE memoRandomDiscreteDynamics #-}
memoRandomDiscreteDynamics :: Dynamics m (DiscretePDF a) -> Simulation m (Dynamics m a)
memoRandomDiscreteDynamics Dynamics m (DiscretePDF a)
dpdf =
  Dynamics m a -> Simulation m (Dynamics m a)
forall (m :: * -> *) e.
MonadMemo m e =>
Dynamics m e -> Simulation m (Dynamics m e)
memo0Dynamics (Dynamics m a -> Simulation m (Dynamics m a))
-> Dynamics m a -> Simulation m (Dynamics m a)
forall a b. (a -> b) -> a -> b
$
  (Point m -> m a) -> Dynamics m a
forall (m :: * -> *) a. (Point m -> m a) -> Dynamics m a
Dynamics ((Point m -> m a) -> Dynamics m a)
-> (Point m -> m a) -> Dynamics m a
forall a b. (a -> b) -> a -> b
$ \Point m
p ->
  do let g :: Generator m
g = Run m -> Generator m
forall (m :: * -> *). Run m -> Generator m
runGenerator (Run m -> Generator m) -> Run m -> Generator m
forall a b. (a -> b) -> a -> b
$ Point m -> Run m
forall (m :: * -> *). Point m -> Run m
pointRun Point m
p
     DiscretePDF a
dpdf' <- Point m -> Dynamics m (DiscretePDF a) -> m (DiscretePDF a)
forall (m :: * -> *) a. Point m -> Dynamics m a -> m a
invokeDynamics Point m
p Dynamics m (DiscretePDF a)
dpdf
     Generator m -> DiscretePDF a -> m a
forall (m :: * -> *) a.
MonadGenerator m =>
Generator m -> DiscretePDF a -> m a
generateDiscrete Generator m
g DiscretePDF a
dpdf'