anofox-regression 0.5.9

A robust statistics library for regression analysis
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Quantile Regression solver.
//!
//! Implements quantile regression using Iteratively Reweighted Least Squares (IRLS)
//! with the asymmetric Huber loss approximation for numerical stability.
//!
//! Quantile regression estimates conditional quantiles of the response variable,
//! providing a more complete picture of the conditional distribution than OLS,
//! which only estimates the conditional mean.
//!
//! # Algorithm
//!
//! The algorithm minimizes the asymmetric loss function:
//! L(u) = Σ ρ_τ(y_i - x_i'β)
//!
//! where ρ_τ(u) = u(τ - I(u < 0)) is the check function.
//!
//! We use IRLS with smoothed weights to handle the non-differentiability at zero:
//! w_i = τ / max(ε, |r_i|) if r_i >= 0
//! w_i = (1-τ) / max(ε, |r_i|) if r_i < 0
//!
//! # References
//!
//! - Koenker, R., & Bassett, G. (1978). Regression quantiles. Econometrica, 33-50.
//! - Validated against R's `quantreg` package: <https://cran.r-project.org/package=quantreg>

use crate::core::{IntervalType, PredictionResult, RegressionResult};
use crate::solvers::traits::{FittedRegressor, RegressionError, Regressor};
use faer::{Col, Mat};

/// Quantile Regression estimator.
///
/// Estimates conditional quantiles of the response variable distribution.
///
/// # Example
///
/// ```rust,ignore
/// use statistics::solvers::{QuantileRegressor, Regressor, FittedRegressor};
/// use faer::{Mat, Col};
///
/// let x = Mat::from_fn(100, 2, |i, j| (i + j) as f64);
/// let y = Col::from_fn(100, |i| 1.0 + 2.0 * i as f64 + rand::random::<f64>());
///
/// // Fit median regression (tau = 0.5)
/// let fitted = QuantileRegressor::builder()
///     .tau(0.5)
///     .build()
///     .fit(&x, &y)?;
///
/// println!("Coefficients: {:?}", fitted.coefficients());
/// ```
#[derive(Debug, Clone)]
pub struct QuantileRegressor {
    /// Quantile to estimate (0 < tau < 1)
    tau: f64,
    /// Whether to include an intercept term
    with_intercept: bool,
    /// Maximum iterations for IRLS
    max_iterations: usize,
    /// Convergence tolerance
    tolerance: f64,
    /// Smoothing parameter for weights
    epsilon: f64,
    /// Optional observation weights
    weights: Option<Col<f64>>,
}

impl QuantileRegressor {
    /// Create a new quantile regressor with the given quantile.
    ///
    /// # Arguments
    /// * `tau` - The quantile to estimate (must be between 0 and 1)
    pub fn new(tau: f64) -> Self {
        Self {
            tau,
            with_intercept: true,
            max_iterations: 100,
            tolerance: 1e-6,
            epsilon: 1e-6,
            weights: None,
        }
    }

    /// Create a builder for configuring the regressor.
    pub fn builder() -> QuantileRegressorBuilder {
        QuantileRegressorBuilder::default()
    }

