anofox-ml-linear 0.1.0

Stochastic Gradient Descent linear models (SGDClassifier, SGDRegressor, PassiveAggressive) for anofox-ml
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
//! SGD-based linear regressor.
//!
//! Supports squared_error, huber, and epsilon_insensitive loss functions,
//! trained with stochastic gradient descent.

use crate::sgd_common::{compute_lr, penalty_gradient, LearningRate, Penalty};
use anofox_ml_core::{Fit, Float, Predict, Result, RustMlError};
use ndarray::{Array1, Array2};
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;

/// Loss function for SGD regressor.
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum RegressorLoss {
    /// Squared error (L2 loss): `0.5 * (y - f(x))^2`.
    SquaredError,
    /// Huber loss: squared for |r| <= epsilon, linear otherwise.
    Huber,
    /// Epsilon-insensitive: zero loss for |r| <= epsilon (SVR-style).
    EpsilonInsensitive,
}

impl Default for RegressorLoss {
    fn default() -> Self {
        RegressorLoss::SquaredError
    }
}

/// Stochastic Gradient Descent regressor.
///
/// Linear regressor trained via SGD, supporting multiple loss functions
/// and regularization options.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SgdRegressor {
    pub loss: RegressorLoss,
    pub penalty: Penalty,
    pub alpha: f64,
    pub l1_ratio: f64,
    pub max_iter: usize,
    pub tol: f64,
    pub eta0: f64,
    pub power_t: f64,
    pub learning_rate: LearningRate,
    /// Epsilon parameter for Huber and EpsilonInsensitive losses.
    pub epsilon: f64,
    pub shuffle: bool,
    pub seed: u64,
}

impl SgdRegressor {
    pub fn new() -> Self {
        Self {
            loss: RegressorLoss::SquaredError,
            penalty: Penalty::L2,
            alpha: 0.0001,
            l1_ratio: 0.15,
            max_iter: 1000,
            tol: 1e-3,
            eta0: 0.01,
            power_t: 0.25,
            learning_rate: LearningRate::InvScaling,
            epsilon: 0.1,
            shuffle: true,
            seed: 0,
        }
    }

    pub fn with_loss(mut self, loss: RegressorLoss) -> Self {
        self.loss = loss;
        self
    }
    pub fn with_penalty(mut self, penalty: Penalty) -> Self {
        self.penalty = penalty;
        self
    }
    pub fn with_alpha(mut self, alpha: f64) -> Self {
        self.alpha = alpha;
        self
    }
    pub fn with_l1_ratio(mut self, l1_ratio: f64) -> Self {
        self.l1_ratio = l1_ratio;
        self
    }
    pub fn with_max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }
    pub fn with_tol(mut self, tol: f64) -> Self {
        self.tol = tol;
        self
    }
    pub fn with_eta0(mut self, eta0: f64) -> Self {
        self.eta0 = eta0;
        self
    }
    pub fn with_power_t(mut self, power_t: f64) -> Self {
        self.power_t = power_t;
        self
    }
    pub fn with_learning_rate(mut self, lr: LearningRate) -> Self {
        self.learning_rate = lr;
        self
    }
    pub fn with_epsilon(mut self, epsilon: f64) -> Self {
        self.epsilon = epsilon;
        self
    }
    pub fn with_shuffle(mut self, shuffle: bool) -> Self {
        self.shuffle = shuffle;
        self
    }
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }
}

impl Default for SgdRegressor {
    fn default() -> Self {
        Self::new()
    }
}

/// A fitted SGD linear regressor.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(bound(deserialize = "F: serde::de::DeserializeOwned"))]
pub struct FittedSgdRegressor<F: Float> {
    weights: Array1<F>,
    bias: F,
    n_features: usize,
}

impl<F: Float> FittedSgdRegressor<F> {
    /// Return the weight vector.
    pub fn weights(&self) -> &Array1<F> {
        &self.weights
    }

    /// Return the bias (intercept).
    pub fn bias(&self) -> F {
        self.bias
    }
}

impl Fit<f64> for SgdRegressor {
    type Fitted = FittedSgdRegressor<f64>;

