{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}

-- |
--   Module      :  ELynx.Tree.Distribution.BirthDeathCritical
--   Description :  Birth and death distribution
--   Copyright   :  (c) Dominik Schrempf 2021
--   License     :  GPL-3.0-or-later
--
--   Maintainer  :  dominik.schrempf@gmail.com
--   Stability   :  unstable
--   Portability :  portable
--
-- Creation date: Tue Feb 13 13:16:18 2018.
--
-- See Gernhard, T. (2008). The conditioned reconstructed process. Journal of
-- Theoretical Biology, 253(4), 769–778. http://doi.org/10.1016/j.jtbi.2008.04.005.
--
-- Distribution of the values of the point process such that it corresponds to
-- reconstructed trees under the birth and death process; critical birth and death
-- process with lambda=mu.
module ELynx.Tree.Distribution.BirthDeathCritical
  ( BirthDeathCriticalDistribution (..),
    cumulative,
    density,
    quantile,
  )
where

import Data.Data
  ( Data,
    Typeable,
  )
import ELynx.Tree.Distribution.Types
import GHC.Generics (Generic)
import qualified Statistics.Distribution as D

-- | Distribution of the values of the point process such that it corresponds to
-- a reconstructed tree of the birth and death process.
data BirthDeathCriticalDistribution = BDCD
  { -- | Time to origin of the tree.
    BirthDeathCriticalDistribution -> Time
bdcdTOr :: Time,
    -- | Birth and death rate.
    BirthDeathCriticalDistribution -> Time
bdcdLa :: Rate
  }
  deriving (BirthDeathCriticalDistribution
-> BirthDeathCriticalDistribution -> Bool
(BirthDeathCriticalDistribution
 -> BirthDeathCriticalDistribution -> Bool)
-> (BirthDeathCriticalDistribution
    -> BirthDeathCriticalDistribution -> Bool)
-> Eq BirthDeathCriticalDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BirthDeathCriticalDistribution
-> BirthDeathCriticalDistribution -> Bool
$c/= :: BirthDeathCriticalDistribution
-> BirthDeathCriticalDistribution -> Bool
== :: BirthDeathCriticalDistribution
-> BirthDeathCriticalDistribution -> Bool
$c== :: BirthDeathCriticalDistribution
-> BirthDeathCriticalDistribution -> Bool
Eq, Typeable, Typeable BirthDeathCriticalDistribution
DataType
Constr
Typeable BirthDeathCriticalDistribution
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> BirthDeathCriticalDistribution
    -> c BirthDeathCriticalDistribution)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r)
    -> Constr
    -> c BirthDeathCriticalDistribution)
-> (BirthDeathCriticalDistribution -> Constr)
-> (BirthDeathCriticalDistribution -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d))
    -> Maybe (c BirthDeathCriticalDistribution))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c BirthDeathCriticalDistribution))
-> ((forall b. Data b => b -> b)
    -> BirthDeathCriticalDistribution
    -> BirthDeathCriticalDistribution)
-> (forall r r'.
    (r -> r' -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> BirthDeathCriticalDistribution
    -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> BirthDeathCriticalDistribution
    -> r)
-> (forall u.
    (forall d. Data d => d -> u)
    -> BirthDeathCriticalDistribution -> [u])
-> (forall u.
    Int
    -> (forall d. Data d => d -> u)
    -> BirthDeathCriticalDistribution
    -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> BirthDeathCriticalDistribution
    -> m BirthDeathCriticalDistribution)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> BirthDeathCriticalDistribution
    -> m BirthDeathCriticalDistribution)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> BirthDeathCriticalDistribution
    -> m BirthDeathCriticalDistribution)