    /// Fit the model using IRLS algorithm.
    fn fit_irls(
        &self,
        x: &Mat<f64>,
        y: &Col<f64>,
    ) -> Result<(Col<f64>, Option<f64>), RegressionError> {
        let n = x.nrows();
        let p = x.ncols();

        // Add intercept column if needed
        let x_aug = if self.with_intercept {
            let mut x_new = Mat::zeros(n, p + 1);
            for i in 0..n {
                x_new[(i, 0)] = 1.0;
                for j in 0..p {
                    x_new[(i, j + 1)] = x[(i, j)];
                }
            }
            x_new
        } else {
            x.clone()
        };
        let p_aug = x_aug.ncols();

        // Initialize with OLS solution
        let mut beta = self.ols_solve(&x_aug, y)?;

        // IRLS iterations
        for _iter in 0..self.max_iterations {
            // Compute residuals
            let mut residuals = Col::zeros(n);
            for i in 0..n {
                let mut pred = 0.0;
                for j in 0..p_aug {
                    pred += x_aug[(i, j)] * beta[j];
                }
                residuals[i] = y[i] - pred;
            }

            // Compute asymmetric weights
            let mut w = Col::zeros(n);
            for i in 0..n {
                let abs_r = residuals[i].abs().max(self.epsilon);
                if residuals[i] >= 0.0 {
                    w[i] = self.tau / abs_r;
                } else {
                    w[i] = (1.0 - self.tau) / abs_r;
                }

                // Apply observation weights if provided
                if let Some(ref obs_weights) = self.weights {
                    w[i] *= obs_weights[i];
                }
            }

            // Solve weighted least squares: (X'WX)^{-1} X'Wy
            let beta_new = self.wls_solve(&x_aug, y, &w)?;

            // Check convergence
            let mut max_change = 0.0f64;
            for j in 0..p_aug {
                let change = (beta_new[j] - beta[j]).abs();
                max_change = max_change.max(change);
            }

            beta = beta_new;

            if max_change < self.tolerance {
                break;
            }
        }

        // Extract intercept and coefficients
        if self.with_intercept {
            let intercept = beta[0];
            let mut coefficients = Col::zeros(p);
            for j in 0..p {
                coefficients[j] = beta[j + 1];
            }
            Ok((coefficients, Some(intercept)))
        } else {
            Ok((beta, None))
        }
    }

    /// Solve OLS using QR decomposition.
    fn ols_solve(&self, x: &Mat<f64>, y: &Col<f64>) -> Result<Col<f64>, RegressionError> {
        let n = x.nrows();
        let p = x.ncols();

        // Compute X'X
        let mut xtx = Mat::zeros(p, p);
        for i in 0..p {
            for j in 0..p {
                let mut sum = 0.0;
                for k in 0..n {
                    sum += x[(k, i)] * x[(k, j)];
                }
                xtx[(i, j)] = sum;
            }
        }

        // Compute X'y
        let mut xty = Col::zeros(p);
        for j in 0..p {
            let mut sum = 0.0;
            for i in 0..n {
                sum += x[(i, j)] * y[i];
            }
            xty[j] = sum;
        }

        // Solve using Cholesky decomposition
        self.solve_symmetric(&xtx, &xty)
    }

    /// Solve weighted least squares.
    fn wls_solve(
        &self,
        x: &Mat<f64>,
        y: &Col<f64>,
        w: &Col<f64>,
    ) -> Result<Col<f64>, RegressionError> {
        let n = x.nrows();
        let p = x.ncols();

        // Compute X'WX
        let mut xtwx = Mat::zeros(p, p);
        for i in 0..p {
            for j in 0..p {
                let mut sum = 0.0;
                for k in 0..n {
                    sum += x[(k, i)] * w[k] * x[(k, j)];
                }
                xtwx[(i, j)] = sum;
            }
        }

        // Compute X'Wy
        let mut xtwy = Col::zeros(p);
        for j in 0..p {
            let mut sum = 0.0;
            for i in 0..n {
                sum += x[(i, j)] * w[i] * y[i];
            }
            xtwy[j] = sum;
        }

        // Solve using Cholesky decomposition
        self.solve_symmetric(&xtwx, &xtwy)
    }

    /// Solve symmetric positive definite system Ax = b using Cholesky decomposition.
    fn solve_symmetric(&self, a: &Mat<f64>, b: &Col<f64>) -> Result<Col<f64>, RegressionError> {
        let n = a.nrows();

        // Cholesky decomposition: A = LL'
        let mut l: Mat<f64> = Mat::zeros(n, n);
        for j in 0..n {
            let mut sum = 0.0;
            for k in 0..j {
                sum += l[(j, k)].powi(2);
            }
            let diag = a[(j, j)] - sum;
            if diag <= 0.0 {
                // Add small regularization if needed
                l[(j, j)] = (a[(j, j)] + 1e-10).sqrt();
            } else {
                l[(j, j)] = diag.sqrt();
            }

            for i in (j + 1)..n {
                let mut sum = 0.0;
                for k in 0..j {
                    sum += l[(i, k)] * l[(j, k)];
                }
                l[(i, j)] = (a[(i, j)] - sum) / l[(j, j)];
            }
        }

        // Forward substitution: Ly = b
        let mut y_sol = Col::zeros(n);
        for i in 0..n {
            let mut sum = b[i];
            for j in 0..i {
                sum -= l[(i, j)] * y_sol[j];
            }
            y_sol[i] = sum / l[(i, i)];
        }

        // Backward substitution: L'x = y
        let mut x = Col::zeros(n);
        for i in (0..n).rev() {
            let mut sum = y_sol[i];
            for j in (i + 1)..n {
                sum -= l[(j, i)] * x[j];
            }
            x[i] = sum / l[(i, i)];
        }

        Ok(x)
    }