    fn fit(&self, x: &Array2<f64>, y: &Array1<f64>) -> Result<Self::Fitted> {
        if x.nrows() != y.len() {
            return Err(RustMlError::ShapeMismatch(format!(
                "X has {} rows but y has {} elements",
                x.nrows(),
                y.len()
            )));
        }
        if x.is_empty() {
            return Err(RustMlError::EmptyInput("training data is empty".into()));
        }
        if self.alpha < 0.0 {
            return Err(RustMlError::InvalidParameter(
                "alpha must be non-negative".into(),
            ));
        }

        let n = x.nrows();
        let p = x.ncols();
        let mut w = Array1::zeros(p);
        let mut b = 0.0;
        let mut rng = StdRng::seed_from_u64(self.seed);
        let mut indices: Vec<usize> = (0..n).collect();
        let mut t: usize = 1;
        let eps = self.epsilon;

        for _epoch in 0..self.max_iter {
            if self.shuffle {
                indices.shuffle(&mut rng);
            }

            let mut total_loss = 0.0;

            for &i in &indices {
                let eta = compute_lr(self.learning_rate, self.eta0, self.alpha, t, self.power_t);
                t += 1;

                // Compute prediction: z = w · x_i + b
                let mut z = b;
                for j in 0..p {
                    z += w[j] * x[[i, j]];
                }
                let r = z - y[i]; // residual

                // Compute loss gradient w.r.t. z
                let dloss = match self.loss {
                    RegressorLoss::SquaredError => {
                        total_loss += 0.5 * r * r;
                        r
                    }
                    RegressorLoss::Huber => {
                        if r.abs() <= eps {
                            total_loss += 0.5 * r * r;
                            r
                        } else {
                            total_loss += eps * r.abs() - 0.5 * eps * eps;
                            eps * r.signum()
                        }
                    }
                    RegressorLoss::EpsilonInsensitive => {
                        if r.abs() <= eps {
                            0.0
                        } else {
                            total_loss += r.abs() - eps;
                            r.signum()
                        }
                    }
                };

                // Update weights
                if dloss != 0.0 {
                    for j in 0..p {
                        w[j] -= eta
                            * (dloss * x[[i, j]]
                                + penalty_gradient(w[j], self.alpha, self.penalty, self.l1_ratio));
                    }
                    b -= eta * dloss;
                } else {
                    for j in 0..p {
                        w[j] -=
                            eta * penalty_gradient(w[j], self.alpha, self.penalty, self.l1_ratio);
                    }
                }
            }

            let avg_loss = total_loss / n as f64;
            if avg_loss < self.tol {
                break;
            }
        }

        Ok(FittedSgdRegressor {
            weights: w,
            bias: b,
            n_features: p,
        })
    }
}

