-
Notifications
You must be signed in to change notification settings - Fork 2
/
Prior.jl
186 lines (150 loc) · 4.84 KB
/
Prior.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
function internal_logpdf(
d::CompoundDirichlet,
b_lens::Array{T},
int_leave_map::Vector{Int64},
)::T where {T<:Real}
blen_int = 0.0
blen_leave = 0.0
blen_int_log = 0.0
blen_leave_log = 0.0
nterm = 0.0
@views @inbounds for i in eachindex(int_leave_map)
if int_leave_map[i] === 1
blen_int += b_lens[i]
blen_int_log += log(b_lens[i])
else
blen_leave += b_lens[i]
blen_leave_log += log(b_lens[i])
nterm += 1
end
end
t_l = blen_int + blen_leave
n_int = nterm - 3.0
first = (d.alpha * log(d.beta)) - log(gamma(d.alpha)) - (t_l * d.beta)
second = -log(gamma(d.a)) - log(gamma(d.c)) + log(gamma(d.a + d.c))
third = blen_leave_log * (d.a - 1.0) + blen_int_log * (d.a * d.c - 1.0)
fourth = (d.alpha - d.a * nterm - d.a * d.c * n_int) * log(t_l)
r2 = first + second + third + fourth
return r2
end
function gradlogpdf(d::CompoundDirichlet, x::GeneralNode)
int_ext = internal_external(x)
blv = get_branchlength_vector(x)
# use let block for proper capturing of variables
f = let d = d, int_ext = int_ext
y -> internal_logpdf(d, y, int_ext)
end
v, g = withgradient(f, blv)
return v, g[1]
end
function gradlogpdf(d::exponentialBL, x::GeneralNode)
bl = get_branchlength_vector(x)
f = let z = d.scale
x -> sum(logpdf.(Exponential(z), x))
end
v, g = withgradient(f, bl)
return v, g[1]
end
function gradlogpdf(
t::Union{UniformConstrained,UniformTopology,UniformBranchLength},
x::GeneralNode,
)::Tuple{Float64,Vector{Float64}}
blv = get_branchlength_vector(x)
0.0, zeros(length(blv))
end
function logpdf(d::CompoundDirichlet, x::GeneralNode)
internal_logpdf(d, get_branchlength_vector(x), internal_external(x))
end
function logpdf(
t::Union{UniformConstrained,UniformTopology,UniformBranchLength},
x::GeneralNode,
)
0.0
end
function logpdf(ex::exponentialBL, x::GeneralNode)
bl = get_branchlength_vector(x)
sum(logpdf.(Exponential(ex.scale), bl))
end
function logpdf_sub(d::CompoundDirichlet, x::GeneralNode, transform::Bool)
insupport(LengthDistribution(d), x) ? logpdf(d, x) : -Inf
end
function insupport(l::LengthDistribution, x::GeneralNode)
bl = get_branchlength_vector(x)
all(isfinite.(bl)) && topo_placeholder(x, l) && !any(isnan.(bl)) && all(0.0 .< bl)
end
function insupport(t::UniformConstrained, x::GeneralNode)::Bool
topological(t, x)
end
function insupport(t::UniformTopology, x::GeneralNode)::Bool
true
end
function insupport(t::UniformTopology, x::AbstractArray)::Bool
true
end
function topo_placeholder(x::GeneralNode, l::LengthDistribution)
true
end
function relistlength(d::CompoundDirichlet, x::AbstractArray)
n = length(x)
(Array(x), n)
end
"""
Strict Molecular Clock - BirthDeath
Implemented following Yang & Rannala 1997
doi.org/10.1093/oxfordjournals.molbev.a025811
"""
struct BirthDeath <: ContinuousMultivariateDistribution
s::Int64
rho::Float64
mu::Float64
lambd::Float64
end # struct
length(d::BirthDeath) = d.s - 1
function insupport(d::BirthDeath, t::AbstractVector{T}) where {T<:Real}
length(d) == length(t) && all(isfinite.(t)) && all(0 .< t)
end # function
function _logpdf(d::BirthDeath, t::AbstractVector{T}) where {T<:Real}
numerator::Float64 =
(d.rho * (d.lambd - d.mu)) /
(d.rho * d.lambd + (d.lambd * (1.0 - d.rho) - d.mu) * exp(d.mu - d.lambd))
denum::Float64 = d.rho * (d.lambd - d.mu)
vt1::Float64 = log(1.0 - ((denum * exp(d.mu - d.lambd)) / (d.rho * numerator)))
f::Float64 = log((2.0^(s - 1.0)) / (factorial(s) * (s - 1.0)))
for i in t
f +=
((d.lambd + (1.0 - d.rho) + 2.0 * (denum - numerator)) + (d.mu - d.lambd) * i) -
vt1
end # for
return f
end # function
"""
Strict Molecular Clock - Simplified Birth Death
Implemented folloing Yang & Rannala 1996
doi.org/10.1007/BF02338839
"""
struct BirthDeathSimplified <: ContinuousMultivariateDistribution
s::Int64
mu::Float64
lambd::Float64
end # struct
length(d::BirthDeathSimplified) = d.s - 1
function insupport(d::BirthDeathSimplified, t::AbstractVector{T}) where {T<:Real}
length(d) == length(t) && all(isfinite.(t)) && all(0 .< t)
end # function
function _logpdf(d::BirthDeathSimplified, t::AbstractVector{T}) where {T<:Real}
f::Float64 = log(2.0) * (d.s - 1.0) + log(d.mu) * (d.s - 2.0)
p0::Float =
(d.s - 2.0) * log(
(d.mu * (1.0 - exp(-(d.lambd - d.mu)))) /
(d.lambd - d.mu * exp(-(d.lambd - d.mu))),
)
p0 += log(factorial(d.s) * (d.s - 1.0))
f -= po
con::Float64 = 2.0 * log(d.lambd - d.mu)
for i in t
f +=
(con - (d.lambd - d.mu) * i) -
log(d.lambd - d.mu * exp(-(d.lambd - d.mu) * i)) * 2.0
end # for
return f
end # function