    /// Compute the check function (quantile loss).
    fn check_loss(&self, residuals: &Col<f64>) -> f64 {
        let mut loss = 0.0;
        for i in 0..residuals.nrows() {
            let r = residuals[i];
            if r >= 0.0 {
                loss += self.tau * r;
            } else {
                loss += (self.tau - 1.0) * r;
            }
        }
        loss
    }
}

impl Regressor for QuantileRegressor {
    type Fitted = FittedQuantile;

    fn fit(&self, x: &Mat<f64>, y: &Col<f64>) -> Result<Self::Fitted, RegressionError> {
        let n_samples = x.nrows();
        let n_features = x.ncols();

        // Validate dimensions
        if x.nrows() != y.nrows() {
            return Err(RegressionError::DimensionMismatch {
                x_rows: x.nrows(),
                y_len: y.nrows(),
            });
        }

        // Need at least p+1 observations
        let min_obs = if self.with_intercept {
            n_features + 2
        } else {
            n_features + 1
        };
        if n_samples < min_obs {
            return Err(RegressionError::InsufficientObservations {
                needed: min_obs,
                got: n_samples,
            });
        }

        // Validate tau
        if self.tau <= 0.0 || self.tau >= 1.0 {
            return Err(RegressionError::NumericalError(format!(
                "tau must be between 0 and 1, got {}",
                self.tau
            )));
        }

        // Validate weights if provided
        if let Some(ref w) = self.weights {
            if w.nrows() != n_samples {
                return Err(RegressionError::DimensionMismatch {
                    x_rows: n_samples,
                    y_len: w.nrows(),
                });
            }
            if w.iter().any(|&wi| wi < 0.0) {
                return Err(RegressionError::InvalidWeights);
            }
        }

        // Fit using IRLS
        let (coefficients, intercept) = self.fit_irls(x, y)?;

        // Compute fitted values and residuals
        let mut fitted_values = Col::zeros(n_samples);
        let mut residuals = Col::zeros(n_samples);

        for i in 0..n_samples {
            let mut pred = intercept.unwrap_or(0.0);
            for j in 0..n_features {
                pred += x[(i, j)] * coefficients[j];
            }
            fitted_values[i] = pred;
            residuals[i] = y[i] - pred;
        }

        // Compute pseudo R-squared (Koenker-Machado)
        // R1 = 1 - V(full) / V(null) where V is the quantile loss
        let full_loss = self.check_loss(&residuals);

        // Null model: just the quantile of y
        let y_quantile = self.compute_quantile(y);
        let null_residuals = Col::from_fn(n_samples, |i| y[i] - y_quantile);
        let null_loss = self.check_loss(&null_residuals);

        let pseudo_r_squared = if null_loss > 0.0 {
            1.0 - full_loss / null_loss
        } else {
            0.0
        };

        // Build result
        let n_params = if intercept.is_some() {
            n_features + 1
        } else {
            n_features
        };

        let mut result = RegressionResult::empty(n_features, n_samples);
        result.coefficients = coefficients.clone();
        result.intercept = intercept;
        result.residuals = residuals;
        result.fitted_values = fitted_values;
        result.n_parameters = n_params;
        result.n_observations = n_samples;
        result.r_squared = pseudo_r_squared;
        result.adj_r_squared = f64::NAN; // Not well-defined for quantile regression
        result.aliased = vec![false; n_features];

        Ok(FittedQuantile {
            tau: self.tau,
            with_intercept: self.with_intercept,
            result,
            check_loss: full_loss,
        })
    }
}

