ferricml 0.2.1

A lean, pure-Rust toolkit for classical machine learning
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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Dense L2-regularized linear regression.

use super::least_squares;
use crate::api::{
    Capabilities, Estimator, HasCapabilities, HasParams, ModelError, Regressor,
    validate_prediction, validate_scalar_row,
};
use crate::artifact::{
    ArtifactCursor, ArtifactError, ArtifactPayloadWriter, MODEL_ARTIFACT_VERSION, ModelArtifact,
    RIDGE_ARTIFACT_KIND, SchemaRole, artifact_version, decode_component, decode_v2_envelope,
    encode_component, encode_v2_envelope,
};
use crate::data::{MatrixView, RegressionTargets, SampleWeights};

const MAX_ARTIFACT_FEATURES: usize = 1_000_000;
const PAYLOAD_VERSION: u16 = 1;
const STATE_COMPONENT_KIND: u16 = 1;
const STATE_COMPONENT_VERSION: u16 = 1;
const FIXED_PAYLOAD_BYTES: usize = 5 * 4;

/// Parameters for [`Ridge`].
#[derive(Clone, Debug, PartialEq)]
pub struct RidgeParams {
    alpha: f32,
    fit_intercept: bool,
}

impl Default for RidgeParams {
    fn default() -> Self {
        Self {
            alpha: 1.0,
            fit_intercept: true,
        }
    }
}

impl RidgeParams {
    /// Sets the non-negative L2 penalty applied to coefficients.
    #[must_use]
    pub fn with_alpha(mut self, alpha: f32) -> Self {
        self.alpha = alpha;
        self
    }

    /// Enables or disables the fitted intercept.
    #[must_use]
    pub fn with_fit_intercept(mut self, fit_intercept: bool) -> Self {
        self.fit_intercept = fit_intercept;
        self
    }

    /// Returns the L2 coefficient penalty.
    pub const fn alpha(&self) -> f32 {
        self.alpha
    }

    /// Returns whether an intercept is fitted.
    pub const fn fit_intercept(&self) -> bool {
        self.fit_intercept
    }
}

/// Dense single-target ridge regression.
///
/// Ridge adds an L2 penalty to the least-squares objective, which shrinks
/// coefficients toward zero without ever setting one exactly to zero. Use it
/// where features are correlated or the fit is unstable; use [`Lasso`] instead
/// when the goal is to *remove* features.
///
/// ```
/// use ferricml::data::{DenseMatrix, RegressionTargets};
/// use ferricml::linear_model::{Ridge, RidgeParams};
///
/// let data = DenseMatrix::new(vec![0.0, 1.0, 2.0, 3.0], 4, 1)?;
/// let targets = RegressionTargets::new(vec![1.0, 3.0, 5.0, 7.0])?;
///
/// let model = Ridge::fit(&data.as_view(), &targets, RidgeParams::default())?;
///
/// assert_eq!(model.n_features_in(), 1);
/// let predictions = model.predict(&data.as_view())?;
/// assert_eq!(predictions.len(), 4);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # `alpha` shrinks, and the retained value is exact
///
/// A larger `alpha` gives a strictly smaller coefficient. The value a fit was
/// given is retained exactly and readable through
/// [`get_params`](crate::api::HasParams::get_params), so a fitted model can
/// always say what it was fitted with.
///
/// ```
/// use ferricml::api::HasParams;
/// use ferricml::data::{DenseMatrix, RegressionTargets};
/// use ferricml::linear_model::{Ridge, RidgeParams};
///
/// let data = DenseMatrix::new(vec![0.0, 1.0, 2.0, 3.0], 4, 1)?;
/// let targets = RegressionTargets::new(vec![1.0, 3.0, 5.0, 7.0])?;
///
/// let weak = Ridge::fit(
///     &data.as_view(),
///     &targets,
///     RidgeParams::default().with_alpha(0.01),
/// )?;
/// let strong = Ridge::fit(
///     &data.as_view(),
///     &targets,
///     RidgeParams::default().with_alpha(100.0),
/// )?;
///
/// assert!(strong.coefficients()[0].abs() < weak.coefficients()[0].abs());
/// assert_eq!(strong.get_params().alpha(), 100.0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// Note that this `alpha` accompanies an *undivided* squared-error term, so it
/// is a different quantity from [`Lasso`]'s and [`ElasticNet`]'s, which are
/// measured against a mean. The two agree at
/// `ridge_alpha = alpha * total_weight`; see the frozen reference semantics for
/// why the scales are stated rather than reconciled.
///
/// [`Lasso`]: crate::linear_model::Lasso
/// [`ElasticNet`]: crate::linear_model::ElasticNet
#[derive(Clone, Debug, PartialEq)]
pub struct Ridge {
    n_features_in: usize,
    params: RidgeParams,
    coefficients: Vec<f32>,
    intercept: f32,
}

