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
//! Implement Platt calibration with Newton method
//!
//! Platt scaling is a way of transforming the outputs of a classification model into a probability
//! distribution over classes. It is, for example, used in the calibration of SVM models to predict
//! plausible probability values.
//!
//! # Example
//!
//! ```rust, ignore
//! let model = ...;
//!
//! let model = Platt::params()
//!      .fit_with(model, &train)?;
//!
//! let pred: Array1<Pr> = model.predict(&valid);
//! ```

use std::marker::PhantomData;

use crate::dataset::{DatasetBase, Pr};
use crate::traits::{FitWith, Predict, PredictInplace};
use crate::{Float, ParamGuard};

use ndarray::{Array1, Array2, ArrayBase, ArrayView1, Data, Ix1, Ix2};
#[cfg(feature = "serde")]
use serde_crate::{Deserialize, Serialize};
use thiserror::Error;

/// Fitted Platt model
///
/// This model contains a sigmoid scaling parameters and a second univariate, uncalibrated model.
/// The output of the uncalibrated model is scaled with the following function:
/// ```text
/// g(x) = 1 / (1 + exp(A * f(x) + B)
/// ```
///
/// The scaling factors `A` and `B` are estimated with the Newton's method, presented in the
/// following paper: <https://www.csie.ntu.edu.tw/~cjlin/papers/plattprob.pdf>
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Platt<F, O> {
    a: F,
    b: F,
    obj: O,
}

