Skip to content

Causal mode: removal of later rows changes no bit; tstat_combine at round-off spread - #154

Merged
jeremymanning merged 13 commits into
masterfrom
causal-removal-exact
Nov 24, 2026
Merged

jeremymanning merged 13 commits into
masterfrom
causal-removal-exact

Conversation

@jeremymanning

@jeremymanning jeremymanning commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Causal mode: removing later rows changes no bit; tstat_combine at round-off spread

Follow-up to #153, before the 0.4.0 release.

Removal is exact

With causal=True and a built-in kernel, row t of the output is now bit-identical when the rows after t are removed, not only when they are replaced. Before, a row could differ in the last bit, because shapes in the estimator (the last incomplete group of rows, blocks of features), the effective sample sizes behind min_samples, the blocks of streamed summaries and the batches given to a fitted model depended on the length of the data; tstat_combine could magnify such a bit (0.12 on a statistic of 3.8e7 in the case that showed it). These now have shapes that do not depend on the number of timepoints or on max_memory_gb. The documentation says this is tested (also by tests/test_causal_removal_random.py, 300 random configurations in every run and 3000 in the slow run), not proven, and keeps "to 1e-9" for kernel arrays and kernel functions of the user's and for models outside the five listed scikit-learn classes.

tstat_combine

Finite values whose range across subjects is at most 128 eps (2.8e-14), or that times their largest magnitude when it exceeds 1, now give the value of equal values (±inf with the sign of the mean, 0 for a mean of 0) in both modes, where round-off could give a finite statistic as large as 1e16 before. Other inputs, including those with non-finite values, are unchanged (compared with master). The changelog's "Changed" entry states this.

Model reducers in causal mode

  • FastICA: scikit-learn's sign convention divides by the loading on the first column, which is constant for vectorized correlations, and gave fits with NaN; causal fits now get the varying columns first, and at most as many components as the rank of the training rows.
  • A model asked for at least as many components as columns changes nothing, also when the training rows are fewer than the columns; an explicit n_components given with the model is treated as the number asked for.
  • A model object given by the caller is cloned, not fitted in place.

Also

  • A Mexican-hat kernel is exactly 0 at one lag; the first column of a chunk is now taken from the kernel's span, not from its weights.

Timings (autofc, loaded machine): timecorr T 5000 x 50: 0.34 s to 0.36 s; features order 2 with "strength", T 2000 x 200: 3.75 s to 4.16 s.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL

jeremymanning and others added 13 commits November 23, 2026 21:03
The causal estimator computed a row with shapes that depended on the
length of the series: the last group of kernel rows had fewer rows and
columns, the group size came from the last column of an incomplete
chunk, and the blocks of features depended on T. BLAS results of one
matrix product depend on which other rows and columns are in it, so the
rows before a cut differed in the last bits from those of the shorter
series (and tstat_combine divided that by a spread near zero).

- _causal_groups: every group has the row count and columns of a
  complete chunk of a series without an end; _group_block and
  _group_span fill rows and columns beyond the data with zeros.
- _time_slices(always=True): a LagKernel chunk's first column is that
  of LagKernel.span, not found from non-zero weights.
- _causal_block: each row of features is one item of a stacked matmul;
  the triangle of an autocorrelation in aligned sets of 8 rows of
  features; _causal_pairwise the same way.
- _causal_smooth: chunks padded to 128 rows and to the columns up to
  the end of the chunk.

