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
use crate::{Algorithm, DeflationMode, Mode, PlsError};
use linfa::{Float, ParamGuard};

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PlsValidParams<F: Float> {
    n_components: usize,
    max_iter: usize,
    tolerance: F,
    scale: bool,
    algorithm: Algorithm,
    deflation_mode: DeflationMode,
    mode: Mode,
}

impl<F: Float> PlsValidParams<F> {
    pub fn n_components(&self) -> usize {
        self.n_components
    }

    pub fn max_iter(&self) -> usize {
        self.max_iter
    }

    pub fn tolerance(&self) -> F {
        self.tolerance
    }

    pub fn scale(&self) -> bool {
        self.scale
    }

    pub fn algorithm(&self) -> Algorithm {
        self.algorithm
    }

    pub fn deflation_mode(&self) -> DeflationMode {
        self.deflation_mode
    }

    pub fn mode(&self) -> Mode {
        self.mode
    }
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PlsParams<F: Float>(pub(crate) PlsValidParams<F>);

impl<F: Float> PlsParams<F> {
    pub fn new(n_components: usize) -> PlsParams<F> {
        Self(PlsValidParams {
            n_components,
            max_iter: 500,
            tolerance: F::cast(1e-6),
            scale: true,
            algorithm: Algorithm::Nipals,
            deflation_mode: DeflationMode::Regression,
            mode: Mode::A,
        })
    }

    #[cfg(test)]
    pub fn max_iterations(mut self, max_iter: usize) -> Self {
        self.0.max_iter = max_iter;
        self
    }

    #[cfg(test)]
    pub fn tolerance(mut self, tolerance: F) -> Self {
        self.0.tolerance = tolerance;
        self
    }

    #[cfg(test)]
    pub fn scale(mut self, scale: bool) -> Self {
        self.0.scale = scale;
        self
    }

    #[cfg(test)]
    pub fn algorithm(mut self, algorithm: Algorithm) -> Self {
        self.0.algorithm = algorithm;
        self
    }

    pub fn deflation_mode(mut self, deflation_mode: DeflationMode) -> Self {
        self.0.deflation_mode = deflation_mode;
        self
    }

    pub fn mode(mut self, mode: Mode) -> Self {
        self.0.mode = mode;
        self
    }
}

impl<F: Float> ParamGuard for PlsParams<F> {
    type Checked = PlsValidParams<F>;
    type Error = PlsError;

    fn check_ref(&self) -> Result<&Self::Checked, Self::Error> {
        if self.0.tolerance.is_negative()
            || self.0.tolerance.is_nan()
            || self.0.tolerance.is_infinite()
        {
            Err(PlsError::InvalidTolerance(
                self.0.tolerance().to_f32().unwrap(),
            ))
        } else if self.0.max_iter == 0 {
            Err(PlsError::ZeroMaxIter)
        } else {
            Ok(&self.0)
        }
    }

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

macro_rules! pls_algo { ($name:ident) => {
    paste::item! {
        pub struct [<Pls $name Params>]<F: Float>(pub(crate) [<Pls $name ValidParams>]<F>);
        pub struct [<Pls $name ValidParams>]<F: Float>(pub(crate) PlsValidParams<F>);

        impl<F: Float> [<Pls $name Params>]<F> {
            /// Set the maximum number of iterations of the power method when algorithm='Nipals'. Ignored otherwise.
            pub fn max_iterations(mut self, max_iter: usize) -> Self {
                self.0.0.max_iter = max_iter;
                self
            }

            /// Set the tolerance used as convergence criteria in the power method: the algorithm
            /// stops whenever the squared norm of u_i - u_{i-1} is less than tol, where u corresponds
            /// to the left singular vector.
            pub fn tolerance(mut self, tolerance: F) -> Self {
                self.0.0.tolerance = tolerance;
                self
            }

            /// Set whether to scale the dataset
            pub fn scale(mut self, scale: bool) -> Self {
                self.0.0.scale = scale;
                self
            }

            /// Set the algorithm used to estimate the first singular vectors of the cross-covariance matrix.
            /// `Nipals` uses the power method while `Svd` will compute the whole SVD.
            pub fn algorithm(mut self, algorithm: Algorithm) -> Self {
                self.0.0.algorithm = algorithm;
                self
            }
        }

        impl<F: Float> ParamGuard for [<Pls $name Params>]<F> {
            type Checked = [<Pls $name ValidParams>]<F>;
            type Error = PlsError;

            fn check_ref(&self) -> Result<&Self::Checked, Self::Error> {
                if self.0.0.tolerance.is_negative() || self.0.0.tolerance.is_nan() || self.0.0.tolerance.is_infinite() {
                    Err(PlsError::InvalidTolerance(self.0.0.tolerance.to_f32().unwrap()))
                } else if self.0.0.max_iter == 0 {
                    Err(PlsError::ZeroMaxIter)
                } else {
                    Ok(&self.0)
                }
            }

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

pls_algo!(Regression);
pls_algo!(Canonical);
pls_algo!(Cca);