Empirical Asset Pricing: A Python Reader’s Companion

Preface

This book aims to provide a reader’s companion to Empirical Asset Pricing: The Cross Section of Stock Returns by Bali, Engle, and Murray (2016) (“BEM”). While I purchased a copy of BEM in 2021, I had never managed to read it in much depth until recently. I think one reason for this is the tight connection between what is presented in the book and the detailed data analysis underlying it. This means one is inevitably thinking about the details of analysis as one is reading the book and, for me at least, wishing that one could run the code while reading the book. Unfortunately, the authors do not provide the source code they used to generate the tables and charts in the book.

With this book, I aim to fill the gap left by BEM by providing code to reproduce every analysis contained in BEM.1

I closely follow the structure of Bali et al. (2016). For example, Chapter 11 of Bali et al. (2016) and 11  The Momentum Effect both cover the momentum effect. I generally follow the table and figure numbers of Bali et al. (2016). Table 11.1 of Bali et al. (2016) corresponds to Table 11.1 here.

Bali et al. (2016) is divided into two parts and I follow that division here. Part I introduces the core ideas using the 1988–2012 “methodologies sample” of Bali et al. (2016). However, I supplement the discussion of Bali et al. (2016) in a few respects. First, because I am providing code, I also discuss the software packages I used to run the analysis included herein, including the production of tables and charts. Second, I expand on some of BEM’s discussions to explain assumed background knowledge or fill gaps in the original explanations.

Part II provides the core empirical analyses of Bali et al. (2016) starting with the construction of the CRSP sample and estimation of market betas before moving on to examine various dimensions studied in prior research on the cross-section of stock returns.

I also include more data visualization than BEM do.

Why Python Polars?

My goal here is to allow readers to open the code behind a table, rerun it in a notebook, and change a breakpoint, a sample screen, or a factor model. My hope is that you can carry the workflow I use here into your own research using software that is freely available.2 To reproduce the results, you’ll also need an internet connection and access to the relevant data from Wharton Research Data Services (WRDS, pronounced “words”).

Pérignon et al. (2024, tbl. 4) report which software 168 research teams used to analyze the same large finance dataset. Stata and Python were each used by 29.2% of teams, followed by R (28.6%), SAS (22.0%), and MATLAB (11.9%).3

My impression is that SAS was even more common than this in the past (e.g., when Bali et al. (2016) was published) and for preparing large datasets in empirical asset pricing. Wharton began using SAS to deliver large financial datasets in the 1990s, and researchers could use SAS to process data on the WRDS server before downloading the results.4 Ordinary data frames in base R (data.frame) and pandas held the data being analyzed in memory. A full extract of the CRSP daily stock file (crsp.dsf), for example, can run to about 20 GB before Parquet compression.5 Stata was also limited to one dataset in memory at a time until Stata 16 introduced frames in 2019.

Today it is easier to work with large datasets in Python and R. Ibis and dbplyr let you write queries in Python or R, respectively, and have the database execute them. For the analyses in this book, I use Polars to work with local Parquet files. Polars can scan Parquet files lazily, reading only the columns a query needs and skipping parts of a file when the filters allow it. Its streaming engine can also process many queries in batches, so the full input does not have to fit in RAM. I also often find Polars expressions easier to read than the corresponding pandas code. If you are new to Polars, or even new to Python, Chapter 2 of the Python Polars edition of Empirical Research in Accounting: Tools and Methods provides a fast-paced introduction to the basic ideas.

With Python Polars, pretty much every analysis in the book can be run in seconds. Even if you are using AI agents to run your analysis, you will appreciate the performance offered by Python Polars, as you will spend less time waiting for output and have less concern that your AI is going to use up all the RAM on your computer.

More information about how to set up your computer to run the analyses in this book is provided in Data.

Data visualization

Chapter 2 of Bali et al. (2016) is unusual in providing a detailed discussion of descriptive statistics. This topic is often skipped over in materials aimed at graduate students in finance and economics, even though experienced researchers will often extol the virtues of these kinds of nuts-and-bolts analyses of research data. However, in some ways I feel that Bali et al. (2016, 16) bury the lede: “While the results presented in the summary statistics table may be sufficient for a reader, they are likely not sufficient for the researcher. It is difficult to conduct high-quality research without having an in-depth understanding of the data.” I couldn’t agree more.

So in this reader’s companion, I occasionally dig a bit deeper than Bali et al. (2016) do. In this regard, I am taking advantage of the fact that I have no publisher-imposed limit on the number of pages I produce. One dimension in particular is that I include more data visualization than is found in Bali et al. (2016), especially when the plots aim to provide the “in-depth understanding of the data” that Bali et al. (2016) suggest is essential for high-quality research.

I have speculated elsewhere about the underutilization of data visualization in research. With regard to descriptive statistics, I conjecture that one reason researchers may use data visualization less than they should is the transaction costs and frictions in moving data from the software used for data analysis to the software used to produce plots.

One approach in empirical finance has been to use SAS to process data and other software to produce figures. Jensen et al. (2023) used SAS to help build their global stock and factor data and R to generate the paper’s figures.6 Similarly, Golez and Matthies (2025) used SAS to process minute-by-minute S&P 500 index option quotes and estimate their main variables, MATLAB to produce figures based on those estimates, and R to produce figures for model calibration.7 Needing to go from one software environment to another to produce plots may discourage a researcher from doing so, especially if the data visualization only serves to understand the data rather than provide output for a paper.

Because this reader’s companion uses Python Polars and it is very easy to produce high-quality graphs, it seems natural to show readers how data visualization can improve one’s understanding of the data. plotnine_polars provides more discussion of the data visualization package used in this book.


  1. The current version of this book omits Chapter 17 of BEM because this requires the OptionMetrics database.↩︎

  2. Bali et al. (2016) do not specify what software they use in writing their book. However, they do mention obtaining SAS data files using SSH (Bali et al. 2016, 104), and it is plausible that they used SAS as the statistical programming language for their book.↩︎

  3. Teams could use more than one package.↩︎

  4. See Ratnaraj’s account of the early WRDS system and Veenman’s guide to obtaining data from WRDS.↩︎

  5. See the discussion of crsp.dsf in Empirical Research in Accounting.↩︎

  6. See the original SAS data code and R figure code. The authors have since rewritten the code that builds the global dataset in Python, using Polars and DuckDB.↩︎

  7. See the authors’ replication guide.↩︎