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
use super::{
    IntervalCensored, LeftCensored, LeftTruncation, LogLikelihood, PartiallyObserved,
    RightCensored, Uncensored, Weighted,
};
use crate::distribution::{CumulativeHazard, LogCumulativeDensity, LogHazard, Survival};
use ndarray::prelude::*;
use ndarray::{Data, OwnedRepr, RawData, ScalarOperand};
use num_traits::{clamp, Float, FromPrimitive};
use std::ops::{Add, Neg, Sub};

/// A convenience method to convert a list of events and their observed status
/// into a set of partially observed events.
pub trait FromEvents<'a, F: 'a> {
    fn from_events(
        events: impl IntoIterator<Item = &'a F>,
        event_observed: impl IntoIterator<Item = &'a bool>,
    ) -> Self;
}

impl<'a, F, C> FromEvents<'a, F> for PartiallyObserved<OwnedRepr<F>, Ix1, C>
where
    F: 'a + Copy,
    C: From<Vec<F>>,
{
    fn from_events(
        events: impl IntoIterator<Item = &'a F>,
        event_observed: impl IntoIterator<Item = &'a bool>,
    ) -> Self {
        let mut observed_events = Vec::new();
        let mut censored_events = Vec::new();

        for (&event, &o) in events.into_iter().zip(event_observed) {
            if o {
                observed_events.push(event)
            } else {
                censored_events.push(event)
            }
        }

        PartiallyObserved {
            observed: Uncensored(Array::from(observed_events)),
            censored: C::from(censored_events),
        }
    }
}

impl<'a, F, C> FromEvents<'a, (F, F)> for PartiallyObserved<OwnedRepr<F>, Ix1, C>
where
    F: 'a + Copy,
    C: From<(Vec<F>, Vec<F>)>,
{
    fn from_events(
        events: impl IntoIterator<Item = &'a (F, F)>,
        event_observed: impl IntoIterator<Item = &'a bool>,
    ) -> Self {
        let mut observed_events = Vec::new();
        let mut censored_starts = Vec::new();
        let mut censored_stops = Vec::new();

        for (&event, &o) in events.into_iter().zip(event_observed) {
            if o {
                let (_, time) = event;
                observed_events.push(time)
            } else {
                let (start, stop) = event;
                censored_starts.push(start);
                censored_stops.push(stop);
            }
        }

        PartiallyObserved {
            observed: Uncensored(Array::from(observed_events)),
            censored: C::from((censored_starts, censored_stops)),
        }
    }
}

impl<T, F> LeftTruncation<T, Ix1>
where
    T: Data<Elem = F>,
    F: Float,
{
    pub fn new(entry_time: ArrayBase<T, Ix1>) -> Result<Self, ()> {
        let zero = F::zero();
        if entry_time.iter().find(|&&t| t <= zero).is_some() {
            Err(())
        } else {
            Ok(LeftTruncation(entry_time))
        }
    }
}

impl<F> From<Vec<F>> for RightCensored<OwnedRepr<F>, Ix1> {
    fn from(vec: Vec<F>) -> Self {
        RightCensored(Array::from(vec))
    }
}

impl<F> From<Vec<F>> for LeftCensored<OwnedRepr<F>, Ix1> {
    fn from(vec: Vec<F>) -> Self {
        LeftCensored(Array::from(vec))
    }
}

impl<F> From<(Vec<F>, Vec<F>)> for IntervalCensored<OwnedRepr<F>, Ix1> {
    fn from((start, stop): (Vec<F>, Vec<F>)) -> Self {
        IntervalCensored {
            start: Array::from(start),
            stop: Array::from(stop),
        }
    }
}

impl<D, F, T, W> LogLikelihood<D, F> for Weighted<T, W, Ix1>
where
    T: LogLikelihood<D, Array1<F>>,
    F: Float + ScalarOperand,
    W: Data<Elem = F>,
{
    fn log_likelihood(&self, distribution: &D) -> F {
        let Weighted { time, weight } = self;

        let log_likelihood = time.log_likelihood(distribution);
        (weight * &log_likelihood).sum() / weight.sum()
    }
}

