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
//! Generic exact-joint location-scale machinery shared by the multi-block
//! families (GAMLSS two-block mean/noise, survival location-scale
//! threshold/log-sigma, …).
//!
//! Every location-scale family with several linear predictors needs the same
//! structural setup for the exact-joint spatial optimizer: concatenate the
//! per-block anisotropic `log κ` seeds, lower/upper data-aware bounds, project
//! the seed onto those bounds, and assemble the [`ExactJointHyperSetup`] over
//! `theta = [rho, psi]`. Only the row likelihood and the meaning of each block
//! differ across families — the κ-coordinate assembly does not. This module is
//! the single home for that assembly so improvements to it land once.
use crate::smooth::{
ExactJointHyperSetup, SpatialLengthScaleOptimizationOptions, SpatialLogKappaCoords,
TermCollectionSpec, spatial_length_scale_term_indices,
};
use ndarray::{Array1, ArrayView2};
/// Bound on every `rho` (log smoothing / log dispersion) coordinate in the
/// exact-joint theta vector. Shared by all location-scale families.
pub(crate) const EXACT_JOINT_RHO_BOUND: f64 = 12.0;
/// Assemble the exact-joint hyperparameter setup for a location-scale family
/// whose linear predictors are described, in theta order, by `blocks`.
///
/// `blocks` lists the per-predictor [`TermCollectionSpec`]s (e.g.
/// `[meanspec, noisespec]` for GAMLSS, `[thresholdspec, log_sigmaspec]` for
/// survival location-scale). The spatial `log κ` seed and its data-aware
/// lower/upper bounds are built per block — using `kappa_options` and the
/// term indices flagged for spatial length-scale optimization — and
/// concatenated in block order, matching the layout the exact-joint optimizer
/// expects.
///
/// `rho0` carries the caller-assembled smoothing/dispersion seed (already
/// ordered to match the penalty layout); its `[-EXACT_JOINT_RHO_BOUND,
/// EXACT_JOINT_RHO_BOUND]` box bounds are supplied here so the seed assembly
/// and bounding live in one place.
pub(crate) fn build_location_scale_exact_joint_setup(
data: ArrayView2<'_, f64>,
blocks: &[&TermCollectionSpec],
rho0: Array1<f64>,
kappa_options: &SpatialLengthScaleOptimizationOptions,
) -> ExactJointHyperSetup {
let rho_lower = Array1::<f64>::from_elem(rho0.len(), -EXACT_JOINT_RHO_BOUND);
let rho_upper = Array1::<f64>::from_elem(rho0.len(), EXACT_JOINT_RHO_BOUND);
// Concatenate per-block anisotropic log(kappa) seeds and their dims in
// block order. The exact-joint setup stores the spatial tail in log(kappa),
// not log(length_scale); each aniso term contributes d psi entries.
let mut all_values = Vec::new();
let mut all_dims = Vec::new();
let mut lower_vals = Vec::new();
let mut upper_vals = Vec::new();
for spec in blocks {
let term_indices = spatial_length_scale_term_indices(spec);
// Re-seed psi from data geometry when the spec does not pin a
// length_scale.
let kappa =
SpatialLogKappaCoords::from_length_scales_aniso(spec, &term_indices, kappa_options)
.reseed_from_data(data, spec, &term_indices, kappa_options);
let dims = kappa.dims_per_term().to_vec();
let lower = SpatialLogKappaCoords::lower_bounds_aniso_from_data(
data,
spec,
&term_indices,
&dims,
kappa_options,
);
let upper = SpatialLogKappaCoords::upper_bounds_aniso_from_data(
data,
spec,
&term_indices,
&dims,
kappa_options,
);
all_values.extend(kappa.as_array().iter());
lower_vals.extend(lower.as_array().iter());
upper_vals.extend(upper.as_array().iter());
all_dims.extend(dims);
}
let log_kappa0 =
SpatialLogKappaCoords::new_with_dims(Array1::from_vec(all_values), all_dims.clone());
let log_kappa_lower =
SpatialLogKappaCoords::new_with_dims(Array1::from_vec(lower_vals), all_dims.clone());
let log_kappa_upper =
SpatialLogKappaCoords::new_with_dims(Array1::from_vec(upper_vals), all_dims);
// Project seed onto bounds; spec.length_scale is a hint, not a constraint.
let log_kappa0 = log_kappa0.clamp_to_bounds(&log_kappa_lower, &log_kappa_upper);
ExactJointHyperSetup::new(
rho0,
rho_lower,
rho_upper,
log_kappa0,
log_kappa_lower,
log_kappa_upper,
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::basis::{CenterStrategy, MaternBasisSpec, MaternIdentifiability, MaternNu};
use crate::smooth::{ShapeConstraint, SmoothBasisSpec, SmoothTermSpec};
use ndarray::{Array2, array};
/// Build a single-term spec carrying a 1-D Matérn smooth with an explicit
/// `length_scale`. Such a term supports outer hyper-optimization and
/// contributes exactly one log(kappa) coordinate, so a two-block setup over
/// `[block_a, block_b]` has a fully predictable κ layout: one coordinate per
/// block, block A before block B.
fn one_term_block(name: &str, feature_col: usize, length_scale: f64) -> TermCollectionSpec {
TermCollectionSpec {
linear_terms: vec![],
random_effect_terms: vec![],
smooth_terms: vec![SmoothTermSpec {
name: name.to_string(),
basis: SmoothBasisSpec::Matern {
feature_cols: vec![feature_col],
spec: MaternBasisSpec {
periodic: None,
center_strategy: CenterStrategy::FarthestPoint { num_centers: 4 },
length_scale,
nu: MaternNu::FiveHalves,
include_intercept: false,
double_penalty: false,
identifiability: MaternIdentifiability::CenterSumToZero,
aniso_log_scales: None,
},
input_scales: None,
},
shape: ShapeConstraint::None,
joint_null_rotation: None,
}],
}
}
/// Reference per-block κ assembly: exactly the steps the engine performs for
/// one block, kept independent of `build_location_scale_exact_joint_setup`
/// so the parity test compares two genuinely separate code paths rather than
/// a value against itself.
fn reference_block_kappa(
data: ArrayView2<'_, f64>,
spec: &TermCollectionSpec,
kappa_options: &SpatialLengthScaleOptimizationOptions,
) -> (Vec<f64>, Vec<f64>, Vec<f64>, Vec<usize>) {
let term_indices = spatial_length_scale_term_indices(spec);
let kappa =
SpatialLogKappaCoords::from_length_scales_aniso(spec, &term_indices, kappa_options)
.reseed_from_data(data, spec, &term_indices, kappa_options);
let dims = kappa.dims_per_term().to_vec();
let lower = SpatialLogKappaCoords::lower_bounds_aniso_from_data(
data,
spec,
&term_indices,
&dims,
kappa_options,
);
let upper = SpatialLogKappaCoords::upper_bounds_aniso_from_data(
data,
spec,
&term_indices,
&dims,
kappa_options,
);
// Mirror the engine's seed projection so the reference seed is comparable
// post-clamp coordinate-for-coordinate.
let kappa = kappa.clamp_to_bounds(&lower, &upper);
(
kappa.as_array().to_vec(),
lower.as_array().to_vec(),
upper.as_array().to_vec(),
dims,
)
}
/// Parity test for the exact-joint Hessian matvec coordinate layout that
/// GAMLSS and survival location-scale both consume.
///
/// The matvec operates over `theta = [rho | log_kappa | auxiliary]`, indexed
/// through [`ExactJointHyperSetup::theta0`] / `lower` / `upper`. Both
/// families route their two predictors through
/// [`build_location_scale_exact_joint_setup`] in block order — GAMLSS as
/// `[mean, noise]`, survival as `[threshold, log_sigma]`. This test pins the
/// invariant they rely on: the engine concatenates per-block κ seeds and
/// bounds in block order, with the rho head boxed to `±RHO_BOUND`, exactly
/// matching an independent per-block assembly. If the layout ever drifts,
/// every family's exact Newton ψ direction would index the wrong
/// coordinates; this catches it before the matvec runs.
#[test]
fn exact_joint_block_layout_parity_across_families() {
// Two distinct 1-D feature columns so each block's geometry — and hence
// its data-aware κ seed/bounds — differs, making block-order errors
// observable rather than masked by identical coordinates.
let mut data = Array2::<f64>::zeros((6, 2));
let col0 = array![0.0, 0.2, 0.4, 0.6, 0.8, 1.0];
let col1 = array![-2.0, -1.0, 0.0, 1.0, 3.0, 5.0];
data.column_mut(0).assign(&col0);
data.column_mut(1).assign(&col1);
let kappa_options = SpatialLengthScaleOptimizationOptions::default();
// Block A plays the location predictor (GAMLSS mean / survival
// threshold), block B the scale predictor (GAMLSS noise / survival
// log-sigma). Distinct length scales give distinct κ seeds.
let block_a = one_term_block("loc", 0, 0.5);
let block_b = one_term_block("scale", 1, 2.0);
// A non-trivial rho seed in penalty order so the head slice is checked,
// not just the κ tail.
let rho0 = array![0.3, -0.7];
let setup = build_location_scale_exact_joint_setup(
data.view(),
&[&block_a, &block_b],
rho0.clone(),
&kappa_options,
);
let rho_dim = rho0.len();
assert_eq!(
setup.rho_dim(),
rho_dim,
"rho head must carry exactly the caller-supplied seed"
);
// Reference per-block assembly, independent of the engine.
let (seed_a, lo_a, hi_a, dims_a) =
reference_block_kappa(data.view(), &block_a, &kappa_options);
let (seed_b, lo_b, hi_b, dims_b) =
reference_block_kappa(data.view(), &block_b, &kappa_options);
// Each single-Matérn block contributes exactly one κ coordinate.
assert_eq!(dims_a, vec![1], "block A must be a single isotropic κ");
assert_eq!(dims_b, vec![1], "block B must be a single isotropic κ");
let expected_kappa: Vec<f64> = seed_a.iter().chain(seed_b.iter()).copied().collect();
let expected_lower: Vec<f64> = lo_a.iter().chain(lo_b.iter()).copied().collect();
let expected_upper: Vec<f64> = hi_a.iter().chain(hi_b.iter()).copied().collect();
assert_eq!(
setup.log_kappa_dim(),
expected_kappa.len(),
"engine κ dim must equal block-A + block-B κ dims"
);
assert_eq!(
setup.auxiliary_dim(),
0,
"no auxiliary axis is supplied for the two-block location-scale setup"
);
let theta0 = setup.theta0();
let lower = setup.lower();
let upper = setup.upper();
// Rho head: seed (sanitized clamp leaves these untouched) and the shared
// ±RHO_BOUND box.
for k in 0..rho_dim {
assert!(
(theta0[k] - rho0[k]).abs() <= 1e-12,
"rho seed mismatch at {k}: {} vs {}",
theta0[k],
rho0[k]
);
assert_eq!(lower[k], -EXACT_JOINT_RHO_BOUND, "rho lower bound at {k}");
assert_eq!(upper[k], EXACT_JOINT_RHO_BOUND, "rho upper bound at {k}");
}
// κ tail: must be block A then block B, coordinate-for-coordinate equal
// to the independent per-block assembly. This is the matvec layout both
// families index.
for (j, &want) in expected_kappa.iter().enumerate() {
let got = theta0[rho_dim + j];
assert!(
(got - want).abs() <= 1e-12,
"κ seed mismatch at tail index {j}: {got} vs {want}"
);
}
for (j, &want) in expected_lower.iter().enumerate() {
let got = lower[rho_dim + j];
assert!(
(got - want).abs() <= 1e-12,
"κ lower-bound mismatch at tail index {j}: {got} vs {want}"
);
}
for (j, &want) in expected_upper.iter().enumerate() {
let got = upper[rho_dim + j];
assert!(
(got - want).abs() <= 1e-12,
"κ upper-bound mismatch at tail index {j}: {got} vs {want}"
);
}
// Block order must be observable: block A's κ seed differs from block
// B's. If the engine ever concatenated in the wrong order (or collapsed
// the blocks), this distinguishes it even though both are 1-D κ.
assert!(
(theta0[rho_dim] - theta0[rho_dim + 1]).abs() > 1e-9,
"block A and block B κ seeds must differ so block order is testable: \
a={}, b={}",
theta0[rho_dim],
theta0[rho_dim + 1]
);
assert!(
(theta0[rho_dim] - seed_a[0]).abs() <= 1e-12,
"first κ coordinate must be block A's, not block B's"
);
assert!(
(theta0[rho_dim + 1] - seed_b[0]).abs() <= 1e-12,
"second κ coordinate must be block B's, not block A's"
);
// Bounds bracket the seed in every κ coordinate (projection invariant
// the matvec relies on for a feasible start).
for j in 0..expected_kappa.len() {
let s = theta0[rho_dim + j];
assert!(
lower[rho_dim + j] <= s + 1e-12 && s <= upper[rho_dim + j] + 1e-12,
"κ seed at {j} must lie within its bounds: {} not in [{}, {}]",
s,
lower[rho_dim + j],
upper[rho_dim + j]
);
}
}
}