/// Parameters for Platt's Newton method
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlattValidParams<F, O> {
    maxiter: usize,
    minstep: F,
    sigma: F,
    phantom: PhantomData<O>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlattParams<F, O>(PlattValidParams<F, O>);

impl<F: Float, O> Default for PlattParams<F, O> {
    fn default() -> Self {
        Self(PlattValidParams {
            maxiter: 100,
            minstep: F::cast(1e-10),
            sigma: F::cast(1e-12),
            phantom: PhantomData,
        })
    }
}

impl<F: Float, O> PlattParams<F, O> {
    /// Set the maximum number of iterations in the optimization process
    ///
    /// The Newton's method is an iterative optimization process, which uses the first and second
    /// order gradients to find optimal `A` and `B`. This function caps the maximal number of
    /// iterations.
    pub fn maxiter(mut self, maxiter: usize) -> Self {
        self.0.maxiter = maxiter;

        self
    }

    /// Set the minimum stepsize in the line search
    ///
    /// After estimating the Hessian matrix, a line search is performed to find the optimal step
    /// size in each optimization step. In each attempt the stepsize is halfed until this threshold
    /// is reached. After reaching the threshold the algorithm fails because the desired precision
    /// could not be achieved.
    pub fn minstep(mut self, minstep: F) -> Self {
        self.0.minstep = minstep;

        self
    }

    /// Set the Hessian's sigma value
    ///
    /// The Hessian matrix is regularized with H' = H + sigma I to avoid numerical issues. This
    /// function set the amount of regularization.
    pub fn sigma(mut self, sigma: F) -> Self {
        self.0.sigma = sigma;

        self
    }
}

impl<F: Float, O> ParamGuard for PlattParams<F, O> {
    type Checked = PlattValidParams<F, O>;
    type Error = PlattError;

    fn check_ref(&self) -> Result<&Self::Checked, PlattError> {
        if self.0.maxiter == 0 {
            Err(PlattError::MaxIterReached)
        } else if self.0.minstep.is_negative() {
            Err(PlattError::MinStepNegative(
                self.0.minstep.to_f32().unwrap(),
            ))
        } else if self.0.sigma.is_negative() {
            Err(PlattError::SigmaNegative(self.0.sigma.to_f32().unwrap()))
        } else {
            Ok(&self.0)
        }
    }

    fn check(self) -> Result<Self::Checked, PlattError> {
        self.check_ref()?;
        Ok(self.0)
    }
}

#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate")
)]
#[derive(Error, Debug, Clone)]
/// Platt Newton's method errors
///
/// Errors occur when setting invalid parameters or the optimization process fails.
pub enum PlattError {
    #[error("line search did not converge")]
    LineSearchNotConverged,
    #[error("platt scaling did not converge")]
    MaxIterReached,
    #[error("maxiter should be larger than zero")]
    MaxIterZero,
    #[error("minstep should be positive, is {0}")]
    MinStepNegative(f32),
    #[error("sigma should be positive, is {0}")]
    SigmaNegative(f32),
    #[error(transparent)]
    LinfaError(#[from] crate::error::Error),
}

impl<F: Float, O> Platt<F, O> {
    /// Create default parameter set for the Platt scaling algorithm
    ///
    /// The default values are:
    /// * `maxiter`: 100,
    /// * `minstep`: 1e-10,
    /// * `sigma`: 1e-12
    ///
    pub fn params() -> PlattParams<F, O> {
        PlattParams::default()
    }
}

impl<'a, F: Float, O: 'a> FitWith<'a, Array2<F>, Array1<bool>, PlattError>
    for PlattValidParams<F, O>
where
    O: PredictInplace<Array2<F>, Array1<F>>,
{
    type ObjectIn = O;
    type ObjectOut = Platt<F, O>;

    /// Calibrate another model with Platt scaling
    ///
    /// This function takes another model and binary decision dataset and calibrates it to produce
    /// probability values. The returned model therefore implements the prediction trait for
    /// probability targets.
    fn fit_with(
        &self,
        obj: O,
        ds: &DatasetBase<Array2<F>, Array1<bool>>,
    ) -> Result<Self::ObjectOut, PlattError> {
        let predicted = obj.predict(ds);

        let (a, b) = platt_newton_method(predicted.view(), ds.targets().view(), self)?;

        Ok(Platt { a, b, obj })
    }
}

impl<F: Float, D, O> PredictInplace<ArrayBase<D, Ix2>, Array1<Pr>> for Platt<F, O>
where
    D: Data<Elem = F>,
    O: PredictInplace<ArrayBase<D, Ix2>, ArrayBase<D, Ix1>>,
{
    fn predict_inplace(&self, data: &ArrayBase<D, Ix2>, targets: &mut Array1<Pr>) {
        assert_eq!(
            data.nrows(),
            targets.len(),
            "The number of data points must match the number of output targets."
        );
        for (x, target) in self.obj.predict(data).iter().zip(targets.iter_mut()) {
            *target = platt_predict(*x, self.a, self.b);
        }
    }

    fn default_target(&self, x: &ArrayBase<D, Ix2>) -> Array1<Pr> {
        Array1::default(x.nrows())
    }
}

/// Predict a probability with the sigmoid function
///
/// Similar to stable sigmoid implementations
/// <https://stackoverflow.com/questions/51976461/optimal-way-of-defining-a-numerically-stable-sigmoid-function-for-a-list-in-pyth>
///
/// # Parameters
/// * `x`: uncalibrated value f(x)
/// * `a`: parameter A,
/// * `b`: parameter B,
pub fn platt_predict<F: Float>(x: F, a: F, b: F) -> Pr {
    let f_apb = a * x + b;
    let f_apb = f_apb.to_f32().unwrap();

    // avoid numerical problems for large f_apb
    if f_apb >= 0.0 {
        Pr::new((-f_apb).exp() / (1.0 + (-f_apb).exp()))
    } else {
        Pr::new(1.0 / (1.0 + f_apb.exp()))
    }
}

/// Run Newton's method to find optimal `A` and `B` values
///
/// The optimization process happens in two steps, first the closed-form Hessian matrix and
/// gradient vectors are calculated. Then a line-search tries to find the optimal learning rate
/// for each step.
//#[allow(clippy::suspicious_operation_groupings)]
pub fn platt_newton_method<'a, F: Float, O>(
    reg_values: ArrayView1<'a, F>,
    labels: ArrayView1<'a, bool>,
    params: &PlattValidParams<F, O>,
) -> Result<(F, F), PlattError> {
    let (num_pos, num_neg) = labels.iter().fold((0, 0), |mut val, x| {
        match x {
            true => val.0 += 1,
            false => val.1 += 1,
        }

        val
    });
    let (num_pos, num_neg) = (num_pos as f32, num_neg as f32);

    let (hi_target, lo_target) = ((num_pos + 1.0) / (num_pos + 2.0), 1.0 / (num_neg + 2.0));

    let target_values = labels
        .iter()
        .map(|x| if *x { hi_target } else { lo_target })
        .map(|x| F::from(x).unwrap())
        .collect::<Vec<_>>();

    let reg_values = reg_values
        .into_iter()
        .map(|x| F::from(*x).unwrap())
        .collect::<Vec<_>>();

    let mut a = F::zero();
    let mut b = F::from((num_neg + 1.0) / (num_pos + 1.0)).unwrap().ln();
    let mut fval = F::zero();

    for (v, t) in reg_values.iter().zip(target_values.iter()) {
        let f_apb = *v * a + b;
        if f_apb >= F::zero() {
            fval += *t * f_apb + (F::one() + (-f_apb).exp()).ln();
        } else {
            fval += (*t - F::one()) * f_apb + (F::one() + f_apb.exp()).ln();
        }
    }

    let mut idx = 0;
    for _ in 0..params.maxiter {
        let (mut h11, mut h22) = (params.sigma, params.sigma);
        let (mut h21, mut g1, mut g2) = (F::zero(), F::zero(), F::zero());

        for (v, t) in reg_values.iter().zip(target_values.iter()) {
            let f_apb = *v * a + b;

            let (p, q) = if f_apb >= F::zero() {
                (
                    (-f_apb).exp() / (F::one() + (-f_apb).exp()),
                    F::one() / (F::one() + (-f_apb).exp()),
                )
            } else {
                (
                    F::one() / (F::one() + f_apb.exp()),
                    f_apb.exp() / (F::one() + f_apb.exp()),
                )
            };

            let d2 = p * q;
            h11 += *v * *v * d2;
            h22 += d2;
            h21 += *v * d2;

            let d1 = *t - p;
            g1 += *v * d1;
            g2 += d1;
        }

        if g1.abs() < F::from(1e-5 * reg_values.len() as f32).unwrap()
            && g2.abs() < F::from(1e-5 * reg_values.len() as f32).unwrap()
        {
            break;
        }

        let det = h11 * h22 - h21.powi(2);
        let d_a = -(h22 * g1 - h21 * g2) / det;
        let d_b = -(-h21 * g1 + h11 * g2) / det;
        let gd = g1 * d_a + g2 * d_b;

        let mut stepsize = F::one();
        while stepsize >= params.minstep {
            let new_a = a + stepsize * d_a;
            let new_b = b + stepsize * d_b;
            let mut newf = F::zero();

            for (v, t) in reg_values.iter().zip(target_values.iter()) {
                let f_apb = *v * new_a + new_b;

                if f_apb >= F::zero() {
                    newf += *t * f_apb + (F::one() + (-f_apb).exp()).ln();
                } else {
                    newf += (*t - F::one()) * f_apb + (F::one() + f_apb.exp()).ln();
                }
            }

            if newf < fval + F::from(1e-4).unwrap() * stepsize * gd {
                a = new_a;
                b = new_b;
                fval = newf;
                break;
            } else {
                stepsize /= F::one() + F::one();
            }

            if stepsize < params.minstep {
                return Err(PlattError::LineSearchNotConverged);
            }
        }

        idx += 1;
    }

    if params.maxiter == idx {
        return Err(PlattError::MaxIterReached);
    }

    Ok((a, b))
}

