oxictl 0.1.1

Pure Rust Real-Time Control Systems Framework
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
//! ARX (AutoRegressive with eXogenous input) model identification.
// Suppress doc-formatting and numerical-loop lints that are intentional in this
// signal-processing module.
#![allow(
    clippy::needless_range_loop,
    clippy::doc_overindented_list_items,
    clippy::doc_lazy_continuation,
    clippy::manual_memcpy
)]
//!
//! The ARX model in shift-operator form is:
//! ```text
//!   A(q)·y(t) = B(q)·u(t) + e(t)
//! ```
//! where
//! ```text
//!   A(q) = 1 + a₁q⁻¹ + … + aₙₐq⁻ⁿᵃ
//!   B(q) = b₀q⁻ⁿᵏ + b₁q⁻⁽ⁿᵏ⁺¹⁾ + … + bₙᵦ₋₁q⁻⁽ⁿᵏ⁺ⁿᵇ⁻¹⁾
//! ```
//!
//! Identification is performed via Ordinary Least Squares (batch) or
//! Recursive Least Squares with forgetting factor (online).
//!
//! # Const parameters
//! - `NA`  — number of AR coefficients (order of A polynomial, excluding leading 1).
//! - `NB`  — number of exogenous (B) coefficients.
//! - `P`   — total regressor dimension, **must equal NA + NB**. This is required because
//!           Rust's stable const generics do not yet support const arithmetic on type
//!           parameters in array-size positions.
//! - `NK`  — dead-time (input delay) in samples (baked into regressor construction).
//!           Used as a const parameter only for `ArxIdentifier` and `RecursiveArx`.

use crate::core::scalar::ControlScalar;
use crate::sysid::SysIdError;

// ── Helper: Cholesky solve for symmetric positive-definite system ─────────────

/// Solve the symmetric positive-definite linear system A·x = b in place.
///
/// `a` is the N×N SPD matrix (modified in-place; upper triangle written with L).
/// `b` is the right-hand side (modified in-place to become the solution).
/// Returns `Err(SysIdError::SingularMatrix)` if a diagonal element is ≤ 0 during
/// the Cholesky factorisation (indicating the matrix is not SPD).
pub(crate) fn cholesky_solve_n<S: ControlScalar, const N: usize>(
    a: &mut [[S; N]; N],
    b: &mut [S; N],
) -> Result<(), SysIdError> {
    // Cholesky factorisation: A = L·Lᵀ  (L stored in lower triangle of a)
    for i in 0..N {
        for j in 0..=i {
            let mut s = a[i][j];
            for k in 0..j {
                s -= a[i][k] * a[j][k];
            }
            if i == j {
                if s <= S::ZERO {
                    return Err(SysIdError::SingularMatrix);
                }
                a[i][j] = s.sqrt();
            } else {
                let diag = a[j][j];
                if diag == S::ZERO {
                    return Err(SysIdError::SingularMatrix);
                }
                a[i][j] = s / diag;
            }
        }
    }

    // Forward substitution: L·z = b
    for i in 0..N {
        let mut s = b[i];
        for k in 0..i {
            s -= a[i][k] * b[k];
        }
        let diag = a[i][i];
        if diag == S::ZERO {
            return Err(SysIdError::SingularMatrix);
        }
        b[i] = s / diag;
    }

    // Back substitution: Lᵀ·x = z
    let mut i = N;
    while i > 0 {
        i -= 1;
        let mut s = b[i];
        for k in (i + 1)..N {
            s -= a[k][i] * b[k];
        }
        let diag = a[i][i];
        if diag == S::ZERO {
            return Err(SysIdError::SingularMatrix);
        }
        b[i] = s / diag;
    }

    Ok(())
}

// ── ArxModel ─────────────────────────────────────────────────────────────────

