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

-- |
--   Module      :  ELynx.Tree.Distribution.BirthDeathCritical
--   Description :  Birth and death distribution
--   Copyright   :  2021 Dominik Schrempf
--   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 -> Double
bdcdTOr :: Time,
    -- | Birth and death rate.
    BirthDeathCriticalDistribution -> Double
bdcdLa :: Rate
  }
  deriving (BirthDeathCriticalDistribution
-> BirthDeathCriticalDistribution -> Bool
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
BirthDeathCriticalDistribution -> DataType
BirthDeathCriticalDistribution -> Constr
(forall b. Data b => b -> b)
-> BirthDeathCriticalDistribution -> 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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
Monad m =>
(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 :: forall u.
Int
-> (forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution
-> u
$cgmapQi :: forall u.
Int
-> (forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution
-> u
gmapQ :: forall u.
(forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u)
-> BirthDeathCriticalDistribution -> [u]
gmapQr :: forall r r'.
(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 :: forall r r'.
(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 (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(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 (t :: * -> *) (c :: * -> *).
Typeable t =>
(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 (c :: * -> *).
(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 (c :: * -> *).
(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
Data, 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 -> Double -> Double
cumulative = BirthDeathCriticalDistribution -> Double -> Double
cumulative

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

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

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

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

instance D.ContGen BirthDeathCriticalDistribution where
  genContVar :: forall g (m :: * -> *).
StatefulGen g m =>
BirthDeathCriticalDistribution -> g -> m Double
genContVar = forall d g (m :: * -> *).
(ContDistr d, StatefulGen g m) =>
d -> g -> m Double
D.genContinuous