Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

name: op-test-chisq
description: Chi-square independence test on a 2D rows × cols contingency table, or goodness of fit of rows against expected proportions.
kind: operator
category: TEST
operator: TEST_CHISQ
type: reference
applies_to: process, compose, predict
examples_tags: [hypothesis-test, tier-1-test, cross-tabulation, proportion-analysis, streaming-friendly]

Tests emit statistic / p-value / effect size; no Response.Components.

Use when

Checks whether two categorical fields are associated, or whether one categorical field’s counts match planned proportions.

Questions it answers:

  • Is preferred channel associated with age band?
  • Is the mix of plan types different across regions?

Use something else:

  • TEST_FISHER_EXACT when the table is 2x2 and some expected counts are small.
  • TEST_SPEARMAN_R when both fields are numeric.

Params

alpha is a top-level key; the rest go in params.

NameTypeDefaultDescription
alphafloat0.05Significance level in (0, 1); reject_null when p < alpha.
expectedobject—{level: proportion}: goodness of fit (variant goodness_of_fit), cols omitted; also the sample-ratio-mismatch check. Rules: test-variant-options.
correctboolfalseYates, 2×2 only: test-variant-options.
  • weight — table of Σw per cell. Frequency: Pearson on the Σw table. Probability: Pearson on the table scaled to n_eff — first-order Kish, not Rao-Scott (survey::svychisq). expected_min, the < 5 guard, V, φ read it; details add scalar sum_weights (+ n_eff); n stays raw rows.

Inputs

rows (required), cols (required unless expected) — categorical: categorical_u8/u16/u32, or packed_bool (levels named "0"/"1"; labels sorted byte-wise, as for any categorical). field is ignored.

Output

statistic = χ² (Yates only under correct); df = (rows−1)(cols−1) (goodness of fit: labels − 1); p_value via χ² survival. Goodness of fit details: labels (sorted), observed, expected (counts; Σw weighted), expected_proportions, n, expected_min. details: contingency (indexed by row_labels/col_labels, both sorted byte-wise), row_totals/col_totals, n, expected_min. effect_size.cramers_v = √(χ²/(n·(min(r,c)−1))); effect_size.phi = √(χ²/n), 2×2 only.

Reading the output

  • statistic: Pearson’s chi-square adds up, over every cell, how far the observed count is from the count expected if the two fields were unrelated. Larger values mean the table departs more from that pattern. With params.expected (Variant goodness_of_fit) the cells are the rows levels and the expected counts come from the given proportions, so it measures how far the observed mix is from the planned one.
  • details.expected_min: The smallest expected count across all cells.

Contract

  • Types (rows, cols): categorical_*, packed_bool; any other scalar type is PULSE_FIELD_TYPE_UNSUPPORTED.
  • Weight: weighted under frequency and probability weights.
  • Multiplicity: a multiplicity method adds p_adjusted and significant_adjusted beside the raw p; families request (default), compose (inside Compose).
  • Streaming: streamable.

Gotchas

  • Any expected cell < 5 emits PULSE_TEST_EXPECTED_COUNT_TOO_LOW.
  • Filter sparse high-cardinality axes first (memory).
  • With groups: one result per group, each with group_key; rows / cols a grouper → PULSE_TEST_CONTINGENCY_DEGENERATE; a single-level group → null figures + a warning (statistical-testing).
  • Pairs with a crosstab; OVERLAY_CHISQ_VS_POP is the FACET-host equivalent.

See