impl Ridge {
    /// Fits an unweighted dense ridge model.
    pub fn fit(
        data: &MatrixView<'_>,
        targets: &RegressionTargets,
        params: RidgeParams,
    ) -> Result<Self, ModelError> {
        Self::fit_internal(data, targets, None, params)
    }

    /// Fits a dense ridge model with per-row sample weights.
    pub fn fit_weighted(
        data: &MatrixView<'_>,
        targets: &RegressionTargets,
        sample_weights: &SampleWeights,
        params: RidgeParams,
    ) -> Result<Self, ModelError> {
        Self::fit_internal(data, targets, Some(sample_weights), params)
    }

    fn fit_internal(
        data: &MatrixView<'_>,
        targets: &RegressionTargets,
        sample_weights: Option<&SampleWeights>,
        params: RidgeParams,
    ) -> Result<Self, ModelError> {
        validate_fit(data, targets, sample_weights, &params)?;
        let fit = least_squares::fit_ridge_dense(
            data,
            targets.as_slice(),
            sample_weights,
            params.fit_intercept,
            params.alpha,
        )?;
        let coefficients = fit
            .coefficients
            .into_iter()
            .map(|value| value as f32)
            .collect::<Vec<_>>();
        let intercept = fit.intercept as f32;
        if coefficients.iter().any(|value| !value.is_finite()) || !intercept.is_finite() {
            return Err(ModelError::LinearSolveFailed);
        }
        Ok(Self {
            n_features_in: data.columns(),
            params,
            coefficients,
            intercept,
        })
    }

    /// Returns fitted coefficients in input-feature order.
    pub fn coefficients(&self) -> &[f32] {
        &self.coefficients
    }

    /// Returns the fitted intercept.
    pub const fn intercept(&self) -> f32 {
        self.intercept
    }

    /// Returns the feature width required by this model.
    pub const fn n_features_in(&self) -> usize {
        self.n_features_in
    }

    /// Returns the exact fit parameters.
    pub const fn get_params(&self) -> &RidgeParams {
        &self.params
    }

    /// Predicts one regression value.
    pub fn predict_one(&self, row: &[f32]) -> Result<f32, ModelError> {
        validate_scalar_row(row, self.n_features_in)?;
        validate_prediction(self.predict_value(row), 0)
    }

    fn predict_value(&self, row: &[f32]) -> f32 {
        row.iter()
            .zip(&self.coefficients)
            .fold(self.intercept, |sum, (&value, &coefficient)| {
                sum + value * coefficient
            })
    }

    /// Allocating batch prediction.
    pub fn predict(&self, data: &MatrixView<'_>) -> Result<Vec<f32>, ModelError> {
        <Self as Regressor>::predict(self, data)
    }

    /// Allocation-free batch prediction.
    pub fn predict_into(
        &self,
        data: &MatrixView<'_>,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        <Self as Regressor>::predict_into(self, data, output)
    }

    fn decode_payload(mut cursor: ArtifactCursor<'_>) -> Result<Self, ArtifactError> {
        let n_features_in = cursor.u32()? as usize;
        let alpha = cursor.f32()?;
        let fit_intercept = match cursor.u32()? {
            0 => false,
            1 => true,
            _ => return Err(ArtifactError::InvalidPayload),
        };
        let intercept = cursor.f32()?;
        let coefficient_count = cursor.u32()? as usize;
        if n_features_in == 0
            || n_features_in > MAX_ARTIFACT_FEATURES
            || coefficient_count != n_features_in
            || !alpha.is_finite()
            || alpha < 0.0
            || !intercept.is_finite()
        {
            return Err(ArtifactError::InvalidPayload);
        }
        let mut coefficients = Vec::with_capacity(cursor.bounded_capacity(coefficient_count, 4));
        for _ in 0..coefficient_count {
            let value = cursor.f32()?;
            if !value.is_finite() {
                return Err(ArtifactError::InvalidPayload);
            }
            coefficients.push(value);
        }
        if !cursor.is_empty() {
            return Err(ArtifactError::TrailingBytes);
        }
        Ok(Self {
            n_features_in,
            params: RidgeParams {
                alpha,
                fit_intercept,
            },
            coefficients,
            intercept,
        })
    }
}

impl ModelArtifact for Ridge {
    const ARTIFACT_KIND: u16 = RIDGE_ARTIFACT_KIND;

