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
//! Control limits for SPM monitoring statistics.
//!
//! Provides upper control limits (UCL) for T-squared and SPE statistics:
//! - **T-squared**: chi-squared distribution quantile. For finite calibration
//! samples of size *n*, T² follows `(n·ncomp/(n−ncomp))·F(ncomp, n−ncomp)`
//! rather than `χ²(ncomp)`. The chi-squared limit is the large-sample
//! (*n* → ∞) limit.
//! - **SPE**: moment-matched chi-squared approximation (Box, 1954, Theorem 1,
//! pp. 292–295). The derivation matches the first two moments of the SPE
//! distribution to a scaled chi-squared: `E[a·χ²(b)] = a·b = mean`,
//! `Var[a·χ²(b)] = 2a²·b = var`, giving `a = var/(2·mean)`,
//! `b = 2·mean²/var`.
//!
//! # Accuracy
//!
//! The moment-matching approximation is exact when SPE follows a scaled
//! chi-squared distribution (holds under Gaussian scores). For non-Gaussian
//! data, the approximation error is O(κ₄) where κ₄ is the excess kurtosis
//! of the SPE distribution. Use [`spe_moment_match_diagnostic`] to assess
//! adequacy.
//!
//! # References
//!
//! - Box, G.E.P. (1954). Some theorems on quadratic forms applied in the
//! study of analysis of variance problems, I. *Annals of Mathematical
//! Statistics*, 25(2), 290–302. Theorem 1, pp. 292–295.
//! - Woodall, W.H. & Ncube, M.M. (1985). Multivariate CUSUM quality-control
//! procedures. *Technometrics*, 27(3), 285–292. §2, pp. 286–288.
use chi2_quantile;
use crateFdarError;
/// A control limit for a monitoring statistic.
/// Compute the T-squared control limit based on the chi-squared distribution.
///
/// UCL = chi2_quantile(1 - alpha, ncomp)
///
/// Based on the chi-squared approximation to the Hotelling T² distribution.
/// For small calibration samples (n < 30), this may be anti-conservative;
/// consider `t2_limit_robust()` with bootstrap method.
///
/// # Finite-sample correction
///
/// For finite calibration samples of size *n*, T² follows
/// `(n·ncomp/(n−ncomp))·F(ncomp, n−ncomp)` rather than `χ²(ncomp)`. The
/// chi-squared limit used here is the large-sample (*n* → ∞) limit. For
/// *n* < 30, the F-based limit should be preferred (Woodall & Ncube, 1985,
/// §2, pp. 286–288).
///
/// # Arguments
/// * `ncomp` - Number of principal components (degrees of freedom)
/// * `alpha` - Significance level (e.g. 0.05)
///
/// # Example
///
/// ```
/// use fdars_core::spm::control::t2_control_limit;
/// let limit = t2_control_limit(3, 0.05).unwrap();
/// assert!(limit.ucl > 0.0);
/// assert!((limit.ucl - 7.815).abs() < 0.01); // chi2(0.95, 3)
/// ```
///
/// # Errors
///
/// Returns [`FdarError::InvalidParameter`] if `ncomp` is 0 or `alpha` is not in (0, 1).
/// Compute the SPE control limit using moment-matched chi-squared approximation.
///
/// Uses the Box (1954, Theorem 1, pp. 292–295) moment-matching approximation:
/// SPE ~ a * chi²(b) where `a = var/(2·mean)`, `b = ceil(2·mean²/var)`. This
/// is accurate when SPE values are approximately chi-squared distributed,
/// which holds when the number of retained components is moderate (5–20)
/// and sample size is adequate (n > 30).
///
/// Estimates parameters a and b such that SPE ~ a * chi2(b):
/// a = var(spe) / (2 * mean(spe))
/// b = 2 * mean(spe)^2 / var(spe)
/// UCL = a * chi2_quantile(1 - alpha, ceil(b))
///
/// # Accuracy
///
/// The moment-matching approximation is exact when SPE follows a scaled
/// chi-squared distribution (holds under Gaussian scores). For non-Gaussian
/// data, the approximation error is O(κ₄) where κ₄ is the excess kurtosis
/// of the SPE distribution. Use [`spe_moment_match_diagnostic`] to check.
///
/// # Rounding choice
///
/// Using `ceil()` rather than `round()` for the degrees-of-freedom parameter
/// *b* gives a conservative (wider) control limit, ensuring the nominal
/// false-alarm rate is not exceeded.
///
/// # Arguments
/// * `spe_values` - In-control SPE values from calibration data
/// * `alpha` - Significance level (e.g. 0.05)
///
/// # Example
///
/// ```
/// use fdars_core::spm::control::spe_control_limit;
/// let spe_values = vec![1.0, 2.0, 1.5, 3.0, 2.5, 1.8, 2.2, 1.2, 2.8, 1.6];
/// let limit = spe_control_limit(&spe_values, 0.05).unwrap();
/// assert!(limit.ucl > 0.0);
/// ```
///
/// # Errors
///
/// Returns [`FdarError::InvalidDimension`] if `spe_values` has fewer than 2 elements.
/// Returns [`FdarError::InvalidParameter`] if `alpha` is not in (0, 1).
/// Returns [`FdarError::ComputationFailed`] if the estimated variance is zero or negative.
/// Diagnostic for the SPE moment-match chi-squared approximation.
///
/// Computes the excess kurtosis of the SPE values and compares it to the
/// theoretical kurtosis of the fitted chi-squared distribution (12/b).
/// A large discrepancy suggests the chi-squared approximation may be poor.
///
/// Returns `(excess_kurtosis, theoretical_kurtosis, is_adequate)` where
/// `is_adequate` is true when the absolute difference is within 50% of
/// the theoretical value. Interpretation: `is_adequate == true` (ratio
/// within 50%) indicates the chi-squared model fits well. When inadequate,
/// the SPE distribution may be far from chi-squared (possibly multi-modal
/// or heavy-tailed); in that case, use `spe_limit_robust()` with bootstrap
/// or KDE method instead.
///
/// # Arguments
/// * `spe_values` - In-control SPE values
///
/// # Errors
///
/// Returns [`FdarError::InvalidDimension`] if fewer than 4 values are provided.