taguchi 0.2.0

State-of-the-art orthogonal array (Taguchi) library for experimental design
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
//! Orthogonal array core types and operations.
//!
//! This module provides the fundamental data structures for representing
//! and manipulating orthogonal arrays.
//!
//! ## Overview
//!
//! - [`OA`]: The main orthogonal array type
//! - [`OAParams`]: Parameters describing an orthogonal array
//!
//! ## Notation
//!
//! An orthogonal array OA(N, k, s, t) has:
//! - N rows (runs/experiments)
//! - k columns (factors/parameters)
//! - s levels (symbols 0, 1, ..., s-1)
//! - strength t (balance property)
//!
//! The defining property is that every N×t subarray contains each possible
//! t-tuple exactly λ = N/s^t times.

mod stats;
mod verify;

pub use stats::BalanceReport;
pub use verify::{VerificationResult, compute_strength, verify_strength};

use ndarray::Array2;
use std::collections::HashMap;
use std::fmt;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

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

/// Parameters describing an orthogonal array.
///
/// This struct encapsulates the mathematical parameters of an OA and
/// provides validation to ensure consistency.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct OAParams {
    /// Number of runs (rows).
    pub runs: usize,
    /// Number of factors (columns).
    pub factors: usize,
    /// Number of levels for each factor.
    pub levels: Vec<u32>,
    /// Strength (orthogonality degree).
    pub strength: u32,
}

impl OAParams {
    /// Create new symmetric OA parameters with automatic validation.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - N is not divisible by s^t
    /// - strength exceeds factors
    /// - levels is 0 or 1
    pub fn new(runs: usize, factors: usize, levels: u32, strength: u32) -> Result<Self> {
        if levels < 2 {
            return Err(Error::invalid_params("levels must be at least 2"));
        }

        if strength as usize > factors {
            return Err(Error::invalid_params(format!(
                "strength {} cannot exceed factors {}",
                strength, factors
            )));
        }

        let s_to_t = (levels as usize)
            .checked_pow(strength)
            .ok_or_else(|| Error::invalid_params(format!("{}^{} overflows", levels, strength)))?;

        if runs % s_to_t != 0 {
            return Err(Error::invalid_params(format!(
                "runs {} must be divisible by levels^strength = {}^{} = {}",
                runs, levels, strength, s_to_t
            )));
        }

        Ok(Self {
            runs,
            factors,
            levels: vec![levels; factors],
            strength,
        })
    }

    /// Create new mixed-level OA parameters.
    pub fn new_mixed(runs: usize, levels: Vec<u32>, strength: u32) -> Result<Self> {
        let factors = levels.len();
        if factors == 0 {
            return Err(Error::invalid_params("factors must be at least 1"));
        }

        for (i, &s) in levels.iter().enumerate() {
            if s < 2 {
                return Err(Error::invalid_params(format!(
                    "levels for factor {} must be at least 2, got {}",
                    i, s
                )));
            }
        }

        if strength as usize > factors {
            return Err(Error::invalid_params(format!(
                "strength {} cannot exceed factors {}",
                strength, factors
            )));
        }

        Ok(Self {
            runs,
            factors,
            levels,
            strength,
        })
    }

    /// Check if the array is symmetric (all factors have same number of levels).
    #[must_use]
    pub fn is_symmetric(&self) -> bool {
        if self.levels.is_empty() {
            return true;
        }
        let s0 = self.levels[0];
        self.levels.iter().all(|&s| s == s0)
    }

    /// Get the common number of levels if symmetric.
    ///
    /// # Panics
    ///
    /// Panics if the array is not symmetric or has no factors.
    #[must_use]
    pub fn symmetric_levels(&self) -> u32 {
        assert!(self.is_symmetric(), "OA is not symmetric");
        self.levels[0]
    }