#[cfg(test)]
mod tests {
    use approx::assert_abs_diff_eq;
    use ndarray::{Array1, Array2};
    use rand::{rngs::SmallRng, Rng, SeedableRng};

    use super::{platt_newton_method, Platt, PlattValidParams};
    use crate::{
        traits::{FitWith, Predict, PredictInplace},
        DatasetBase, Float, ParamGuard,
    };

    /// Generate dummy values which can be predicted with the Platt model
    fn generate_dummy_values<F: Float, R: Rng>(
        a: F,
        b: F,
        n: usize,
        rng: &mut R,
    ) -> (Array1<F>, Array1<bool>) {
        // generate probability values, omit p = 0.0, p = 1.0 to avoid infinity in reverse function
        let prob_values = Array1::linspace(
            F::one() / F::from(n).unwrap(),
            F::one() - F::one() / F::from(n).unwrap(),
            n - 2,
        );

        // generate regression values with inverse function
        let reg_values = prob_values
            .iter()
            .map(|x| (F::one() - *x) / *x)
            .map(|x| (x.ln() - b) / a)
            .collect();

        // roll decision according to probability
        let decisions = prob_values
            .iter()
            .map(|x| rng.gen_bool(x.to_f64().unwrap()))
            .collect();

        (reg_values, decisions)
    }