    /// Encodes this model in FerricML's bounded artifact format.
    fn to_artifact(&self, schema: [u8; 32]) -> Result<Vec<u8>, ArtifactError> {
        if self.n_features_in > MAX_ARTIFACT_FEATURES {
            return Err(ArtifactError::InvalidPayload);
        }
        let n_features =
            u32::try_from(self.n_features_in).map_err(|_| ArtifactError::InvalidPayload)?;
        let mut state =
            ArtifactPayloadWriter::with_capacity(FIXED_PAYLOAD_BYTES + self.coefficients.len() * 4);
        state.u32(n_features);
        state.f32(self.params.alpha);
        state.u32(u32::from(self.params.fit_intercept));
        state.f32(self.intercept);
        state.u32(n_features);
        for &coefficient in &self.coefficients {
            state.f32(coefficient);
        }
        let component = encode_component(
            STATE_COMPONENT_KIND,
            STATE_COMPONENT_VERSION,
            &state.finish(),
        )?;
        encode_v2_envelope(
            Self::ARTIFACT_KIND,
            PAYLOAD_VERSION,
            &[(SchemaRole::Input, schema)],
            &component,
        )
    }

    /// Decodes a ridge model after checking integrity and feature identity.
    fn from_artifact(bytes: &[u8], schema: [u8; 32]) -> Result<Self, ArtifactError> {
        let version = artifact_version(bytes)?;
        if version != MODEL_ARTIFACT_VERSION {
            return Err(ArtifactError::UnsupportedVersion { found: version });
        }
        let mut envelope = decode_v2_envelope(
            bytes,
            Self::ARTIFACT_KIND,
            PAYLOAD_VERSION,
            &[(SchemaRole::Input, schema)],
        )?;
        let component =
            decode_component(&mut envelope, STATE_COMPONENT_KIND, STATE_COMPONENT_VERSION)?;
        if !envelope.is_empty() {
            return Err(ArtifactError::TrailingBytes);
        }
        Self::decode_payload(component)
    }
}

impl Estimator for Ridge {
    fn n_features_in(&self) -> usize {
        self.n_features_in
    }
}

/// Weighted fitting and persistence — the same pair ordinary least squares
/// declares, because the penalty changes the system being solved and not what
/// the estimator can be asked for.
///
/// `probability`, `multiclass` and `decision_function` are classifier
/// vocabulary and have no meaning for a predicted value.
impl HasCapabilities for Ridge {
    const CAPABILITIES: Capabilities = Capabilities::NONE
        .with_sample_weights(true)
        .with_artifact(true);
}

impl HasParams for Ridge {
    type Params = RidgeParams;
    fn get_params(&self) -> &Self::Params {
        &self.params
    }
}

impl Regressor for Ridge {
    fn predict_into(&self, data: &MatrixView<'_>, output: &mut [f32]) -> Result<(), ModelError> {
        if data.columns() != self.n_features_in {
            return Err(ModelError::FeatureDimension {
                expected: self.n_features_in,
                actual: data.columns(),
            });
        }
        if output.len() != data.rows() {
            return Err(ModelError::OutputLength {
                expected: data.rows(),
                actual: output.len(),
            });
        }
        for (row_index, (row, slot)) in data.iter_rows().zip(output).enumerate() {
            *slot = validate_prediction(self.predict_value(row), row_index)?;
        }
        Ok(())
    }
}

