-- |
-- Module     : Simulation.Aivika.Parameter.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 parameters of simulation experiments.
--
-- To create a parameter that would return the same value within the simulation run,
-- you should memoize the computation with help of 'memoParameter', which is important
-- for the Monte-Carlo simulation.
--
-- To create a random function that would return the same values in the integration
-- time points within the simulation run, you should either lift the computation to
-- the 'Dynamics' computation and then memoize it too but using the 'memo0Dynamics'
-- function for that computation, or just take the predefined function that does
-- namely this.

module Simulation.Aivika.Parameter.Random
       (randomUniform,
        randomUniformInt,
        randomTriangular,
        randomNormal,
        randomLogNormal,
        randomExponential,
        randomErlang,
        randomPoisson,
        randomBinomial,
        randomGamma,
        randomBeta,
        randomWeibull,
        randomDiscrete,
        randomTrue,
        randomFalse) where

import Control.Monad.Trans

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

-- | Computation that generates a new random number distributed uniformly.
randomUniform :: Double     -- ^ minimum
                 -> Double  -- ^ maximum
                 -> Parameter Double
randomUniform :: Double -> Double -> Parameter Double
randomUniform Double
min Double
max =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Double -> IO Double
generateUniform Generator
g Double
min Double
max

-- | Computation that generates a new random integer number distributed uniformly.
randomUniformInt :: Int     -- ^ minimum
                    -> Int  -- ^ maximum
                    -> Parameter Int
randomUniformInt :: Int -> Int -> Parameter Int
randomUniformInt Int
min Int
max =
  (Run -> IO Int) -> Parameter Int
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Int) -> Parameter Int)
-> (Run -> IO Int) -> Parameter Int
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Int -> Int -> IO Int
generateUniformInt Generator
g Int
min Int
max

-- | Computation that generates a new random number from the triangular distribution.
randomTriangular :: Double     -- ^ minimum
                    -> Double  -- ^ median
                    -> Double  -- ^ maximum
                    -> Parameter Double
randomTriangular :: Double -> Double -> Double -> Parameter Double
randomTriangular Double
min Double
median Double
max =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Double -> Double -> IO Double
generateTriangular Generator
g Double
min Double
median Double
max

-- | Computation that generates a new random number distributed normally.
randomNormal :: Double     -- ^ mean
                -> Double  -- ^ deviation
                -> Parameter Double
randomNormal :: Double -> Double -> Parameter Double
randomNormal Double
mu Double
nu =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Double -> IO Double
generateNormal Generator
g Double
mu Double
nu

-- | Computation that generates a new random number from the lognormal distribution.
randomLogNormal :: 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
                   -> Parameter Double
randomLogNormal :: Double -> Double -> Parameter Double
randomLogNormal Double
mu Double
nu =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Double -> IO Double
generateLogNormal Generator
g Double
mu Double
nu

-- | Computation that returns a new exponential random number with the specified mean
-- (the reciprocal of the rate).
randomExponential :: Double
                     -- ^ the mean (the reciprocal of the rate)
                     -> Parameter Double
randomExponential :: Double -> Parameter Double
randomExponential Double
mu =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> IO Double
generateExponential Generator
g Double
mu

-- | Computation that returns a new Erlang random number with the specified scale
-- (the reciprocal of the rate) and integer shape.
randomErlang :: Double
                -- ^ the scale (the reciprocal of the rate)
                -> Int
                -- ^ the shape
                -> Parameter Double
randomErlang :: Double -> Int -> Parameter Double
randomErlang Double
beta Int
m =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Int -> IO Double
generateErlang Generator
g Double
beta Int
m

-- | Computation that returns a new Poisson random number with the specified mean.
randomPoisson :: Double
                 -- ^ the mean
                 -> Parameter Int
randomPoisson :: Double -> Parameter Int
randomPoisson Double
mu =
  (Run -> IO Int) -> Parameter Int
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Int) -> Parameter Int)
-> (Run -> IO Int) -> Parameter Int
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> IO Int
generatePoisson Generator
g Double
mu

-- | Computation that returns a new binomial random number with the specified
-- probability and trials.
randomBinomial :: Double  -- ^ the probability
                  -> Int  -- ^ the number of trials
                  -> Parameter Int
randomBinomial :: Double -> Int -> Parameter Int
randomBinomial Double
prob Int
trials =
  (Run -> IO Int) -> Parameter Int
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Int) -> Parameter Int)
-> (Run -> IO Int) -> Parameter Int
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Int -> IO Int
generateBinomial Generator
g Double
prob Int
trials

-- | Computation that returns 'True' in case of success.
randomTrue :: Double      -- ^ the probability of the success
              -> Parameter Bool
randomTrue :: Double -> Parameter Bool
randomTrue Double
p =
  do Double
x <- Double -> Double -> Parameter Double
randomUniform Double
0 Double
1
     Bool -> Parameter Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
p)

-- | Computation that returns 'False' in case of success.
randomFalse :: Double      -- ^ the probability of the success
              -> Parameter Bool
randomFalse :: Double -> Parameter Bool
randomFalse Double
p =
  do Double
x <- Double -> Double -> Parameter Double
randomUniform Double
0 Double
1
     Bool -> Parameter Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
p)     

-- | Computation that returns a new random number from the Gamma distribution.
randomGamma :: Double     -- ^ the shape
               -> Double  -- ^ the scale (a reciprocal of the rate)
               -> Parameter Double
randomGamma :: Double -> Double -> Parameter Double
randomGamma Double
kappa Double
theta =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Double -> IO Double
generateGamma Generator
g Double
kappa Double
theta

-- | Computation that returns a new random number from the Beta distribution.
randomBeta :: Double     -- ^ the shape (alpha)
              -> Double  -- ^ the shape (beta)
              -> Parameter Double
randomBeta :: Double -> Double -> Parameter Double
randomBeta Double
alpha Double
beta =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Double -> IO Double
generateBeta Generator
g Double
alpha Double
beta

-- | Computation that returns a new random number from the Weibull distribution.
randomWeibull :: Double     -- ^ shape
                 -> Double  -- ^ scale
                 -> Parameter Double
randomWeibull :: Double -> Double -> Parameter Double
randomWeibull Double
alpha Double
beta =
  (Run -> IO Double) -> Parameter Double
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO Double) -> Parameter Double)
-> (Run -> IO Double) -> Parameter Double
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> Double -> Double -> IO Double
generateWeibull Generator
g Double
alpha Double
beta

-- | Computation that returns a new random value from the specified discrete distribution.
randomDiscrete :: DiscretePDF a -> Parameter a
randomDiscrete :: DiscretePDF a -> Parameter a
randomDiscrete DiscretePDF a
dpdf =
  (Run -> IO a) -> Parameter a
forall a. (Run -> IO a) -> Parameter a
Parameter ((Run -> IO a) -> Parameter a) -> (Run -> IO a) -> Parameter a
forall a b. (a -> b) -> a -> b
$ \Run
r ->
  let g :: Generator
g = Run -> Generator
runGenerator Run
r
  in Generator -> DiscretePDF a -> IO a
Generator -> forall a. DiscretePDF a -> IO a
generateDiscrete Generator
g DiscretePDF a
dpdf