consistency¶
Phase 3 — range and relationship consistency checks, plus raw-data spot-checks.
consistency
¶
Phase 3 — internal consistency checks, group checks, and spot-checks.
Internal consistency checks confirm that each reported statistic is the kind of number it claims to be (an R-squared in [0, 1], a p-value in [0, 1], a standardized loading in [-1, 1], an OLS-with-intercept residual sum near zero, a coefficient of an expected sign, and so on). Spot-checks recompute a reported value directly from the raw data so the analysis cannot quietly disagree with its source.
Scope notes (see docs/PROTOCOL.md):
- residual_sum ~ 0 is a property of OLS with an intercept. It does NOT
hold for regression through the origin, GLM/logistic, WLS/GLS, or penalized
fits. Declare a column so the tolerance scales to the response magnitude
(a fixed absolute tolerance produces false failures on large-scale data).
- loading in [-1, 1] holds only for standardized (correlation-scaled)
loadings; pass standardized: false for covariance-based/unstandardized
loadings, which legitimately exceed 1.
- variance_explained expects a proportion in [0, 1], not a percentage
(0-100) or an eigenvalue.
- an unexpected coefficient sign is reported as INFO by default — it is often
the substantive finding, not a computation error. Set severity: fail to
make a sign mismatch a hard failure.
consistency_checks(results, project, data_ranges, data_scales=None, near_zero_atol=1e-06)
¶
Run every declared consistency check against the emitted statistics.
Iterates the project's checks mapping and dispatches each declared statistic to
:func:_one, which confirms the reported value is the kind of number it claims to be
(an R-squared in [0, 1], a standardized loading in [-1, 1], an
OLS-with-intercept residual sum near zero, a coefficient of an expected sign, and so
on).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Mapping of statistic name to the value emitted by |
required | |
project
|
The loaded |
required | |
data_ranges
|
Mapping of column name to an observed |
required | |
data_scales
|
Optional mapping of column name to a response magnitude used to
scale the |
None
|
|
near_zero_atol
|
Absolute tolerance for "near zero" comparisons (e.g. the default
|
1e-06
|
Returns:
| Type | Description |
|---|---|
|
A list of phase-3 |
|
|
|
Source code in crossverify/consistency.py
_one(name, value, spec, present, data_ranges, data_scales, near_zero_atol)
¶
Evaluate a single declared statistic according to its kind.
Coerces value to float and dispatches on spec["kind"] to the matching
rule: fixed-interval kinds (r_squared, p_value, proportion,
variance_explained, correlation), loading, count, coefficient,
residual_sum, converged, and centroid. A missing statistic, a
non-numeric value, or an unrecognised kind each yields an appropriate failing or
informational result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Statistic name (used in the result id and description). |
required | |
value
|
The emitted value; coerced via |
required | |
spec
|
The check spec mapping for this statistic; |
required | |
present
|
Whether |
required | |
data_ranges
|
Mapping of column name to observed |
required | |
data_scales
|
Mapping of column name to response magnitude, consulted for
|
required | |
near_zero_atol
|
Absolute tolerance forwarded to the |
required |
Returns:
| Type | Description |
|---|---|
|
A single phase-3 |
|
|
outcomes (e.g. unstandardized loadings, unconstrained coefficient signs, or an |
|
|
unknown kind). |
Source code in crossverify/consistency.py
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 | |
_coefficient(name, rid, v, spec)
¶
Check a coefficient against its expected_sign, defaulting an opposite sign to INFO.
Compares v against spec["expected_sign"] (positive, negative,
nonzero, or any). A coefficient with the opposite sign is frequently the
substantive finding rather than a computation error, so by default a mismatch is
reported as informational; set spec["severity"] to fail to make a sign
mismatch a hard failure instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Statistic name (used in the result description). |
required | |
rid
|
Pre-built result id for this check. |
required | |
v
|
The coefficient value as a |
required | |
spec
|
The check spec; reads |
required |
Returns:
| Type | Description |
|---|---|
|
A phase-3 |
|
|
only when it mismatches and |
|
|
unconstrained sign, an unknown |
Source code in crossverify/consistency.py
_residual_sum(name, rid, v, spec, data_scales, near_zero_atol)
¶
Check that a residual sum is ~ 0, scaling the tolerance to the response magnitude.
|Sigma resid| ~ 0 is a theorem for OLS with an intercept only; it does not hold
for regression through the origin, GLM/logistic, WLS/GLS, or penalized fits. The
tolerance scales to the response magnitude (Sigma|y|) when a column (resolved
via data_scales) or an explicit scale is given, so a correct large-scale fit
is not failed by ordinary floating-point accumulation. The effective tolerance is
atol + rtol * scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Statistic name (used in the result description). |
required | |
rid
|
Pre-built result id for this check. |
required | |
v
|
The reported residual sum as a |
required | |
spec
|
The check spec; reads |
required | |
data_scales
|
Mapping of column name to response magnitude; supplies the scale when
|
required | |
near_zero_atol
|
Fallback absolute tolerance when |
required |
Returns:
| Type | Description |
|---|---|
|
A phase-3 |
|
|
scaled) tolerance. |
Source code in crossverify/consistency.py
group_checks(results, project, n_rows)
¶
Check arithmetic relations across groups of statistics (sums to N, 1, or <= 1).
For each spec in project.group_checks, sums the named statistics and verifies the
declared relation: sum_to_n (equals N, defaulting to n_rows),
sum_to_one (equals 1), or sum_le_one (at most 1). A spec referencing a
statistic absent from results fails with the missing keys listed; an unsupported
kind is skipped as informational.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Mapping of statistic name to value; every key in a spec's |
required | |
project
|
The loaded |
required | |
n_rows
|
Row count used as the default |
required |
Returns:
| Type | Description |
|---|---|
|
A list of phase-3 |
|
|
|
Source code in crossverify/consistency.py
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 | |
spot_checks(results, project, df)
¶
Recompute reported statistics directly from the raw data and compare them.
For each spec in project.spot_checks, recomputes the statistic straight from
df so the analysis cannot quietly disagree with its source. Supports std (with
a configurable ddof, defaulting to the pandas/R convention of 1) and the
aggregations in _AGGS (mean, sum, count, median, min,
max), optionally restricted by a where equality filter. The recomputed value
is compared to the reported one via :func:~crossverify.checks.is_close under the
per-key tolerance from project.tolerance. Any exception during a spot-check is
captured and reported as a failing result rather than propagated, and an unsupported
op is reported as informational.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Mapping of statistic name to the value reported by the analysis; the comparison target for each spot-check. |
required | |
project
|
The loaded |
required | |
df
|
The analyzed |
required |
Returns:
| Type | Description |
|---|---|
|
A list of phase-3 |
|
|
|