sklears-decomposition 0.2.0

Matrix decomposition algorithms for sklears: PCA, ICA, NMF, SVD
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
531
532
533
534
//! Core Dictionary Learning Types and Configuration
//!
//! This module provides the fundamental dictionary learning types and configurations
//! that comply with SciRS2 Policy for matrix decomposition algorithms.

use scirs2_core::ndarray::Array2;
use scirs2_core::random::thread_rng;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use sklears_core::{
    error::{Result, SklearsError},
    traits::{Estimator, Fit, Trained, Transform, Untrained},
    types::Float,
};

/// Dictionary transformation algorithms
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DictionaryTransformAlgorithm {
    /// Orthogonal Matching Pursuit
    OMP,
    /// Least Angle Regression
    LARS,
    /// Coordinate Descent
    CoordinateDescent,
    /// Thresholding
    Threshold,
}

/// Dictionary Learning configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DictionaryLearningConfig {
    /// Number of dictionary atoms
    pub n_components: usize,
    /// Maximum number of iterations
    pub max_iter: usize,
    /// Convergence tolerance
    pub tol: Float,
    /// Transform algorithm for sparse coding
    pub transform_algorithm: DictionaryTransformAlgorithm,
    /// Regularization parameter
    pub alpha: Float,
    /// Random state for reproducibility
    pub random_state: Option<u64>,
}

impl Default for DictionaryLearningConfig {
    fn default() -> Self {
        Self {
            n_components: 100,
            max_iter: 1000,
            tol: 1e-8,
            transform_algorithm: DictionaryTransformAlgorithm::OMP,
            alpha: 1.0,
            random_state: None,
        }
    }
}

/// Dictionary Learning estimator
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DictionaryLearning<State = Untrained> {
    config: DictionaryLearningConfig,
    state: std::marker::PhantomData<State>,
    /// Dictionary components (atoms) - only available when trained
    components_: Option<Array2<Float>>,
    /// Number of iterations performed - only available when trained
    n_iter_: Option<usize>,
}

impl DictionaryLearning<Untrained> {
    /// Create a new dictionary learning instance
    pub fn new(config: DictionaryLearningConfig) -> Self {
        Self {
            config,
            state: std::marker::PhantomData,
            components_: None,
            n_iter_: None,
        }
    }

    /// Builder pattern constructor
    pub fn builder() -> DictionaryLearningBuilder {
        DictionaryLearningBuilder::default()
    }
}

impl DictionaryLearning<Trained> {
    /// Get the dictionary components
    pub fn components(&self) -> &Array2<Float> {
        self.components_.as_ref().expect("Model is trained")
    }

    /// Get the number of iterations performed
    pub fn n_iter(&self) -> usize {
        self.n_iter_.expect("Model is trained")
    }
}

impl Estimator for DictionaryLearning<Untrained> {
    type Config = DictionaryLearningConfig;
    type Error = SklearsError;
    type Float = Float;

    fn config(&self) -> &Self::Config {
        &self.config
    }
}

impl Fit<Array2<Float>, ()> for DictionaryLearning<Untrained> {
    type Fitted = DictionaryLearning<Trained>;