-> Data BirthDeathCriticalDistribution
BirthDeathCriticalDistribution -> DataType
BirthDeathCriticalDistribution -> Constr
(forall b. Data b => b -> b)
-> BirthDeathCriticalDistribution -> BirthDeathCriticalDistribution
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BirthDeathCriticalDistribution
-> c BirthDeathCriticalDistribution
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c BirthDeathCriticalDistribution
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int
-> (forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution
-> u
forall u.
(forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution -> [u]
forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalDistribution
-> r
forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalDistribution
-> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalDistribution
-> m BirthDeathCriticalDistribution
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalDistribution
-> m BirthDeathCriticalDistribution
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c BirthDeathCriticalDistribution
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BirthDeathCriticalDistribution
-> c BirthDeathCriticalDistribution
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c BirthDeathCriticalDistribution)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BirthDeathCriticalDistribution)
$cBDCD :: Constr
$tBirthDeathCriticalDistribution :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> BirthDeathCriticalDistribution
-> m BirthDeathCriticalDistribution
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalDistribution
-> m BirthDeathCriticalDistribution
gmapMp :: (forall d. Data d => d -> m d)
-> BirthDeathCriticalDistribution
-> m BirthDeathCriticalDistribution
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalDistribution
-> m BirthDeathCriticalDistribution
gmapM :: (forall d. Data d => d -> m d)
-> BirthDeathCriticalDistribution
-> m BirthDeathCriticalDistribution
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalDistribution
-> m BirthDeathCriticalDistribution
gmapQi :: Int
-> (forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution
-> u
$cgmapQi :: forall u.
Int
-> (forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution
-> u
gmapQ :: (forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution -> [u]
gmapQr :: (r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalDistribution
-> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalDistribution
-> r
gmapQl :: (r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalDistribution
-> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalDistribution
-> r
gmapT :: (forall b. Data b => b -> b)
-> BirthDeathCriticalDistribution -> BirthDeathCriticalDistribution
$cgmapT :: (forall b. Data b => b -> b)
-> BirthDeathCriticalDistribution -> BirthDeathCriticalDistribution
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BirthDeathCriticalDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BirthDeathCriticalDistribution)
dataCast1 :: (forall d. Data d => c (t d))
-> Maybe (c BirthDeathCriticalDistribution)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c BirthDeathCriticalDistribution)
dataTypeOf :: BirthDeathCriticalDistribution -> DataType
$cdataTypeOf :: BirthDeathCriticalDistribution -> DataType
toConstr :: BirthDeathCriticalDistribution -> Constr
$ctoConstr :: BirthDeathCriticalDistribution -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c BirthDeathCriticalDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c BirthDeathCriticalDistribution
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BirthDeathCriticalDistribution
-> c BirthDeathCriticalDistribution
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BirthDeathCriticalDistribution
-> c BirthDeathCriticalDistribution
$cp1Data :: Typeable BirthDeathCriticalDistribution
Data, (forall x.
 BirthDeathCriticalDistribution
 -> Rep BirthDeathCriticalDistribution x)
-> (forall x.
    Rep BirthDeathCriticalDistribution x
    -> BirthDeathCriticalDistribution)
-> Generic BirthDeathCriticalDistribution
forall x.
Rep BirthDeathCriticalDistribution x
-> BirthDeathCriticalDistribution
forall x.
BirthDeathCriticalDistribution
-> Rep BirthDeathCriticalDistribution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep BirthDeathCriticalDistribution x
-> BirthDeathCriticalDistribution
$cfrom :: forall x.
BirthDeathCriticalDistribution
-> Rep BirthDeathCriticalDistribution x
Generic)

instance D.Distribution BirthDeathCriticalDistribution where
  cumulative :: BirthDeathCriticalDistribution -> Time -> Time
cumulative = BirthDeathCriticalDistribution -> Time -> Time
cumulative

-- | Cumulative distribution function section 2.1.2, second formula.
cumulative :: BirthDeathCriticalDistribution -> Time -> Double
cumulative :: BirthDeathCriticalDistribution -> Time -> Time
cumulative (BDCD Time
t Time
l) Time
x
  | Time
x Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
<= Time
0 = Time
0
  | Time
x Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
> Time
t = Time
1
  | Bool
otherwise = Time
x Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/ (Time
1.0 Time -> Time -> Time
forall a. Num a => a -> a -> a
+ Time
l Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
x) Time -> Time -> Time
forall a. Num a => a -> a -> a
* (Time
1.0 Time -> Time -> Time
forall a. Num a => a -> a -> a
+ Time
l Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
t) Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/ Time
t

instance D.ContDistr BirthDeathCriticalDistribution where
  density :: BirthDeathCriticalDistribution -> Time -> Time
density = BirthDeathCriticalDistribution -> Time -> Time
density
  quantile :: BirthDeathCriticalDistribution -> Time -> Time
quantile = BirthDeathCriticalDistribution -> Time -> Time
quantile

-- | Density function section 2.1.2, first formula.
density :: BirthDeathCriticalDistribution -> Time -> Double
density :: BirthDeathCriticalDistribution -> Time -> Time
density (BDCD Time
t Time
l) Time
x
  | Time
x Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
< Time
0 = Time
0
  | Time
x Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
> Time
t = Time
0
  | Bool
otherwise = (Time
1.0 Time -> Time -> Time
forall a. Num a => a -> a -> a
+ Time
l Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
t) Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/ (Time
t Time -> Time -> Time
forall a. Num a => a -> a -> a
* (Time
1.0 Time -> Time -> Time
forall a. Num a => a -> a -> a
+ Time
l Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
x) Time -> Time -> Time
forall a. Floating a => a -> a -> a
** Time
2)

-- | Inverted cumulative probability distribution 'cumulative'. See also
-- 'D.ContDistr'.
quantile :: BirthDeathCriticalDistribution -> Double -> Time
quantile :: BirthDeathCriticalDistribution -> Time -> Time
quantile (BDCD Time
t Time
l) Time
p
  | Time
p Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
>= Time
0 Bool -> Bool -> Bool
&& Time
p Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
<= Time
1 =
    Time
res
  | Bool
otherwise =
    [Char] -> Time
forall a. HasCallStack => [Char] -> a
error ([Char] -> Time) -> [Char] -> Time
forall a b. (a -> b) -> a -> b
$
      [Char]
"PointProcess.quantile: p must be in [0,1] range. Got: "
        [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Time -> [Char]
forall a. Show a => a -> [Char]
show Time
p
        [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"."
  where
    res :: Time
res = Time
p Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
t Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/ (Time
1 Time -> Time -> Time
forall a. Num a => a -> a -> a
+ Time
l Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
t Time -> Time -> Time
forall a. Num a => a -> a -> a
- Time
l Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
p Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
t)

instance D.ContGen BirthDeathCriticalDistribution where
  genContVar :: BirthDeathCriticalDistribution -> Gen (PrimState m) -> m Time
genContVar = BirthDeathCriticalDistribution -> Gen (PrimState m) -> m Time
forall d (m :: * -> *).
(ContDistr d, PrimMonad m) =>
d -> Gen (PrimState m) -> m Time
D.genContinuous