fn validate_fit(
    data: &MatrixView<'_>,
    targets: &RegressionTargets,
    weights: Option<&SampleWeights>,
    params: &RidgeParams,
) -> Result<(), ModelError> {
    if data.rows() != targets.len() {
        return Err(ModelError::TargetLength {
            rows: data.rows(),
            targets: targets.len(),
        });
    }
    if let Some(weights) = weights
        && data.rows() != weights.len()
    {
        return Err(ModelError::SampleWeightLength {
            rows: data.rows(),
            weights: weights.len(),
        });
    }
    if !params.alpha.is_finite() || params.alpha < 0.0 {
        return Err(ModelError::InvalidRidgeAlpha);
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::DenseMatrix;

    fn assert_close(actual: f32, expected: f32) {
        assert!(
            (actual - expected).abs() <= 2.0e-5,
            "{actual} != {expected}"
        );
    }

    #[test]
    fn weighted_and_intercept_fit_bits_are_frozen() {
        let data = DenseMatrix::new(
            vec![0.0, 1.0, 2.0, 1.0, 1.0, 0.0, 3.0, 2.0, 4.0, 1.0, 5.0, 3.0],
            6,
            2,
        )
        .unwrap();
        let targets = RegressionTargets::new(vec![0.5, 1.5, 1.0, 3.5, 4.0, 5.5]).unwrap();
        let weights = SampleWeights::new(vec![1.0, 2.0, 0.5, 1.5, 3.0, 2.0]).unwrap();

        let cases = [
            (false, true, [1_062_563_850, 1_052_534_438], 1_035_716_202),
            (false, false, [1_062_832_872, 1_053_127_666], 0),
            (true, true, [1_063_562_964, 1_052_156_503], 3_181_533_034),
            (true, false, [1_063_313_837, 1_051_753_503], 0),
        ];
        for (weighted, fit_intercept, expected_coefficients, expected_intercept) in cases {
            let params = RidgeParams::default().with_fit_intercept(fit_intercept);
            let model = if weighted {
                Ridge::fit_weighted(&data.as_view(), &targets, &weights, params).unwrap()
            } else {
                Ridge::fit(&data.as_view(), &targets, params).unwrap()
            };
            assert_eq!(
                model
                    .coefficients()
                    .iter()
                    .map(|value| value.to_bits())
                    .collect::<Vec<_>>(),
                expected_coefficients
            );
            assert_eq!(model.intercept().to_bits(), expected_intercept);
        }
    }

    #[test]
    fn fits_default_ridge_and_excludes_intercept_from_penalty() {
        let data = DenseMatrix::new(vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 2.0, 3.0], 4, 2).unwrap();
        let targets = RegressionTargets::new(vec![3.0, 4.0, 5.0, 11.0]).unwrap();
        let model = Ridge::fit(&data.as_view(), &targets, RidgeParams::default()).unwrap();
        assert_close(model.coefficients()[0], 0.942_028_9);
        assert_close(model.coefficients()[1], 1.739_130_3);
        assert_close(model.intercept(), 3.304_348);
    }

    #[test]
    fn alpha_zero_matches_minimum_norm_linear_regression() {
        let data = DenseMatrix::new(vec![1.0, 2.0, 2.0, 4.0, 3.0, 6.0], 3, 2).unwrap();
        let targets = RegressionTargets::new(vec![1.0, 2.0, 3.0]).unwrap();
        let ridge = Ridge::fit(
            &data.as_view(),
            &targets,
            RidgeParams::default()
                .with_alpha(0.0)
                .with_fit_intercept(false),
        )
        .unwrap();
        assert_close(ridge.coefficients()[0], 0.2);
        assert_close(ridge.coefficients()[1], 0.4);
        assert_eq!(ridge.intercept().to_bits(), 0.0_f32.to_bits());
    }

    #[test]
    fn weighted_artifact_round_trip_and_validation() {
        let data = DenseMatrix::new(vec![0.0, 1.0, 2.0, 4.0], 4, 1).unwrap();
        let targets = RegressionTargets::new(vec![1.0, 2.0, 2.0, 5.0]).unwrap();
        let model = Ridge::fit_weighted(
            &data.as_view(),
            &targets,
            &SampleWeights::new(vec![1.0, 2.0, 1.0, 2.0]).unwrap(),
            RidgeParams::default(),
        )
        .unwrap();
        let bytes = model.to_artifact([9; 32]).unwrap();
        assert_eq!(Ridge::from_artifact(&bytes, [9; 32]).unwrap(), model);
        assert_eq!(
            Ridge::fit(
                &data.as_view(),
                &targets,
                RidgeParams::default().with_alpha(-1.0)
            )
            .unwrap_err(),
            ModelError::InvalidRidgeAlpha
        );
    }

    /// `fit_intercept` is the one payload field with two legal encodings, and
    /// only one of them was ever decoded anywhere in the crate: every other
    /// round-trip, fingerprint and hardening fixture persists the fitting
    /// default. A reader that lost the `0` arm would reject every stored
    /// no-intercept model and no test would notice, so both configurations are
    /// decoded here — and each is decoded back to the *same bytes*, which is
    /// what makes the flag round-trip rather than merely survive.
    #[test]
    fn both_persisted_intercept_configurations_decode_to_the_model_that_wrote_them() {
        let data = DenseMatrix::new(vec![0.0, 1.0, 1.0, 0.0, 2.0, 1.0, 3.0, 2.0], 4, 2).unwrap();
        let targets = RegressionTargets::new(vec![1.0, 2.0, 3.0, 5.0]).unwrap();
        let mut encodings = Vec::new();
        for fit_intercept in [false, true] {
            let model = Ridge::fit(
                &data.as_view(),
                &targets,
                RidgeParams::default().with_fit_intercept(fit_intercept),
            )
            .unwrap();
            assert_eq!(model.get_params().fit_intercept(), fit_intercept);
            let bytes = model.to_artifact([5; 32]).unwrap();
            let decoded = Ridge::from_artifact(&bytes, [5; 32]).unwrap();
            assert_eq!(decoded, model);
            assert_eq!(decoded.get_params().fit_intercept(), fit_intercept);
            assert_eq!(decoded.to_artifact([5; 32]).unwrap(), bytes);
            encodings.push(bytes);
        }
        assert_ne!(
            encodings[0], encodings[1],
            "the two configurations must not encode to the same bytes"
        );
    }
}