Colab Primer

How to run the course notebooks in Google Colab

Every notebook in this course runs in Google Colab. Here is an example notebook to try the steps below on.

Why Colab

Setting up Python for empirical finance on your own laptop used to eat the first week of a course like this. You installed Python (or Anaconda), then pandas, numpy, statsmodels and matplotlib, then found that two packages wanted different versions of a third. Windows and Mac behaved differently. A 2 GB dataset filled your disk and a regression over millions of rows froze your laptop. And when something broke, “it works on my machine” was no help to anyone.

Colab makes all of that go away:

1. Open and save your own copy

  1. Click the notebook's Colab link. It opens a read-only copy straight from the course's GitHub repository.
  2. Choose File → Save a copy in Drive before you edit anything. Your edited notebook now saves to your own Google Drive, in a folder called Colab Notebooks. If you skip this step, your changes are lost when you close the tab.

2. Run cells

3. Where things live

This is the one part of Colab worth understanding properly. Your notebook and your data live in different places:

Place What is there How long it lasts
Your Google Drive Your saved notebook (the code, text and last outputs) Permanent
The Colab machine, a temporary computer at Google; its working folder is /content Downloaded data (e.g. /content/data), packages you installed, every variable in memory Until the machine shuts down

When you open a notebook, Colab rents you a fresh machine. The setup cell then downloads the class data onto that machine. In our notebooks you paste the data's Google Drive link (from the week's password-protected notes page) and run the cell:

DATA_ZIP_LINK = "https://drive.google.com/file/d/..."

Click the folder icon in the left sidebar to see the machine's files. You can also look from code: a line that starts with ! runs a command on the machine itself.

!pwd           # where am I?  -> /content
!ls -lh data   # what did the setup cell download?

The machine shuts down after about 90 minutes of inactivity, and after 12 hours at most. When it does, your notebook is safe in Drive, but the data, installed packages and variables are gone. That is normal: run the notebook again from the top, setup cell included, and everything comes back in a minute.

If you create files you want to keep (say, a cleaned dataset for your project), save them to Drive, not to /content. Connect your Drive to the machine first:

from google.colab import drive
drive.mount("/content/drive")             # approve access in the pop-up
df.to_parquet("/content/drive/MyDrive/qis_project/clean.parquet")

For one-off files, you can also drag a file from your computer into the folder sidebar to upload it, or right-click a file there to download it. Uploaded files go to the temporary machine too.

4. Install any package you need

The machine is yours while it runs, so you can install any Python package from PyPI with one line in a code cell:

!pip install wrds linearmodels

It takes a few seconds and needs no permission or setup. Two things to know:

5. When something breaks

6. Connect to WRDS

Some notebooks, and your project, pull data directly from WRDS. You need a WRDS account for this (register with your UMD account).

!pip install wrds
import wrds
db = wrds.Connection()   # asks for your WRDS username and password

Type your username and password in the boxes that appear under the cell. Approve the Duo prompt on your phone if one arrives. If asked whether to create a .pgpass file, answer n: it would be saved on the temporary machine and erased when it shuts down. The query results come straight onto the Colab machine; nothing is saved on your laptop.

7. Use the AI assistant, carefully

Colab has a built-in AI assistant (Gemini) that can explain code, fix errors and write new cells. Use it; that is part of the course. But treat its code like any code you did not write: run it, read it, and check its output against what you expect.

8. Submit your work

The views expressed here are my own and do not necessarily represent those of the Federal Reserve Board or its staff.