impl Predict<f64> for FittedSgdRegressor<f64> {
    fn predict(&self, x: &Array2<f64>) -> Result<Array1<f64>> {
        if x.ncols() != self.n_features {
            return Err(RustMlError::ShapeMismatch(format!(
                "expected {} features, got {}",
                self.n_features,
                x.ncols()
            )));
        }
        let n = x.nrows();
        let mut preds = Array1::zeros(n);
        for i in 0..n {
            let mut z = self.bias;
            for j in 0..self.n_features {
                z += self.weights[j] * x[[i, j]];
            }
            preds[i] = z;
        }
        Ok(preds)
    }
}

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

    fn make_linear_data() -> (Array2<f64>, Array1<f64>) {
        // y = 2 + 3*x1 + 0.5*x2
        let x = Array2::from_shape_vec(
            (20, 2),
            (0..20)
                .flat_map(|i| vec![i as f64, (i as f64) * 0.5])
                .collect(),
        )
        .unwrap();
        let y = Array1::from_vec(
            (0..20)
                .map(|i| 2.0 + 3.0 * (i as f64) + 0.5 * (i as f64) * 0.5)
                .collect(),
        );
        (x, y)
    }

    #[test]
    fn test_sgd_regressor_squared_error() {
        let (x, y) = make_linear_data();
        let reg = SgdRegressor::new()
            .with_loss(RegressorLoss::SquaredError)
            .with_max_iter(2000)
            .with_eta0(0.001)
            .with_learning_rate(LearningRate::Constant)
            .with_alpha(0.0);
        let fitted = reg.fit(&x, &y).unwrap();

        let preds = fitted.predict(&x).unwrap();
        // Check that predictions are reasonable (not exact due to SGD noise)
        for (p, t) in preds.iter().zip(y.iter()) {
            assert_abs_diff_eq!(*p, *t, epsilon = 5.0);
        }
    }

    #[test]
    fn test_sgd_regressor_huber() {
        let (x, y) = make_linear_data();
        let reg = SgdRegressor::new()
            .with_loss(RegressorLoss::Huber)
            .with_epsilon(1.0)
            .with_max_iter(2000)
            .with_eta0(0.001)
            .with_learning_rate(LearningRate::Constant)
            .with_alpha(0.0);
        let fitted = reg.fit(&x, &y).unwrap();

        let preds = fitted.predict(&x).unwrap();
        assert_eq!(preds.len(), 20);
        for &p in preds.iter() {
            assert!(p.is_finite());
        }
    }

    #[test]
    fn test_sgd_regressor_epsilon_insensitive() {
        let (x, y) = make_linear_data();
        let reg = SgdRegressor::new()
            .with_loss(RegressorLoss::EpsilonInsensitive)
            .with_epsilon(0.5)
            .with_max_iter(2000)
            .with_eta0(0.001)
            .with_learning_rate(LearningRate::Constant)
            .with_alpha(0.0);
        let fitted = reg.fit(&x, &y).unwrap();

        let preds = fitted.predict(&x).unwrap();
        assert_eq!(preds.len(), 20);
    }

    #[test]
    fn test_sgd_regressor_l1_penalty() {
        let (x, y) = make_linear_data();
        let fitted = SgdRegressor::new()
            .with_penalty(Penalty::L1)
            .with_alpha(0.01)
            .with_max_iter(500)
            .fit(&x, &y)
            .unwrap();

        // L1 should produce sparser weights than no regularization
        assert!(fitted.weights().iter().all(|w| w.is_finite()));
    }

    #[test]
    fn test_sgd_regressor_elastic_net() {
        let (x, y) = make_linear_data();
        let fitted = SgdRegressor::new()
            .with_penalty(Penalty::ElasticNet)
            .with_l1_ratio(0.5)
            .with_max_iter(500)
            .fit(&x, &y)
            .unwrap();

        assert_eq!(fitted.weights().len(), 2);
    }

    #[test]
    fn test_sgd_regressor_inv_scaling() {
        let (x, y) = make_linear_data();
        let fitted = SgdRegressor::new()
            .with_learning_rate(LearningRate::InvScaling)
            .with_eta0(0.1)
            .with_power_t(0.25)
            .with_max_iter(1000)
            .fit(&x, &y)
            .unwrap();

        let preds = fitted.predict(&x).unwrap();
        assert_eq!(preds.len(), 20);
    }

    #[test]
    fn test_sgd_regressor_shape_mismatch() {
        let x = Array2::from_shape_vec((3, 2), vec![0.0; 6]).unwrap();
        let y = Array1::from_vec(vec![1.0, 2.0]);
        assert!(SgdRegressor::new().fit(&x, &y).is_err());
    }

    #[test]
    fn test_sgd_regressor_empty_input() {
        let x = Array2::<f64>::zeros((0, 2));
        let y = Array1::<f64>::zeros(0);
        assert!(SgdRegressor::new().fit(&x, &y).is_err());
    }

    #[test]
    fn test_sgd_regressor_predict_shape_mismatch() {
        let (x, y) = make_linear_data();
        let fitted = SgdRegressor::new().with_max_iter(10).fit(&x, &y).unwrap();

        let x_bad = Array2::from_shape_vec((2, 3), vec![0.0; 6]).unwrap();
        assert!(fitted.predict(&x_bad).is_err());
    }

    #[test]
    fn test_sgd_regressor_weights_and_bias() {
        let (x, y) = make_linear_data();
        let fitted = SgdRegressor::new().with_max_iter(100).fit(&x, &y).unwrap();

        assert_eq!(fitted.weights().len(), 2);
        assert!(fitted.bias().is_finite());
    }
}

impl anofox_ml_core::RegressorScore<f64> for FittedSgdRegressor<f64> {}