/// Identified ARX model.
///
/// Holds the AR coefficients `a[0..NA]` (denominator, excluding leading 1) and
/// the exogenous coefficients `b[0..NB]` (numerator).
///
/// The one-step-ahead prediction is:
/// ```text
///   ŷ(t) = −a₀·y(t-1) − … − aₙₐ₋₁·y(t-NA)
///         + b₀·u(t-NK) + … + bₙᵦ₋₁·u(t-NK-NB+1)
/// ```
#[derive(Debug, Clone, Copy)]
pub struct ArxModel<S: ControlScalar, const NA: usize, const NB: usize> {
    /// AR coefficients a₁ … aₙₐ  (stored raw: the LS solution θ[0..NA]).
    ///
    /// The regressor φ[i] = -y(t-1-i), so the prediction is:
    ///   ŷ = Σ a[i]·φ[i] = -Σ a[i]·y(t-1-i)
    /// i.e. the system pole polynomial is A(q) = 1 + a[0]q⁻¹ + … + a[NA-1]q⁻ᴺᴬ
    pub a: [S; NA],
    /// Exogenous coefficients b₀ … bₙᵦ₋₁.
    pub b: [S; NB],
}

impl<S: ControlScalar, const NA: usize, const NB: usize> ArxModel<S, NA, NB> {
    /// Create a zero-initialised model.
    pub fn zeros() -> Self {
        Self {
            a: [S::ZERO; NA],
            b: [S::ZERO; NB],
        }
    }

    /// One-step-ahead prediction.
    ///
    /// `y_history` — most recent `NA` output samples, **newest first**:
    ///   `y_history[0] = y(t-1)`, `y_history[1] = y(t-2)`, …
    ///
    /// `u_history` — most recent `NB` input samples **starting at the dead-time
    ///   offset**, newest first:
    ///   `u_history[0] = u(t-NK)`, `u_history[1] = u(t-NK-1)`, …
    pub fn predict(&self, y_history: &[S; NA], u_history: &[S; NB]) -> S {
        let mut y_hat = S::ZERO;
        for i in 0..NA {
            y_hat -= self.a[i] * y_history[i];
        }
        for i in 0..NB {
            y_hat += self.b[i] * u_history[i];
        }
        y_hat
    }

    /// Simulate the model over an input sequence of length `M`, returning
    /// predicted outputs.
    ///
    /// `u_seq` — input sequence.  The layout expected is:
    ///   - Index 0 … NB-2: pre-sequence input history (oldest first if NK > 0).
    ///   - Index NB-1 … NB-1+M-1: the M actual inputs to simulate.
    ///   Minimum length: `max(NB, 1) + M - 1`.  For NB=0 length must be ≥ M.
    ///
    /// `y0` — most recent `NA` historical outputs, newest first.
    ///
    /// Returns `Err(InsufficientData)` if `u_seq` is too short.
    pub fn simulate<const M: usize>(
        &self,
        u_seq: &[S],
        y0: &[S; NA],
    ) -> Result<[S; M], SysIdError> {
        let needed = if NB > 0 { NB - 1 + M } else { M };
        if u_seq.len() < needed {
            return Err(SysIdError::InsufficientData);
        }

        let mut y_buf = *y0;
        let mut out = [S::ZERO; M];

        for step in 0..M {
            let base = if NB > 0 { NB - 1 } else { 0 };
            let mut u_hist = [S::ZERO; NB];
            for j in 0..NB {
                let idx = base + step;
                if idx >= j {
                    u_hist[j] = u_seq[idx - j];
                }
            }

            let y_new = self.predict(&y_buf, &u_hist);
            out[step] = y_new;

            // Shift y_buf newest-first
            let mut j = NA;
            while j > 1 {
                y_buf[j - 1] = y_buf[j - 2];
                j -= 1;
            }
            if NA > 0 {
                y_buf[0] = y_new;
            }
        }

        Ok(out)
    }
}

// ── ArxIdentifier (batch LS) ──────────────────────────────────────────────────

/// Batch ARX identifier using Ordinary Least Squares.
///
/// Builds the regression matrix Φ from the provided data and solves
/// θ = (ΦᵀΦ)⁻¹Φᵀy via Cholesky decomposition.
///
/// # Const parameters
/// - `NA` — AR order.
/// - `NB` — exogenous order.
/// - `P`  — regressor dimension = NA + NB  (**caller must ensure P == NA + NB**).
/// - `NK` — dead time (samples of input delay ≥ 0).
pub struct ArxIdentifier<
    S: ControlScalar,
    const NA: usize,
    const NB: usize,
    const P: usize,
    const NK: usize,
> {
    _phantom: core::marker::PhantomData<S>,
}

