rohitg00/ai-engineering-from-scratch · error · ValueError

log scale metric must be positive, got

Error message

log scale metric must be positive, got {v}

What it means

Log-transform guard: a metric declared with LOG scale received a zero or negative value, but log() is only defined on positive inputs (the reason LOG scale suits ratios like perplexity, never raw accuracy deltas).

Solutions

  1. Use LINEAR scale for metrics that can be zero or negative
  2. Filter or floor invalid measurements before evaluation
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/53-result-evaluator/code/main.py:215 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/b5b314f4b68a48e8. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/53-result-evaluator/code/main.py:215

        raise PairingError("no shared seeds between candidate and baseline")
    return [cand_map[s] for s in shared], [base_map[s] for s in shared]


def _improvement(candidate_mean: float, baseline_mean: float, direction: str) -> float:
    denom = abs(baseline_mean) if baseline_mean != 0.0 else 1.0
    raw = (candidate_mean - baseline_mean) / denom
    if direction == LOWER:
        return -raw
    return raw


def _log_transform(values: list[float], scale: str) -> list[float]:
    if scale != LOG:
        return list(values)
    out = []
    for v in values:
        if v <= 0.0:
            raise ValueError(f"log scale metric must be positive, got {v}")
        out.append(math.log(v))
    return out


@dataclass
class EvaluatorConfig:
    improvement_threshold: float = 0.02
    significance_threshold: float = 0.05


class Evaluator:
    """Pure function over (candidate, baseline) result lists; returns a Verdict."""

    def __init__(self, config: EvaluatorConfig | None = None) -> None:
        self._cfg = config or EvaluatorConfig()

    def evaluate(
        self,

View on GitHub (pinned to 39ea8a1c6d)