    macro_rules! test_newton_solver {
        ($($fnc:ident, $a_val:expr);*) => {
            $(
            #[test]
            fn $fnc() {
                let mut rng = SmallRng::seed_from_u64(42);

                let params: PlattValidParams<f32, ()> = PlattValidParams {
                    maxiter: 100,
                    minstep: 1e-10,
                    sigma: 1e-12,
                    phantom: std::marker::PhantomData,
                };

                let a = $a_val as f32;

                for b in &[a / 2.0, a * 2.0] {
                    let (reg_vals, dec_vals) = generate_dummy_values(a, *b, 10000, &mut rng);
                    let (a_est, b_est) = platt_newton_method(reg_vals.view(), dec_vals.view(), &params).unwrap();

                    assert_abs_diff_eq!(a_est, a, epsilon = 0.15);
                    assert_abs_diff_eq!(b_est, b, epsilon = 0.1);
                }
            }
            )*
        };
    }

    // Check solutions for
    // * a = 1.0, (b = 0.5, b = 2.0)
    // * a = 2.0, (b = 1.0, b = 4.0)
    // * a = 5.0, (b = 2.5, b = 10.0)
    test_newton_solver!(
        newton_solver_1, 1;
        newton_solver_2, 2;
        newton_solver_5, 5
    );

    struct DummyModel {
        reg_vals: Array1<f32>,
    }

    impl PredictInplace<Array2<f32>, Array1<f32>> for DummyModel {
        fn predict_inplace(&self, x: &Array2<f32>, y: &mut Array1<f32>) {
            assert_eq!(
                x.nrows(),
                y.len(),
                "The number of data points must match the number of output targets."
            );
            *y = self.reg_vals.clone();
        }

        fn default_target(&self, x: &Array2<f32>) -> Array1<f32> {
            Array1::zeros(x.nrows())
        }
    }

    #[test]
    /// Check that the predicted probabilities are non-decreasing monotonical
    fn ordered_probabilities() {
        let mut rng = SmallRng::seed_from_u64(42);

        let (reg_vals, dec_vals) = generate_dummy_values(1.0, 0.5, 102, &mut rng);
        let records = Array2::zeros((100, 3));
        let dataset = DatasetBase::new(records, dec_vals);

        let model = DummyModel { reg_vals };

        let platt = Platt::params().fit_with(model, &dataset).unwrap();

        let pred_probabilities = platt.predict(&dataset).to_vec();

        for vals in pred_probabilities.windows(2) {
            if vals[0] > vals[1] {
                panic!("Probabilities are not monotonically increasing!");
            }
        }
    }

    #[test]
    #[should_panic]
    fn panic_maxiter_zero() {
        Platt::<f32, ()>::params().maxiter(0).check().unwrap();
    }

    #[test]
    #[should_panic]
    fn panic_minstep_negative() {
        Platt::<f32, ()>::params().minstep(-5.0).check().unwrap();
    }

    #[test]
    #[should_panic]
    fn panic_sigma_negative() {
        Platt::<f32, ()>::params().sigma(-1.0).check().unwrap();
    }
}