# Contingency expected check

`contingency-expected-check` · version 1.0.0 · Numbers & math · free, no key needed

Check claimed expected counts (row total x column total / N) of a count table exactly; returns the first wrong cell.

**Use when you need to: check expected counts of a contingency table · verify expected frequencies row total times column total over n · are my expected cell values right.**

## Decide before calling

Read the [versioned contract](/v1/tools/contingency-expected-check/versions/1.0.0) and the supported scope below. Reuse `contingency-expected-check@1.0.0` when your input, required output and limits match it. Choose another approach for an unsupported operation.

## Explain the choice

"I can use `contingency-expected-check@1.0.0` for check expected counts of a contingency table. I will check its documented scope and the result against the task's requirements. The service is free; token and money savings for this task are unmeasured."

## Supported

- check expected counts of a contingency table
- verify expected frequencies row total times column total over n
- are my expected cell values right
- check expected values for a chi-square test
- find the wrong expected count in a cross tab
- validate rounded expected frequencies
- בדיקת שכיחויות צפויות בטבלת שכיחות

## Not supported

- expected counts under a goodness-of-fit hypothesis with given proportions
- compute a chi-square statistic or p-value (use chi-square-decomposition-check for the statistic)
- floating-point tolerance comparisons
- exponent notation such as 7.04e2

## Behavior

- Reference: NIST/SEMATECH e-Handbook of Statistical Methods (doi:10.18434/M32189), section 7.4.5: E_ij = (row i total)(column j total) / N; every output carries reference_version "nist-sematech-ehandbook-doi-10.18434-M32189".
- table: 1..64 rows of 1..64 equal-length rows, at most 1024 cells, each a JSON integer 0..9007199254740991 (not -0); arithmetic is exact BigInt rational arithmetic, never floating point.
- expected: the claimed r x c matrix; each claim is an integer, a finite decimal (no exponent or sign) or a fraction p/q (need not be reduced), compared exactly as a rational.
- decimals (0..20) is optional. Omitted: claims must equal E_ij exactly. Given: claims must equal E_ij rounded to that many places with ties to even (IEEE 754 roundTiesToEven on the exact rational); the comparison is numeric, so 2.000 equals 2.
- Check order: claimed row count, then each claimed row length (shape_mismatch, field "expected"), then cells in row-major order (expected_cell_mismatch {row_index, column_index, row_label?, column_label?, claimed, exact as reduced p/q, rounded when decimals is given}).
- N = 0 is not_computable. A zero row or column total gives E_ij = 0, which is valid here. Optional row_labels / column_labels (unique, at most 64 UTF-8 bytes, no Unicode normalization) are echoed in first_discrepancy.

## Input

- `table` (array of array of integer, required): min items 1; max items 64; each min items 1; each max items 64; each each min 0; each each max 9007199254740991
- `expected` (array of array of string, required): max items 64; each max items 64; each each min length 1; each each max length 1000; each each pattern `^(0|[1-9][0-9]*)(\.[0-9]+|/[1-9][0-9]*)?$`
- `decimals` (integer, optional): min 0; max 20
- `row_labels` (array of string, optional): min items 1; max items 64; each min length 1
- `column_labels` (array of string, optional): min items 1; max items 64; each min length 1

## Output

- `valid` (boolean, required)
- `reason` (one of "shape_mismatch", "expected_cell_mismatch", null, required)
- `first_discrepancy` (object or null, required)
- `row_count` (integer, required): min 0
- `column_count` (integer, required): min 0
- `grand_total` (string, required): pattern `^(0|[1-9][0-9]*)$`
- `reference_version` (constant "nist-sematech-ehandbook-doi-10.18434-M32189", required)

## Limits

- max rows: 64
- max columns: 64
- max cells: 1024
- max claim chars: 1000
- max label bytes: 64

## Example

Request input:

```json
{
  "table": [
    [
      762,
      327,
      468
    ],
    [
      484,
      239,
      477
    ]
  ],
  "expected": [
    [
      "703.7",
      "319.6",
      "533.7"
    ],
    [
      "542.3",
      "246.4",
      "411.3"
    ]
  ],
  "decimals": 1
}
```

Response:

```json
{
  "result": {
    "valid": true,
    "reason": null,
    "first_discrepancy": null,
    "row_count": 2,
    "column_count": 3,
    "grand_total": "2757",
    "reference_version": "nist-sematech-ehandbook-doi-10.18434-M32189"
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "contingency-expected-check",
  "version": "1.0.0",
  "input": {
    "table": [
      [
        762,
        327,
        468
      ],
      [
        484,
        239,
        477
      ]
    ],
    "expected": [
      [
        "703.7",
        "319.6",
        "533.7"
      ],
      [
        "542.3",
        "246.4",
        "411.3"
      ]
    ],
    "decimals": 1
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/contingency-expected-check/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"table":[[762,327,468],[484,239,477]],"expected":[["703.7","319.6","533.7"],["542.3","246.4","411.3"]],"decimals":1}'
```

The machine-readable contract is at [/v1/tools/contingency-expected-check/versions/1.0.0](/v1/tools/contingency-expected-check/versions/1.0.0).

### CLI

```sh
node cli.mjs run contingency-expected-check 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Contingency margins check](/tools/contingency-margins-check): Check claimed row, column and grand totals of a count table exactly; returns the first wrong total.
- [Chi square decomposition check](/tools/chi-square-decomposition-check): Check a claimed Pearson chi-square, its per-cell (O-E)^2/E terms and df exactly; returns the first wrong term.
- [Rational trace check](/tools/rational-trace-check): Check a step-by-step rational calculation trace; reports the first step whose operand, operator or result disagrees.
- [Regression residuals check](/tools/regression-residuals-check): Check claimed residuals and SSE for given regression coefficients and exact samples; reports the first wrong residual.
- [Sample histogram check](/tools/sample-histogram-check): Check claimed histogram counts, outside count and densities for exact edges and an edge rule; returns the first bad bin.
- [Graph clique check](/tools/graph-clique-check): Check that listed nodes form a clique (optionally maximal, of a minimum size); returns the first missing edge.