impl<S: ControlScalar, const NA: usize, const NB: usize, const P: usize, const NK: usize>
    ArxIdentifier<S, NA, NB, P, NK>
{
    /// Identify an ARX model from output sequence `y` and input sequence `u`.
    ///
    /// Both slices must have equal length ≥ `NA + NK + NB`.
    /// Returns `Err(InsufficientData)` if there are not enough samples.
    pub fn fit(y: &[S], u: &[S]) -> Result<ArxModel<S, NA, NB>, SysIdError> {
        let n = y.len();
        if u.len() != n {
            return Err(SysIdError::InvalidData);
        }
        for &v in y.iter().chain(u.iter()) {
            if !v.is_finite() {
                return Err(SysIdError::InvalidData);
            }
        }

        let min_start = if NK + NB > NA { NK + NB } else { NA };
        if n <= min_start {
            return Err(SysIdError::InsufficientData);
        }

        if P == 0 {
            return Ok(ArxModel::zeros());
        }

        let mut ata = [[S::ZERO; P]; P];
        let mut aty = [S::ZERO; P];

        for t in min_start..n {
            let mut phi = [S::ZERO; P];
            for i in 0..NA {
                phi[i] = -y[t - 1 - i];
            }
            for i in 0..NB {
                let delay = NK + i;
                if delay <= t {
                    phi[NA + i] = u[t - delay];
                }
            }
            let yt = y[t];

            for i in 0..P {
                for j in 0..P {
                    ata[i][j] += phi[i] * phi[j];
                }
                aty[i] += phi[i] * yt;
            }
        }

        cholesky_solve_n::<S, P>(&mut ata, &mut aty)?;

        let mut model = ArxModel::zeros();
        for i in 0..NA {
            model.a[i] = aty[i];
        }
        for i in 0..NB {
            model.b[i] = aty[NA + i];
        }
        Ok(model)
    }
}

// ── RecursiveArx (online RLS) ─────────────────────────────────────────────────

/// Online ARX identifier using Recursive Least Squares with forgetting factor.
///
/// # Const parameters
/// - `NA`  — AR order.
/// - `NB`  — exogenous order.
/// - `P`   — regressor dimension = NA + NB (**caller must ensure P == NA + NB**).
/// - `NK`  — dead time (samples of input delay ≥ 0).
/// - `UBN` — input buffer size = NK + NB (**caller must ensure UBN == NK + NB**).
#[derive(Debug, Clone)]
pub struct RecursiveArx<
    S: ControlScalar,
    const NA: usize,
    const NB: usize,
    const P: usize,
    const NK: usize,
    const UBN: usize,
> {
    theta: [S; P],
    cov: [[S; P]; P],
    lambda: S,
    y_buf: [S; NA],
    u_buf: [S; UBN],
    steps: u32,
}

