title calculating the hubble parameter description the hubble parameter h is the universe expansion rate in any time in cosmology is customary to use the redshift redshift in place of time becausethe redshift is directily mensure in the light of galaxies moving away from us so the general relation that we obtain is h hubbleconstantradiationdensityredshift14 matterdensityredshift13 curvatureredshift12 darkenergy12 where radiationdensity matterdensity darkenergy are the relativity the percentage energy densities that exist in the universe today here matterdensity is the sum of the barion density and the dark matter curvature is the curvature parameter and can be written in term of the densities by the completeness curvature 1 matterdensity radiationdensity darkenergy source https www sciencedirect comtopicsmathematicshubbleparameter input parameters hubbleconstant hubble constante is the expansion rate today usually given in kmsmpc radiationdensity relative radiation density today matterdensity relative mass density today darkenergy relative dark energy density today redshift the light redshift returns result hubble parameter in and the unit kmsmpc the unit can be changed if you want just need to change the unit of the hubble constant hubbleparameterhubbleconstant68 3 radiationdensity1e4 matterdensity0 3 darkenergy0 7 redshift1 traceback most recent call last valueerror all input parameters must be positive hubbleparameterhubbleconstant68 3 radiationdensity1e4 matterdensity 1 2 darkenergy0 7 redshift1 traceback most recent call last valueerror relative densities cannot be greater than one hubbleparameterhubbleconstant68 3 radiationdensity1e4 matterdensity 0 3 darkenergy0 7 redshift0 68 3 run doctest demo lcdm approximation input parameters hubble_constant hubble constante is the expansion rate today usually given in km s mpc radiation_density relative radiation density today matter_density relative mass density today dark_energy relative dark energy density today redshift the light redshift returns result hubble parameter in and the unit km s mpc the unit can be changed if you want just need to change the unit of the hubble constant 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 run doctest demo lcdm approximation
def hubble_parameter(
    hubble_constant: float,
    radiation_density: float,
    matter_density: float,
    dark_energy: float,
    redshift: float,
) -> float:
    
    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__":
    import doctest

    
    doctest.testmod()

    
    matter_density = 0.3

    print(
        hubble_parameter(
            hubble_constant=68.3,
            radiation_density=1e-4,
            matter_density=matter_density,
            dark_energy=1 - matter_density,
            redshift=0,
        )
    )