Skip to content

crashmetrics.margins

margins

Distance to performance limits, as continuous targets for ML models.

NCAP points and colour bands jump in steps, which regression models learn badly. The distance of each criterion to its limits is continuous, so it is a better training target; apply the scoring rules to the prediction afterwards.

For a criterion value v with higher performance limit HPL and lower performance limit LPL:

  • position = (v - HPL) / (LPL - HPL): 0 at the HPL, 1 at the LPL, negative inside the full-points region, above 1 beyond the LPL.
  • margin_lpl = (LPL - v) / LPL: share of the LPL still available (1 at zero, 0 at the LPL, negative beyond). For a single limit (a regulation limit) this is the safety margin.
  • margin_hpl = (HPL - v) / HPL: the same for the HPL.
  • sliding = clip(1 - position, 0, 1): the linear sliding-scale fraction (1 at or below the HPL, 0 at or beyond the LPL), as used by Bharat NCAP and older Euro NCAP protocols.

Values are compared by magnitude (|v|).

Functions:

  • margin, margins: For any limits (a Limit, an (hpl, lpl) pair or a single limit).
  • euroncap_margins: Euro NCAP 2026 frontal limits of a dummy.
  • margin_features: Flat dict for crashmetrics.features.feature_table.

Margin dataclass

Margin(
    name: str,
    value: float,
    hpl: float | None,
    lpl: float,
    position: float | None,
    margin_lpl: float,
    margin_hpl: float | None,
    sliding: float,
)

Distance of one criterion value to its limits.

position instance-attribute

position: float | None

(v - HPL) / (LPL - HPL); None without an HPL.

sliding instance-attribute

sliding: float

Linear sliding-scale fraction 0..1 (without an HPL: 1 below the LPL, else 0).

margin

margin(name: str, value: float, limit: LimitLike) -> Margin

Margin of one value to a limit (Limit, (hpl, lpl) or one limit).

margins

margins(
    values: Mapping[str, float],
    limits: Mapping[str, LimitLike],
) -> dict[str, Margin]

margin for every value that has a limit (others are skipped).

euroncap_margins

euroncap_margins(
    dummy: str, values: Mapping[str, float], speed: int = 50
) -> dict[str, Margin]

Margins to the Euro NCAP 2026 frontal limits of a dummy (crashmetrics.frontal_scoring.DUMMY_LIMITS; chest limits by speed). Values without a limit for that dummy are skipped.

margin_features

margin_features(
    result: Mapping[str, Margin],
    prefix: str = "",
    fields: tuple[str, ...] = (
        "position",
        "margin_lpl",
        "sliding",
    ),
) -> dict[str, float]

Flatten margins into {"<prefix><criterion>.<field>": value}.

Fields that are None (no HPL) become NaN, so feature tables keep their columns.