]> git.donarmstrong.com Git - ape.git/blob - R/dbd.R
final commit for ape 3.0-8
[ape.git] / R / dbd.R
1 ## dbd.R (2012-09-14)
2
3 ##   Probability Density Under Birth--Death Models
4
5 ## Copyright 2012 Emmanuel Paradis
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 dyule <- function(x, lambda = 0.1, t = 1, log = FALSE)
11 {
12     tmp <- exp(-lambda * t)
13     res <- if (log) log(tmp) + (x - 1) * log(1 - tmp) else tmp * (1 - tmp)^(x - 1)
14     out.of.range <- x <= 0
15     if (any(out.of.range))
16         res[out.of.range] <- if (log) -Inf else 0
17     res
18 }
19
20 dbd <- function(x, lambda, mu, t, conditional = FALSE, log = FALSE)
21 {
22     if (length(lambda) > 1) {
23         lambda <- lambda[1]
24         warning("only the first value of 'lambda' was considered")
25     }
26     if (length(mu) > 1) {
27         mu <- mu[1]
28         warning("only the first value of 'mu' was considered")
29     }
30
31     if (mu == 0) return(dyule(x, lambda, t, log))
32
33     ## for the unconditional case, we have to consider x=0 separately:
34     if (!conditional) {
35         zero <- x == 0
36         out.of.range <- x < 0
37     } else {
38         out.of.range <- x <= 0
39     }
40
41     res <- numeric(length(x))
42
43     ## the situation were speciation and extinction probabilities are equal:
44     if (lambda == mu) {
45         tmp <- lambda * t
46         eta <- tmp/(1 + tmp)
47         if (conditional) {
48             res[] <- if (log) log(1 - eta) + (x - 1) * log(eta) else (1 - eta) * eta^(x - 1)
49         } else { # the unconditional case:
50             if (length(zero)) {
51                 res[zero] <- eta
52                 res[!zero] <- (1 - eta)^2 * eta^(x[!zero] - 1)
53             } else res[] <- (1 - eta)^2 * eta^(x - 1)
54         }
55     } else { # the general case with lambda != mu
56
57         ## this expression is common to the conditional and unconditional cases:
58         Ent <- exp((lambda - mu) * t)
59
60         if (conditional) {
61             if (log) {
62                 res[] <- log(lambda - mu) - log(lambda * Ent - mu) +
63                     (x - 1) * (log(lambda) + log(Ent - 1) - log(lambda * Ent - mu))
64             } else {
65                 eta <- lambda * (Ent - 1)/(lambda * Ent - mu)
66                 res[] <- (1 - eta) * eta^(x - 1)
67             }
68         } else { # finally, the unconditional case:
69             eta <- lambda * (Ent - 1)/(lambda * Ent - mu)
70             if (length(zero)) {
71                 res[zero] <- eta * mu / lambda
72                 res[!zero] <- (1 - mu * eta / lambda) * (1 - eta) * eta^(x[!zero] - 1)
73             } else res[] <- (1 - mu * eta / lambda) * (1 - eta) * eta^(x - 1)
74         }
75     }
76
77     if (any(out.of.range))
78         res[out.of.range] <- if (log) -Inf else 0
79     res
80 }
81
82 dbdTime <- function(x, birth, death, t, conditional = FALSE,
83                     BIRTH = NULL, DEATH = NULL, fast = FALSE)
84 {
85     if (length(t) > 1) {
86         t <- t[1]
87         warning("only the first value of 't' was considered")
88     }
89
90     if (conditional) {
91         PrNt <- function(t, T, x) {
92             tmp <- exp(-RHO(t, T))
93             Wt <- tmp * (1 + INT(t))
94             (1/Wt)*(1 - 1/Wt)^(x - 1)
95         }
96     } else { # the unconditional case:
97         PrNt <- function(t, T, x)
98         {
99             tmp <- exp(-RHO(t, T))
100             Wt <- tmp * (1 + INT(t))
101             out <- numeric(length(x))
102             zero <- x == 0
103             if (length(zero)) {
104                 out[zero] <- 1 - tmp/Wt
105                 out[!zero] <- (tmp/Wt^2)*(1 - 1/Wt)^(x[!zero] - 1)
106             } else out[] <- (tmp/Wt^2)*(1 - 1/Wt)^(x - 1)
107             out
108         }
109     }
110     case <- .getCase(birth, death, BIRTH, DEATH)
111     ff <- .getRHOetINT(birth, death, BIRTH, DEATH, case = case, fast = fast)
112     RHO <- ff[[1]]
113     INT <- ff[[2]]
114     environment(RHO) <- environment(INT) <- environment()
115     Tmax <- t
116     PrNt(0, t, x)
117 }