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
use super::exact_eval_cache::*;
use super::family::*;
use super::gradient_paths::*;
use super::hessian_paths::*;
use super::*;
// ── RowKernel<2> implementation (rigid path only) ────────────────────
pub(super) struct BernoulliRigidRowKernel {
pub(super) family: BernoulliMarginalSlopeFamily,
pub(super) block_states: Vec<ParameterBlockState>,
pub(super) slices: BlockSlices,
/// Per-row uncontracted third-derivative tensor, lazily populated in a
/// single parallel pass on first access. Every ψ-axis directional
/// derivative operator that consults this kernel shares this cache via
/// its `Arc`; the empirical-grid closed-form third-derivative tensor
/// (`empirical_rigid_third_full_closed_form`) runs at most once per row
/// across the full ext-dim sweep, instead of once per (row, ψ-axis) pair.
/// Per-axis `row_third_contracted` becomes
/// a 2×2 bilinear contraction against the cached tensor.
pub(super) third_full_cache: crate::resource::RayonSafeOnce<Vec<[[[f64; 2]; 2]; 2]>>,
/// Per-row uncontracted fourth-derivative tensor — the outer-Hessian
/// analogue of `third_full_cache`. The second-directional-derivative
/// operator's trace path touches every row × (u, v) pair; with this
/// cache the heavy 8-direction empirical jet (or closed-form 5-component
/// build) runs at most once per row, leaving each pair with a cheap
/// [`contract_fourth_full`] bilinear.
pub(super) fourth_full_cache: crate::resource::RayonSafeOnce<Vec<[[[[f64; 2]; 2]; 2]; 2]>>,
}
impl BernoulliRigidRowKernel {
pub(super) fn new(
family: BernoulliMarginalSlopeFamily,
block_states: Vec<ParameterBlockState>,
) -> Self {
let slices = block_slices(&family);
Self {
family,
block_states,
slices,
third_full_cache: crate::resource::RayonSafeOnce::new(),
fourth_full_cache: crate::resource::RayonSafeOnce::new(),
}
}
/// Lazy-build the per-row uncontracted third-derivative tensor cache. The
/// first caller pays one parallel row pass that materialises the full
/// `[[[f64; 2]; 2]; 2]` tensor for every observation; subsequent callers
/// (every other ψ-axis operator that shares this kernel via `Arc`) get
/// an `O(1)` lookup. A failed jet evaluation here means the underlying
/// likelihood is non-finite at the converged β snapshot — propagate via
/// panic, mirroring how every other kernel-level numerical contract in
/// this module surfaces post-PIRLS invariant violations.
pub(super) fn third_full_cache(&self) -> &[[[[f64; 2]; 2]; 2]] {
self.third_full_cache
.get_or_compute(|| {
(0..self.family.y.len())
.into_par_iter()
.map(|row| {
let marginal_eta = self.block_states[0].eta[row];
let marginal = self.family.marginal_link_map(marginal_eta)?;
let slope = self.block_states[1].eta[row];
self.family.rigid_row_third_full(row, marginal, slope)
})
.collect::<Result<Vec<_>, String>>()
.expect(
"BernoulliRigidRowKernel third-full cache build failed; \
per-row jet should not error at the converged β snapshot",
)
})
.as_slice()
}
/// Lazy-build the per-row uncontracted fourth-derivative tensor cache —
/// outer-Hessian analogue of [`third_full_cache`]. Concurrent first
/// callers may redundantly run the parallel row pass; the first published
/// value wins and every subsequent caller reads the same vector. Used by
/// `row_fourth_contracted` so each (u, v) ψ-axis pair finishes in a
/// 16-multiply [`contract_fourth_full`] bilinear instead of triggering
/// a fresh empirical-grid 8-direction jet.
pub(super) fn fourth_full_cache(&self) -> &[[[[[f64; 2]; 2]; 2]; 2]] {
self.fourth_full_cache
.get_or_compute(|| {
(0..self.family.y.len())
.into_par_iter()
.map(|row| {
let marginal_eta = self.block_states[0].eta[row];
let marginal = self.family.marginal_link_map(marginal_eta)?;
let slope = self.block_states[1].eta[row];
self.family.rigid_row_fourth_full(row, marginal, slope)
})
.collect::<Result<Vec<_>, String>>()
.expect(
"BernoulliRigidRowKernel fourth-full cache build failed; \
per-row jet should not error at the converged β snapshot",
)
})
.as_slice()
}
}
impl RowKernel<2> for BernoulliRigidRowKernel {
fn n_rows(&self) -> usize {
self.family.y.len()
}
fn n_coefficients(&self) -> usize {
self.slices.total
}
fn row_kernel(&self, row: usize) -> Result<(f64, [f64; 2], [[f64; 2]; 2]), String> {
let marginal_eta = self.block_states[0].eta[row];
let marginal = self.family.marginal_link_map(marginal_eta)?;
let g = self.block_states[1].eta[row];
self.family.rigid_row_kernel_eval(row, marginal, g)
}
fn jacobian_action(&self, row: usize, d_beta: &[f64]) -> [f64; 2] {
let d_beta = ndarray::ArrayView1::from(d_beta);
[
self.family
.marginal_design
.dot_row_view(row, d_beta.slice(s![self.slices.marginal.clone()])),
self.family
.logslope_design
.dot_row_view(row, d_beta.slice(s![self.slices.logslope.clone()])),
]
}
fn jacobian_transpose_action(&self, row: usize, v: &[f64; 2], out: &mut [f64]) {
{
let mut m = ndarray::ArrayViewMut1::from(&mut out[self.slices.marginal.clone()]);
self.family
.marginal_design
.axpy_row_into(row, v[0], &mut m)
.expect("marginal axpy dim mismatch");
}
{
let mut g = ndarray::ArrayViewMut1::from(&mut out[self.slices.logslope.clone()]);
self.family
.logslope_design
.axpy_row_into(row, v[1], &mut g)
.expect("logslope axpy dim mismatch");
}
}
fn add_pullback_hessian(&self, row: usize, h: &[[f64; 2]; 2], target: &mut Array2<f64>) {
self.family
.marginal_design
.syr_row_into_view(
row,
h[0][0],
target.slice_mut(s![
self.slices.marginal.clone(),
self.slices.marginal.clone()
]),
)
.expect("marginal syr dim mismatch");
if h[0][1] != 0.0 {
self.family
.marginal_design
.row_outer_into_view(
row,
&self.family.logslope_design,
h[0][1],
target.slice_mut(s![
self.slices.marginal.clone(),
self.slices.logslope.clone()
]),
)
.expect("marginal-logslope outer dim mismatch");
self.family
.logslope_design
.row_outer_into_view(
row,
&self.family.marginal_design,
h[0][1],
target.slice_mut(s![
self.slices.logslope.clone(),
self.slices.marginal.clone()
]),
)
.expect("logslope-marginal outer dim mismatch");
}
self.family
.logslope_design
.syr_row_into_view(
row,
h[1][1],
target.slice_mut(s![
self.slices.logslope.clone(),
self.slices.logslope.clone()
]),
)
.expect("logslope syr dim mismatch");
}
fn add_diagonal_quadratic(&self, row: usize, h: &[[f64; 2]; 2], diag: &mut [f64]) {
{
let mut md = ndarray::ArrayViewMut1::from(&mut diag[self.slices.marginal.clone()]);
self.family
.marginal_design
.squared_axpy_row_into(row, h[0][0], &mut md)
.expect("marginal squared_axpy dim mismatch");
}
{
let mut gd = ndarray::ArrayViewMut1::from(&mut diag[self.slices.logslope.clone()]);
self.family
.logslope_design
.squared_axpy_row_into(row, h[1][1], &mut gd)
.expect("logslope squared_axpy dim mismatch");
}
}
fn row_third_contracted(&self, row: usize, dir: &[f64; 2]) -> Result<[[f64; 2]; 2], String> {
let cache = self.third_full_cache();
Ok(contract_third_full(&cache[row], dir[0], dir[1]))
}
/// Force-build the per-row uncontracted third-derivative tensor cache
/// at top-level rayon. Called by [`RowKernelHessianWorkspace::new`]
/// before any outer `par_iter` enters; subsequent
/// `row_third_contracted` calls inside the parallel ext-idx sweep then
/// hit a populated cache and skip straight to a 2×2 contraction.
fn warm_up_directional_caches(&self) -> Result<(), String> {
// Touch both caches so their parallel builds run here, not later
// (nested inside the outer ext-idx par_iter where the lock-holder
// thread would have to do each row pass alone).
let third_cache_len = self.third_full_cache().len();
let fourth_cache_len = self.fourth_full_cache().len();
let expected_len = self.family.y.len();
if third_cache_len != expected_len || fourth_cache_len != expected_len {
return Err(format!(
"bernoulli rigid row-kernel cache warm-up length mismatch: third={third_cache_len} fourth={fourth_cache_len} expected={expected_len}"
));
}
Ok(())
}
fn row_fourth_contracted(
&self,
row: usize,
dir_u: &[f64; 2],
dir_v: &[f64; 2],
) -> Result<[[f64; 2]; 2], String> {
let cache = self.fourth_full_cache();
Ok(contract_fourth_full(
&cache[row],
dir_u[0],
dir_u[1],
dir_v[0],
dir_v[1],
))
}
/// BLAS-3 batched override of the generic per-row `J · F` build (see
/// `RowKernel::jacobian_action_matrix` for the contract and the
/// algebra).
///
/// The bernoulli marginal-slope row Jacobian is a pure pair of
/// design-row dot products against disjoint coefficient blocks:
///
/// ```text
/// jacobian_action(r, β)[0] = marginal_design.row(r) · β[marg_range]
/// jacobian_action(r, β)[1] = logslope_design.row(r) · β[logs_range]
/// ```
///
/// So the full `(n × 2·rank)` projection is two dense matrix-matrix
/// products, one per axis. For dense designs we dispatch through
/// ndarray's `.dot(matrix)` which hits BLAS-3 (`matrixmultiply`)
/// directly. For other backings we fall back to the generic per-
/// row path by returning `None`; the operator-backed regime where
/// the row kernel was deliberately matrix-free at large scale
/// still pays the per-row jet costs we have today.
///
/// **Correctness contract.** Output matches the per-row reference
/// `jf[r, k * rank + c] = jacobian_action(r, F[:, c])[k]` exactly
/// (it's the same arithmetic in a different order — BLAS-3
/// summation reduces in-row).
fn jacobian_action_matrix(&self, factor: ArrayView2<'_, f64>) -> Option<Array2<f64>> {
let p_total = self.slices.total;
if factor.nrows() != p_total {
return None;
}
let n_rows = self.family.y.len();
let rank = factor.ncols();
if rank == 0 {
return Some(Array2::<f64>::zeros((n_rows, 2 * rank)));
}
// Slice F into the two coefficient-block factors. Standard-
// layout owned copies let downstream `dot` paths stride
// contiguous columns.
let f_marg = factor
.slice(s![self.slices.marginal.clone(), ..])
.as_standard_layout()
.into_owned();
let f_logs = factor
.slice(s![self.slices.logslope.clone(), ..])
.as_standard_layout()
.into_owned();
// Compute J_block · F_block for both axes.
//
// Fast path: when the design has a materialized dense Array2
// backing we hit ndarray's `dot(matrix)` directly, which routes
// through `matrixmultiply` (BLAS-3) and reduces ~n×rank
// strided per-row gathers to one cache-friendly contiguous
// matmul per axis. At large-scale shape (n ≈ 1e4–1e5, rank ≈ 81)
// this turns the dominant per-trace cost from ~3 s into ~50 ms.
//
// Generic path: for operator-backed / sparse / chunked designs
// (Lazy with no contiguous `as_dense_ref`) we fall through to
// `DesignMatrix::dot` on a per-column basis. This is still the
// same arithmetic as the per-row reference (`jacobian_action`
// does a single design-row dot product per call) but with
// batched matrix-vector products that let the underlying
// operator amortise any per-call dispatch cost. Importantly,
// this path stays available for sparse-design fits where the
// dense fast path is structurally inapplicable.
let jf_marg = match self.family.marginal_design.as_dense_ref() {
Some(dense) => dense.dot(&f_marg),
None => self::axis_jf_via_column_dot(&self.family.marginal_design, &f_marg, n_rows),
};
let jf_logs = match self.family.logslope_design.as_dense_ref() {
Some(dense) => dense.dot(&f_logs),
None => self::axis_jf_via_column_dot(&self.family.logslope_design, &f_logs, n_rows),
};
assert_eq!(jf_marg.dim(), (n_rows, rank));
assert_eq!(jf_logs.dim(), (n_rows, rank));
// Pack into row-major (n × 2·rank): first `rank` columns are
// k=0 (marginal axis), next `rank` are k=1 (logslope axis). This
// mirrors the layout written by `compute_jf`'s strided write.
let mut jf = Array2::<f64>::zeros((n_rows, 2 * rank));
jf.slice_mut(s![.., 0..rank]).assign(&jf_marg);
jf.slice_mut(s![.., rank..2 * rank]).assign(&jf_logs);
Some(jf)
}
}
/// Per-column matrix-vector dispatch for `DesignMatrix · F_block` when
/// no contiguous dense backing is available (sparse or operator-backed
/// designs). Mirrors what the per-row reference path does row-by-row,
/// but as `rank` batched mat-vec products so the underlying operator
/// can amortise per-call dispatch.
fn axis_jf_via_column_dot(
design: &crate::linalg::matrix::DesignMatrix,
f_block: &Array2<f64>,
n_rows: usize,
) -> Array2<f64> {
let rank = f_block.ncols();
let mut out = Array2::<f64>::zeros((n_rows, rank));
for c in 0..rank {
let col_owned = f_block.column(c).to_owned();
let result = design.dot(&col_owned);
out.column_mut(c).assign(&result);
}
out
}
pub(super) struct BernoulliMarginalSlopeExactNewtonJointHessianWorkspace {
pub(super) family: BernoulliMarginalSlopeFamily,
pub(super) block_states: Vec<ParameterBlockState>,
pub(super) cache: Arc<BernoulliMarginalSlopeExactEvalCache>,
pub(super) matvec_calls: AtomicUsize,
pub(super) fused_gradient_dense:
OnceLock<Result<Arc<ExactNewtonJointFusedDenseEvaluation>, String>>,
/// Outer-only joint-Hessian directional-derivative options. The
/// `outer_score_subsample` field is the row mask threaded through the
/// `_with_options` directional-derivative helpers so the cached joint
/// Hessian Hv-action paths can downscale to the stratified subsample at
/// large scale. When `None`, the row iteration is identical to the
/// legacy full-data path.
pub(super) options: BlockwiseFitOptions,
}
pub(super) struct ExactNewtonJointFusedDenseEvaluation {
pub(super) gradient: ExactNewtonJointGradientEvaluation,
pub(super) hessian: Array2<f64>,
}
pub(super) struct BernoulliMarginalSlopeExactNewtonJointPsiWorkspace {
pub(super) family: BernoulliMarginalSlopeFamily,
pub(super) block_states: Vec<ParameterBlockState>,
pub(super) specs: Vec<ParameterBlockSpec>,
pub(super) derivative_blocks: Vec<Vec<crate::custom_family::CustomFamilyBlockPsiDerivative>>,
pub(super) cache: Arc<BernoulliMarginalSlopeExactEvalCache>,
/// Outer-only ψ-calculus options. The `outer_score_subsample` field is
/// the row mask threaded through `sigma_exact_joint_psi_terms_with_options`
/// and the second-order / Hessian-drift counterparts to make the cached
/// ψ calculus subsample-aware.
pub(super) options: BlockwiseFitOptions,
}
pub(super) fn bernoulli_margslope_line_search_ll_with_early_exit<F>(
weighted_rows: &[WeightedOuterRow],
threshold: f64,
row_ll: F,
) -> Result<f64, String>
where
F: Fn(usize) -> Result<f64, String> + Sync,
{
if !threshold.is_finite() {
return Err(format!(
"bernoulli marginal-slope early-exit threshold must be finite, got {threshold}"
));
}
let mut total_ll = 0.0;
for chunk in weighted_rows.chunks(BERNOULLI_MARGSLOPE_LINE_SEARCH_EARLY_EXIT_CHUNK_ROWS) {
let chunk_ll: f64 = chunk
.into_par_iter()
.try_fold(
|| 0.0,
|mut acc, wr| -> Result<_, String> {
acc += wr.weight * row_ll(wr.index)?;
Ok(acc)
},
)
.try_reduce(
|| 0.0,
|left, right| -> Result<_, String> { Ok(left + right) },
)?;
total_ll += chunk_ll;
// Every Bernoulli marginal-slope row contribution is <= 0 because it is
// weight_i * log(CDF(.)) with nonnegative weights, so the running sum is
// monotone-down and `-total_ll` only grows as rows are added. When the
// line search passes the full-data row set (the only caller — line-search
// accept/reject is an exact full-data decision, see
// `log_likelihood_only_with_options`), `-total_ll` is therefore a genuine
// lower bound on the final full-data negative log-likelihood and can prove
// the trial rejected before the sweep finishes. NOTE: this lower-bound
// guarantee holds only for the full-data measure; a Horvitz-Thompson
// subsample sum (inverse-inclusion weights) is an *unbiased estimator* of
// the full-data NLL, not a lower bound, so it must never drive a reject
// against a full-data threshold.
if -total_ll > threshold {
return Err(format!(
"bernoulli marginal-slope line-search rejected early: partial_nll={} threshold={}",
-total_ll, threshold
));
}
}
Ok(total_ll)
}
#[cfg(test)]
mod early_exit_soundness_tests {
use super::*;
fn full_data_rows(n: usize) -> Vec<WeightedOuterRow> {
(0..n)
.map(|index| WeightedOuterRow {
index,
weight: 1.0,
stratum: 0,
})
.collect()
}
/// On the full-data measure (weight 1, every row) the running `-total_ll`
/// is a genuine monotone lower bound on the full-data NLL, so the early
/// exit is a valid reject certificate: it rejects iff the full NLL exceeds
/// the threshold and otherwise returns the exact LL.
#[test]
fn full_data_early_exit_is_a_valid_reject_certificate() {
// Each row contributes log Φ = -1.0, i.e. NLL contribution 1.0; full
// NLL over 100 rows is exactly 100.
let rows = full_data_rows(100);
let row_ll = |_i: usize| -> Result<f64, String> { Ok(-1.0) };
// Threshold below the full NLL → must reject.
assert!(
bernoulli_margslope_line_search_ll_with_early_exit(&rows, 50.0, row_ll).is_err(),
"full-data NLL 100 > threshold 50 must reject"
);
// Threshold above the full NLL → must accept with the exact LL.
let ll = bernoulli_margslope_line_search_ll_with_early_exit(&rows, 150.0, row_ll)
.expect("full-data NLL 100 < threshold 150 must accept");
assert!(
(ll - (-100.0)).abs() < 1e-9,
"accepted LL must be exact, got {ll}"
);
}
/// Regression for the large-scale `IntegrationError`: a Horvitz-Thompson
/// subsample sum is an *unbiased estimator* of the full-data NLL, not a
/// lower bound on it, so feeding the kernel an HT-weighted subset against a
/// full-data threshold can falsely reject a trial whose true full-data NLL
/// is below the threshold. This is exactly why the BMS line search must
/// only ever pass the full-data row set — the auto line-search subsample
/// that used to violate this was removed.
#[test]
fn ht_subsample_against_full_data_threshold_can_falsely_reject() {
// True per-row NLL: rows 0..10 contribute 10 each, rows 10..100
// contribute 0. Full-data NLL = 100.
let row_ll = |i: usize| -> Result<f64, String> { if i < 10 { Ok(-10.0) } else { Ok(0.0) } };
let threshold = 500.0;
// Full-data decision: NLL 100 < 500 → accept (the correct decision).
let full_rows = full_data_rows(100);
let full =
bernoulli_margslope_line_search_ll_with_early_exit(&full_rows, threshold, row_ll)
.expect("full-data NLL 100 < threshold 500 must accept");
assert!((full - (-100.0)).abs() < 1e-9);
// HT subsample that happens to draw the 10 high-NLL rows with
// inverse-inclusion weight 10: weighted sum = 10·10·10 = 1000 > 500.
// Fed against the full-data threshold the kernel rejects — a FALSE
// reject. The product invariant is that this row set is never built
// for a line-search probe; the assertion documents the hazard.
let ht_rows: Vec<WeightedOuterRow> = (0..10)
.map(|index| WeightedOuterRow {
index,
weight: 10.0,
stratum: 1,
})
.collect();
assert!(
bernoulli_margslope_line_search_ll_with_early_exit(&ht_rows, threshold, row_ll)
.is_err(),
"HT-weighted sum 1000 spuriously exceeds the full-data threshold 500 — \
demonstrates why an HT subsample must never certify a line-search reject"
);
}
}