inferust 0.1.10

Statistical modeling for Rust — OLS/WLS regression, GLM, survival analysis, ARIMA/VAR, nonparametric tests, and more. A statsmodels-style library.
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
//! Rolling OLS and Recursive OLS (CUSUM).
//!
//! | Model | Description |
//! |---|---|
//! | [`RollingOls`] | Fits OLS inside a sliding window of fixed width, producing per-window coefficient paths. |
//! | [`RecursiveOls`] | Fits OLS incrementally (one observation at a time) using the Sherman-Morrison rank-1 update; computes CUSUM stability test. |
//!
//! # Quick start
//! ```rust
//! use inferust::regression::{RollingOls, RecursiveOls};
//!
//! let x: Vec<Vec<f64>> = (0..20).map(|i| vec![i as f64]).collect();
//! let y: Vec<f64> = (0..20).map(|i| 2.0 * i as f64 + 1.0).collect();
//!
//! // Rolling coefficients
//! let rolling = RollingOls::new(8).fit(&x, &y).unwrap();
//! println!("slopes over time: {:?}", rolling.slopes());
//!
//! // Recursive OLS + CUSUM
//! let rec = RecursiveOls::new().fit(&x, &y).unwrap();
//! rec.print_cusum();
//! ```

use nalgebra::{DMatrix, DVector};

use crate::error::{InferustError, Result};

// ── Rolling OLS ───────────────────────────────────────────────────────────────

/// OLS fitted inside a sliding window of fixed width.
///
/// For each window position the full OLS problem is solved independently,
/// yielding a **path** of coefficients, standard errors, and R² values that
/// reveal structural changes over time.
#[derive(Debug, Clone)]
pub struct RollingOls {
    window: usize,
    add_intercept: bool,
    feature_names: Vec<String>,
}

/// Fitted rolling OLS result.
#[derive(Debug, Clone)]
pub struct RollingOlsResult {
    /// Coefficient matrix: `coefficients[t]` = coefficients estimated in window ending at index `t + window - 1`.
    /// Each inner vec is `[const?, x1, x2, ...]`.
    pub coefficients: Vec<Vec<f64>>,
    /// Standard errors matching `coefficients`.
    pub std_errors: Vec<Vec<f64>>,
    /// R² for each window.
    pub r_squared: Vec<f64>,
    /// Residual MSE for each window.
    pub mse_resid: Vec<f64>,
    /// Window width.
    pub window: usize,
    /// Number of windows (= n − window + 1).
    pub n_windows: usize,
    /// Feature names.
    pub feature_names: Vec<String>,
}

impl RollingOls {
    /// Create a rolling OLS builder with the given window size.
    pub fn new(window: usize) -> Self {
        Self {
            window,
            add_intercept: true,
            feature_names: Vec::new(),
        }
    }

    /// Do not add an intercept column.
    pub fn no_intercept(mut self) -> Self {
        self.add_intercept = false;
        self
    }

    /// Set human-readable predictor names.
    pub fn with_feature_names(mut self, names: Vec<String>) -> Self {
        self.feature_names = names;
        self
    }

