Correlate¶
Correlation across a lag range, with a multiple-comparison correction and an effective sample size.
Correlation between two series across a range of lags.
Why this exists. A zero-lag correlation asks whether two things rise and fall together at the same instant. That is rarely the question. This project's underlying question --- when does an action begin relative to the sound it makes --- is entirely about displacement in time, and a relationship offset by a second is invisible to a correlation computed where the two series happen to sit.
The sign convention, stated once. A positive lag means y follows x. If the
motion comes two seconds after the sound, best_lag_s is +2.0. Get this backwards and
every conclusion inverts, so it is asserted in both directions in the tests.
Why the p-value is corrected. Scanning many lags and reporting the best one is a multiple comparison. With enough lags, some pair of unrelated series will always show a healthy-looking r somewhere, and quoting its uncorrected p would be a false positive dressed as a finding. Both are returned, and the corrected one is what a claim rests on.
The correction is Bonferroni over the number of lags examined, which is conservative:
neighbouring lags of a smooth series are far from independent, so the true number of
independent tests is smaller than n_lags and the real p lies between the two reported
values. Conservative is the right direction to err when the purpose is checking a claim
rather than making one.
Why the sample size is discounted too. Bonferroni handles scanning many lags. It does
nothing about the other inflation, which is larger: consecutive samples of a smooth series
are not independent observations. One-second bins of a motion envelope are mostly a copy
of the bin before, so treating n bins as n observations inflates every t statistic and
turns autocorrelated noise into a finding. n_effective discounts the length by how much
each series repeats itself, after Bartlett, and the reported p-values use it. On this
project's corpus that was not academic: one session's best lag moved from p < 0.001 to
p = 1.0 once the effective size was used.
Related, and not the same. _alignment.xcorr_lag also sweeps a lag and returns the
best one; use it when the lag itself is the answer. Use this when the answer is a CLAIM
about the lag, because a claim needs the two corrections below and xcorr_lag provides
neither.
Ported from xcov() in https://github.com/finn42/Laughter_Dance by Finn Upham,
accompanying Upham et al., Frontiers in Psychology 2026,
doi:10.3389/fpsyg.2026.1754425. The sign convention and the multiple-comparison
correction are additions; the idea of sweeping the lag is theirs.
LaggedCorrelation
dataclass
¶
LaggedCorrelation(lags_s, r, best_lag_s, best_r, p_uncorrected, p_corrected, n_lags, n_overlap, n_effective)
The result of sweeping a correlation across lags.
Attributes:
| Name | Type | Description |
|---|---|---|
lags_s |
ndarray
|
The lags examined, in seconds, ascending. Positive means
|
r |
ndarray
|
Pearson correlation at each lag, aligned with |
best_lag_s |
float
|
The lag of the strongest correlation, by absolute value. |
best_r |
float
|
The correlation there. NaN when either series is constant. |
p_uncorrected |
float
|
Two-sided p for |
p_corrected |
float
|
|
n_lags |
int
|
How many lags were examined. |
n_overlap |
int
|
Samples overlapping at |
n_effective |
float
|
|
lagged_correlation ¶
lagged_correlation(x, y, fs, max_lag_s)
Correlate x against y at every lag out to max_lag_s.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
First series, one dimension. |
required | |
y
|
Second series, same length as |
required | |
fs
|
float
|
Sampling rate of both series, in samples per second. |
required |
max_lag_s
|
float
|
Largest lag to examine, in seconds, in both directions. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
LaggedCorrelation |
LaggedCorrelation
|
The sweep, its peak, and both p-values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the series differ in length, or |
Source code in musicalgestures/_correlate.py
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 | |
cross_recurrence ¶
cross_recurrence(x, y, fs, dim=3, delay_s=1.0, radius_quantile=0.2, max_lag_s=20.0, min_line=2, n_surrogates=200, seed=0)
Cross-recurrence quantification of two series, against circular-shift surrogates.
Two trajectories are recurrent where their delay-embedded states come within a radius of
each other. The share of such points (recurrence rate) is fixed here by choosing the radius
as a quantile of all distances, so what varies is their structure: determinism (the share
lying on diagonal lines of at least min_line points, where the two evolve alike for a
while), the mean line length, and the diagonal profile --- recurrence as a function of lag,
whose peak says at what delay the two run alike. Each is compared with the same statistic
for y circularly shifted, because with the radius fixed a high determinism is easy to
get from two smooth series that have nothing to do with each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x, y
|
The two series, equal length and rate; NaN is filled with the mean. |
required | |
fs
|
float
|
Sampling rate. |
required |
dim
|
int
|
Embedding dimension. Defaults to 3. |
3
|
delay_s
|
float
|
Embedding delay in seconds. Defaults to 1.0. |
1.0
|
radius_quantile
|
float
|
Distance quantile defining recurrence. Defaults to 0.2. |
0.2
|
max_lag_s
|
float
|
Range of the diagonal profile. Defaults to ±20 s. |
20.0
|
min_line
|
int
|
Shortest diagonal counted as a line. Defaults to 2. |
2
|
n_surrogates
|
int
|
Circular shifts. Defaults to 200. |
200
|
seed
|
int
|
For the shifts. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
|
dict
|
|
|
dict
|
|
|
dict
|
recurrence matrix as |
Source code in musicalgestures/_correlate.py
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 | |