impl<D, O, T, C> LogLikelihood<D, O> for PartiallyObserved<T, Ix1, C>
where
    D: LogHazard<ArrayBase<T, Ix1>, O> + CumulativeHazard<ArrayBase<T, Ix1>, O>,
    O: Sub<Output = O> + Add<Output = O>,
    T: RawData,
    C: LogLikelihood<D, O>,
{
    fn log_likelihood(&self, distribution: &D) -> O {
        let PartiallyObserved { observed, censored } = self;
        observed.log_likelihood(distribution) + censored.log_likelihood(distribution)
    }
}

impl<D, O, T> LogLikelihood<D, O> for Uncensored<T, Ix1>
where
    D: LogHazard<ArrayBase<T, Ix1>, O> + CumulativeHazard<ArrayBase<T, Ix1>, O>,
    O: Sub<Output = O>,
    T: RawData,
{
    fn log_likelihood(&self, distribution: &D) -> O {
        let Uncensored(time) = self;
        distribution.log_hazard(time) - distribution.cumulative_hazard(time)
    }
}

impl<D, O, T> LogLikelihood<D, O> for RightCensored<T, Ix1>
where
    D: LogHazard<ArrayBase<T, Ix1>, O> + CumulativeHazard<ArrayBase<T, Ix1>, O>,
    O: Neg<Output = O>,
    T: RawData,
{
    fn log_likelihood(&self, distribution: &D) -> O {
        let RightCensored(time) = self;
        -distribution.cumulative_hazard(&time)
    }
}

impl<D, O, T> LogLikelihood<D, O> for LeftCensored<T, Ix1>
where
    D: LogCumulativeDensity<ArrayBase<T, Ix1>, O>,
    T: RawData,
{
    fn log_likelihood(&self, distribution: &D) -> O {
        let LeftCensored(time) = self;
        distribution.log_cumulative_density(&time)
    }
}

/// A trait used to allow both log likelihood to return both scalar and vector
/// types for IntervalCensored data.
pub trait UpstreamTraitHack {}

impl UpstreamTraitHack for f32 {}
impl UpstreamTraitHack for f64 {}

impl<D, F, T> LogLikelihood<D, F> for IntervalCensored<T, Ix1>
where
    D: Survival<ArrayBase<T, Ix1>, Array1<F>>,
    F: Float + FromPrimitive + UpstreamTraitHack,
    T: Data<Elem = F>,
{
    fn log_likelihood(&self, distribution: &D) -> F {
        let array: Array1<F> = self.log_likelihood(distribution);
        array.sum()
    }
}

impl<D, F, T> LogLikelihood<D, Array1<F>> for IntervalCensored<T, Ix1>
where
    D: Survival<ArrayBase<T, Ix1>, Array1<F>>,
    F: Float + FromPrimitive,
    T: Data<Elem = F>,
{
    fn log_likelihood(&self, distribution: &D) -> Array1<F> {
        let IntervalCensored { start, stop } = self;

        let min = F::from_f64(-1e50).unwrap();
        let max = F::from_f64(1e50).unwrap();

        let survival = (distribution.survival(&start) - distribution.survival(&stop))
            .mapv_into(F::ln)
            .mapv_into(|x| clamp(x, min, max));

        survival
    }
}

impl<D, F, T> LogLikelihood<D, Array1<F>> for LeftTruncation<T, Ix1>
where
    D: CumulativeHazard<ArrayBase<T, Ix1>, Array1<F>>,
    F: Float + FromPrimitive,
    T: Data<Elem = F>,
{
    fn log_likelihood(&self, distribution: &D) -> Array1<F> {
        let LeftTruncation(entry_time) = self;
        distribution.cumulative_hazard(entry_time)
    }
}