    fn fit(self, x: &Array2<Float>, _y: &()) -> Result<Self::Fitted> {
        let (_n_samples, n_features) = x.dim();

        if self.config.n_components > n_features {
            return Err(SklearsError::InvalidInput(
                "n_components cannot be larger than n_features".to_string(),
            ));
        }

        // Initialize dictionary randomly
        let mut rng = thread_rng();
        let mut components = Array2::zeros((self.config.n_components, n_features));

        for i in 0..self.config.n_components {
            for j in 0..n_features {
                components[[i, j]] = rng.random::<Float>() - 0.5;
            }
        }

        // Normalize initial dictionary atoms
        for mut row in components.rows_mut() {
            let norm = row.mapv(|x| x * x).sum().sqrt();
            if norm > 1e-10 {
                row.mapv_inplace(|x| x / norm);
            }
        }

        // Dictionary learning via alternating optimization (MOD algorithm)
        // Alternates between sparse coding and dictionary update
        use super::omp_algorithms::{OMPConfig, OMPEncoder};

        let n_samples = x.nrows();
        let n_atoms = self.config.n_components;

        // Configure OMP for sparse coding step
        let omp_config = OMPConfig {
            n_nonzero_coefs: Some((self.config.alpha * n_atoms as Float).max(1.0) as usize),
            tol: Some(self.config.tol * 0.1), // Tighter tolerance for inner iterations
        };

        let mut n_iter = 0;
        let mut prev_error = Float::INFINITY;

        for iter in 0..self.config.max_iter {
            n_iter = iter + 1;

            // Step 1: Sparse coding - encode all samples with current dictionary
            let mut codes = Array2::zeros((n_samples, n_atoms));
            let encoder = OMPEncoder::new(omp_config.clone());
            let dict_transposed = components.t().to_owned();

            for i in 0..n_samples {
                let signal = x.row(i).to_owned();
                if let Ok(result) = encoder.encode(&dict_transposed, &signal) {
                    for j in 0..n_atoms {
                        codes[[i, j]] = result.coefficients[j];
                    }
                }
            }

            // Step 2: Dictionary update - minimize reconstruction error
            // For each atom, update to minimize ||X - codes * Dictionary||_F^2
            // Using simple least squares: each atom = X^T * codes_column / ||codes_column||^2

            for atom_idx in 0..n_atoms {
                // Compute residual without current atom
                let mut residual = x.clone();
                for i in 0..n_samples {
                    for k in 0..n_atoms {
                        if k != atom_idx {
                            let coef = codes[[i, k]];
                            for j in 0..n_features {
                                residual[[i, j]] -= coef * components[[k, j]];
                            }
                        }
                    }
                }

                // Update atom: atom = (residual^T * codes[:, atom_idx]) / ||codes[:, atom_idx]||^2
                let codes_col_norm_sq: Float = (0..n_samples)
                    .map(|i| codes[[i, atom_idx]] * codes[[i, atom_idx]])
                    .sum();

                if codes_col_norm_sq > 1e-10 {
                    for j in 0..n_features {
                        let mut numerator = 0.0;
                        for i in 0..n_samples {
                            numerator += residual[[i, j]] * codes[[i, atom_idx]];
                        }
                        components[[atom_idx, j]] = numerator / codes_col_norm_sq;
                    }

                    // Normalize the updated atom
                    let atom_norm: Float = (0..n_features)
                        .map(|j| components[[atom_idx, j]] * components[[atom_idx, j]])
                        .sum::<Float>()
                        .sqrt();

                    if atom_norm > 1e-10 {
                        for j in 0..n_features {
                            components[[atom_idx, j]] /= atom_norm;
                        }
                    }
                }
            }

            // Step 3: Check convergence using reconstruction error
            let mut reconstruction_error = 0.0;
            for i in 0..n_samples {
                for j in 0..n_features {
                    let mut pred = 0.0;
                    for k in 0..n_atoms {
                        pred += codes[[i, k]] * components[[k, j]];
                    }
                    let diff = x[[i, j]] - pred;
                    reconstruction_error += diff * diff;
                }
            }
            reconstruction_error =
                (reconstruction_error / (n_samples * n_features) as Float).sqrt();

            // Check for convergence
            if (prev_error - reconstruction_error).abs() < self.config.tol {
                break;
            }
            prev_error = reconstruction_error;
        }

        Ok(DictionaryLearning {
            config: self.config,
            state: std::marker::PhantomData,
            components_: Some(components),
            n_iter_: Some(n_iter),
        })
    }
}

impl Transform<Array2<Float>, Array2<Float>> for DictionaryLearning<Trained> {
    fn transform(&self, x: &Array2<Float>) -> Result<Array2<Float>> {
        use super::omp_algorithms::{OMPConfig, OMPEncoder};

        let (n_samples, n_features) = x.dim();
        let components = self.components();
        let (n_atoms, dict_features) = components.dim();

        // Validate dimensions
        if n_features != dict_features {
            return Err(SklearsError::InvalidInput(format!(
                "Input features {} don't match dictionary features {}",
                n_features, dict_features
            )));
        }

        // Configure OMP encoder based on dictionary learning config
        let omp_config = match self.config.transform_algorithm {
            DictionaryTransformAlgorithm::OMP => OMPConfig {
                n_nonzero_coefs: Some((self.config.alpha * n_atoms as Float) as usize),
                tol: Some(self.config.tol),
            },
            _ => {
                // For other algorithms, use OMP with default settings
                OMPConfig {
                    n_nonzero_coefs: None,
                    tol: Some(self.config.tol),
                }
            }
        };

        let encoder = OMPEncoder::new(omp_config);
        let mut codes = Array2::zeros((n_samples, n_atoms));

        // Encode each sample using OMP
        // Dictionary is transposed: components is (n_atoms × n_features)
        // OMP expects dictionary as (n_features × n_atoms)
        let dict_transposed = components.t().to_owned();

        for i in 0..n_samples {
            let signal = x.row(i).to_owned();
            let result = encoder.encode(&dict_transposed, &signal)?;

            // Copy coefficients to codes matrix
            for j in 0..n_atoms {
                codes[[i, j]] = result.coefficients[j];
            }
        }

        Ok(codes)
    }
}

