3 ## Probability Density Under Birth--Death Models
5 ## Copyright 2012 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 dyule <- function(x, lambda = 0.1, t = 1, log = FALSE)
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
20 dbd <- function(x, lambda, mu, t, conditional = FALSE, log = FALSE)
22 if (length(lambda) > 1) {
24 warning("only the first value of 'lambda' was considered")
28 warning("only the first value of 'mu' was considered")
31 if (mu == 0) return(dyule(x, lambda, t, log))
33 ## for the unconditional case, we have to consider x=0 separately:
38 out.of.range <- x <= 0
41 res <- numeric(length(x))
43 ## the situation were speciation and extinction probabilities are equal:
48 res[] <- if (log) log(1 - eta) + (x - 1) * log(eta) else (1 - eta) * eta^(x - 1)
49 } else { # the unconditional case:
52 res[!zero] <- (1 - eta)^2 * eta^(x[!zero] - 1)
53 } else res[] <- (1 - eta)^2 * eta^(x - 1)
55 } else { # the general case with lambda != mu
57 ## this expression is common to the conditional and unconditional cases:
58 Ent <- exp((lambda - mu) * t)
62 res[] <- log(lambda - mu) - log(lambda * Ent - mu) +
63 (x - 1) * (log(lambda) + log(Ent - 1) - log(lambda * Ent - mu))
65 eta <- lambda * (Ent - 1)/(lambda * Ent - mu)
66 res[] <- (1 - eta) * eta^(x - 1)
68 } else { # finally, the unconditional case:
69 eta <- lambda * (Ent - 1)/(lambda * Ent - mu)
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)
77 if (any(out.of.range))
78 res[out.of.range] <- if (log) -Inf else 0
82 dbdTime <- function(x, birth, death, t, conditional = FALSE,
83 BIRTH = NULL, DEATH = NULL, fast = FALSE)
87 warning("only the first value of 't' was considered")
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)
96 } else { # the unconditional case:
97 PrNt <- function(t, T, x)
99 tmp <- exp(-RHO(t, T))
100 Wt <- tmp * (1 + INT(t))
101 out <- numeric(length(x))
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)
110 case <- .getCase(birth, death, BIRTH, DEATH)
111 ff <- .getRHOetINT(birth, death, BIRTH, DEATH, case = case, fast = fast)
114 environment(RHO) <- environment(INT) <- environment()