-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_sum_log_trans_prob.jl
320 lines (277 loc) · 9.83 KB
/
naive_sum_log_trans_prob.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using KahanSummation;
using SpecialFunctions;
# support functions aware of floating-point arithmetic
function log1mexp(x)
if x > -log(2)
log(-expm1(x))
else
log1p(-exp(x))
end
end
function logexpm1(x)
if x >= 37
x
elseif x >= 19
x - exp(-x)
else
log(expm1(x))
end
end
function log1pexp(x)
if x <= -37
exp(x)
elseif x <= 18
log1p(exp(x))
elseif x <= 33.3
x + exp(-x)
else
x
end
end
# these functions are the log-addends in the transition probability summation
# depending on parameter values, they are designed to reduce floating-point
# rounding error as much as possible
function log_addend_equal_rates_v1(h, i, j, θ, ω)
log(i) + lgamma(i + j - h) - lgamma(1 + h) - lgamma(i + 1 - h) -
lgamma(j + 1 - h) + (i + j - 2 * h) * ω + h * log((1 - θ) / (1 + θ))
end
function log_addend_equal_rates_v2(h, i, j, θ, ω)
log(i) + lgamma(i + j - h) - lgamma(1 + h) - lgamma(i + 1 - h) -
lgamma(j + 1 - h) + (i + j - 2 * h) * ω + h * log((θ - 1) / (θ + 1))
end
function log_addend_birth_rate_greater_v1(h, i, j, θ, ω)
log(i) + lgamma(i + j - h) - lgamma(1 + h) - lgamma(i + 1 - h) -
lgamma(j + 1 - h) + i * ω +
(i + j - 2 * h) * (log1mexp(θ) - log1mexp(ω + θ)) +
h * log(-expm1(θ - ω) / expm1(θ + ω))
end
function log_addend_birth_rate_greater_v2(h, i, j, θ, ω)
log(i) + lgamma(i + j - h) - lgamma(1 + h) - lgamma(i + 1 - h) -
lgamma(j + 1 - h) + i * ω +
(i + j - 2 * h) * (log1mexp(θ) - log1mexp(ω + θ)) +
h * log(expm1(θ - ω) / expm1(θ + ω))
end
function log_addend_death_rate_greater_v1(h, i, j, θ, ω)
log(i) + lgamma(i + j - h) - lgamma(1 + h) - lgamma(i + 1 - h) -
lgamma(j + 1 - h) + j * ω +
(i + j - 2 * h) * (log1mexp(θ) - log1mexp(ω + θ)) +
h * log(-expm1(θ - ω) / expm1(θ + ω))
end
function log_addend_death_rate_greater_v2(h, i, j, θ, ω)
log(i) + lgamma(i + j - h) - lgamma(1 + h) - lgamma(i + 1 - h) -
lgamma(j + 1 - h) + j * ω +
(i + j - 2 * h) * (log1mexp(θ) - log1mexp(ω + θ)) +
h * log(expm1(θ - ω) / expm1(θ + ω))
end
# naive summation algorithm with compensated summation
function log_trans_prob(
i::F,
j::F,
t::F,
λ::F,
μ::F
)::F where {
F <: AbstractFloat
}
# anything lower than ϵ (subnormal numbers) are treated as zero
ϵ = floatmin(F)
# a = max(i, j)
# b = min(i, j)
a, b = (j <= i) ? (i, j) : (j, i)
if (t < ϵ) || ((λ < ϵ) && (μ < ϵ))
# time is zero or both parameters are zero
if i == j
# probability is 1 => log(1) = 0
0
else
# probability is 0 => by convention we set log(0) = -Inf
-Inf
end
elseif λ ≈ μ
# rates are equal for our floating-point machine
ξ = 1 / λ
θ = λ * t
ω = log(θ / (1 + θ))
if j > 0
if t ≈ ξ
# in this case we have a simple closed form solution
# log(2) is automatically converted to a Float64 therefore we use
# log(F(2)) for type stability
lgamma(i + j) - lgamma(i) - lgamma(j + 1) - (i + j) * log(F(2))
elseif t < ξ
# addends are all positive and we can safely evaluate each value
x = [log_addend_equal_rates_v1(h, i, j, θ, ω) for h = 0:b]
# we have logarithms but we must sum exp(x)
# in this case each and every exp(x[u]) is very close to zero (their sum
# must be in [0, 1] by definition of probability)
# use Kahan summation algorithm and other numerical tricks to reduce
# rounding error
sort!(x, rev=true)
# log(exp(x[1]) + exp(x[2]) + ... + exp(x[n])) =
# = x[1] + log(1 + exp(x[2] - x[1]) + ... + exp(x[n] - x[1]))
x[1] + log1p(sum_kbn(exp.(x[2:end] .- x[1])))
else
# addends are alternating in sign and we have a huge problem
# compute the positive and negative values separately
x = [log_addend_equal_rates_v2(h, i, j, θ, ω) for h = 0:2:b]
y = [log_addend_equal_rates_v2(h, i, j, θ, ω) for h = 1:2:b]
# we sort them and try to subtract similar magnitude numbers
sort!(x, rev=true)
sort!(y, rev=true)
# we cannot use exp(x) and exp(y) directly because they might have a
# magnitude that is considered Inf by our machine
# we normalize them by the absolute maximum
M = max(x[1], y[1])
# when b is odd, length(x) and length(y) are the same
# when b is even, length(x) = length(y) + 1
z = if (b % 2) != 0
exp.(x .- M) .- exp.(y .- M)
else
[exp.(x[1:(end - 1)] .- M) .- exp.(y .- M); exp(x[end] - M)]
end
s = sum_kbn(z)
# the sum must be positive because otherwise the probability would
# be negative. If `s` is negative it means that numerical error is too
# big
if s > 0
M + log(s)
else
-Inf
end
end
else
# j = 0
if t ≈ ξ
- i * log(F(2))
else
i * log(ω)
end
end
elseif μ < ϵ
# pure birth process
if j >= i
θ = λ * t
lgamma(j) + (j - i) * logexpm1(θ) -
(lgamma(i) + lgamma(j - i + 1) + j * θ)
else
# final population size cannot be lower than the initial population size
-Inf
end
elseif λ < ϵ
# pure death process
if j <= i
θ = μ * t
lgamma(i + 1) + (i - j) * logexpm1(θ) -
(lgamma(j + 1) + lgamma(i - j + 1) + i * θ)
else
# final population size cannot be higher than the initial population size
-Inf
end
else
ξ = log(λ / μ) / (λ - μ)
if j > 0
if t ≈ ξ
# in this case we have a simple closed form solution
lgamma(i + j) + i * log(μ) + j * log(λ) -
(lgamma(i) + lgamma(j + 1) + (i + j) * log(λ + μ))
elseif λ > μ
θ = (μ - λ) * t
ω = log(μ / λ)
if t < ξ
# addends are all positive and we can safely evaluate each value
x = [log_addend_birth_rate_greater_v1(h, i, j, θ, ω) for h = 0:b]
# we have logarithms but we must sum exp(x)
# in this case each and every exp(x[u]) is very close to zero (their
# sum must be in [0, 1] by definition of probability)
# use Kahan summation algorithm and other numerical tricks to reduce
# rounding error
sort!(x, rev=true)
# log(exp(x[1]) + exp(x[2]) + ... + exp(x[n])) =
# = x[1] + log(1 + exp(x[2] - x[1]) + ... + exp(x[n] - x[1]))
x[1] + log1p(sum_kbn(exp.(x[2:end] .- x[1])))
else
# addends are alternating in sign and we have a huge problem
# compute the positive and negative values separately
x = [log_addend_birth_rate_greater_v2(h, i, j, θ, ω) for h = 0:2:b]
y = [log_addend_birth_rate_greater_v2(h, i, j, θ, ω) for h = 1:2:b]
# we sort them and try to subtract similar magnitude numbers
sort!(x, rev=true)
sort!(y, rev=true)
# we cannot use exp(x) and exp(y) directly because they might have a
# magnitude that is considered Inf by our machine
# we normalize them by the absolute maximum
M = max(x[1], y[1])
# when b is odd, length(x) and length(y) are the same
# when b is even, length(x) = length(y) + 1
z = if (b % 2) != 0
exp.(x .- M) .- exp.(y .- M)
else
[exp.(x[1:(end - 1)] .- M) .- exp.(y .- M); exp(x[end] - M)]
end
s = sum_kbn(z)
# the sum must be positive because otherwise the probability would
# be negative. If `s` is negative it means that numerical error is too
# big
if s > 0
M + log(s)
else
-Inf
end
end
else
θ = (λ - μ) * t
ω = log(λ / μ)
if t < ξ
# addends are all positive and we can safely evaluate each value
x = [log_addend_death_rate_greater_v1(h, i, j, θ, ω) for h = 0:b]
# we have logarithms but we must sum exp(x)
# in this case each and every exp(x[u]) is very close to zero (their
# sum must be in [0, 1] by definition of probability)
# use Kahan summation algorithm and other numerical tricks to reduce
# rounding error
sort!(x, rev=true)
# log(exp(x[1]) + exp(x[2]) + ... + exp(x[n])) =
# = x[1] + log(1 + exp(x[2] - x[1]) + ... + exp(x[n] - x[1]))
x[1] + log1p(sum_kbn(exp.(x[2:end] .- x[1])))
else
# addends are alternating in sign and we have a huge problem
# compute the positive and negative values separately
x = [log_addend_death_rate_greater_v2(h, i, j, θ, ω) for h = 0:2:b]
y = [log_addend_death_rate_greater_v2(h, i, j, θ, ω) for h = 1:2:b]
# we sort them and try to subtract similar magnitude numbers
sort!(x, rev=true)
sort!(y, rev=true)
# we cannot use exp(x) and exp(y) directly because they might have a
# magnitude that is considered Inf by our machine
# we normalize them by the absolute maximum
M = max(x[1], y[1])
# when b is odd, length(x) and length(y) are the same
# when b is even, length(x) = length(y) + 1
z = if (b % 2) != 0
exp.(x .- M) .- exp.(y .- M)
else
[exp.(x[1:(end - 1)] .- M) .- exp.(y .- M); exp(x[end] - M)]
end
s = sum_kbn(z)
# the sum must be positive because otherwise the probability would
# be negative. If `s` is negative it means that numerical error is too
# big
if s > 0
M + log(s)
else
-Inf
end
end
end
else
# j = 0
if t ≈ ξ
i * log(μ / (λ + μ))
else
θ = (λ - μ) * t
ω = log(λ / μ)
i * log(expm1(θ) / expm1(ω + θ))
end
end
end
end