    /// Get the index (lambda) for symmetric OAs.
    ///
    /// For mixed-level OAs, this is not a single value.
    #[must_use]
    pub fn index(&self) -> usize {
        if !self.is_symmetric() || self.levels.is_empty() {
            return 0; // Not well-defined for mixed-level
        }
        let s = self.levels[0] as usize;
        let s_to_t = s.pow(self.strength);
        self.runs / s_to_t
    }

    /// Validate that these parameters are internally consistent.
    pub fn validate(&self) -> Result<()> {
        if self.factors != self.levels.len() {
            return Err(Error::invalid_params("factors must match levels length"));
        }

        if self.strength as usize > self.factors {
            return Err(Error::invalid_params("strength cannot exceed factors"));
        }

        // For mixed level, divisibility check is more complex.
        // For any t columns, runs must be divisible by product of their levels.
        // We only check a few small combinations if k is large, or all if k is small.
        // For now, let's at least check that runs is divisible by each individual level.
        for (i, &s) in self.levels.iter().enumerate() {
            if s < 2 {
                return Err(Error::invalid_params(format!(
                    "level for factor {} is < 2",
                    i
                )));
            }
            if self.runs % (s as usize) != 0 {
                return Err(Error::invalid_params(format!(
                    "runs {} must be divisible by level {} of factor {}",
                    self.runs, s, i
                )));
            }
        }

        Ok(())
    }
}

impl fmt::Display for OAParams {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_symmetric() {
            write!(
                f,
                "OA({}, {}, {}, {})",
                self.runs, self.factors, self.levels[0], self.strength
            )
        } else {
            let mut levels_map: HashMap<u32, usize> = HashMap::new();
            for &s in &self.levels {
                *levels_map.entry(s).or_insert(0) += 1;
            }
            let mut levels_sorted: Vec<_> = levels_map.into_iter().collect();
            levels_sorted.sort_by_key(|&(s, _)| s);

            let levels_str: Vec<String> = levels_sorted
                .into_iter()
                .map(|(s, k)| {
                    if k == 1 {
                        s.to_string()
                    } else {
                        format!("{}^{}", s, k)
                    }
                })
                .collect();

            write!(
                f,
                "OA({}, {}, {}, {})",
                self.runs,
                levels_str.join(" "),
                self.strength,
                self.runs // Placeholder for index? No, just omit it for mixed
            )
        }
    }
}

/// An orthogonal array.
///
/// This is the main data structure representing an orthogonal array.
/// The array data is stored as a 2D matrix of integers where each
/// column j has elements in the range `[0, levels[j])`.
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct OA {
    /// The array data, shape (runs, factors).
    data: Array2<u32>,
    /// Array parameters.
    params: OAParams,
}

impl OA {
    /// Create a new orthogonal array from data and parameters.
    ///
    /// # Panics
    ///
    /// Panics if the data dimensions don't match the parameters.
    #[must_use]
    pub fn new(data: Array2<u32>, params: OAParams) -> Self {
        assert_eq!(
            data.nrows(),
            params.runs,
            "data rows {} must match params.runs {}",
            data.nrows(),
            params.runs
        );
        assert_eq!(
            data.ncols(),
            params.factors,
            "data cols {} must match params.factors {}",
            data.ncols(),
            params.factors
        );

        Self { data, params }
    }

    /// Create a new orthogonal array, validating data dimensions.
    ///
    /// # Errors
    ///
    /// Returns an error if the data dimensions don't match the parameters.
    pub fn try_new(data: Array2<u32>, params: OAParams) -> Result<Self> {
        if data.nrows() != params.runs {
            return Err(Error::DimensionMismatch {
                expected: format!("{} rows", params.runs),
                actual: format!("{} rows", data.nrows()),
            });
        }
        if data.ncols() != params.factors {
            return Err(Error::DimensionMismatch {
                expected: format!("{} columns", params.factors),
                actual: format!("{} columns", data.ncols()),
            });
        }

        Ok(Self { data, params })
    }

    /// Get the number of runs (rows).
    #[must_use]
    pub fn runs(&self) -> usize {
        self.params.runs
    }