impl QuantileRegressor {
    /// Compute the tau-th sample quantile of y.
    fn compute_quantile(&self, y: &Col<f64>) -> f64 {
        let mut sorted: Vec<f64> = y.iter().copied().collect();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let n = sorted.len();
        let index = (self.tau * (n - 1) as f64).floor() as usize;
        let frac = self.tau * (n - 1) as f64 - index as f64;

        if index + 1 < n {
            sorted[index] * (1.0 - frac) + sorted[index + 1] * frac
        } else {
            sorted[index]
        }
    }
}

/// A fitted Quantile Regression model.
#[derive(Debug, Clone)]
pub struct FittedQuantile {
    /// The quantile that was estimated
    tau: f64,
    /// Whether model includes intercept
    #[allow(dead_code)]
    with_intercept: bool,
    /// Regression result
    result: RegressionResult,
    /// Check function loss (quantile loss)
    check_loss: f64,
}

impl FittedQuantile {
    /// Get the quantile (tau) that was estimated.
    pub fn tau(&self) -> f64 {
        self.tau
    }

    /// Get the check function loss (quantile loss).
    pub fn check_loss(&self) -> f64 {
        self.check_loss
    }

    /// Get the pseudo R-squared (Koenker-Machado).
    pub fn pseudo_r_squared(&self) -> f64 {
        self.result.r_squared
    }
}

impl FittedRegressor for FittedQuantile {
    fn predict(&self, x: &Mat<f64>) -> Col<f64> {
        let n_samples = x.nrows();
        let n_features = x.ncols();
        let mut predictions = Col::zeros(n_samples);

        let intercept = self.result.intercept.unwrap_or(0.0);

        for i in 0..n_samples {
            let mut pred = intercept;
            for j in 0..n_features {
                pred += x[(i, j)] * self.result.coefficients[j];
            }
            predictions[i] = pred;
        }

        predictions
    }

    fn result(&self) -> &RegressionResult {
        &self.result
    }

    fn predict_with_interval(
        &self,
        x: &Mat<f64>,
        interval: Option<IntervalType>,
        level: f64,
    ) -> PredictionResult {
        let predictions = self.predict(x);

        match interval {
            None => PredictionResult::point_only(predictions),
            Some(_interval_type) => {
                // Quantile regression intervals require bootstrap or rank-based methods
                // For now, return NaN intervals
                let n = x.nrows();
                let mut lower = Col::zeros(n);
                let mut upper = Col::zeros(n);
                let mut se = Col::zeros(n);
                for i in 0..n {
                    lower[i] = f64::NAN;
                    upper[i] = f64::NAN;
                    se[i] = f64::NAN;
                }
                let _ = level;
                PredictionResult::with_intervals(predictions, lower, upper, se)
            }
        }
    }
}

/// Builder for `QuantileRegressor`.
#[derive(Debug, Clone)]
pub struct QuantileRegressorBuilder {
    tau: f64,
    with_intercept: bool,
    max_iterations: usize,
    tolerance: f64,
    epsilon: f64,
    weights: Option<Col<f64>>,
}

impl Default for QuantileRegressorBuilder {
    fn default() -> Self {
        Self {
            tau: 0.5,
            with_intercept: true,
            max_iterations: 100,
            tolerance: 1e-6,
            epsilon: 1e-6,
            weights: None,
        }
    }
}

impl QuantileRegressorBuilder {
    /// Create a new builder with default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the quantile to estimate.
    ///
    /// Default is 0.5 (median). Must be between 0 and 1.
    pub fn tau(mut self, tau: f64) -> Self {
        self.tau = tau;
        self
    }

    /// Set whether to include an intercept term.
    ///
    /// Default is true.
    pub fn with_intercept(mut self, include: bool) -> Self {
        self.with_intercept = include;
        self
    }

    /// Set the maximum number of IRLS iterations.
    ///
    /// Default is 100.
    pub fn max_iterations(mut self, max_iter: usize) -> Self {
        self.max_iterations = max_iter;
        self
    }