    /// Fit rolling OLS.
    pub fn fit(&self, x: &[Vec<f64>], y: &[f64]) -> Result<RollingOlsResult> {
        let n = y.len();
        if x.len() != n {
            return Err(InferustError::DimensionMismatch {
                x_rows: x.len(),
                y_len: n,
            });
        }
        if self.window < 2 {
            return Err(InferustError::InvalidInput(
                "window must be at least 2".into(),
            ));
        }
        if n < self.window {
            return Err(InferustError::InsufficientData {
                needed: self.window,
                got: n,
            });
        }
        let p = x[0].len();
        let ncols = if self.add_intercept { p + 1 } else { p };
        if self.window <= ncols {
            return Err(InferustError::InvalidInput(format!(
                "window ({}) must be larger than number of parameters ({})",
                self.window, ncols
            )));
        }

        let n_windows = n - self.window + 1;
        let mut coefficients = Vec::with_capacity(n_windows);
        let mut std_errors = Vec::with_capacity(n_windows);
        let mut r_squared = Vec::with_capacity(n_windows);
        let mut mse_resid = Vec::with_capacity(n_windows);

        for start in 0..n_windows {
            let end = start + self.window;
            let x_w = &x[start..end];
            let y_w = &y[start..end];

            let x_mat = build_x(x_w, self.window, p, self.add_intercept);
            let y_vec = DVector::from_column_slice(y_w);

            let xtx = x_mat.transpose() * &x_mat;
            let xty = x_mat.transpose() * &y_vec;
            let xtx_inv = match xtx.try_inverse() {
                Some(inv) => inv,
                None => {
                    // Singular window — fill with NaN
                    coefficients.push(vec![f64::NAN; ncols]);
                    std_errors.push(vec![f64::NAN; ncols]);
                    r_squared.push(f64::NAN);
                    mse_resid.push(f64::NAN);
                    continue;
                }
            };
            let beta = &xtx_inv * &xty;
            let fitted: DVector<f64> = &x_mat * &beta;
            let resids: DVector<f64> = &y_vec - &fitted;
            let df = self.window - ncols;
            let sigma2 = resids.dot(&resids) / df as f64;
            let var_beta = &xtx_inv * sigma2;
            let se: Vec<f64> = (0..ncols).map(|j| var_beta[(j, j)].abs().sqrt()).collect();

            let y_mean = y_w.iter().sum::<f64>() / self.window as f64;
            let sst = y_w
                .iter()
                .map(|yi| (yi - y_mean).powi(2))
                .sum::<f64>()
                .max(f64::EPSILON);
            let ssr = resids.dot(&resids);
            let r2 = (1.0 - ssr / sst).max(0.0);

            coefficients.push(beta.iter().copied().collect());
            std_errors.push(se);
            r_squared.push(r2);
            mse_resid.push(sigma2);
        }

        let feat = make_feat_names(&self.feature_names, p, self.add_intercept);
        Ok(RollingOlsResult {
            coefficients,
            std_errors,
            r_squared,
            mse_resid,
            window: self.window,
            n_windows,
            feature_names: feat,
        })
    }
}

impl RollingOlsResult {
    /// Convenience: extract the slope (first non-intercept coefficient) path.
    ///
    /// Returns the second column if an intercept is present, or the first column otherwise.
    pub fn slopes(&self) -> Vec<f64> {
        let col = if self.feature_names.first().is_some_and(|n| n == "const") {
            1
        } else {
            0
        };
        self.coefficients
            .iter()
            .map(|c| c.get(col).copied().unwrap_or(f64::NAN))
            .collect()
    }

    /// Print a brief summary of coefficient paths.
    pub fn print_summary(&self) {
        println!();
        println!(
            "── Rolling OLS (window = {}, {} windows) ──",
            self.window, self.n_windows
        );
        println!("{:>8} {:>12} {:>10}", "Window", "Const/Slope", "");
        println!("{}", "".repeat(32));
        for (i, coefs) in self.coefficients.iter().enumerate() {
            println!(
                "{:>8} {:>12.4} {:>10.4}",
                i + 1,
                coefs.last().copied().unwrap_or(f64::NAN),
                self.r_squared[i]
            );
        }
        println!();
    }
}

// ── Recursive OLS ─────────────────────────────────────────────────────────────

/// Recursive OLS using the Sherman-Morrison rank-1 covariance update.
///
/// Fits OLS progressively from an initial batch of `init_obs` observations, then
/// adds one observation at a time.  At each step it records:
/// - the coefficient vector
/// - the recursive residual (one-step-ahead prediction error, scaled)
/// - the CUSUM statistic (cumulative sum of scaled recursive residuals)
///
/// The **CUSUM test** (Brown, Durbin & Evans 1975) rejects parameter stability
/// if the CUSUM path crosses the 5 % significance boundaries ±c·√(T−k) where
/// c ≈ 0.948.
#[derive(Debug, Clone)]
pub struct RecursiveOls {
    add_intercept: bool,
    feature_names: Vec<String>,
    init_obs: Option<usize>,
}

/// Fitted recursive OLS result.
#[derive(Debug, Clone)]
pub struct RecursiveOlsResult {
    /// Coefficient path: `coefficients[t]` = β estimated after observing the t-th data point.
    /// The first `init_obs - 1` entries contain the initial OLS fit replicated.
    pub coefficients: Vec<Vec<f64>>,
    /// Recursive residuals w_t = (y_t − x_t′β_{t-1}) / sqrt(1 + x_t′P_{t-1}x_t).
    /// NaN for the first `init_obs` observations (used for initialisation).
    pub recursive_residuals: Vec<f64>,
    /// CUSUM statistic path W_t = Σ w_s / σ̂.
    pub cusum: Vec<f64>,
    /// 5 % significance boundaries at each time step: ±boundary[t].
    pub cusum_boundary: Vec<f64>,
    /// Estimated residual standard deviation.
    pub sigma: f64,
    /// Number of observations used for initialisation.
    pub init_obs: usize,
    /// Total observations.
    pub n: usize,
    /// Feature names.
    pub feature_names: Vec<String>,
}

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

