Tools for agents: Exact least-squares line fit

Fit a straight line to observations and compute exact rational coefficients, residuals and prediction values.

Connect with MCP

{
  "mcpServers": {
    "super-agent-tools": {
      "url": "https://app.getsupers.com/sites/agent-line-fit/mcp"
    }
  }
}
Streamable HTTP · No API key · Stateless

Use this URL in any client supporting remote Streamable HTTP MCP. Calls return structured JSON; inputs are processed without persistence.

Call fit_least_squares_line

Fits y=intercept+slope*x by ordinary unweighted least squares with an intercept, minimizing vertical squared errors. Supply at least two observations with at least two distinct x values; repeated x values and duplicate observations are allowed and each counts separately. Inputs are integers or quoted finite decimal/fraction strings; JSON floats and booleans are rejected. All numeric results are reduced rational strings. residual means observed y minus predicted y. mean_squared_error is SSE divided by sample_count, not a degrees-of-freedom adjusted variance estimate. R-squared is 1-SSE/SST; it is null for constant observed y because SST is zero. predict_x may be empty; predictions preserve its order. This computes a descriptive fit, not causal effects, confidence intervals, robust regression, error-in-variables regression or evidence that extrapolation is reliable. No observations are fetched, generated or persisted.

Input and output schemas
{
  "name": "fit_least_squares_line",
  "title": "Exact least-squares line fit",
  "description": "Minimize squared vertical residuals for y=intercept+slope*x with exact rational arithmetic. Return coefficients, training residuals, predictions, SSE and R-squared.",
  "inputSchema": {
    "properties": {
      "samples": {
        "items": {
          "maxItems": 2,
          "minItems": 2,
          "prefixItems": [
            {
              "anyOf": [
                {
                  "type": "integer"
                },
                {
                  "type": "string"
                }
              ]
            },
            {
              "anyOf": [
                {
                  "type": "integer"
                },
                {
                  "type": "string"
                }
              ]
            }
          ],
          "type": "array"
        },
        "title": "Samples",
        "type": "array"
      },
      "predict_x": {
        "items": {
          "anyOf": [
            {
              "type": "integer"
            },
            {
              "type": "string"
            }
          ]
        },
        "title": "Predict X",
        "type": "array"
      }
    },
    "required": [
      "samples",
      "predict_x"
    ],
    "title": "fit_least_squares_lineArguments",
    "type": "object"
  },
  "outputSchema": {
    "additionalProperties": true,
    "title": "fit_least_squares_lineDictOutput",
    "type": "object"
  },
  "icons": null,
  "annotations": {
    "title": null,
    "readOnlyHint": true,
    "destructiveHint": false,
    "idempotentHint": true,
    "openWorldHint": false
  },
  "meta": null,
  "execution": null
}

Run the example to see the actual result.

HTTP alternative

POST https://app.getsupers.com/sites/agent-line-fit/call
Content-Type: application/json

{
  "samples": [
    [
      0,
      1
    ],
    [
      1,
      2
    ],
    [
      2,
      2
    ]
  ],
  "predict_x": [
    "3/2",
    3
  ]
}

The HTTP and MCP interfaces execute the same implementation. Validation errors are returned explicitly. See the tool notes above for its supported inputs and behavior.

Vertical residuals define an exact descriptive line fit

Read the explanation

The three supplied observations are zero one, one two and two two. Their mean x is one and mean y five thirds. Centered x squared sum is two and centered x y product sum one, so slope is one half. Intercept is five thirds minus one half, or seven sixths. At one hundred twenty pixels per unit slope measures sixty and intercept one hundred forty. This is hand-derived arithmetic from the saved fixture, not an executed HTTP or MCP result. At the three training x values the predictions are seven sixths, five thirds and thirteen sixths. Observed minus predicted residuals are negative one sixth, one third and negative one sixth. Squared residuals are one thirty sixth, one ninth and one thirty sixth. At two thousand one hundred sixty pixels per squared unit the bars are sixty, two hundred forty and sixty. Their sum is one sixth and mean squared error one eighteenth because the divisor is three observations, not a degrees of freedom adjustment. The observed y total centered square is two thirds. R squared is one minus one sixth divided by two thirds, giving three quarters. Prediction at three halves is twenty three twelfths and at three eight thirds. At one hundred pixels per predicted unit the bars measure one hundred ninety one point six seven and two hundred sixty six point six seven, while R squared at the same scale measures seventy five. The fit is descriptive: no causal conclusion or trustworthy extrapolation follows automatically. Inputs must be integers or quoted finite decimal or fraction strings. The saved app is missing offline, so native text editing is preserved while actual backend fitting is not invoked.

Exact Least-Squares Line Fit for Agent Workflows

How does this tool calculate the unweighted ordinary least-squares line and rational statistics from discrete coordinate samples?

This tool implements unweighted ordinary least-squares regression for the linear model y = intercept + slope * x. Using Python's exact Fraction arithmetic, it computes sample coordinate means, the sum of squared deviations for x, and minimizes the vertical sum of squared errors without IEEE-754 floating-point rounding. The resulting coefficients, sample residuals, predictions, SSE, MSE, and coefficient of determination (R-squared) are all formatted as reduced rational strings.

The implementation computes mean_squared_error as SSE / sample_count rather than the unbiased regression variance estimator SSE / (sample_count - 2). If all sample y values are identical, total sum of squares (SST) is zero and r_squared returns null. It only minimizes vertical errors (assuming x values are exact error-free constants) and does not compute parameter confidence intervals, p-values, or robust M-estimators.

Try a worked example

Click 'Run tool' with the preset samples [[0, 1], [1, 2], [2, 2]] and predict_x ['3/2', 3]. The tool processes the inputs, updating the Structured result view with exact rational strings: slope '1/2', intercept '7/6', sample_count 3, sum_squared_errors '1/6', mean_squared_error '1/18', and r_squared '3/4'. Predictions evaluate to '23/12' for x = 3/2 and '8/3' for x = 3.

Exact Rational Normal Equations

The implementation parses all x and y inputs as exact Fraction objects. It calculates sample means mx = (sum x) / n and my = (sum y) / n, followed by the sum of squares sxx = sum((x - mx)^2). If sxx is zero (fewer than two distinct x coordinates), execution halts with a descriptive validation error. Otherwise, the slope is computed as sum((x - mx) * (y - my)) / sxx, and the intercept as my - slope * mx.

Residuals, Goodness of Fit, and Prediction Evaluation

Each sample residual is calculated as observed y minus predicted (intercept + slope * x). Sum of squared errors (SSE) is the sum of squared residuals, and SST is the sum of squared differences from the sample mean of y. R-squared is computed as 1 - SSE / SST whenever SST is non-zero. For each requested query x in predict_x, the line equation intercept + slope * x is evaluated directly using exact rational arithmetic.