    /// Set the convergence tolerance.
    ///
    /// Default is 1e-6.
    pub fn tolerance(mut self, tol: f64) -> Self {
        self.tolerance = tol;
        self
    }

    /// Set the smoothing parameter for weight computation.
    ///
    /// Default is 1e-6. Smaller values give more exact quantile regression
    /// but may cause numerical instability.
    pub fn epsilon(mut self, eps: f64) -> Self {
        self.epsilon = eps;
        self
    }

    /// Set observation weights.
    pub fn weights(mut self, w: Col<f64>) -> Self {
        self.weights = Some(w);
        self
    }

    /// Build the quantile regressor.
    pub fn build(self) -> QuantileRegressor {
        QuantileRegressor {
            tau: self.tau,
            with_intercept: self.with_intercept,
            max_iterations: self.max_iterations,
            tolerance: self.tolerance,
            epsilon: self.epsilon,
            weights: self.weights,
        }
    }
}

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

    #[test]
    fn test_median_regression() {
        // Simple linear relationship
        let x = Mat::from_fn(50, 1, |i, _| i as f64);
        let y = Col::from_fn(50, |i| 2.0 + 1.5 * i as f64);

        let model = QuantileRegressor::builder().tau(0.5).build();
        let fitted = model.fit(&x, &y).expect("model should fit");

        // Coefficients should be close to [1.5] with intercept ~2.0
        assert!((fitted.intercept().unwrap() - 2.0).abs() < 0.5);
        assert!((fitted.coefficients()[0] - 1.5).abs() < 0.1);
    }

    #[test]
    fn test_different_quantiles() {
        let x = Mat::from_fn(100, 1, |i, _| i as f64);
        let y = Col::from_fn(100, |i| 1.0 + 2.0 * i as f64);

        // Lower quantile
        let model_25 = QuantileRegressor::builder().tau(0.25).build();
        let fitted_25 = model_25.fit(&x, &y).expect("model should fit");

        // Upper quantile
        let model_75 = QuantileRegressor::builder().tau(0.75).build();
        let fitted_75 = model_75.fit(&x, &y).expect("model should fit");

        // Both should have similar slopes (data is homoscedastic)
        assert!((fitted_25.coefficients()[0] - 2.0).abs() < 0.2);
        assert!((fitted_75.coefficients()[0] - 2.0).abs() < 0.2);
    }

    #[test]
    fn test_no_intercept() {
        let x = Mat::from_fn(50, 1, |i, _| (i + 1) as f64);
        let y = Col::from_fn(50, |i| 2.5 * (i + 1) as f64);

        let model = QuantileRegressor::builder()
            .tau(0.5)
            .with_intercept(false)
            .build();
        let fitted = model.fit(&x, &y).expect("model should fit");

        assert!(fitted.intercept().is_none());
        assert!((fitted.coefficients()[0] - 2.5).abs() < 0.1);
    }

    #[test]
    fn test_predict() {
        let x = Mat::from_fn(50, 1, |i, _| i as f64);
        let y = Col::from_fn(50, |i| 1.0 + 2.0 * i as f64);

        let model = QuantileRegressor::builder().tau(0.5).build();
        let fitted = model.fit(&x, &y).expect("model should fit");

        let x_new = Mat::from_fn(5, 1, |i, _| (i + 100) as f64);
        let preds = fitted.predict(&x_new);

        // Check predictions
        for i in 0..5 {
            let expected = 1.0 + 2.0 * (i + 100) as f64;
            assert!((preds[i] - expected).abs() < 1.0);
        }
    }

    #[test]
    fn test_invalid_tau() {
        let x = Mat::from_fn(20, 1, |i, _| i as f64);
        let y = Col::from_fn(20, |i| i as f64);

        let model = QuantileRegressor::builder().tau(1.5).build();
        let result = model.fit(&x, &y);
        assert!(result.is_err());
    }

    #[test]
    fn test_dimension_mismatch() {
        let x = Mat::from_fn(10, 2, |i, j| (i + j) as f64);
        let y = Col::from_fn(5, |i| i as f64);

        let model = QuantileRegressor::builder().build();
        let result = model.fit(&x, &y);
        assert!(result.is_err());
    }
}