    /// Get the number of factors (columns).
    #[must_use]
    pub fn factors(&self) -> usize {
        self.params.factors
    }

    /// Get the common number of levels for a symmetric OA.
    ///
    /// # Panics
    ///
    /// Panics if the OA is mixed-level and has no factors.
    #[must_use]
    pub fn levels(&self) -> u32 {
        self.params.levels[0]
    }

    /// Get the levels for all factors.
    #[must_use]
    pub fn levels_vec(&self) -> &[u32] {
        &self.params.levels
    }

    /// Get the number of levels for a specific factor.
    #[must_use]
    pub fn levels_for(&self, factor: usize) -> u32 {
        self.params.levels[factor]
    }

    /// Get the common number of levels for a symmetric OA.
    ///
    /// # Panics
    ///
    /// Panics if the OA is mixed-level.
    #[must_use]
    pub fn symmetric_levels(&self) -> u32 {
        self.params.symmetric_levels()
    }

    /// Get the strength.
    #[must_use]
    pub fn strength(&self) -> u32 {
        self.params.strength
    }

    /// Get the index (lambda) if symmetric.
    #[must_use]
    pub fn index(&self) -> usize {
        self.params.index()
    }

    /// Get the parameters.
    #[must_use]
    pub fn params(&self) -> &OAParams {
        &self.params
    }

    /// Get a reference to the underlying data.
    #[must_use]
    pub fn data(&self) -> &Array2<u32> {
        &self.data
    }

    /// Get a mutable reference to the underlying data.
    pub fn data_mut(&mut self) -> &mut Array2<u32> {
        &mut self.data
    }

    /// Consume the OA and return the underlying data.
    #[must_use]
    pub fn into_data(self) -> Array2<u32> {
        self.data
    }

    /// Get the value at a specific position.
    ///
    /// # Panics
    ///
    /// Panics if the indices are out of bounds.
    #[must_use]
    pub fn get(&self, row: usize, col: usize) -> u32 {
        self.data[[row, col]]
    }

    /// Get a row of the array as a slice.
    #[must_use]
    pub fn row(&self, idx: usize) -> ndarray::ArrayView1<'_, u32> {
        self.data.row(idx)
    }

    /// Get a column of the array as a slice.
    #[must_use]
    pub fn column(&self, idx: usize) -> ndarray::ArrayView1<'_, u32> {
        self.data.column(idx)
    }

    /// Iterate over rows.
    pub fn rows(&self) -> impl Iterator<Item = ndarray::ArrayView1<'_, u32>> {
        self.data.rows().into_iter()
    }

    /// Select a subset of columns, returning a new OA.
    ///
    /// # Panics
    ///
    /// Panics if any column index is out of bounds.
    #[must_use]
    pub fn select_columns(&self, cols: &[usize]) -> Self {
        let new_factors = cols.len();
        let mut new_data = Array2::zeros((self.runs(), new_factors));
        let mut new_levels = Vec::with_capacity(new_factors);

        for (new_col, &old_col) in cols.iter().enumerate() {
            new_levels.push(self.params.levels[old_col]);
            for row in 0..self.runs() {
                new_data[[row, new_col]] = self.data[[row, old_col]];
            }
        }

        let new_params = OAParams {
            runs: self.runs(),
            factors: new_factors,
            levels: new_levels,
            strength: self.strength().min(new_factors as u32),
        };

        Self::new(new_data, new_params)
    }

    /// Verify that this array has the claimed strength.
    ///
    /// # Errors
    ///
    /// Returns an error if verification fails or finds issues.
    pub fn verify(&self) -> Result<VerificationResult> {
        verify_strength(self, self.strength())
    }

    /// Check if all values are in the valid range [0, levels).
    #[must_use]
    pub fn values_in_range(&self) -> bool {
        for col in 0..self.factors() {
            let s = self.params.levels[col];
            for row in 0..self.runs() {
                if self.data[[row, col]] >= s {
                    return false;
                }
            }
        }
        true
    }

    /// Collapse the levels of a specific factor.
    ///
    /// This reduces the number of levels for a factor by mapping the original
    /// levels `v` to `v % new_levels`.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - `new_levels` is less than 2
    /// - `new_levels` does not divide the original number of levels
    ///
    /// # Example
    ///
    /// ```
    /// # use taguchi::OABuilder;
    /// let oa = OABuilder::new().levels(4).factors(3).build().unwrap();
    /// let mixed = oa.collapse_levels(0, 2).unwrap();
    /// assert_eq!(mixed.levels_for(0), 2);
    /// assert_eq!(mixed.levels_for(1), 4);
    /// ```
    pub fn collapse_levels(&self, factor: usize, new_levels: u32) -> Result<Self> {
        if factor >= self.factors() {
            return Err(Error::IndexOutOfBounds {
                index: factor,
                size: self.factors(),
            });
        }

        let old_levels = self.params.levels[factor];
        if new_levels < 2 {
            return Err(Error::invalid_params("new levels must be at least 2"));
        }

        if old_levels % new_levels != 0 {
            return Err(Error::invalid_params(format!(
                "new levels {} must divide old levels {}",
                new_levels, old_levels
            )));
        }

        let mut new_data = self.data.clone();
        for row in 0..self.runs() {
            new_data[[row, factor]] %= new_levels;
        }

        let mut new_levels_vec = self.params.levels.clone();
        new_levels_vec[factor] = new_levels;

        let new_params = OAParams::new_mixed(self.runs(), new_levels_vec, self.strength())?;

        Ok(Self::new(new_data, new_params))
    }
}

