-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhmm.rs
424 lines (374 loc) · 12.3 KB
/
hmm.rs
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::fmt::{Display, Formatter};
use std::ops::Range;
use anyhow::bail;
use log_once::debug_once;
const STATE_NUM: usize = 2usize;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(usize)]
pub(crate) enum States {
Same = 0,
Different = 1,
}
impl Into<usize> for States {
fn into(self) -> usize {
self as usize
}
}
impl From<usize> for States {
fn from(value: usize) -> Self {
match value {
0 => Self::Same,
1 => Self::Different,
_ => unreachable!("invalid {value}"),
}
}
}
impl Display for States {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let label = match self {
States::Same => "same",
States::Different => "different",
};
write!(f, "{label}")
}
}
#[derive(Copy, Clone, Debug)]
struct DpCell {
inner: [f64; STATE_NUM], // todo make the state num const generic
}
impl DpCell {
#[inline]
fn new_full(val: f64) -> Self {
Self { inner: [val; STATE_NUM] }
}
fn new_empty() -> Self {
Self::new_full(f64::NEG_INFINITY)
}
// fn total_probability(&self) -> f64 {
// rv::misc::logsumexp(&self.inner)
// }
fn get_value(&self, state: States) -> f64 {
self.inner[state as usize]
}
fn get_value_mut(&mut self, state: States) -> &mut f64 {
&mut self.inner[state as usize]
}
fn set_value(&mut self, state: States, value: f64) {
assert!(
value.is_finite() && !value.is_nan(),
"cannot set {value} to state {state:?}"
);
self.inner[state as usize] = value;
}
fn argmax(&self) -> States {
self.inner
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
.map(|(i, _)| States::from(i))
.unwrap()
}
}
#[derive(Debug)]
struct PointerCell {
inner: [Option<States>; STATE_NUM],
}
impl PointerCell {
fn empty() -> Self {
Self { inner: [None; STATE_NUM] }
}
fn get_value(&self, state: States) -> Option<States> {
self.inner[state as usize]
}
fn set_value(&mut self, state: States, value: States) {
self.inner[state as usize] = Some(value);
}
}
pub(crate) struct HmmModel {
same_to_same: f64,
// diff_to_diff: f64,
same_to_diff: f64,
// diff_to_same: f64,
dmr_prior: f64,
same_state_factor: f64,
diff_state_factor: f64,
significance_factor: f64,
linear_proj: bool,
projection: Projection,
}
impl HmmModel {
fn prob_to_factor(fpr: f64) -> anyhow::Result<f64> {
if fpr < 0f64 {
bail!("fpr cannot be less than 0")
} else if fpr >= 1.0 {
bail!("fpr cannot be >= 1.0")
} else {
Ok((fpr / (1f64 - fpr)).ln())
}
}
pub(crate) fn new(
dmr_prior: f64,
diff_stay: f64,
same_state_factor: f64,
diff_state_factor: f64,
significance_factor: f64,
decay_distance: u32,
linear_proj: bool,
) -> anyhow::Result<Self> {
let same_to_diff = dmr_prior.ln();
let same_to_same = (1f64 - dmr_prior).ln();
// let diff_to_diff = diff_stay.ln();
// let diff_to_same = (1f64 - diff_stay).ln();
let projection = Projection::new(decay_distance, diff_stay, dmr_prior)?;
let significance_factor = Self::prob_to_factor(significance_factor)?;
Ok(Self {
same_to_same,
same_to_diff,
same_state_factor,
dmr_prior,
diff_state_factor,
significance_factor,
linear_proj,
projection,
})
}
pub(crate) fn viterbi_path(
&self,
scores: &[f64],
positions: &[u64],
) -> Vec<States> {
// P_s = e^(-score)
let probs = scores
.iter()
.map(|&x| if x < 0f64 { 0f64 } else { x })
.map(|x| (-1f64 * x).exp())
.collect::<Vec<f64>>();
let transitions =
positions.windows(2).fold(vec![self.dmr_prior], |mut agg, wind| {
assert_eq!(wind.len(), 2);
assert!(wind[1] > wind[0]);
let gap = (wind[1] - wind[0]) as f64;
assert!(gap > 0f64, "gap should be greater than zero");
let p_diff_to_diff = if self.linear_proj {
self.projection.linear_project_prob(gap)
} else {
self.projection.ln_project_prob(gap)
};
agg.push(p_diff_to_diff);
agg
});
assert_eq!(probs.len(), transitions.len());
let (dp_matrix, pointers) = self.viterbi_forward(&probs, &transitions);
let path = self.viterbi_decode(&dp_matrix, &pointers);
assert_eq!(path.len(), scores.len() - 1);
path
}
fn viterbi_decode(
&self,
dp_matrix: &[DpCell],
pointers: &[PointerCell],
) -> Vec<States> {
let final_state = dp_matrix.last().unwrap().argmax();
// dbg!(final_state);
let mut path = vec![final_state];
let mut curr_pointer =
pointers.last().unwrap().get_value(final_state).unwrap();
for pointers in pointers.iter().rev().skip(1) {
let pointer = pointers.get_value(curr_pointer);
if let Some(pointer) = pointer {
path.push(pointer);
curr_pointer = pointer;
} else {
break;
}
}
path.pop();
path.reverse();
path
}
fn viterbi_forward(
&self,
scores: &[f64],
transitions: &[f64],
) -> (Vec<DpCell>, Vec<PointerCell>) {
let first_cell = {
let mut first_cell = DpCell::new_full(0f64);
self.initialize_start_end_cell(&mut first_cell);
first_cell
};
let first_pointers = PointerCell::empty();
let (mut dp_matrix, pointers, last_cell) =
scores.iter().zip(transitions).enumerate().fold(
(Vec::new(), vec![first_pointers], first_cell),
|(mut cells, mut pointers, prev_cell), (i, (x, t))| {
let mut next_cell = DpCell::new_empty();
let mut pointer_cell = PointerCell::empty();
self.forward(
&prev_cell,
&mut next_cell,
&mut pointer_cell,
*t,
*x,
i,
);
cells.push(prev_cell);
pointers.push(pointer_cell);
(cells, pointers, next_cell)
},
);
dp_matrix.push(last_cell);
assert_eq!(dp_matrix.len(), pointers.len());
assert_eq!(dp_matrix.len(), scores.len() + 1);
(dp_matrix, pointers)
}
#[inline]
fn emission_probs(&self, p: f64, state: States) -> f64 {
let p = if p == 0f64 {
debug_once!("encountered 0 prob");
1e-5
} else {
p
};
assert!(p <= 1f64, "p {p} cannot be greater than 1");
let (factor, p) = match state {
States::Same => (self.same_state_factor, p.ln()),
States::Different => {
(self.diff_state_factor, (1f64 - p + 1e-5).ln())
}
};
let p = p - self.significance_factor;
factor * p
}
fn forward(
&self,
prev_cell: &DpCell,
current_cell: &mut DpCell,
pointers: &mut PointerCell,
p_diff2diff: f64,
score: f64,
_idx: usize,
) {
// todo make the naming convention here less terrible!
// emission probs
let e_diff = self.emission_probs(score, States::Different);
let e_same = self.emission_probs(score, States::Same);
// "dynamic" transition probs
assert!(p_diff2diff > 0f64, "p_diff2diff should not be zero");
assert!(
p_diff2diff < 1.0,
"p_diff2diff should be less than zero {p_diff2diff}"
);
let lnp_diff2diff = p_diff2diff.ln();
let lnp_diff_to_same = (1f64 - p_diff2diff).ln();
// previous state
let p_same = prev_cell.get_value(States::Same);
let p_diff = prev_cell.get_value(States::Different);
Self::check_emission_prob(e_diff, "e_d");
Self::check_emission_prob(e_same, "e_s");
Self::check_emission_prob(p_diff, "p_d");
Self::check_emission_prob(p_same, "p_s");
Self::check_emission_prob(p_diff2diff, "p_diff2diff");
Self::check_emission_prob(lnp_diff2diff, "lnp_diff2diff");
Self::check_emission_prob(lnp_diff_to_same, "lnp_diff_to_same");
// Same-state
let same2same = p_same + self.same_to_same;
let diff2same = p_diff + lnp_diff_to_same;
let (current_same, same_pointer) =
[(same2same, States::Same), (diff2same, States::Different)]
.into_iter()
.max_by(|(a, _), (b, _)| a.partial_cmp(b).unwrap())
.unwrap();
// Diff-state
let diff2diff = p_diff + lnp_diff2diff;
let same2diff = p_same + self.same_to_diff;
let (current_diff, diff_pointer) =
[(diff2diff, States::Different), (same2diff, States::Same)]
.into_iter()
.max_by(|(a, _), (b, _)| a.partial_cmp(b).unwrap())
.unwrap();
Self::check_emission_prob(current_diff, "current_diff");
Self::check_emission_prob(current_same, "current_same");
current_cell.set_value(States::Same, current_same + e_same);
current_cell.set_value(States::Different, current_diff + e_diff);
pointers.set_value(States::Same, same_pointer);
pointers.set_value(States::Different, diff_pointer);
}
// todo make this a compile time no-op
#[inline(always)]
fn check_emission_prob(x: f64, which: &str) {
assert!(x.is_finite(), "{which} is not finite {x}");
assert!(!x.is_nan(), "{which} is NaN {x}");
}
fn initialize_start_end_cell(&self, cell: &mut DpCell) {
*cell.get_value_mut(States::Same) = self.same_to_same;
*cell.get_value_mut(States::Different) = self.same_to_diff;
}
}
struct Projection {
prob_range: Range<f64>,
distance_range: Range<f64>,
prob_span: f64,
ratio: f64,
}
impl Projection {
fn new(
max_distance: u32,
max_diff_stay: f64,
dmr_prob: f64,
) -> anyhow::Result<Self> {
if max_diff_stay <= dmr_prob {
bail!("max_diff_stay must be > switch_prob")
}
let low = 1f64 - max_diff_stay;
let high = 1f64 - dmr_prob;
let prob_range = low..high;
let max_distance = max_distance as f64;
let distance_range = 2f64..max_distance;
let prob_span = prob_range.end - prob_range.start;
let ratio = prob_span / (distance_range.end - distance_range.start);
Ok(Self { prob_range, distance_range, prob_span, ratio })
}
#[inline]
fn clamp_value(&self, x: f64) -> f64 {
if x > self.distance_range.end {
self.distance_range.end
} else {
x
}
}
fn linear_project_prob(&self, x: f64) -> f64 {
let x = self.clamp_value(x);
let adjusted = ((x - self.distance_range.start) * self.ratio)
+ self.prob_range.start;
1f64 - adjusted
}
fn ln_project_prob(&self, x: f64) -> f64 {
if x == 1f64 {
return 1f64 - self.prob_range.start;
}
let x = self.clamp_value(x);
let ln_ratio =
self.distance_range.end.ln() - self.distance_range.start.ln();
let adjusted = ((x.ln() - self.distance_range.start.ln()) / ln_ratio)
* (self.prob_span)
+ self.prob_range.start;
let prob = 1f64 - adjusted;
if prob > 1.0 {
panic!(
"prob should not be >1 x: {x}, prob: {prob}, adjusted \
{adjusted}"
)
}
prob
}
}
#[cfg(test)]
mod hmm_tests {
use crate::hmm::HmmModel;
#[test]
fn test_prob_to_factor() {
let sig_fact = 0.01;
let fact = HmmModel::prob_to_factor(sig_fact).unwrap();
dbg!(fact);
}
}