TheAlgorithms/Python · error · ValueError

All input parameters must be positive

Error message

All input parameters must be positive

What it means

Raised by hubble_parameter() when any of redshift, radiation_density, matter_density, or dark_energy is negative. The function computes the Hubble parameter H(z) from density parameters that are physically non-negative fractions. Zero is allowed (the doctest shows redshift=0 returning hubble_constant unchanged).

Source

Thrown at physics/hubble_parameter.py:74

    >>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
    ... matter_density=-0.3, dark_energy=0.7, redshift=1)
    Traceback (most recent call last):
    ...
    ValueError: All input parameters must be positive

    >>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
    ... matter_density= 1.2, dark_energy=0.7, redshift=1)
    Traceback (most recent call last):
    ...
    ValueError: Relative densities cannot be greater than one

    >>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,
    ... matter_density= 0.3, dark_energy=0.7, redshift=0)
    68.3
    """
    parameters = [redshift, radiation_density, matter_density, dark_energy]
    if any(p < 0 for p in parameters):
        raise ValueError("All input parameters must be positive")

    if any(p > 1 for p in parameters[1:4]):
        raise ValueError("Relative densities cannot be greater than one")
    else:
        curvature = 1 - (matter_density + radiation_density + dark_energy)

        e_2 = (
            radiation_density * (redshift + 1) ** 4
            + matter_density * (redshift + 1) ** 3
            + curvature * (redshift + 1) ** 2
            + dark_energy
        )

        hubble = hubble_constant * e_2 ** (1 / 2)
        return hubble


if __name__ == "__main__":

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp or validate all four parameters (redshift, radiation_density, matter_density, dark_energy) to >= 0 before the call.
  2. If you need past-epoch (negative redshift) values, compute them outside this function or patch the guard, since the library intentionally forbids z < 0.
  3. Catch ValueError and report which parameter was negative.

Example fix

# before
hubble_parameter(68.3, 1e-4, 0.3, 0.7, -0.5)  # ValueError

# after
hubble_parameter(68.3, 1e-4, 0.3, 0.7, 0.0)  # z >= 0 only
Defensive patterns

Strategy: validation

Validate before calling

params = {'redshift': z, 'radiation_density': wr, 'matter_density': wm, 'dark_energy': wl}
bad = [k for k, v in params.items() if v < 0]
if bad:
    raise ValueError(f'negative cosmology params: {bad}')
hubble_parameter(hubble_constant, wr, wm, wl, z)

Try / catch

try:
    hubble_parameter(h0, wr, wm, wl, z)
except ValueError as e:
    # distinguish the two guards by message
    print('cosmology input invalid:', e)

Prevention

When it happens

Trigger: hubble_parameter(hubble_constant=68.3, radiation_density=-1e-4, matter_density=0.3, dark_energy=0.7, redshift=1), or a negative redshift such as redshift=-0.5 intended to model an earlier epoch.

Common situations: Using negative redshifts for past cosmological epochs (physically standard, rejected here); sign errors when converting between density conventions; data-entry mistakes in parameter tables.

Related errors


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