impl<
        S: ControlScalar,
        const NA: usize,
        const NB: usize,
        const P: usize,
        const NK: usize,
        const UBN: usize,
    > RecursiveArx<S, NA, NB, P, NK, UBN>
{
    /// Construct a new online ARX identifier.
    ///
    /// # Arguments
    /// * `lambda` — forgetting factor ∈ (0, 1].
    /// * `p_init` — initial diagonal value of the covariance matrix.
    pub fn new(lambda: S, p_init: S) -> Self {
        let mut cov = [[S::ZERO; P]; P];
        for i in 0..P {
            cov[i][i] = p_init;
        }
        Self {
            theta: [S::ZERO; P],
            cov,
            lambda,
            y_buf: [S::ZERO; NA],
            u_buf: [S::ZERO; UBN],
            steps: 0,
        }
    }

    /// Process a new output/input observation pair.
    pub fn update(&mut self, y_new: S, u_new: S) -> Result<(), SysIdError> {
        if !y_new.is_finite() || !u_new.is_finite() {
            return Err(SysIdError::InvalidData);
        }

        // Shift input buffer newest-first
        {
            let mut i = UBN;
            while i > 1 {
                self.u_buf[i - 1] = self.u_buf[i - 2];
                i -= 1;
            }
            if UBN > 0 {
                self.u_buf[0] = u_new;
            }
        }

        // Build regressor φ = [-y(t-1), …, -y(t-NA), u(t-NK), …, u(t-NK-NB+1)]
        let mut phi = [S::ZERO; P];
        for i in 0..NA {
            phi[i] = -self.y_buf[i];
        }
        for i in 0..NB {
            let delay = NK + i;
            if delay < UBN {
                phi[NA + i] = self.u_buf[delay];
            }
        }

        // P·φ
        let mut p_phi = [S::ZERO; P];
        for i in 0..P {
            for j in 0..P {
                p_phi[i] += self.cov[i][j] * phi[j];
            }
        }

        // φᵀ·P·φ
        let mut phi_t_p_phi = S::ZERO;
        for i in 0..P {
            phi_t_p_phi += phi[i] * p_phi[i];
        }

        let denom = self.lambda + phi_t_p_phi;
        if denom == S::ZERO {
            return Err(SysIdError::SingularMatrix);
        }

        // Gain k
        let mut k = [S::ZERO; P];
        for i in 0..P {
            k[i] = p_phi[i] / denom;
        }

        // Innovation
        let mut y_hat = S::ZERO;
        for i in 0..P {
            y_hat += phi[i] * self.theta[i];
        }
        let innovation = y_new - y_hat;

        // θ update
        for i in 0..P {
            self.theta[i] += k[i] * innovation;
        }

        // P update
        let mut phi_t_p = [S::ZERO; P];
        for j in 0..P {
            for l in 0..P {
                phi_t_p[j] += phi[l] * self.cov[l][j];
            }
        }
        let lambda_inv = S::ONE / self.lambda;
        for i in 0..P {
            for j in 0..P {
                self.cov[i][j] = (self.cov[i][j] - k[i] * phi_t_p[j]) * lambda_inv;
            }
        }

        // Shift output buffer newest-first
        {
            let mut i = NA;
            while i > 1 {
                self.y_buf[i - 1] = self.y_buf[i - 2];
                i -= 1;
            }
            if NA > 0 {
                self.y_buf[0] = y_new;
            }
        }

        self.steps += 1;
        Ok(())
    }

    /// Return the current ARX model estimate.
    pub fn model(&self) -> ArxModel<S, NA, NB> {
        let mut model = ArxModel::zeros();
        for i in 0..NA {
            model.a[i] = self.theta[i];
        }
        for i in 0..NB {
            model.b[i] = self.theta[NA + i];
        }
        model
    }

    /// Number of samples processed so far.
    pub fn steps(&self) -> u32 {
        self.steps
    }
}

// ── fit_percent ───────────────────────────────────────────────────────────────

