Skip to main content
Glama

nonlinear_mixed_lm

Fit a nonlinear model with a per-group random intercept using penalized least squares, returning fixed parameters and random-effect estimates for grouped data.

Instructions

Fit a nonlinear model with one random intercept per group, approximately.

The model is ``y = f(x; params) + b_g + error`` where ``b_g`` is a random
intercept shared by the observations of group ``g``. ``model`` is a sympy
expression in ``x`` (the independent-variable name is always ``x``, taken
from the column named by the ``x`` argument) and the parameter names given
by the keys of ``p0``.

``data`` is either a ``{column: list}`` mapping or an ``"@<path>.csv"``
reference (relative paths resolve against ``output_dir()``). ``groups``,
``x`` and ``y`` are column names. All fixed parameters and every ``b_g``
are optimized together with ``scipy.optimize.least_squares``, minimizing
``sum((y - f(x; params) - b_g)^2) + penalty * sum(b_g^2)``. Setting
``random_effect=False`` drops the ``b_g`` terms and returns no
``random_effects`` field.

This is a **penalized least-squares approximation**, not a full nonlinear
mixed-effects marginal-likelihood fit: random-effect variance components
are not estimated, there is no integration over random effects, and no
standard errors are produced. The response always carries
``approximation: true`` and an explanatory ``note``.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
xYes
yYes
p0Yes
dataYes
modelYes
groupsYes
maxiterNo
penaltyNo
random_effectNo

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.1.0

TDQS

A4.5/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries the full burden and does so well: it discloses that random-effect variances are not estimated, there is no integration, no standard errors are produced, the objective includes a penalty term, and the response always carries approximation: true plus a note. The random_effect=False branch behavior is also spelled out.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The one-line summary is front-loaded before the equation and detail, and nearly every sentence adds substantive information. It is dense with math rather than padded, though the critical approximation caveat sits at the very end where it could be more prominent.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a 9-parameter tool with no annotations and no output schema, the definition covers inputs, optimization behavior, and key return signals (random_effects field, approximation flag, note). It stops short of a full return-shape description, but given there is no output schema this is a reasonable level of detail.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 0%, so the description must compensate, and it largely does: model, x, y, groups, p0, data (both mapping and '@<path>.csv' forms), random_effect, and the role of penalty are all explained. Only maxiter is left undocumented, a minor gap for a conventional solver setting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The opening sentence states a specific verb (Fit) and resource (nonlinear model with one random intercept per group), and the model equation makes the exact form unambiguous. This cleanly distinguishes it from siblings like mixed_lm (linear) and curve_fit (no random effects).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description gives the model form and the important caveat that this is an approximate penalized fit rather than a full NLME, which tells an agent when its results are appropriate. However, it never explicitly names an alternative (e.g., a full mixed-model tool) or states exclusions, so routing is left partly to inference.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.