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

-- |
--   Module      :  ELynx.Tree.Distribution.BirthDeathCriticalNoTime
--   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.BirthDeathCriticalNoTime
  ( BirthDeathCriticalNoTimeDistribution (..),
    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.
newtype BirthDeathCriticalNoTimeDistribution = BDCNTD
  { -- | Birth and death rate.
    BirthDeathCriticalNoTimeDistribution -> Rate
bdcntdLa :: Rate
  }
  deriving (BirthDeathCriticalNoTimeDistribution
-> BirthDeathCriticalNoTimeDistribution -> Bool
(BirthDeathCriticalNoTimeDistribution
 -> BirthDeathCriticalNoTimeDistribution -> Bool)
-> (BirthDeathCriticalNoTimeDistribution
    -> BirthDeathCriticalNoTimeDistribution -> Bool)
-> Eq BirthDeathCriticalNoTimeDistribution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BirthDeathCriticalNoTimeDistribution
-> BirthDeathCriticalNoTimeDistribution -> Bool
$c/= :: BirthDeathCriticalNoTimeDistribution
-> BirthDeathCriticalNoTimeDistribution -> Bool
== :: BirthDeathCriticalNoTimeDistribution
-> BirthDeathCriticalNoTimeDistribution -> Bool
$c== :: BirthDeathCriticalNoTimeDistribution
-> BirthDeathCriticalNoTimeDistribution -> Bool
Eq, Typeable, Typeable BirthDeathCriticalNoTimeDistribution
DataType
Constr
Typeable BirthDeathCriticalNoTimeDistribution
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> BirthDeathCriticalNoTimeDistribution
    -> c BirthDeathCriticalNoTimeDistribution)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r)
    -> Constr
    -> c BirthDeathCriticalNoTimeDistribution)
-> (BirthDeathCriticalNoTimeDistribution -> Constr)
-> (BirthDeathCriticalNoTimeDistribution -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d))
    -> Maybe (c BirthDeathCriticalNoTimeDistribution))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c BirthDeathCriticalNoTimeDistribution))
-> ((forall b. Data b => b -> b)
    -> BirthDeathCriticalNoTimeDistribution
    -> BirthDeathCriticalNoTimeDistribution)
-> (forall r r'.
    (r -> r' -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> BirthDeathCriticalNoTimeDistribution
    -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> BirthDeathCriticalNoTimeDistribution
    -> r)
-> (forall u.
    (forall d. Data d => d -> u)
    -> BirthDeathCriticalNoTimeDistribution -> [u])
-> (forall u.
    Int
    -> (forall d. Data d => d -> u)
    -> BirthDeathCriticalNoTimeDistribution
    -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> BirthDeathCriticalNoTimeDistribution
    -> m BirthDeathCriticalNoTimeDistribution)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> BirthDeathCriticalNoTimeDistribution
    -> m BirthDeathCriticalNoTimeDistribution)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> BirthDeathCriticalNoTimeDistribution
    -> m BirthDeathCriticalNoTimeDistribution)