impl fmt::Debug for OA {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} with data {:?}", self.params, self.data)
    }
}

impl fmt::Display for OA {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "{}", self.params)?;
        for row in self.data.rows() {
            let row_str: Vec<String> = row.iter().map(|v| v.to_string()).collect();
            writeln!(f, "  {}", row_str.join(" "))?;
        }
        Ok(())
    }
}

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

    #[test]
    fn test_params_new() {
        let params = OAParams::new(9, 4, 3, 2).unwrap();
        assert_eq!(params.runs, 9);
        assert_eq!(params.factors, 4);
        assert_eq!(params.levels[0], 3);
        assert_eq!(params.strength, 2);
        assert_eq!(params.index(), 1);
    }

    #[test]
    fn test_params_invalid() {
        // levels < 2
        assert!(OAParams::new(9, 4, 1, 2).is_err());

        // strength > factors
        assert!(OAParams::new(9, 2, 3, 3).is_err());

        // runs not divisible by s^t
        assert!(OAParams::new(10, 4, 3, 2).is_err());
    }

    #[test]
    fn test_oa_creation() {
        let params = OAParams::new(4, 3, 2, 2).unwrap();
        let data =
            Array2::from_shape_vec((4, 3), vec![0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0]).unwrap();

        let oa = OA::new(data, params);
        assert_eq!(oa.runs(), 4);
        assert_eq!(oa.factors(), 3);
        assert_eq!(oa.symmetric_levels(), 2);
    }

    #[test]
    fn test_select_columns() {
        let params = OAParams::new(4, 4, 2, 2).unwrap();
        let data =
            Array2::from_shape_vec((4, 4), vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0])
                .unwrap();

        let oa = OA::new(data, params);
        let sub = oa.select_columns(&[0, 2]);

        assert_eq!(sub.factors(), 2);
        assert_eq!(sub.get(0, 0), 0);
        assert_eq!(sub.get(0, 1), 0);
        assert_eq!(sub.get(2, 0), 1);
        assert_eq!(sub.get(2, 1), 1);
    }

    #[test]
    fn test_display() {
        let params = OAParams::new(4, 3, 2, 2).unwrap();
        assert_eq!(format!("{}", params), "OA(4, 3, 2, 2)");
    }
}