impl RecursiveOls {
    /// Create a recursive OLS builder.
    pub fn new() -> Self {
        Self {
            add_intercept: true,
            feature_names: Vec::new(),
            init_obs: None,
        }
    }

    /// Do not add an intercept column.
    pub fn no_intercept(mut self) -> Self {
        self.add_intercept = false;
        self
    }

    /// Override the number of observations used for the initial OLS estimate
    /// (default = number of parameters + 1).
    pub fn init_obs(mut self, k: usize) -> Self {
        self.init_obs = Some(k);
        self
    }

    /// Set human-readable predictor names.
    pub fn with_feature_names(mut self, names: Vec<String>) -> Self {
        self.feature_names = names;
        self
    }

    /// Fit recursive OLS.
    pub fn fit(&self, x: &[Vec<f64>], y: &[f64]) -> Result<RecursiveOlsResult> {
        let n = y.len();
        if x.len() != n {
            return Err(InferustError::DimensionMismatch {
                x_rows: x.len(),
                y_len: n,
            });
        }
        let p = x[0].len();
        let ncols = if self.add_intercept { p + 1 } else { p };
        let k0 = self.init_obs.unwrap_or(ncols + 1).max(ncols + 1);
        if n < k0 + 1 {
            return Err(InferustError::InsufficientData {
                needed: k0 + 1,
                got: n,
            });
        }

        // Initial OLS on first k0 observations
        let x_init = build_x(&x[..k0], k0, p, self.add_intercept);
        let y_init = DVector::from_column_slice(&y[..k0]);
        let xtx = x_init.transpose() * &x_init;
        let xty = x_init.transpose() * &y_init;
        let mut p_mat = xtx.try_inverse().ok_or(InferustError::SingularMatrix)?;
        let mut beta = &p_mat * &xty;

        let fitted_init: DVector<f64> = &x_init * &beta;
        let resid_init: DVector<f64> = &y_init - &fitted_init;
        let sigma2_est = resid_init.dot(&resid_init) / (k0 - ncols).max(1) as f64;
        let sigma = sigma2_est.sqrt().max(f64::EPSILON);

        let mut coef_path: Vec<Vec<f64>> = vec![beta.iter().copied().collect(); k0];
        let mut rec_resids: Vec<f64> = vec![f64::NAN; k0];
        let mut cusum: Vec<f64> = vec![0.0; k0];
        let mut cusum_sum = 0.0_f64;

        // Recursive update for t = k0 .. n-1
        for t in k0..n {
            let xt = DVector::from_fn(ncols, |r, _| {
                if self.add_intercept {
                    if r == 0 {
                        1.0
                    } else {
                        x[t][r - 1]
                    }
                } else {
                    x[t][r]
                }
            });

            // Innovation and scaling
            let innov = y[t] - beta.dot(&xt);
            let ft = 1.0 + xt.dot(&(&p_mat * &xt));
            let w = innov / (sigma * ft.sqrt().max(f64::EPSILON));

            // Sherman-Morrison update
            let k_gain = &p_mat * &xt / ft;
            beta += &k_gain * innov;
            p_mat -= &k_gain * xt.transpose() * &p_mat;

            cusum_sum += w;
            rec_resids.push(w);
            coef_path.push(beta.iter().copied().collect());
            cusum.push(cusum_sum);
        }

        // CUSUM 5% boundaries: ±0.948 * sqrt(T - k) at each step
        let t_total = (n - k0) as f64;
        // Simpler symmetric boundary: ±0.948 * sqrt(n - k0)
        let boundary_val = 0.948 * t_total.sqrt();
        let cusum_boundary: Vec<f64> = (0..n).map(|_| boundary_val).collect();

        let feat = make_feat_names(&self.feature_names, p, self.add_intercept);
        Ok(RecursiveOlsResult {
            coefficients: coef_path,
            recursive_residuals: rec_resids,
            cusum,
            cusum_boundary,
            sigma,
            init_obs: k0,
            n,
            feature_names: feat,
        })
    }
}