-> Data BirthDeathCriticalNoTimeDistribution
BirthDeathCriticalNoTimeDistribution -> DataType
BirthDeathCriticalNoTimeDistribution -> Constr
(forall b. Data b => b -> b)
-> BirthDeathCriticalNoTimeDistribution
-> BirthDeathCriticalNoTimeDistribution
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BirthDeathCriticalNoTimeDistribution
-> c BirthDeathCriticalNoTimeDistribution
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c BirthDeathCriticalNoTimeDistribution
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)
-> BirthDeathCriticalNoTimeDistribution
-> u
forall u.
(forall d. Data d => d -> u)
-> BirthDeathCriticalNoTimeDistribution -> [u]
forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalNoTimeDistribution
-> r
forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalNoTimeDistribution
-> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalNoTimeDistribution
-> m BirthDeathCriticalNoTimeDistribution
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalNoTimeDistribution
-> m BirthDeathCriticalNoTimeDistribution
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c BirthDeathCriticalNoTimeDistribution
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BirthDeathCriticalNoTimeDistribution
-> c BirthDeathCriticalNoTimeDistribution
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c BirthDeathCriticalNoTimeDistribution)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BirthDeathCriticalNoTimeDistribution)
$cBDCNTD :: Constr
$tBirthDeathCriticalNoTimeDistribution :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> BirthDeathCriticalNoTimeDistribution
-> m BirthDeathCriticalNoTimeDistribution
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalNoTimeDistribution
-> m BirthDeathCriticalNoTimeDistribution
gmapMp :: (forall d. Data d => d -> m d)
-> BirthDeathCriticalNoTimeDistribution
-> m BirthDeathCriticalNoTimeDistribution
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalNoTimeDistribution
-> m BirthDeathCriticalNoTimeDistribution
gmapM :: (forall d. Data d => d -> m d)
-> BirthDeathCriticalNoTimeDistribution
-> m BirthDeathCriticalNoTimeDistribution
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> BirthDeathCriticalNoTimeDistribution
-> m BirthDeathCriticalNoTimeDistribution
gmapQi :: Int
-> (forall d. Data d => d -> u)
-> BirthDeathCriticalNoTimeDistribution
-> u
$cgmapQi :: forall u.
Int
-> (forall d. Data d => d -> u)
-> BirthDeathCriticalNoTimeDistribution
-> u
gmapQ :: (forall d. Data d => d -> u)
-> BirthDeathCriticalNoTimeDistribution -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u)
-> BirthDeathCriticalNoTimeDistribution -> [u]
gmapQr :: (r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalNoTimeDistribution
-> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalNoTimeDistribution
-> r
gmapQl :: (r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalNoTimeDistribution
-> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> BirthDeathCriticalNoTimeDistribution
-> r
gmapT :: (forall b. Data b => b -> b)
-> BirthDeathCriticalNoTimeDistribution
-> BirthDeathCriticalNoTimeDistribution
$cgmapT :: (forall b. Data b => b -> b)
-> BirthDeathCriticalNoTimeDistribution
-> BirthDeathCriticalNoTimeDistribution
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BirthDeathCriticalNoTimeDistribution)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c BirthDeathCriticalNoTimeDistribution)
dataCast1 :: (forall d. Data d => c (t d))
-> Maybe (c BirthDeathCriticalNoTimeDistribution)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c BirthDeathCriticalNoTimeDistribution)
dataTypeOf :: BirthDeathCriticalNoTimeDistribution -> DataType
$cdataTypeOf :: BirthDeathCriticalNoTimeDistribution -> DataType
toConstr :: BirthDeathCriticalNoTimeDistribution -> Constr
$ctoConstr :: BirthDeathCriticalNoTimeDistribution -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c BirthDeathCriticalNoTimeDistribution
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c BirthDeathCriticalNoTimeDistribution
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BirthDeathCriticalNoTimeDistribution
-> c BirthDeathCriticalNoTimeDistribution
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> BirthDeathCriticalNoTimeDistribution
-> c BirthDeathCriticalNoTimeDistribution
$cp1Data :: Typeable BirthDeathCriticalNoTimeDistribution
Data, (forall x.
 BirthDeathCriticalNoTimeDistribution
 -> Rep BirthDeathCriticalNoTimeDistribution x)
-> (forall x.
    Rep BirthDeathCriticalNoTimeDistribution x
    -> BirthDeathCriticalNoTimeDistribution)
-> Generic BirthDeathCriticalNoTimeDistribution
forall x.
Rep BirthDeathCriticalNoTimeDistribution x
-> BirthDeathCriticalNoTimeDistribution
forall x.
BirthDeathCriticalNoTimeDistribution
-> Rep BirthDeathCriticalNoTimeDistribution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep BirthDeathCriticalNoTimeDistribution x
-> BirthDeathCriticalNoTimeDistribution
$cfrom :: forall x.
BirthDeathCriticalNoTimeDistribution
-> Rep BirthDeathCriticalNoTimeDistribution x
Generic)

instance D.Distribution BirthDeathCriticalNoTimeDistribution where
  cumulative :: BirthDeathCriticalNoTimeDistribution -> Rate -> Rate
cumulative = BirthDeathCriticalNoTimeDistribution -> Rate -> Rate
cumulative

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

instance D.ContDistr BirthDeathCriticalNoTimeDistribution where
  density :: BirthDeathCriticalNoTimeDistribution -> Rate -> Rate
density = BirthDeathCriticalNoTimeDistribution -> Rate -> Rate
density
  quantile :: BirthDeathCriticalNoTimeDistribution -> Rate -> Rate
quantile = BirthDeathCriticalNoTimeDistribution -> Rate -> Rate
quantile

-- | Density function section 2.1.2, first formula; t cancels out because it is
-- expected to be much larger than 1.0; because t \in [0, \infty].
density :: BirthDeathCriticalNoTimeDistribution -> Time -> Double
density :: BirthDeathCriticalNoTimeDistribution -> Rate -> Rate
density (BDCNTD Rate
l) Rate
x
  | Rate
x Rate -> Rate -> Bool
forall a. Ord a => a -> a -> Bool
< Rate
0 = Rate
0
  | Bool
otherwise = Rate
l Rate -> Rate -> Rate
forall a. Fractional a => a -> a -> a
/ ((Rate
1.0 Rate -> Rate -> Rate
forall a. Num a => a -> a -> a
+ Rate
x Rate -> Rate -> Rate
forall a. Num a => a -> a -> a
* Rate
l) Rate -> Rate -> Rate
forall a. Floating a => a -> a -> a
** Rate
2)

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

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