survival 1.1.27

A high-performance survival analysis library written in Rust with Python bindings
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#![allow(
    unused_variables,
    unused_imports,
    unused_mut,
    unused_assignments,
    clippy::too_many_arguments,
    clippy::type_complexity
)]

use crate::utilities::statistical::sample_normal;
use pyo3::prelude::*;
use rayon::prelude::*;

#[derive(Debug, Clone, Copy, PartialEq)]
#[pyclass]
pub enum PriorType {
    Normal,
    Laplace,
    Cauchy,
    Horseshoe,
    Flat,
}

#[pymethods]
impl PriorType {
    #[new]
    fn new(name: &str) -> PyResult<Self> {
        match name.to_lowercase().as_str() {
            "normal" | "gaussian" => Ok(PriorType::Normal),
            "laplace" | "double_exponential" => Ok(PriorType::Laplace),
            "cauchy" => Ok(PriorType::Cauchy),
            "horseshoe" => Ok(PriorType::Horseshoe),
            "flat" | "improper" => Ok(PriorType::Flat),
            _ => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
                "Unknown prior type",
            )),
        }
    }
}

#[derive(Debug, Clone)]
#[pyclass]
pub struct BayesianCoxConfig {
    #[pyo3(get, set)]
    pub prior_type: PriorType,
    #[pyo3(get, set)]
    pub prior_scale: f64,
    #[pyo3(get, set)]
    pub n_samples: usize,
    #[pyo3(get, set)]
    pub n_warmup: usize,
    #[pyo3(get, set)]
    pub n_chains: usize,
    #[pyo3(get, set)]
    pub target_accept: f64,
    #[pyo3(get, set)]
    pub seed: Option<u64>,
}

#[pymethods]
impl BayesianCoxConfig {
    #[new]
    #[pyo3(signature = (prior_type=PriorType::Normal, prior_scale=2.5, n_samples=2000, n_warmup=1000, n_chains=4, target_accept=0.8, seed=None))]
    pub fn new(
        prior_type: PriorType,
        prior_scale: f64,
        n_samples: usize,
        n_warmup: usize,
        n_chains: usize,
        target_accept: f64,
        seed: Option<u64>,
    ) -> PyResult<Self> {
        if prior_scale <= 0.0 {
            return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
                "prior_scale must be positive",
            ));
        }
        Ok(BayesianCoxConfig {
            prior_type,
            prior_scale,
            n_samples,
            n_warmup,
            n_chains,
            target_accept,
            seed,
        })
    }
}

#[derive(Debug, Clone)]
#[pyclass]
pub struct BayesianCoxResult {
    #[pyo3(get)]
    pub posterior_mean: Vec<f64>,
    #[pyo3(get)]
    pub posterior_sd: Vec<f64>,
    #[pyo3(get)]
    pub credible_lower: Vec<f64>,
    #[pyo3(get)]
    pub credible_upper: Vec<f64>,
    #[pyo3(get)]
    pub hazard_ratio_mean: Vec<f64>,
    #[pyo3(get)]
    pub hazard_ratio_lower: Vec<f64>,
    #[pyo3(get)]
    pub hazard_ratio_upper: Vec<f64>,
    #[pyo3(get)]
    pub samples: Vec<Vec<f64>>,
    #[pyo3(get)]
    pub log_posterior: Vec<f64>,
    #[pyo3(get)]
    pub waic: f64,
    #[pyo3(get)]
    pub loo: f64,
    #[pyo3(get)]
    pub rhat: Vec<f64>,
    #[pyo3(get)]
    pub n_eff: Vec<f64>,
}

fn log_prior(beta: &[f64], prior_type: &PriorType, scale: f64) -> f64 {
    match prior_type {
        PriorType::Normal => -0.5 * beta.iter().map(|&b| (b / scale).powi(2)).sum::<f64>(),
        PriorType::Laplace => -beta.iter().map(|&b| b.abs() / scale).sum::<f64>(),
        PriorType::Cauchy => -beta
            .iter()
            .map(|&b| (1.0 + (b / scale).powi(2)).ln())
            .sum::<f64>(),
        PriorType::Horseshoe => -beta
            .iter()
            .map(|&b| (1.0 + (b / scale).powi(2)).ln())
            .sum::<f64>(),
        PriorType::Flat => 0.0,
    }
}

