Skip to main content

sklears_kernel_approximation/kernel_ridge_regression/
multitask_regression.rs

1//! Multi-Task Kernel Ridge Regression Implementation
2//!
3//! This module provides multi-task learning capabilities for kernel ridge regression,
4//! allowing simultaneous learning across multiple related tasks with cross-task
5//! regularization strategies to improve generalization.
6
7use crate::{
8    FastfoodTransform, Nystroem, RBFSampler, StructuredRandomFeatures, Trained, Untrained,
9};
10use scirs2_linalg::compat::ArrayLinalgExt;
11// Removed SVD import - using ArrayLinalgExt for both solve and svd methods
12
13use scirs2_core::ndarray::{Array1, Array2, Axis};
14use sklears_core::error::{Result, SklearsError};
15use sklears_core::prelude::{Estimator, Fit, Float, Predict};
16use std::marker::PhantomData;
17
18use super::core_types::*;
19
20/// Multi-Task Kernel Ridge Regression
21///
22/// Performs kernel ridge regression simultaneously across multiple related tasks,
23/// with optional cross-task regularization to encourage similarity between tasks.
24///
25/// This is particularly useful when you have multiple regression targets that are
26/// related and can benefit from shared representations and joint learning.
27///
28/// # Parameters
29///
30/// * `approximation_method` - Method for kernel approximation
31/// * `alpha` - Within-task regularization parameter
32/// * `task_regularization` - Cross-task regularization strategy
33/// * `solver` - Method for solving the linear system
34/// * `random_state` - Random seed for reproducibility
35#[derive(Debug, Clone)]
36pub struct MultiTaskKernelRidgeRegression<State = Untrained> {
37    pub approximation_method: ApproximationMethod,
38    pub alpha: Float,
39    pub task_regularization: TaskRegularization,
40    pub solver: Solver,
41    pub random_state: Option<u64>,
42
43    // Fitted parameters
44    weights_: Option<Array2<Float>>, // Shape: (n_features, n_tasks)
45    feature_transformer_: Option<FeatureTransformer>,
46    n_tasks_: Option<usize>,
47
48    _state: PhantomData<State>,
49}
50
51/// Cross-task regularization strategies for multi-task learning
52#[derive(Debug, Clone)]
53pub enum TaskRegularization {
54    /// No cross-task regularization (independent tasks)
55    None,
56    /// L2 regularization on task weight differences
57    L2 { beta: Float },
58    /// L1 regularization promoting sparsity across tasks
59    L1 { beta: Float },
60    /// Nuclear norm regularization on weight matrix (low-rank)
61    NuclearNorm { beta: Float },
62    /// Group sparsity regularization
63    GroupSparsity { beta: Float },
64    /// Custom regularization function
65    Custom {
66        beta: Float,
67        regularizer: fn(&Array2<Float>) -> Float,
68    },
69}
70
71impl Default for TaskRegularization {
72    fn default() -> Self {
73        Self::None
74    }
75}
76
77impl MultiTaskKernelRidgeRegression<Untrained> {
78    /// Create a new multi-task kernel ridge regression model
79    pub fn new(approximation_method: ApproximationMethod) -> Self {
80        Self {
81            approximation_method,
82            alpha: 1.0,
83            task_regularization: TaskRegularization::None,
84            solver: Solver::Direct,
85            random_state: None,
86            weights_: None,
87            feature_transformer_: None,
88            n_tasks_: None,
89            _state: PhantomData,
90        }
91    }
92
93    /// Set regularization parameter
94    pub fn alpha(mut self, alpha: Float) -> Self {
95        self.alpha = alpha;
96        self
97    }
98
99    /// Set cross-task regularization strategy
100    pub fn task_regularization(mut self, regularization: TaskRegularization) -> Self {
101        self.task_regularization = regularization;
102        self
103    }
104
105    /// Set solver method
106    pub fn solver(mut self, solver: Solver) -> Self {
107        self.solver = solver;
108        self
109    }
110
111    /// Set random state for reproducibility
112    pub fn random_state(mut self, seed: u64) -> Self {
113        self.random_state = Some(seed);
114        self
115    }
116
117    /// Compute regularization penalty for the weight matrix
118    fn compute_task_regularization_penalty(&self, weights: &Array2<Float>) -> Float {
119        match &self.task_regularization {
120            TaskRegularization::None => 0.0,
121            TaskRegularization::L2 { beta } => {
122                // L2 penalty on differences between task weights
123                let mut penalty = 0.0;
124                let n_tasks = weights.ncols();
125                for i in 0..n_tasks {
126                    for j in (i + 1)..n_tasks {
127                        let diff = &weights.column(i) - &weights.column(j);
128                        penalty += diff.mapv(|x| x * x).sum();
129                    }
130                }
131                beta * penalty / (n_tasks * (n_tasks - 1) / 2) as Float
132            }
133            TaskRegularization::L1 { beta } => {
134                // L1 penalty on task weights
135                beta * weights.mapv(|x| x.abs()).sum()
136            }
137            TaskRegularization::NuclearNorm { beta } => {
138                // Nuclear norm (sum of singular values)
139                // Approximate with Frobenius norm for efficiency
140                beta * weights.mapv(|x| x * x).sum().sqrt()
141            }
142            TaskRegularization::GroupSparsity { beta } => {
143                // Group sparsity: L2,1 norm
144                let mut penalty = 0.0;
145                for row in weights.axis_iter(Axis(0)) {
146                    penalty += row.mapv(|x| x * x).sum().sqrt();
147                }
148                beta * penalty
149            }
150            TaskRegularization::Custom { beta, regularizer } => beta * regularizer(weights),
151        }
152    }
153}
154
155impl Estimator for MultiTaskKernelRidgeRegression<Untrained> {
156    type Config = ();
157    type Error = SklearsError;
158    type Float = Float;
159
160    fn config(&self) -> &Self::Config {
161        &()
162    }
163}
164
165impl Fit<Array2<Float>, Array2<Float>> for MultiTaskKernelRidgeRegression<Untrained> {
166    type Fitted = MultiTaskKernelRidgeRegression<Trained>;
167
168    fn fit(self, x: &Array2<Float>, y: &Array2<Float>) -> Result<Self::Fitted> {
169        if x.nrows() != y.nrows() {
170            return Err(SklearsError::InvalidInput(
171                "Number of samples must match".to_string(),
172            ));
173        }
174
175        let _n_samples = x.nrows();
176        let n_tasks = y.ncols();
177
178        // Fit the feature transformer
179        let feature_transformer = self.fit_feature_transformer(x)?;
180        let x_transformed = feature_transformer.transform(x)?;
181        let _n_features = x_transformed.ncols();
182
183        // Solve multi-task regression problem
184        let weights = match self.solver {
185            Solver::Direct => self.solve_direct_multitask(&x_transformed, y)?,
186            Solver::SVD => self.solve_svd_multitask(&x_transformed, y)?,
187            Solver::ConjugateGradient { max_iter, tol } => {
188                self.solve_cg_multitask(&x_transformed, y, max_iter, tol)?
189            }
190        };
191
192        Ok(MultiTaskKernelRidgeRegression {
193            approximation_method: self.approximation_method,
194            alpha: self.alpha,
195            task_regularization: self.task_regularization,
196            solver: self.solver,
197            random_state: self.random_state,
198            weights_: Some(weights),
199            feature_transformer_: Some(feature_transformer),
200            n_tasks_: Some(n_tasks),
201            _state: PhantomData,
202        })
203    }
204}
205
206impl MultiTaskKernelRidgeRegression<Untrained> {
207    /// Fit the feature transformer based on the approximation method
208    fn fit_feature_transformer(&self, x: &Array2<Float>) -> Result<FeatureTransformer> {
209        match &self.approximation_method {
210            ApproximationMethod::Nystroem {
211                kernel,
212                n_components,
213                sampling_strategy,
214            } => {
215                let mut nystroem = Nystroem::new(kernel.clone(), *n_components)
216                    .sampling_strategy(sampling_strategy.clone());
217                if let Some(seed) = self.random_state {
218                    nystroem = nystroem.random_state(seed);
219                }
220                let fitted = nystroem.fit(x, &())?;
221                Ok(FeatureTransformer::Nystroem(fitted))
222            }
223            ApproximationMethod::RandomFourierFeatures {
224                n_components,
225                gamma,
226            } => {
227                let mut rff = RBFSampler::new(*n_components).gamma(*gamma);
228                if let Some(seed) = self.random_state {
229                    rff = rff.random_state(seed);
230                }
231                let fitted = rff.fit(x, &())?;
232                Ok(FeatureTransformer::RBFSampler(fitted))
233            }
234            ApproximationMethod::StructuredRandomFeatures {
235                n_components,
236                gamma,
237            } => {
238                let mut srf = StructuredRandomFeatures::new(*n_components).gamma(*gamma);
239                if let Some(seed) = self.random_state {
240                    srf = srf.random_state(seed);
241                }
242                let fitted = srf.fit(x, &())?;
243                Ok(FeatureTransformer::StructuredRFF(fitted))
244            }
245            ApproximationMethod::Fastfood {
246                n_components,
247                gamma,
248            } => {
249                let mut fastfood = FastfoodTransform::new(*n_components).gamma(*gamma);
250                if let Some(seed) = self.random_state {
251                    fastfood = fastfood.random_state(seed);
252                }
253                let fitted = fastfood.fit(x, &())?;
254                Ok(FeatureTransformer::Fastfood(fitted))
255            }
256        }
257    }
258
259    /// Solve multi-task problem using direct method
260    fn solve_direct_multitask(
261        &self,
262        x: &Array2<Float>,
263        y: &Array2<Float>,
264    ) -> Result<Array2<Float>> {
265        let n_features = x.ncols();
266        let n_tasks = y.ncols();
267
268        // For multi-task learning, we solve each task separately but with shared features
269        // and apply cross-task regularization
270        let mut all_weights = Array2::zeros((n_features, n_tasks));
271
272        for task_idx in 0..n_tasks {
273            let y_task = y.column(task_idx);
274
275            // Standard ridge regression for this task
276            let x_f64 = Array2::from_shape_fn(x.dim(), |(i, j)| x[[i, j]]);
277            let y_task_f64 = Array1::from_vec(y_task.iter().copied().collect());
278
279            let xtx = x_f64.t().dot(&x_f64);
280            let regularized_xtx = xtx + Array2::<f64>::eye(n_features) * self.alpha;
281
282            let xty = x_f64.t().dot(&y_task_f64);
283            let weights_task_f64 =
284                regularized_xtx
285                    .solve(&xty)
286                    .map_err(|e| SklearsError::InvalidParameter {
287                        name: "regularization".to_string(),
288                        reason: format!("Linear system solving failed: {:?}", e),
289                    })?;
290
291            // Convert back to Float
292            let weights_task =
293                Array1::from_vec(weights_task_f64.iter().map(|&val| val as Float).collect());
294            all_weights.column_mut(task_idx).assign(&weights_task);
295        }
296
297        // Apply cross-task regularization (simplified approach)
298        // In practice, you might want to solve a joint optimization problem
299        match &self.task_regularization {
300            TaskRegularization::L2 { beta } => {
301                // Apply additional regularization penalty
302                let mean_weight = all_weights
303                    .mean_axis(Axis(1))
304                    .expect("operation should succeed");
305                for mut col in all_weights.axis_iter_mut(Axis(1)) {
306                    let diff = &col.to_owned() - &mean_weight;
307                    col.scaled_add(-beta, &diff);
308                }
309            }
310            _ => {} // Other regularization methods would be implemented here
311        }
312
313        Ok(all_weights)
314    }
315
316    /// Solve multi-task problem using SVD
317    fn solve_svd_multitask(&self, x: &Array2<Float>, y: &Array2<Float>) -> Result<Array2<Float>> {
318        let n_features = x.ncols();
319        let n_tasks = y.ncols();
320        let mut all_weights = Array2::zeros((n_features, n_tasks));
321
322        for task_idx in 0..n_tasks {
323            let y_task = y.column(task_idx);
324
325            // Use SVD for more stable solution
326            let x_f64 = Array2::from_shape_fn(x.dim(), |(i, j)| x[[i, j]]);
327            let y_task_f64 = Array1::from_vec(y_task.iter().copied().collect());
328
329            let xtx = x_f64.t().dot(&x_f64);
330            let regularized_xtx = xtx + Array2::<f64>::eye(n_features) * self.alpha;
331
332            let (u, s, vt) =
333                regularized_xtx
334                    .svd(true)
335                    .map_err(|e| SklearsError::InvalidParameter {
336                        name: "svd".to_string(),
337                        reason: format!("SVD decomposition failed: {:?}", e),
338                    })?;
339
340            // Solve using SVD
341            let xty = x_f64.t().dot(&y_task_f64);
342            let ut_b = u.t().dot(&xty);
343            let s_inv = s.mapv(|x| if x > 1e-10 { 1.0 / x } else { 0.0 });
344            let y_svd = ut_b * s_inv;
345            let weights_task_f64 = vt.t().dot(&y_svd);
346
347            // Convert back to Float
348            let weights_task =
349                Array1::from_vec(weights_task_f64.iter().map(|&val| val as Float).collect());
350            all_weights.column_mut(task_idx).assign(&weights_task);
351        }
352
353        Ok(all_weights)
354    }
355
356    /// Solve multi-task problem using conjugate gradient
357    fn solve_cg_multitask(
358        &self,
359        x: &Array2<Float>,
360        y: &Array2<Float>,
361        max_iter: usize,
362        tol: Float,
363    ) -> Result<Array2<Float>> {
364        let n_features = x.ncols();
365        let n_tasks = y.ncols();
366        let mut all_weights = Array2::zeros((n_features, n_tasks));
367
368        for task_idx in 0..n_tasks {
369            let y_task = y.column(task_idx);
370            let xty = x.t().dot(&y_task);
371
372            // Conjugate gradient solver for each task
373            let mut weights = Array1::zeros(n_features);
374            let mut r = xty.clone();
375            let mut p = r.clone();
376            let mut rsold = r.dot(&r);
377
378            for _iter in 0..max_iter {
379                let xtx_p = x.t().dot(&x.dot(&p)) + &p * self.alpha;
380                let alpha_cg = rsold / p.dot(&xtx_p);
381
382                weights = weights + &p * alpha_cg;
383                r = r - &xtx_p * alpha_cg;
384
385                let rsnew = r.dot(&r);
386
387                if rsnew.sqrt() < tol {
388                    break;
389                }
390
391                let beta = rsnew / rsold;
392                p = &r + &p * beta;
393                rsold = rsnew;
394            }
395
396            all_weights.column_mut(task_idx).assign(&weights);
397        }
398
399        Ok(all_weights)
400    }
401}
402
403impl Predict<Array2<Float>, Array2<Float>> for MultiTaskKernelRidgeRegression<Trained> {
404    fn predict(&self, x: &Array2<Float>) -> Result<Array2<Float>> {
405        let feature_transformer =
406            self.feature_transformer_
407                .as_ref()
408                .ok_or_else(|| SklearsError::NotFitted {
409                    operation: "predict".to_string(),
410                })?;
411
412        let weights = self
413            .weights_
414            .as_ref()
415            .ok_or_else(|| SklearsError::NotFitted {
416                operation: "predict".to_string(),
417            })?;
418
419        let x_transformed = feature_transformer.transform(x)?;
420        let predictions = x_transformed.dot(weights);
421
422        Ok(predictions)
423    }
424}
425
426impl MultiTaskKernelRidgeRegression<Trained> {
427    /// Get the number of tasks
428    pub fn n_tasks(&self) -> usize {
429        self.n_tasks_.unwrap_or(0)
430    }
431
432    /// Get the fitted weights for all tasks
433    pub fn weights(&self) -> Option<&Array2<Float>> {
434        self.weights_.as_ref()
435    }
436
437    /// Get the weights for a specific task
438    pub fn task_weights(&self, task_idx: usize) -> Result<Array1<Float>> {
439        let weights = self
440            .weights_
441            .as_ref()
442            .ok_or_else(|| SklearsError::NotFitted {
443                operation: "predict".to_string(),
444            })?;
445
446        if task_idx >= weights.ncols() {
447            return Err(SklearsError::InvalidInput(format!(
448                "Task index {} out of range",
449                task_idx
450            )));
451        }
452
453        Ok(weights.column(task_idx).to_owned())
454    }
455
456    /// Predict for a specific task only
457    pub fn predict_task(&self, x: &Array2<Float>, task_idx: usize) -> Result<Array1<Float>> {
458        let predictions = self.predict(x)?;
459
460        if task_idx >= predictions.ncols() {
461            return Err(SklearsError::InvalidInput(format!(
462                "Task index {} out of range",
463                task_idx
464            )));
465        }
466
467        Ok(predictions.column(task_idx).to_owned())
468    }
469}
470
471#[allow(non_snake_case)]
472#[cfg(test)]
473mod tests {
474    use super::*;
475    use scirs2_core::ndarray::array;
476
477    #[test]
478    fn test_multitask_kernel_ridge_regression() {
479        let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
480        let y = array![[1.0, 2.0], [4.0, 5.0], [9.0, 10.0], [16.0, 17.0]]; // Two tasks
481
482        let approximation = ApproximationMethod::RandomFourierFeatures {
483            n_components: 20,
484            gamma: 0.1,
485        };
486
487        let mtkrr = MultiTaskKernelRidgeRegression::new(approximation).alpha(0.1);
488        let fitted = mtkrr.fit(&x, &y).expect("operation should succeed");
489        let predictions = fitted.predict(&x).expect("operation should succeed");
490
491        assert_eq!(predictions.shape(), &[4, 2]);
492        assert_eq!(fitted.n_tasks(), 2);
493
494        // Test individual task prediction
495        let task0_pred = fitted
496            .predict_task(&x, 0)
497            .expect("operation should succeed");
498        let task1_pred = fitted
499            .predict_task(&x, 1)
500            .expect("operation should succeed");
501
502        assert_eq!(task0_pred.len(), 4);
503        assert_eq!(task1_pred.len(), 4);
504
505        // Check that predictions are reasonable
506        for pred in predictions.iter() {
507            assert!(pred.is_finite());
508        }
509    }
510
511    #[test]
512    fn test_multitask_with_regularization() {
513        let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
514        let y = array![[1.0, 1.1], [2.0, 2.1], [3.0, 3.1]]; // Similar tasks
515
516        let approximation = ApproximationMethod::RandomFourierFeatures {
517            n_components: 15,
518            gamma: 1.0,
519        };
520
521        let mtkrr = MultiTaskKernelRidgeRegression::new(approximation)
522            .alpha(0.1)
523            .task_regularization(TaskRegularization::L2 { beta: 0.1 });
524
525        let fitted = mtkrr.fit(&x, &y).expect("operation should succeed");
526        let predictions = fitted.predict(&x).expect("operation should succeed");
527
528        assert_eq!(predictions.shape(), &[3, 2]);
529
530        // With L2 regularization, task weights should be similar
531        let weights = fitted.weights().expect("operation should succeed");
532        let task0_weights = weights.column(0);
533        let task1_weights = weights.column(1);
534        let weight_diff = (&task0_weights - &task1_weights)
535            .mapv(|x| x.abs())
536            .mean()
537            .expect("operation should succeed");
538
539        // Tasks should have similar weights due to regularization
540        assert!(weight_diff < 1.0);
541    }
542
543    #[test]
544    fn test_multitask_different_solvers() {
545        let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
546        let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
547
548        let approximation = ApproximationMethod::RandomFourierFeatures {
549            n_components: 10,
550            gamma: 1.0,
551        };
552
553        // Test different solvers
554        let solvers = vec![
555            Solver::Direct,
556            Solver::SVD,
557            Solver::ConjugateGradient {
558                max_iter: 100,
559                tol: 1e-6,
560            },
561        ];
562
563        for solver in solvers {
564            let mtkrr = MultiTaskKernelRidgeRegression::new(approximation.clone())
565                .solver(solver)
566                .alpha(0.1);
567
568            let fitted = mtkrr.fit(&x, &y).expect("operation should succeed");
569            let predictions = fitted.predict(&x).expect("operation should succeed");
570
571            assert_eq!(predictions.shape(), &[3, 2]);
572        }
573    }
574
575    #[test]
576    fn test_multitask_single_task() {
577        // Test that multi-task regression works with a single task
578        let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
579        let y = array![[1.0], [2.0], [3.0]]; // Single task
580
581        let approximation = ApproximationMethod::RandomFourierFeatures {
582            n_components: 10,
583            gamma: 1.0,
584        };
585
586        let mtkrr = MultiTaskKernelRidgeRegression::new(approximation).alpha(0.1);
587        let fitted = mtkrr.fit(&x, &y).expect("operation should succeed");
588        let predictions = fitted.predict(&x).expect("operation should succeed");
589
590        assert_eq!(predictions.shape(), &[3, 1]);
591        assert_eq!(fitted.n_tasks(), 1);
592    }
593
594    #[test]
595    fn test_multitask_reproducibility() {
596        let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
597        let y = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]];
598
599        let approximation = ApproximationMethod::RandomFourierFeatures {
600            n_components: 10,
601            gamma: 1.0,
602        };
603
604        let mtkrr1 = MultiTaskKernelRidgeRegression::new(approximation.clone())
605            .alpha(0.1)
606            .random_state(42);
607        let fitted1 = mtkrr1.fit(&x, &y).expect("operation should succeed");
608        let pred1 = fitted1.predict(&x).expect("operation should succeed");
609
610        let mtkrr2 = MultiTaskKernelRidgeRegression::new(approximation)
611            .alpha(0.1)
612            .random_state(42);
613        let fitted2 = mtkrr2.fit(&x, &y).expect("operation should succeed");
614        let pred2 = fitted2.predict(&x).expect("operation should succeed");
615
616        assert_eq!(pred1.shape(), pred2.shape());
617        for i in 0..pred1.nrows() {
618            for j in 0..pred1.ncols() {
619                assert!((pred1[[i, j]] - pred2[[i, j]]).abs() < 1e-10);
620            }
621        }
622    }
623
624    #[test]
625    fn test_task_regularization_penalties() {
626        let weights = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
627
628        let model =
629            MultiTaskKernelRidgeRegression::new(ApproximationMethod::RandomFourierFeatures {
630                n_components: 10,
631                gamma: 1.0,
632            });
633
634        // Test different regularization types
635        let reg_l2 = TaskRegularization::L2 { beta: 0.1 };
636        let reg_l1 = TaskRegularization::L1 { beta: 0.1 };
637        let reg_nuclear = TaskRegularization::NuclearNorm { beta: 0.1 };
638        let reg_group = TaskRegularization::GroupSparsity { beta: 0.1 };
639
640        let model_l2 = model.clone().task_regularization(reg_l2);
641        let model_l1 = model.clone().task_regularization(reg_l1);
642        let model_nuclear = model.clone().task_regularization(reg_nuclear);
643        let model_group = model.clone().task_regularization(reg_group);
644
645        let penalty_l2 = model_l2.compute_task_regularization_penalty(&weights);
646        let penalty_l1 = model_l1.compute_task_regularization_penalty(&weights);
647        let penalty_nuclear = model_nuclear.compute_task_regularization_penalty(&weights);
648        let penalty_group = model_group.compute_task_regularization_penalty(&weights);
649
650        // All penalties should be non-negative
651        assert!(penalty_l2 >= 0.0);
652        assert!(penalty_l1 >= 0.0);
653        assert!(penalty_nuclear >= 0.0);
654        assert!(penalty_group >= 0.0);
655
656        // L1 penalty should be larger than others for this matrix
657        assert!(penalty_l1 > penalty_l2);
658    }
659}