/// Builder for Dictionary Learning
#[derive(Debug, Clone, Default)]
pub struct DictionaryLearningBuilder {
    config: DictionaryLearningConfig,
}

impl DictionaryLearningBuilder {
    pub fn n_components(mut self, n_components: usize) -> Self {
        self.config.n_components = n_components;
        self
    }

    pub fn max_iter(mut self, max_iter: usize) -> Self {
        self.config.max_iter = max_iter;
        self
    }

    pub fn tol(mut self, tol: Float) -> Self {
        self.config.tol = tol;
        self
    }

    pub fn transform_algorithm(mut self, algorithm: DictionaryTransformAlgorithm) -> Self {
        self.config.transform_algorithm = algorithm;
        self
    }

    pub fn alpha(mut self, alpha: Float) -> Self {
        self.config.alpha = alpha;
        self
    }

    pub fn random_state(mut self, random_state: Option<u64>) -> Self {
        self.config.random_state = random_state;
        self
    }

    pub fn build(self) -> DictionaryLearning<Untrained> {
        DictionaryLearning::new(self.config)
    }
}

// Type alias for backward compatibility
pub type TrainedDictionaryLearning = DictionaryLearning<Trained>;

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

    #[test]
    fn test_dictionary_learning_transform() {
        // Create a simple dataset
        let x = Array2::from_shape_vec(
            (10, 5),
            vec![
                1.0, 2.0, 3.0, 4.0, 5.0, 2.0, 3.0, 4.0, 5.0, 6.0, 3.0, 4.0, 5.0, 6.0, 7.0, 1.5,
                2.5, 3.5, 4.5, 5.5, 2.5, 3.5, 4.5, 5.5, 6.5, 1.2, 2.2, 3.2, 4.2, 5.2, 2.2, 3.2,
                4.2, 5.2, 6.2, 1.8, 2.8, 3.8, 4.8, 5.8, 2.8, 3.8, 4.8, 5.8, 6.8, 1.1, 2.1, 3.1,
                4.1, 5.1,
            ],
        )
        .expect("operation should succeed");

        // Create and fit dictionary learning model
        let config = DictionaryLearningConfig {
            n_components: 3,
            max_iter: 50,
            tol: 1e-4,
            transform_algorithm: DictionaryTransformAlgorithm::OMP,
            alpha: 0.5,
            random_state: Some(42),
        };

        let model = DictionaryLearning::new(config);
        let fitted_model = model.fit(&x, &()).expect("model fitting should succeed");

        // Test transform
        let codes = fitted_model
            .transform(&x)
            .expect("transformation should succeed");

        // Check dimensions
        assert_eq!(codes.nrows(), 10); // n_samples
        assert_eq!(codes.ncols(), 3); // n_components

        // Check that codes are not all zeros (indicating actual encoding happened)
        let sum: Float = codes.iter().map(|&x| x.abs()).sum();
        assert!(sum > 0.0, "Codes should not be all zeros");
    }

    #[test]
    fn test_dictionary_learning_dimension_validation() {
        // Create a simple dataset
        let x_train = Array2::from_shape_vec((5, 3), vec![1.0; 15])
            .expect("shape and data length should match");
        let x_test = Array2::from_shape_vec((3, 4), vec![1.0; 12])
            .expect("shape and data length should match");

        let config = DictionaryLearningConfig {
            n_components: 2, // Less than n_features (3)
            ..Default::default()
        };
        let model = DictionaryLearning::new(config);
        let fitted_model = model
            .fit(&x_train, &())
            .expect("model fitting should succeed");

        // Should fail due to dimension mismatch (x_test has 4 features, but model expects 3)
        let result = fitted_model.transform(&x_test);
        assert!(result.is_err());
    }

    #[test]
    fn test_dictionary_learning_builder() {
        let model = DictionaryLearning::builder()
            .n_components(5)
            .max_iter(100)
            .tol(1e-5)
            .alpha(0.3)
            .random_state(Some(123))
            .build();

        assert_eq!(model.config.n_components, 5);
        assert_eq!(model.config.max_iter, 100);
        assert_eq!(model.config.tol, 1e-5);
        assert_eq!(model.config.alpha, 0.3);
        assert_eq!(model.config.random_state, Some(123));
    }

    #[test]
    fn test_dictionary_learning_convergence() {
        // Create a dataset with clear structure
        let x = Array2::from_shape_vec(
            (20, 8),
            vec![
                // Pattern 1: high values in first half
                5.0, 5.0, 5.0, 5.0, 1.0, 1.0, 1.0, 1.0, 5.5, 5.5, 5.5, 5.5, 0.5, 0.5, 0.5, 0.5, 4.5,
                4.5, 4.5, 4.5, 1.5, 1.5, 1.5, 1.5, 5.2, 5.2, 5.2, 5.2, 0.8, 0.8, 0.8, 0.8, 4.8,
                4.8, 4.8, 4.8, 1.2, 1.2, 1.2, 1.2,
                // Pattern 2: high values in second half
                1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0, 0.5, 0.5, 0.5, 0.5, 5.5, 5.5, 5.5, 5.5, 1.5,
                1.5, 1.5, 1.5, 4.5, 4.5, 4.5, 4.5, 0.8, 0.8, 0.8, 0.8, 5.2, 5.2, 5.2, 5.2, 1.2,
                1.2, 1.2, 1.2, 4.8, 4.8, 4.8, 4.8, // Pattern 3: alternating
                5.0, 1.0, 5.0, 1.0, 5.0, 1.0, 5.0, 1.0, 5.5, 0.5, 5.5, 0.5, 5.5, 0.5, 5.5, 0.5,
                4.5, 1.5, 4.5, 1.5, 4.5, 1.5, 4.5, 1.5, 5.2, 0.8, 5.2, 0.8, 5.2, 0.8, 5.2, 0.8,
                4.8, 1.2, 4.8, 1.2, 4.8, 1.2, 4.8, 1.2, // Mixed patterns
                3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.5, 2.5, 3.5, 2.5, 3.5, 2.5, 3.5, 2.5,
                2.5, 3.5, 2.5, 3.5, 2.5, 3.5, 2.5, 3.5, 4.0, 2.0, 3.0, 3.0, 2.0, 4.0, 3.0, 3.0,
                2.0, 4.0, 3.0, 3.0, 4.0, 2.0, 3.0, 3.0,
            ],
        )
        .expect("operation should succeed");

        // Configure dictionary learning with reasonable parameters
        let config = DictionaryLearningConfig {
            n_components: 4,
            max_iter: 30,
            tol: 1e-3,
            transform_algorithm: DictionaryTransformAlgorithm::OMP,
            alpha: 0.3,
            random_state: Some(42),
        };

        let model = DictionaryLearning::new(config);
        let fitted_model = model.fit(&x, &()).expect("model fitting should succeed");

        // Check that dictionary was learned
        let components = fitted_model.components();
        assert_eq!(components.nrows(), 4);
        assert_eq!(components.ncols(), 8);

        // Check that dictionary atoms are normalized
        for atom_idx in 0..4 {
            let norm: Float = (0..8)
                .map(|j| components[[atom_idx, j]] * components[[atom_idx, j]])
                .sum::<Float>()
                .sqrt();
            assert!(
                (norm - 1.0).abs() < 1e-6,
                "Atom {} not normalized: {}",
                atom_idx,
                norm
            );
        }

        // Check that transform produces reasonable sparse codes
        let codes = fitted_model
            .transform(&x)
            .expect("transformation should succeed");
        assert_eq!(codes.nrows(), 20);
        assert_eq!(codes.ncols(), 4);

        // Verify sparsity of codes (most entries should be zero or very small)
        let mut nonzero_count = 0;
        for i in 0..codes.nrows() {
            for j in 0..codes.ncols() {
                if codes[[i, j]].abs() > 1e-10 {
                    nonzero_count += 1;
                }
            }
        }
        let sparsity_ratio = nonzero_count as f64 / (codes.nrows() * codes.ncols()) as f64;
        assert!(
            sparsity_ratio < 0.5,
            "Codes should be sparse, but {}% are nonzero",
            sparsity_ratio * 100.0
        );

        // Verify reconstruction is reasonable (not perfect, but better than random)
        let mut reconstruction_error = 0.0;
        for i in 0..x.nrows() {
            for j in 0..x.ncols() {
                let mut reconstruction = 0.0;
                for k in 0..components.nrows() {
                    reconstruction += codes[[i, k]] * components[[k, j]];
                }
                let diff = x[[i, j]] - reconstruction;
                reconstruction_error += diff * diff;
            }
        }
        reconstruction_error = (reconstruction_error / (x.nrows() * x.ncols()) as Float).sqrt();

        // Error should be reasonable (not too large)
        assert!(
            reconstruction_error < 3.0,
            "Reconstruction error too high: {}",
            reconstruction_error
        );
    }
}