impl RecursiveOlsResult {
    /// Return `true` if the CUSUM statistic ever exceeds the 5 % boundary.
    pub fn cusum_reject(&self) -> bool {
        let boundary = self.cusum_boundary.last().copied().unwrap_or(f64::INFINITY);
        self.cusum.iter().any(|&c| c.abs() > boundary)
    }

    /// Print the CUSUM path and stability verdict.
    pub fn print_cusum(&self) {
        println!();
        println!("── Recursive OLS CUSUM Test ─────────────────────────────");
        println!(
            "  n = {}   init_obs = {}   σ̂ = {:.6}",
            self.n, self.init_obs, self.sigma
        );
        println!(
            "  5% boundary = ±{:.4}",
            self.cusum_boundary.last().copied().unwrap_or(0.0)
        );
        println!();
        println!("{:>6}  {:>10}  {:>10}", "t", "CUSUM", "Boundary");
        println!("{}", "".repeat(30));
        for (t, (&c, &b)) in self
            .cusum
            .iter()
            .zip(self.cusum_boundary.iter())
            .enumerate()
        {
            if t < self.init_obs {
                continue;
            }
            let flag = if c.abs() > b { " *** " } else { "" };
            println!("{:>6}  {:>10.4}  {:>10.4}{}", t, c, b, flag);
        }
        let verdict = if self.cusum_reject() {
            "REJECT parameter stability (CUSUM exceeds 5% boundary)"
        } else {
            "Fail to reject parameter stability"
        };
        println!("  Verdict: {verdict}");
        println!();
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn build_x(x: &[Vec<f64>], n: usize, p: usize, add_intercept: bool) -> DMatrix<f64> {
    let ncols = if add_intercept { p + 1 } else { p };
    DMatrix::from_fn(n, ncols, |r, c| {
        if add_intercept {
            if c == 0 {
                1.0
            } else {
                x[r][c - 1]
            }
        } else {
            x[r][c]
        }
    })
}

fn make_feat_names(names: &[String], p: usize, add_intercept: bool) -> Vec<String> {
    let ncols = if add_intercept { p + 1 } else { p };
    let mut out = Vec::with_capacity(ncols);
    if add_intercept {
        out.push("const".to_string());
    }
    if names.len() == p {
        out.extend_from_slice(names);
    } else {
        for i in 1..=p {
            out.push(format!("x{i}"));
        }
    }
    out
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::{RecursiveOls, RollingOls};

    #[test]
    fn rolling_ols_correct_window_count() {
        let x: Vec<Vec<f64>> = (0..20).map(|i| vec![i as f64]).collect();
        let y: Vec<f64> = (0..20).map(|i| 2.0 * i as f64 + 1.0).collect();
        let res = RollingOls::new(8).fit(&x, &y).unwrap();
        assert_eq!(res.n_windows, 13); // 20 - 8 + 1
        assert_eq!(res.coefficients.len(), 13);
    }

    #[test]
    fn rolling_ols_stable_slope_for_linear_data() {
        let x: Vec<Vec<f64>> = (0..30).map(|i| vec![i as f64]).collect();
        let y: Vec<f64> = (0..30).map(|i| 3.0 * i as f64 + 5.0).collect();
        let res = RollingOls::new(10).fit(&x, &y).unwrap();
        for slope in res.slopes() {
            assert!((slope - 3.0).abs() < 0.01, "slope = {slope:.4}");
        }
    }

    #[test]
    fn recursive_ols_coefficient_length() {
        let x: Vec<Vec<f64>> = (0..25).map(|i| vec![i as f64]).collect();
        let y: Vec<f64> = (0..25).map(|i| 2.0 * i as f64 + 1.0).collect();
        let res = RecursiveOls::new().fit(&x, &y).unwrap();
        assert_eq!(res.coefficients.len(), 25);
        assert_eq!(res.cusum.len(), 25);
    }

    #[test]
    fn recursive_ols_stable_linear_data_passes_cusum() {
        let x: Vec<Vec<f64>> = (0..40).map(|i| vec![i as f64]).collect();
        let y: Vec<f64> = (0..40).map(|i| 2.5 * i as f64 + 0.5).collect();
        let res = RecursiveOls::new().fit(&x, &y).unwrap();
        assert!(!res.cusum_reject(), "stable linear data should pass CUSUM");
    }
}