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:
- Nothing to install. A browser and a Google account are enough. Python and the standard data-science packages (pandas, numpy, scipy, statsmodels, scikit-learn, matplotlib) are already there.
- Everyone has the same setup. When a notebook runs for me, it runs for you, on any laptop, tablet or lab computer.
- Google's machine does the work. The code runs on a computer in Google's data center, not on your laptop. Big downloads and long loops don't slow your laptop down.
- One click from the course page. Notebook links open straight from GitHub, ready to run.
- Missing a package? Install it in one line (see section 4).
1. Open and save your own copy
- Click the notebook's Colab link. It opens a read-only copy straight from the course's GitHub repository.
- 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
- A notebook is a list of cells: text cells explain, code cells run. Click a code cell and press Shift+Enter to run it and move to the next one.
- Run cells from the top, in order. Later cells use variables and data that earlier cells create.
- Runtime → Run all runs the whole notebook. The first run of the day takes a minute while Colab starts a machine for you.
- Anything printed or plotted appears below the cell. Read it: many notebooks ask you a question about the output.
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:
- Installs last only as long as the machine. Put your
!pip installlines in the first cell of the notebook so a fresh run always installs them. - To check whether a package is already there, try importing it, or run
!pip show pandas.
5. When something breaks
NameError: name '...' is not definedalmost always means an earlier cell was not run, or the machine restarted. Run the notebook from the top.FileNotFoundErrorusually means the data is not on this machine (yet). Run the setup cell again.ModuleNotFoundErrormeans a package is not installed:!pip installit.- If the notebook gets into a strange state after you have edited and re-run cells, use Runtime → Restart session and run all.
- Read the last line of an error message first. It usually says what went wrong.
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
- First use Runtime → Restart session and run all. Your notebook should run cleanly from top to bottom on a fresh machine, which is exactly what will happen when someone else opens it.
- To share a link: Share (top right) → set General access to Anyone with the link → Copy link.
- To submit a file: File → Download → Download .ipynb.