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
//! Module defining Conformal Predictors.
//!
//! A `ConfidencePredictor<T>` implements all methods to provide a confidence
//! prediction for new input vectors.
//! Examples of confidence predictors are inductive and transductive
//! Conformal Predictors.
pub mod cp;

use ndarray::prelude::*;
use rusty_machine::learning::LearningResult;

pub use self::cp::CP;


/// A Confidence Predictor (either transductive or inductive)
///
/// This trait is parametrized over `T`, the element type.
/// It provides all the methods for making a confidence prediction.
pub trait ConfidencePredictor<T> {
    /// Trains a Conformal Predictor on a training set.
    ///
    /// Pedantic note: because CP is a transductive method, it never
    /// actually trains a model.
    /// This function, however, structures the training data so that
    /// it can be easily used in the prediction phase.
    ///
    /// # Arguments
    ///
    /// * `inputs` - Matrix (Array2<T>) with values of type T of training
    ///              vectors.
    /// * `targets` - Vector (Array1<T>) of labels corresponding to the
    ///               training vectors.
    ///
    /// # Examples
    ///
    /// Please, see [CP](/cp/cp/struct.CP.html).
    fn train(&mut self, inputs: &ArrayView2<T>, targets: &ArrayView1<usize>) -> LearningResult<()>;
    /// Returns candidate labels (region prediction) for test vectors.
    ///
    /// The return value is a matrix of `bool` (`Array2<bool>`) with shape
    /// `(n_inputs, n_labels)`, where `n_inputs = inputs.rows()` and
    /// `n_labels` is the number of possible labels;
    /// in such matrix, each column `y` corresponds to a label,
    /// each row `i` to an input object, and the value at `[i,y]` is
    /// true if the label conforms the distribution, false otherwise.
    ///
    /// # Examples
    ///
    /// Please, see [CP](/cp/cp/struct.CP.html).
    fn predict(&mut self, inputs: &ArrayView2<T>) -> LearningResult<Array2<bool>>;
    /// Returns the p-values for test vectors.
    ///
    /// The return value is a matrix of `f64` (`Array2<f64>`) with shape
    /// `(n_inputs, n_labels)`, where `n_inputs = inputs.rows()` and
    /// `n_labels` is the number of possible labels;
    /// in such matrix, each column `y` corresponds to a label,
    /// each row `i` to an input object, and the value at `[i,y]` is
    /// the p-value obtained when assuming `y` as a label for the
    /// `i`-th input object.
    ///
    /// # Examples
    ///
    /// Please, see [CP](/cp/cp/struct.CP.html).
    fn predict_confidence(&mut self, inputs: &ArrayView2<T>) -> LearningResult<Array2<f64>>;
    /// Sets the significance level.
    ///
    /// # Arguments
    ///
    /// * `epsilon` - Significance level in [0,1].
    fn set_epsilon(&mut self, epsilon: f64);
}