Exploring Cosmological Parameters with Functions#

William Cerny, Yasmeen Asali, Pratik Gandhi (Yale University)

Description: Write and call Python functions and numpy array operations to estimate the expansion rate, age, and shape of the universe with the Hubble-Lemaitre Law.

Intended Audience: Early Undergraduate

tags: basic-python, cosmology, f-strings, functions

Requirements: numpy (requirements.txt) (optional)

Last Updated: July 28, 2025

Learning Objectives

  1. Define Python functions and use their outputs within other functions, which is useful for calculations dependent on previous calculations.

  2. Understand how to apply functions over array inputs both with loops and with vectorized operations.

  3. Characterize and identify different models for the universe with the Hubble-Lemaitre Law, a common exercise in introductory astronomy courses.

  4. Document functions properly with type decorators and docstrings.

Background: The Hubble-Lemaitre Law#

The Hubble-Lemaitre law (often referred to as just “Hubble’s Law”) describes the relationship between the velocity of a galaxy and its distance from us:

\[ v = H_0 \times d \]

where \(v\) is is the recession velocity of a galaxy (in km/s), \(H_0\) is the Hubble constant (in km/s/Mpc; assume 70 km/s/Mpc), and \(d\) is the distance of the galaxy (in Mpc). \(H_0\) is the present-day expansion rate of the universe. In general, we cannot measure direct distances to very distant galaxies, and so we measure the recessional velocity \(v\) and can use that to find \(d\). The fact that this correlation exists is a consequence of the fact that our universe is expanding.

Questions 5-6 above get at a core concept in Python: vectorization. In Python, it will essentially always be faster to carry out an operation over a vector (array) simultaneously (part 5) as opposed to using a loop (part 4). This might not matter for small datasets but will become increasingly relevant as your dataset grows.

Now we can make our approach even more sophisticated! We will now build subsequent functions that expand upon the one above. This incremental function-building is a core component of research, since you rarely write a single function that does everything you want.

Background: The Cosmic Critical Density#

The cosmic critical density is the average density of matter and energy needed for the universe to be flat. If the actual density is higher, the universe is “closed” and may eventually collapse; if the density is lower, the universe is “open” and will expand forever. The density can be calculated from the Hubble Constant via \(\rho_c = \frac{3H_0^2}{8\pi G}\)

Background: The Age of the Universe#

Because the universe has been continuously expanding since the origin of the universe, the present-day expansion rate can be used to approximate the age of the universe. Put explicitly, the age of the universe can be estimated roughly as \( t = \frac{1}{H_0}\), but you need to be careful about units: you need to first convert \(H_0\) to units of inverse seconds for this to work.