/// Compute the MATLAB-style FIT% metric.
///
/// ```text
///   FIT% = 100 · (1 − ‖y − ŷ‖₂ / ‖y − mean(y)‖₂)
/// ```
///
/// Returns `S::ZERO` if the denominator is zero (constant signal).
pub fn fit_percent<S: ControlScalar>(model_output: &[S], actual: &[S]) -> S {
    let n = model_output.len().min(actual.len());
    if n == 0 {
        return S::ZERO;
    }

    let mut sum = S::ZERO;
    for i in 0..n {
        sum += actual[i];
    }
    let mean = sum / S::from_f64(n as f64);

    let mut num_sq = S::ZERO;
    let mut den_sq = S::ZERO;
    for i in 0..n {
        let e = actual[i] - model_output[i];
        num_sq += e * e;
        let d = actual[i] - mean;
        den_sq += d * d;
    }

    if den_sq == S::ZERO {
        return S::ZERO;
    }

    let ratio = (num_sq / den_sq).sqrt();
    S::from_f64(100.0) * (S::ONE - ratio)
}

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

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

    /// Generate data from a known first-order ARX system (noiseless):
    ///   y(t) = −a₁·y(t−1) + b₀·u(t−1)
    ///   true: A polynomial: 1 − 0.7q⁻¹  (pole at 0.7), b₀ = 0.3
    fn generate_arx1(n: usize) -> (heapless::Vec<f64, 4096>, heapless::Vec<f64, 4096>) {
        // True model: y(t) = 0.7·y(t-1) + 0.3·u(t-1)
        // In ARX form: a[0] stores coefficient such that ŷ = -a[0]·y(t-1) + b[0]·u(t-1)
        // i.e. -a[0] = 0.7  →  a[0] = -0.7 (the raw LS coefficient on -y(t-1))
        let a_true = 0.7_f64;
        let b_true = 0.3_f64;
        let mut y: heapless::Vec<f64, 4096> = heapless::Vec::new();
        let mut u: heapless::Vec<f64, 4096> = heapless::Vec::new();
        let _ = y.push(0.0);
        let _ = u.push(0.0);
        for t in 1..n {
            let ut = libm::sin(0.1 * t as f64) + libm::cos(0.07 * t as f64) * 0.5;
            let yt = a_true * y[t - 1] + b_true * u[t - 1];
            let _ = y.push(yt);
            let _ = u.push(ut);
        }
        (y, u)
    }

    #[test]
    fn batch_arx_identifies_first_order() {
        let (y, u) = generate_arx1(800);
        // NA=1, NB=1, P=2, NK=1
        let model = ArxIdentifier::<f64, 1, 1, 2, 1>::fit(y.as_slice(), u.as_slice())
            .expect("batch ARX fit should succeed");

        let mut predicted: heapless::Vec<f64, 4096> = heapless::Vec::new();
        for t in 1..y.len() {
            let y_hist = [y[t - 1]];
            let u_hist = [u[t - 1]];
            let _ = predicted.push(model.predict(&y_hist, &u_hist));
        }
        let fp = fit_percent(predicted.as_slice(), &y.as_slice()[1..]);
        assert!(
            fp > 99.0,
            "FIT% should exceed 99% for noiseless ARX-1, got {fp:.2}%"
        );
    }

    #[test]
    fn recursive_arx_identifies_first_order() {
        let (y, u) = generate_arx1(2000);
        // NA=1, NB=1, P=2, NK=1, UBN=NK+NB=2
        let mut rls = RecursiveArx::<f64, 1, 1, 2, 1, 2>::new(0.99, 1e4);
        for t in 1..y.len() {
            rls.update(y[t], u[t]).expect("update should not error");
        }
        let model = rls.model();

        let mut predicted: heapless::Vec<f64, 4096> = heapless::Vec::new();
        for t in 1..y.len() {
            let y_hist = [y[t - 1]];
            let u_hist = [u[t - 1]];
            let _ = predicted.push(model.predict(&y_hist, &u_hist));
        }
        let fp = fit_percent(predicted.as_slice(), &y.as_slice()[1..]);
        assert!(
            fp > 99.0,
            "RLS ARX FIT% should exceed 99% for noiseless ARX-1, got {fp:.2}%"
        );
    }

    #[test]
    fn fit_percent_perfect_prediction() {
        let y: [f64; 5] = [1.0, 2.0, 3.0, 4.0, 5.0];
        let fp = fit_percent(&y, &y);
        assert!(
            (fp - 100.0).abs() < 1e-9,
            "FIT% of perfect prediction should be 100%"
        );
    }

    #[test]
    fn fit_percent_zero_for_mean_prediction() {
        let y: [f64; 5] = [1.0, 2.0, 3.0, 4.0, 5.0];
        let mean = [3.0_f64; 5];
        let fp = fit_percent(&mean, &y);
        assert!(fp <= 1e-9, "FIT% of mean-level prediction should be ≤ 0");
    }

    #[test]
    fn cholesky_solve_2x2() {
        // Solve [4, 2; 2, 3]·x = [8; 7]
        // det = 4·3 - 2·2 = 8
        // x[0] = (3·8 - 2·7) / 8 = (24 - 14) / 8 = 10/8 = 1.25
        // x[1] = (4·7 - 2·8) / 8 = (28 - 16) / 8 = 12/8 = 1.5
        let mut a = [[4.0_f64, 2.0], [2.0, 3.0]];
        let mut b = [8.0_f64, 7.0];
        cholesky_solve_n::<f64, 2>(&mut a, &mut b).expect("should succeed");
        assert!((b[0] - 1.25).abs() < 1e-10, "x[0]={}", b[0]);
        assert!((b[1] - 1.5).abs() < 1e-10, "x[1]={}", b[1]);
    }

    #[test]
    fn simulate_produces_finite_output() {
        let model: ArxModel<f64, 1, 1> = ArxModel {
            a: [-0.7],
            b: [0.3],
        };
        let mut u_seq = [0.0_f64; 101];
        for i in 1..=100 {
            u_seq[i] = libm::sin(0.1 * i as f64);
        }
        let y0 = [0.0_f64];
        let out: [f64; 100] = model
            .simulate(&u_seq, &y0)
            .expect("simulate should succeed");
        for i in 0..100 {
            assert!(out[i].is_finite(), "simulate output[{i}] is not finite");
        }
    }
}