# Regression residuals check

`regression-residuals-check` · version 1.0.0 · Numbers & math · free, no key needed

Check claimed residuals and SSE for given regression coefficients and exact samples; reports the first wrong residual.

**Use when you need to: verify regression residuals · check sum of squared errors of a fit · are these residuals correct for my model.**

## Decide before calling

Read the [versioned contract](/v1/tools/regression-residuals-check/versions/1.0.0) and the supported scope below. Reuse `regression-residuals-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 `regression-residuals-check@1.0.0` for verify regression residuals. 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

- verify regression residuals
- check sum of squared errors of a fit
- are these residuals correct for my model
- validate sse of a linear regression exactly
- check residual table of a fitted line
- verify fitted minus observed values exactly
- בדיקת שאריות של רגרסיה

## Not supported

- checking that the coefficients are optimal (use regression-normal-equations-check)
- standardized or studentized residuals
- R squared or residual standard error
- approximate or tolerance-based comparison

## Behavior

- Reference: NIST/SEMATECH e-Handbook of Statistical Methods sec. 4.1.4.1 and 4.4.3.1 (ordinary least squares, normal equations); exact rational arithmetic.
- Model (NIST e-Handbook 4.1.4.1): y_i = sum_j beta_j X_ij + e_i; X has a leading column of ones when intercept is true (required, no default), then the x features in order; coefficient 0 is the intercept when present.
- Values are exact strings: integer, decimal ("3.5") or fraction ("7/2"), never JSON numbers or exponents; comparisons are exact and outputs canonical reduced fractions. difference = claimed - actual.
- Ragged rows, len(y) != rows or empty x are invalid_input; a claimed vector of the wrong length is valid:false dimension_mismatch, checked first.
- Residuals are observed minus fitted, e_i = y_i - sum_j beta_j X_ij, and SSE = sum e_i^2 (NIST e-Handbook 4.4.3.1). The claimed coefficients are taken as given; rank is not checked.
- Check order: coefficient count, residual count, residuals in sample order (residual_mismatch), then SSE (sse_mismatch). sse is the exact SSE of the claimed coefficients, or null when the coefficient count is wrong.
- Limits: 256 samples, 8 features, 30 digits per sample value, 1000 digits per claimed value, 9 claimed coefficients, 256 claimed residuals, and common denominators of at most 100 digits for the x values and for the y values and 1000 digits for the claimed coefficients; over a limit is limit_exceeded.

## Input

- `x` (array of array of string, required): min items 1; max items 256; each min items 1; each max items 8; each each pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+|/[1-9][0-9]*)?$`
- `y` (array of string, required): min items 1; max items 256; each pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+|/[1-9][0-9]*)?$`
- `intercept` (boolean, required)
- `coefficients` (array of string, required): max items 9; each pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+|/[1-9][0-9]*)?$`
- `residuals` (array of string, required): max items 256; each pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+|/[1-9][0-9]*)?$`
- `sse` (string, required): pattern `^-?(0|[1-9][0-9]*)(\.[0-9]+|/[1-9][0-9]*)?$`

## Output

- `valid` (boolean, required)
- `reason` (one of null, "dimension_mismatch", "residual_mismatch", "sse_mismatch", required)
- `sse` (string or null, required): pattern `^-?(0|[1-9][0-9]*)(/[1-9][0-9]*)?$`
- `first_discrepancy` (object or null, required)
- `reference_version` (constant "NIST/SEMATECH e-Handbook of Statistical Methods sec. 4.1.4.1 and 4.4.3.1 (ordinary least squares, normal equations); exact rational arithmetic", required)

## Limits

- max samples: 256
- max features: 8
- max sample digits: 30
- max claim digits: 1000
- max data denominator digits: 100
- max claim denominator digits: 1000

## Example

Request input:

```json
{
  "x": [
    [
      "1"
    ],
    [
      "2"
    ],
    [
      "3"
    ],
    [
      "4"
    ]
  ],
  "y": [
    "6",
    "5",
    "7",
    "10"
  ],
  "intercept": true,
  "coefficients": [
    "3.5",
    "1.4"
  ],
  "residuals": [
    "1.1",
    "-1.3",
    "-0.7",
    "0.9"
  ],
  "sse": "4.2"
}
```

Response:

```json
{
  "result": {
    "valid": true,
    "reason": null,
    "sse": "21/5",
    "first_discrepancy": null,
    "reference_version": "NIST/SEMATECH e-Handbook of Statistical Methods sec. 4.1.4.1 and 4.4.3.1 (ordinary least squares, normal equations); exact rational arithmetic"
  }
}
```

## How to call it

### MCP

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

```json
{
  "id": "regression-residuals-check",
  "version": "1.0.0",
  "input": {
    "x": [
      [
        "1"
      ],
      [
        "2"
      ],
      [
        "3"
      ],
      [
        "4"
      ]
    ],
    "y": [
      "6",
      "5",
      "7",
      "10"
    ],
    "intercept": true,
    "coefficients": [
      "3.5",
      "1.4"
    ],
    "residuals": [
      "1.1",
      "-1.3",
      "-0.7",
      "0.9"
    ],
    "sse": "4.2"
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/regression-residuals-check/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"x":[["1"],["2"],["3"],["4"]],"y":["6","5","7","10"],"intercept":true,"coefficients":["3.5","1.4"],"residuals":["1.1","-1.3","-0.7","0.9"],"sse":"4.2"}'
```

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

### CLI

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

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

## Related tools

- [Linear residual check](/tools/linear-residual-check): Check a claimed residual r = b - A x (per component and/or its max norm) exactly for an approximate solution x.
- [Regression coefficients check](/tools/regression-coefficients-check): Check claimed least-squares regression coefficients against exact samples; reports rank and the first mismatch.
- [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.
- [Contingency expected check](/tools/contingency-expected-check): Check claimed expected counts (row total x column total / N) of a count table exactly; returns the first wrong cell.
- [Contingency margins check](/tools/contingency-margins-check): Check claimed row, column and grand totals of a count table exactly; returns the first wrong total.
- [Regression normal equations check](/tools/regression-normal-equations-check): Check that claimed coefficients and residuals satisfy X^T e = 0 exactly; rejects singular, ambiguous designs.