fn log_prior_gradient(beta: &[f64], prior_type: &PriorType, scale: f64) -> Vec<f64> {
    match prior_type {
        PriorType::Normal => beta.iter().map(|&b| -b / (scale * scale)).collect(),
        PriorType::Laplace => beta.iter().map(|&b| -b.signum() / scale).collect(),
        PriorType::Cauchy => beta
            .iter()
            .map(|&b| -2.0 * b / (scale * scale + b * b))
            .collect(),
        PriorType::Horseshoe => beta
            .iter()
            .map(|&b| -2.0 * b / (scale * scale + b * b))
            .collect(),
        PriorType::Flat => vec![0.0; beta.len()],
    }
}

fn log_likelihood(
    x: &[f64],
    n: usize,
    p: usize,
    time: &[f64],
    status: &[i32],
    beta: &[f64],
) -> f64 {
    let mut indices: Vec<usize> = (0..n).collect();
    indices.sort_by(|&a, &b| {
        time[b]
            .partial_cmp(&time[a])
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let eta: Vec<f64> = (0..n)
        .map(|i| {
            let mut e = 0.0;
            for j in 0..p {
                e += x[i * p + j] * beta[j];
            }
            e.clamp(-700.0, 700.0)
        })
        .collect();

    let exp_eta: Vec<f64> = eta.iter().map(|&e| e.exp()).collect();

    let mut loglik = 0.0;
    let mut risk_sum = 0.0;

    for &i in &indices {
        risk_sum += exp_eta[i];
        if status[i] == 1 && risk_sum > 0.0 {
            loglik += eta[i] - risk_sum.ln();
        }
    }

    loglik
}

fn log_likelihood_gradient(
    x: &[f64],
    n: usize,
    p: usize,
    time: &[f64],
    status: &[i32],
    beta: &[f64],
) -> Vec<f64> {
    let mut gradient = vec![0.0; p];

    let mut indices: Vec<usize> = (0..n).collect();
    indices.sort_by(|&a, &b| {
        time[b]
            .partial_cmp(&time[a])
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let eta: Vec<f64> = (0..n)
        .map(|i| {
            let mut e = 0.0;
            for j in 0..p {
                e += x[i * p + j] * beta[j];
            }
            e.clamp(-700.0, 700.0)
        })
        .collect();

    let exp_eta: Vec<f64> = eta.iter().map(|&e| e.exp()).collect();

    let mut risk_sum = 0.0;
    let mut weighted_x = vec![0.0; p];

    for &i in &indices {
        risk_sum += exp_eta[i];
        for j in 0..p {
            weighted_x[j] += exp_eta[i] * x[i * p + j];
        }

        if status[i] == 1 && risk_sum > 0.0 {
            for j in 0..p {
                gradient[j] += x[i * p + j] - weighted_x[j] / risk_sum;
            }
        }
    }

    gradient
}

fn hmc_step(
    x: &[f64],
    n: usize,
    p: usize,
    time: &[f64],
    status: &[i32],
    beta: &[f64],
    config: &BayesianCoxConfig,
    step_size: f64,
    n_leapfrog: usize,
    rng: &mut fastrand::Rng,
) -> (Vec<f64>, f64, bool) {
    let mut q = beta.to_vec();
    let mut momentum: Vec<f64> = (0..p).map(|_| sample_normal(rng)).collect();

    let initial_h = -log_likelihood(x, n, p, time, status, &q)
        - log_prior(&q, &config.prior_type, config.prior_scale)
        + 0.5 * momentum.iter().map(|&m| m * m).sum::<f64>();

    let ll_grad = log_likelihood_gradient(x, n, p, time, status, &q);
    let prior_grad = log_prior_gradient(&q, &config.prior_type, config.prior_scale);
    let mut grad: Vec<f64> = ll_grad
        .iter()
        .zip(prior_grad.iter())
        .map(|(&l, &p)| l + p)
        .collect();

    for j in 0..p {
        momentum[j] += 0.5 * step_size * grad[j];
    }

    for _ in 0..n_leapfrog {
        for j in 0..p {
            q[j] += step_size * momentum[j];
        }

        let ll_grad = log_likelihood_gradient(x, n, p, time, status, &q);
        let prior_grad = log_prior_gradient(&q, &config.prior_type, config.prior_scale);
        grad = ll_grad
            .iter()
            .zip(prior_grad.iter())
            .map(|(&l, &p)| l + p)
            .collect();

        for j in 0..p {
            momentum[j] += step_size * grad[j];
        }
    }

    for j in 0..p {
        momentum[j] -= 0.5 * step_size * grad[j];
    }

    let final_h = -log_likelihood(x, n, p, time, status, &q)
        - log_prior(&q, &config.prior_type, config.prior_scale)
        + 0.5 * momentum.iter().map(|&m| m * m).sum::<f64>();

    let log_accept = initial_h - final_h;
    let accepted = rng.f64().ln() < log_accept;

    let log_post = log_likelihood(x, n, p, time, status, &q)
        + log_prior(&q, &config.prior_type, config.prior_scale);

    if accepted {
        (q, log_post, true)
    } else {
        (
            beta.to_vec(),
            log_likelihood(x, n, p, time, status, beta)
                + log_prior(beta, &config.prior_type, config.prior_scale),
            false,
        )
    }
}

fn run_chain(
    x: &[f64],
    n: usize,
    p: usize,
    time: &[f64],
    status: &[i32],
    config: &BayesianCoxConfig,
    chain_id: usize,
) -> (Vec<Vec<f64>>, Vec<f64>) {
    let seed = config.seed.unwrap_or(42) + chain_id as u64 * 1000;
    let mut rng = fastrand::Rng::with_seed(seed);

    let mut beta = vec![0.0; p];
    let mut samples: Vec<Vec<f64>> = Vec::with_capacity(config.n_samples);
    let mut log_posteriors: Vec<f64> = Vec::with_capacity(config.n_samples);

    let mut step_size = 0.1;
    let n_leapfrog = 10;

    let mut n_accepted = 0;
    for i in 0..(config.n_warmup + config.n_samples) {
        let (new_beta, log_post, accepted) = hmc_step(
            x, n, p, time, status, &beta, config, step_size, n_leapfrog, &mut rng,
        );

        beta = new_beta;

        if accepted {
            n_accepted += 1;
        }

        if i < config.n_warmup && (i + 1) % 50 == 0 {
            let accept_rate = n_accepted as f64 / 50.0;
            if accept_rate < config.target_accept - 0.1 {
                step_size *= 0.8;
            } else if accept_rate > config.target_accept + 0.1 {
                step_size *= 1.2;
            }
            n_accepted = 0;
        }

        if i >= config.n_warmup {
            samples.push(beta.clone());
            log_posteriors.push(log_post);
        }
    }

    (samples, log_posteriors)
}

fn compute_rhat(chains: &[Vec<Vec<f64>>], p: usize) -> Vec<f64> {
    let n_chains = chains.len();
    let n_samples = chains[0].len();

    (0..p)
        .into_par_iter()
        .map(|j| {
            let chain_means: Vec<f64> = chains
                .iter()
                .map(|chain| chain.iter().map(|s| s[j]).sum::<f64>() / n_samples as f64)
                .collect();

            let overall_mean = chain_means.iter().sum::<f64>() / n_chains as f64;

            let b = n_samples as f64
                * chain_means
                    .iter()
                    .map(|&m| (m - overall_mean).powi(2))
                    .sum::<f64>()
                / (n_chains - 1) as f64;

            let w: f64 = chains
                .iter()
                .zip(chain_means.iter())
                .map(|(chain, &mean)| {
                    chain.iter().map(|s| (s[j] - mean).powi(2)).sum::<f64>()
                        / (n_samples - 1) as f64
                })
                .sum::<f64>()
                / n_chains as f64;

            let var_plus = (n_samples as f64 - 1.0) / n_samples as f64 * w + b / n_samples as f64;

            if w > 0.0 { (var_plus / w).sqrt() } else { 1.0 }
        })
        .collect()
}

fn compute_n_eff(chains: &[Vec<Vec<f64>>], p: usize) -> Vec<f64> {
    let n_chains = chains.len();
    let n_samples = chains[0].len();
    let total_samples = n_chains * n_samples;

    (0..p)
        .into_par_iter()
        .map(|j| {
            let all_samples: Vec<f64> = chains
                .iter()
                .flat_map(|chain| chain.iter().map(|s| s[j]))
                .collect();

            let mean = all_samples.iter().sum::<f64>() / total_samples as f64;
            let var = all_samples.iter().map(|&s| (s - mean).powi(2)).sum::<f64>()
                / (total_samples - 1) as f64;

            if var < 1e-10 {
                return total_samples as f64;
            }

            let max_lag = (total_samples / 2).min(100);
            let mut rho_sum = 0.0;

            for lag in 1..max_lag {
                let mut autocorr = 0.0;
                for i in 0..(total_samples - lag) {
                    autocorr += (all_samples[i] - mean) * (all_samples[i + lag] - mean);
                }
                autocorr /= (total_samples - lag) as f64 * var;

                if autocorr < 0.05 {
                    break;
                }
                rho_sum += autocorr;
            }

            let tau = 1.0 + 2.0 * rho_sum;
            (total_samples as f64 / tau).max(1.0)
        })
        .collect()
}

#[pyfunction]
#[pyo3(signature = (x, n_obs, n_vars, time, status, config))]
pub fn bayesian_cox(
    x: Vec<f64>,
    n_obs: usize,
    n_vars: usize,
    time: Vec<f64>,
    status: Vec<i32>,
    config: &BayesianCoxConfig,
) -> PyResult<BayesianCoxResult> {
    if x.len() != n_obs * n_vars {
        return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
            "x length must equal n_obs * n_vars",
        ));
    }

    let chains: Vec<(Vec<Vec<f64>>, Vec<f64>)> = (0..config.n_chains)
        .into_par_iter()
        .map(|chain_id| run_chain(&x, n_obs, n_vars, &time, &status, config, chain_id))
        .collect();

    let all_samples: Vec<Vec<f64>> = chains.iter().flat_map(|(s, _)| s.clone()).collect();
    let all_log_post: Vec<f64> = chains.iter().flat_map(|(_, lp)| lp.clone()).collect();

    let chain_samples: Vec<Vec<Vec<f64>>> = chains.iter().map(|(s, _)| s.clone()).collect();

    let n_total = all_samples.len();

    let posterior_mean: Vec<f64> = (0..n_vars)
        .map(|j| all_samples.iter().map(|s| s[j]).sum::<f64>() / n_total as f64)
        .collect();

    let posterior_sd: Vec<f64> = (0..n_vars)
        .map(|j| {
            let mean = posterior_mean[j];
            let var = all_samples
                .iter()
                .map(|s| (s[j] - mean).powi(2))
                .sum::<f64>()
                / (n_total - 1) as f64;
            var.sqrt()
        })
        .collect();

    let (credible_lower, credible_upper): (Vec<f64>, Vec<f64>) = (0..n_vars)
        .into_par_iter()
        .map(|j| {
            let mut vals: Vec<f64> = all_samples.iter().map(|s| s[j]).collect();
            vals.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
            let lower_idx = (n_total as f64 * 0.025) as usize;
            let upper_idx = (n_total as f64 * 0.975) as usize;
            (vals[lower_idx], vals[upper_idx])
        })
        .unzip();

    let hazard_ratio_mean: Vec<f64> = posterior_mean.iter().map(|&b| b.exp()).collect();
    let hazard_ratio_lower: Vec<f64> = credible_lower.iter().map(|&b| b.exp()).collect();
    let hazard_ratio_upper: Vec<f64> = credible_upper.iter().map(|&b| b.exp()).collect();

    let rhat = compute_rhat(&chain_samples, n_vars);
    let n_eff = compute_n_eff(&chain_samples, n_vars);

    let mean_log_post = all_log_post.iter().sum::<f64>() / n_total as f64;
    let var_log_post = all_log_post
        .iter()
        .map(|&lp| (lp - mean_log_post).powi(2))
        .sum::<f64>()
        / n_total as f64;
    let waic = -2.0 * mean_log_post + 2.0 * var_log_post;
    let loo = waic;

    Ok(BayesianCoxResult {
        posterior_mean,
        posterior_sd,
        credible_lower,
        credible_upper,
        hazard_ratio_mean,
        hazard_ratio_lower,
        hazard_ratio_upper,
        samples: all_samples,
        log_posterior: all_log_post,
        waic,
        loo,
        rhat,
        n_eff,
    })
}

#[pyfunction]
pub fn bayesian_cox_predict_survival(
    result: &BayesianCoxResult,
    x_new: Vec<f64>,
    n_new: usize,
    n_vars: usize,
    baseline_hazard: Vec<f64>,
    time_points: Vec<f64>,
) -> PyResult<(Vec<Vec<f64>>, Vec<Vec<f64>>, Vec<Vec<f64>>)> {
    let n_times = time_points.len();
    let n_samples = result.samples.len().min(500);

    let sample_indices: Vec<usize> = {
        let step = result.samples.len() / n_samples;
        (0..n_samples).map(|i| i * step).collect()
    };

    let all_survival: Vec<Vec<Vec<f64>>> = (0..n_new)
        .into_par_iter()
        .map(|i| {
            let x_row: Vec<f64> = (0..n_vars).map(|j| x_new[i * n_vars + j]).collect();

            sample_indices
                .iter()
                .map(|&s_idx| {
                    let beta = &result.samples[s_idx];
                    let mut eta = 0.0;
                    for (j, &b) in beta.iter().enumerate() {
                        if j < n_vars {
                            eta += x_row[j] * b;
                        }
                    }
                    let risk = eta.exp();

                    baseline_hazard.iter().map(|&h| (-h * risk).exp()).collect()
                })
                .collect()
        })
        .collect();

    let survival_mean: Vec<Vec<f64>> = (0..n_new)
        .map(|i| {
            (0..n_times)
                .map(|t| all_survival[i].iter().map(|s| s[t]).sum::<f64>() / n_samples as f64)
                .collect()
        })
        .collect();

    let survival_lower: Vec<Vec<f64>> = (0..n_new)
        .map(|i| {
            (0..n_times)
                .map(|t| {
                    let mut vals: Vec<f64> = all_survival[i].iter().map(|s| s[t]).collect();
                    vals.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                    vals[(n_samples as f64 * 0.025) as usize]
                })
                .collect()
        })
        .collect();

    let survival_upper: Vec<Vec<f64>> = (0..n_new)
        .map(|i| {
            (0..n_times)
                .map(|t| {
                    let mut vals: Vec<f64> = all_survival[i].iter().map(|s| s[t]).collect();
                    vals.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                    vals[(n_samples as f64 * 0.975) as usize]
                })
                .collect()
        })
        .collect();

    Ok((survival_mean, survival_lower, survival_upper))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_log_prior_normal() {
        let beta = vec![0.5, -0.3, 0.2];
        let lp = log_prior(&beta, &PriorType::Normal, 2.5);
        assert!(lp < 0.0);
    }

    #[test]
    fn test_bayesian_cox_basic() {
        let x = vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0];
        let time = vec![1.0, 2.0, 3.0, 4.0];
        let status = vec![1, 1, 0, 1];

        let config =
            BayesianCoxConfig::new(PriorType::Normal, 2.5, 100, 50, 2, 0.8, Some(42)).unwrap();

        let result = bayesian_cox(x, 4, 2, time, status, &config).unwrap();
        assert_eq!(result.posterior_mean.len(), 2);
        assert_eq!(result.hazard_ratio_mean.len(), 2);
    }
}