Tests: removal compared with np.array_equal at cuts at and around
multiples of 16 and 128, every built-in kernel, events, NaN samples,
min_samples, wide kernels, 150 features, every length from 1, features
with and without fitted reducers, FeaturesModel.transform, refits.
Docs, CHANGELOG, CLAUDE.md and the causal tutorial (executed again)
state what is exact and what stays documented to 1e-9 (kernels of the
user's).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
isfc of two subjects computes the same correlations in two orders; where
they differed in their last bits, the t statistic was the mean divided
by that round-off (finite |t| up to 1.3e16 next to +-inf, with scipy's
precision-loss RuntimeWarning). An entry whose values have a range of
at most 128 eps (times the largest magnitude above 1;
helpers._round_off_spread) now has the value of an entry without
spread: +-inf with the sign of the mean, 0 for a mean of 0, in causal
and non-causal mode. Entries of subjects that differ are unchanged bit
for bit (3000 random lists of 3 to 6 matrices; a real spread of 1e-8
stays finite).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
…measured now

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
[-inf, +inf] has an infinite range and an infinite magnitude, and
inf <= 128 eps * inf is true, so the entry got t = 0, p = 0 where it
had NaN, NaN; [+inf, 0] changed likewise. The round-off rule now needs
finite values. Every list of 2 and 3 of ten values with an infinite or
NaN value (708 lists) gives what commit 589c2a1 gave
(tests/fixtures/tstat_nonfinite_reference.npz).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
_kernel_samples summed each kernel row over the columns of the series,
and the last bit of a sum depends on its number of terms: a row whose
effective size equals min_samples was NaN with 17 rows and finite with
15. The sums are now over the columns of a chunk of a series without an
end (zeros beyond the data). _causal_smooth takes the same columns for a
kernel array, whose chunks span every column.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
Two findings of the review of the exact-removal branch.

Streamed steps: _symmetrize_block averaged a pair with its transpose
only when both features were in the block, and the blocks depend on T
and max_memory_gb (block sizes 17 and 8 differed in 982 entries of the
reviewer's input). In causal mode a streamed block now keeps every
entry as the product of its own row of features gave it and sets the
diagonal only.

Frozen models: _FittedModel.apply gave hypertools all rows in one
product, whose bits depend on the number of rows (refit_every with PCA
at order 3 differed between 259 and 127 rows). Each array is now reduced
128 rows at a time, the last batch filled with rows of zeros.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
FastICA (Linux CI failure "slice 0 has a NaN entry"): scikit-learn
multiplies each whitening vector by the sign of its loading on the first
column, which for vectorized autocorrelations is a diagonal entry, 1 in
every row. The loading is 0 up to the round-off of the SVD, and where it
is exactly 0 the fit holds NaN (136 of 240 order-2 calls on macOS). A
causal FastICA fit now gets the columns that vary first
(_varying_first, _FittedModel.order) and at most as many components as
such columns; no varying column raises a ValueError that says so.

Frozen models: only the models of the guarantee (exact scikit-learn
classes) are reduced in zero-filled batches of 128 rows; any other model
gets all rows together as before, since its transform may read the rows
it is given.

tests/test_causal_removal_random.py: causal timecorr and features calls
drawn at random (T, features, subjects, kernels, min_samples at
effective sizes that occur, events, NaN/inf, combine functions,
reducers, refit_every, orders, memory limits), each compared bit for
bit with the call on the first rows and, for fitted pipelines, with
FeaturesModel.transform: 300 draws in every run, 3000 marked slow.
Streamed steps compared across memory limits. Docs say what the
exactness rests on.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
The docstring, CHANGELOG and CLAUDE.md now say that the bound is 128 eps,
or that times the largest magnitude when it exceeds 1, and that values
closer than 2.8e-14 are one value whatever their size (1e-16 and 2e-16
give +inf; pinned by a test). Measured: the same correlation below 0.01
computed in two orders differs by at most 0.19 eps, up to 14,000 eps of
its own size, so a purely relative bound would miss it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
Two findings of the third review.

A model asked for at least as many components as columns returns the
columns as they are; the handling of FastICA's constant columns came
before that shortcut and fitted one component with zero columns (K=2,
ndims=3) or raised (K=1). It now applies only when a model is fitted.

_varying_first recognized the name only. A model dict with scikit-learn's
class or an object of it is FastICA too (_is_fastica); an object whose
n_components is None or more than the columns that vary is fitted as a
clone with that number.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
FastICA divides by the singular values of its centered rows. Asked for
more components than their rank (9 defined training rows and 15
components; 10 rows of rank 4 at the start of a series), its fit did not
reproduce the training rows and the call raised. It now gets at most
numpy.linalg.matrix_rank of the centered rows; the others are zero
columns, with the warning of reduce.

Tests of this branch: two assertions required a round-off difference to
exist (a product of all rows differing with the number of rows; the two
orientations of isfc differing in some bit), which holds with Accelerate
and not with OpenBLAS. They are comments now.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
A draw was discarded on any ValueError or MemoryError. A draw is now
left out only when its message matches one of the invalid configurations
named in INVALID; any other exception fails the test, and so does a
shorter series that does not raise for the same reason as the whole one
(a memory limit excepted).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
Two findings of the fourth review, both in helpers._model_reduce (one
function, _causal_components, so one commit).

The components were capped by the number of training rows before the
request was compared with the number of columns: with 4 defined training
rows, 10 columns and ndims=10 a model was fitted and padded (FastICA
differed from the correlations by 31.9, PCA by 2.63). A model asked for
at least as many components as columns is now not fitted, whatever the
number of rows.

n_components given with the model (kwargs or params of a model dict, or
a model object) is what hypertools uses in place of ndims, and it
bypassed the caps: FastICA with n_components=4 on rows of rank 3 did not
reproduce them. It is now the number asked for, replaced by the number
the rows give (rows; for FastICA their rank) when it is larger, with
zero columns for the rest, for every model.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
…cumented

A model object given in a model dict was fitted in place by a causal
call; a clone is fitted now and the object stays as it was.

The width with n_components given with the model is that of the call
without causal=True (measured for PCA, kwargs and object forms): the
number given when a model is fitted, the input's columns with zero
columns up to ndims when none is.  The docstrings and the changelog
said otherwise and are corrected; a test pins the widths, also through
FeaturesModel.transform and after pickling.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtyroRyyp5UK7L9zwqxL
@jeremymanning
jeremymanning marked this pull request as ready for review November 24, 2026 05:48
@jeremymanning
jeremymanning merged commit bbf20fd into master Nov 24, 2026
6 checks passed
@jeremymanning
jeremymanning deleted the causal-removal-exact branch November 24, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant