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
//! The `PenaltyMatrix` carrier (dense / Kronecker / scaled) used by every
//! custom-family block, plus its constructors and the `Array2` conversion.
use ndarray::{Array1, Array2, Axis};
use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator};
/// A penalty matrix that may be stored in Kronecker-factored form.
///
/// For tensor-product terms (e.g. time-varying survival covariates), the penalty
/// has the structure `S = left ⊗ right` (Kronecker product). Keeping this
/// factored avoids materializing (p_left × p_right)² dense entries and enables
/// exact log-determinant computation via `log|A ⊗ B| = n_B log|A| + n_A log|B|`.
///
/// Dense penalties are stored as-is. Callers that need a raw `Array2<f64>` can
/// call `as_dense()` (zero-cost for Dense, lazy-materialized for KroneckerFactored).
#[derive(Clone, Debug)]
pub enum PenaltyMatrix {
Dense(Array2<f64>),
KroneckerFactored {
left: Array2<f64>,
right: Array2<f64>,
},
/// Block-local penalty: `local` is `block_dim × block_dim`, embedded at
/// `col_range` in the full parameter space of dimension `total_dim`.
/// Avoids materializing the full `total_dim × total_dim` matrix.
Blockwise {
local: Array2<f64>,
col_range: std::ops::Range<usize>,
total_dim: usize,
},
/// Wrapper assigning this penalty component to a user-visible precision
/// label. Components with the same label share one smoothing parameter.
Labeled {
label: String,
inner: Box<PenaltyMatrix>,
},
/// Wrapper fixing this penalty component at a physical log-precision.
/// Fixed components remain in the block-local physical penalty layout but
/// are removed from the REML outer coordinate vector.
Fixed {
log_lambda: f64,
inner: Box<PenaltyMatrix>,
},
}
impl PenaltyMatrix {
/// Number of rows (= number of columns, since penalties are square).
pub fn dim(&self) -> usize {
match self {
Self::Dense(m) => m.nrows(),
Self::KroneckerFactored { left, right } => left.nrows() * right.nrows(),
Self::Blockwise { total_dim, .. } => *total_dim,
Self::Labeled { inner, .. } | Self::Fixed { inner, .. } => inner.dim(),
}
}
/// Returns (nrows, ncols) like Array2::dim().
pub fn shape(&self) -> (usize, usize) {
let d = self.dim();
(d, d)
}
/// Materialize the full dense matrix.
pub fn to_dense(&self) -> Array2<f64> {
match self {
Self::Dense(m) => m.clone(),
Self::KroneckerFactored { left, right } => kronecker_product(left, right),
Self::Blockwise {
local,
col_range,
total_dim,
} => {
let mut g = Array2::zeros((*total_dim, *total_dim));
g.slice_mut(ndarray::s![
col_range.start..col_range.end,
col_range.start..col_range.end
])
.assign(local);
g
}
Self::Labeled { inner, .. } | Self::Fixed { inner, .. } => inner.to_dense(),
}
}
/// Borrow the inner dense matrix if Dense, otherwise materialize.
pub fn as_dense_cow(&self) -> std::borrow::Cow<'_, Array2<f64>> {
match self {
Self::Dense(m) => std::borrow::Cow::Borrowed(m),
Self::KroneckerFactored { .. }
| Self::Blockwise { .. }
| Self::Labeled { .. }
| Self::Fixed { .. } => std::borrow::Cow::Owned(self.to_dense()),
}
}
/// Returns a reference to the inner matrix if this is a Dense variant.
pub fn as_dense_ref(&self) -> Option<&Array2<f64>> {
match self {
Self::Dense(m) => Some(m),
Self::Fixed { inner, .. } => inner.as_dense_ref(),
Self::KroneckerFactored { .. } | Self::Blockwise { .. } | Self::Labeled { .. } => None,
}
}
pub fn with_precision_label(self, label: impl Into<String>) -> Self {
Self::Labeled {
label: label.into(),
inner: Box::new(self),
}
}
pub fn precision_label(&self) -> Option<&str> {
match self {
Self::Labeled { label, .. } => Some(label.as_str()),
Self::Fixed { .. } => None,
_ => None,
}
}
pub fn with_fixed_log_lambda(self, log_lambda: f64) -> Self {
Self::Fixed {
log_lambda,
inner: Box::new(self),
}
}
pub fn fixed_log_lambda(&self) -> Option<f64> {
match self {
Self::Fixed { log_lambda, .. } => Some(*log_lambda),
Self::Labeled { inner, .. } => inner.fixed_log_lambda(),
_ => None,
}
}
/// Compute S * v using the row-major Kronecker vec trick when factored:
/// (A ⊗ B) vec_rm(V) = vec_rm(A V Bᵀ)
/// where V = reshape(v, (p_left, p_right)).
pub fn dot(&self, v: &Array1<f64>) -> Array1<f64> {
match self {
Self::Dense(m) => m.dot(v),
Self::KroneckerFactored { left, right } => {
let p_left = left.nrows();
let p_right = right.nrows();
// v is ordered by i_left * p_right + i_right.
let v_mat =
ndarray::ArrayView2::from_shape((p_left, p_right), v.as_slice().unwrap())
.unwrap();
let avbt = left.dot(&v_mat).dot(&right.t());
let standard = avbt.as_standard_layout();
Array1::from_iter(standard.iter().copied())
}
Self::Blockwise {
local,
col_range,
total_dim,
} => {
let mut out = Array1::zeros(*total_dim);
let v_block = v.slice(ndarray::s![col_range.clone()]);
let result_block = local.dot(&v_block);
out.slice_mut(ndarray::s![col_range.clone()])
.assign(&result_block);
out
}
Self::Labeled { inner, .. } | Self::Fixed { inner, .. } => inner.dot(v),
}
}
/// Add λ * self to a mutable dense accumulator.
pub fn add_scaled_to(&self, lambda: f64, target: &mut Array2<f64>) {
match self {
Self::Dense(m) => {
target.scaled_add(lambda, m);
}
Self::KroneckerFactored { left, right } => {
let p_left = left.nrows();
let p_right = right.nrows();
for i1 in 0..p_left {
for j1 in 0..p_left {
let a_ij = left[[i1, j1]];
if a_ij == 0.0 {
continue;
}
let scaled_a = lambda * a_ij;
for i2 in 0..p_right {
let row = i1 * p_right + i2;
for j2 in 0..p_right {
let col = j1 * p_right + j2;
target[[row, col]] += scaled_a * right[[i2, j2]];
}
}
}
}
}
Self::Blockwise {
local, col_range, ..
} => {
target
.slice_mut(ndarray::s![col_range.clone(), col_range.clone()])
.scaled_add(lambda, local);
}
Self::Labeled { inner, .. } | Self::Fixed { inner, .. } => {
inner.add_scaled_to(lambda, target)
}
}
}
/// Add λ * diag(self) to a mutable diagonal accumulator.
pub fn add_scaled_diag_to(&self, lambda: f64, target: &mut Array1<f64>) {
match self {
Self::Dense(m) => {
let p = m.nrows().min(target.len());
for j in 0..p {
target[j] += lambda * m[[j, j]];
}
}
Self::KroneckerFactored { left, right } => {
let p_left = left.nrows();
let p_right = right.nrows();
assert_eq!(target.len(), p_left * p_right);
for i_left in 0..p_left {
let left_diag = left[[i_left, i_left]];
if left_diag == 0.0 {
continue;
}
let scaled_left = lambda * left_diag;
for i_right in 0..p_right {
target[i_left * p_right + i_right] +=
scaled_left * right[[i_right, i_right]];
}
}
}
Self::Blockwise {
local, col_range, ..
} => {
let width = local.nrows().min(col_range.len());
for local_idx in 0..width {
target[col_range.start + local_idx] += lambda * local[[local_idx, local_idx]];
}
}
Self::Labeled { inner, .. } | Self::Fixed { inner, .. } => {
inner.add_scaled_diag_to(lambda, target)
}
}
}
/// Compute the quadratic form β' S β.
pub fn quadratic_form(&self, beta: &Array1<f64>) -> f64 {
match self {
Self::Dense(m) => beta.dot(&m.dot(beta)),
Self::KroneckerFactored { .. } => {
let sv = self.dot(beta);
beta.dot(&sv)
}
Self::Blockwise {
local, col_range, ..
} => {
let beta_block = beta.slice(ndarray::s![col_range.clone()]);
let sv = local.dot(&beta_block);
beta_block.dot(&sv)
}
Self::Labeled { inner, .. } | Self::Fixed { inner, .. } => inner.quadratic_form(beta),
}
}
/// Access dimensions like an Array2.
pub fn nrows(&self) -> usize {
self.dim()
}
pub fn ncols(&self) -> usize {
self.dim()
}
}
impl From<Array2<f64>> for PenaltyMatrix {
fn from(m: Array2<f64>) -> Self {
Self::Dense(m)
}
}
/// Computes the Kronecker product A ⊗ B for penalty matrix construction.
/// This is used to create tensor product penalties that enforce smoothness
/// in multiple dimensions for interaction terms.
fn kronecker_product(a: &Array2<f64>, b: &Array2<f64>) -> Array2<f64> {
let (arows, a_cols) = a.dim();
let (brows, b_cols) = b.dim();
if arows == 0 || a_cols == 0 || brows == 0 || b_cols == 0 {
return Array2::zeros((arows * brows, a_cols * b_cols));
}
let mut result = Array2::zeros((arows * brows, a_cols * b_cols));
result
.axis_chunks_iter_mut(Axis(0), brows)
.into_par_iter()
.enumerate()
.for_each(|(i, mut row_block)| {
let arow = a.row(i);
let col_chunks = row_block.axis_chunks_iter_mut(Axis(1), b_cols);
for (j, mut block) in col_chunks.into_iter().enumerate() {
let aval = arow[j];
if aval == 0.0 {
continue;
}
for (dest, &src) in block.iter_mut().zip(b.iter()) {
*dest = aval * src;
}
}
});
result
}