TheAlgorithms/Python · error · ValueError

All parameters must be positive.

Error message

All parameters must be positive.

What it means

Raised by the intensity function in physics/rainfall_intensity.py when any of coefficient_k, coefficient_a, coefficient_b, coefficient_c, return_period, or duration is <= 0. The empirical IDF (intensity-duration-frequency) formula i = k*T^a / (duration + b)^c requires all six parameters strictly positive; the single shared message does not identify which parameter failed.

Source

Thrown at physics/rainfall_intensity.py:133

    Traceback (most recent call last):
    ...
    ValueError: All parameters must be positive.

    >>> rainfall_intensity(1000, 0.2, 11.6, 0.81, 10, 0)
    Traceback (most recent call last):
    ...
    ValueError: All parameters must be positive.

    """
    if (
        coefficient_k <= 0
        or coefficient_a <= 0
        or coefficient_b <= 0
        or coefficient_c <= 0
        or return_period <= 0
        or duration <= 0
    ):
        raise ValueError("All parameters must be positive.")
    intensity = (coefficient_k * (return_period**coefficient_a)) / (
        (duration + coefficient_b) ** coefficient_c
    )
    return intensity


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check each of the six values is > 0 before the call; log all offenders since the error message does not name one
  2. Verify the IDF coefficient table for the region (k, a, b, c must all be positive from the regression source)
  3. Pass duration >= 1 minute (in whatever unit the coefficients were fitted with), never 0
  4. Confirm argument order matches the function signature when porting formulas from literature

Example fix

# before
intensity(0, 0.2, 10, 0.8, 5, 60)  # k=0 slips through from an empty table cell
# ValueError: All parameters must be positive.

# after
params = {'k': 32.5, 'a': 0.2, 'b': 10, 'c': 0.8, 'return_period': 5, 'duration': 60}
assert all(v > 0 for v in params.values()), params
intensity(**params)
Defensive patterns

Strategy: validation

Validate before calling

params = {
    'coefficient_k': k, 'coefficient_a': a, 'coefficient_b': b,
    'coefficient_c': c, 'return_period': T, 'duration': D,
}
bad = [name for name, v in params.items() if v <= 0]
if bad:
    raise ValueError(f"non-positive IDF parameters: {bad}")
intensity(k, a, b, c, T, D)

Try / catch

try:
    i = intensity(k, a, b, c, T, D)
except ValueError:
    raise ValueError(f"check IDF params k={k} a={a} b={b} c={c} T={T} D={D}")

Prevention

When it happens

Trigger: intensity(k=0, a=0.2, b=10, c=0.8, return_period=5, duration=60); any call where duration=0 (common when testing instantaneous intensity) or return_period=0; negative regional regression coefficients from a malformed IDF table.

Common situations: Loading IDF coefficients from regional tables where some entries are 0 or blank-parsed-as-0; passing duration in the wrong unit that truncates to 0; mixing up argument order so a duration lands in return_period.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/3a0ca8229070584b. Report an issue: GitHub.