library(nanoparquet)
crsp_monthly <- read_parquet("data/crsp_monthly.parquet")Exercise 1: Introduction to Tidy Finance
The main aim of this chapter is to familiarize yourself with tidy coding principles. We start by downloading and visualizing stock data before we move to a simple portfolio choice problem. These examples introduce you to our approach of Tidy Finance.
Working with stock market data
Exercises:
- Download daily prices for one stock market ticker of your choice (e.g. AAPL) from Yahoo!Finance. Plot the time series of adjusted closing prices. To download the data you can use the command
download_datafrom thetidyfinancepackage (R) or thetidyfinancelibrary (Python). If you do not know how to use it, read its help:?download_datain R,help(tf.download_data)in Python afterimport tidyfinance as tf. - Compute daily net returns for the asset and visualize the distribution of daily returns in a histogram using
ggplot2(R) orplotnine(Python). Also, usegeom_vline()to add a dashed red line that indicates the 5% quantile of the daily returns within the histogram. - Compute summary statistics (mean, standard deviation, minimum and maximum) for the daily returns.
Solutions: All tasks are solved and described in detail in the book chapter Working with stock returns.
Scaling up the analysis
As a next step, we generalize the code from before such that all the computations can handle an arbitrary vector of tickers (e.g., all constituents of an index). Following tidy principles, it is quite easy to download the data, plot the price time series, and tabulate the summary statistics for an arbitrary number of assets.
Exercises:
- Now the tidyverse magic starts: Take your code from before and generalize it such all the computations are performed for an arbitrary vector of tickers (e.g.,
ticker <- c("AAPL", "MMM", "BA")(R) orticker = ["AAPL", "MMM", "BA"](Python)). Automate the download, the plot of the price time series, and create a table of return summary statistics for this arbitrary number of assets. - Consider the research question Are days with high aggregate trading volume often followed by high aggregate trading volume days? How would you investigate this question? Hint: Compute the aggregate daily trading volume (in USD) and find an appropriate visualization to analyze the question.
- Ask yourself how the visualization could be improved by adding further information (e.g., highlighting days with extreme trading volume), improving axis labels, changing the style (colors, themes, etc.). Implement your ideas and create a final version of the plot. Make sure the plot is self-explanatory (e.g., contains captions, legends, an appropriate title, etc.).
Submit: post your final figure as an image in a reply to the Absalon discussion Exercise 1: aggregate trading volume, with a one-line caption that says what the reader should see, by Sunday 14 February, 23:59.
Solutions: All tasks are solved and described in detail in the sections Scaling up the analysis and Other forms of data aggregation of the book chapter Working with stock returns.
Accessing and managing financial data
Exercises:
Read the book chapters Accessing and managing financial data and WRDS, CRSP, and Compustat entirely; both show the R and the Python code side by side. They describe almost every dataset that matters for research in empirical finance. Since CRSP’s update in July 2022, monthly returns already include delisting returns, so you no longer need to adjust for delistings yourself; the second chapter explains the details.
Download the licensed data of the course from Absalon and save them in the
datafolder of your repository, under exactly these names:crsp_monthly.parquet: monthly returns and market capitalizations of all US common stocks from CRSP (120 MB);volare_realized_measures_daily.parquet: daily realized variances from VOLARE of 40 US stocks and five futures, among them the E-mini S&P 500; the columnasset_classsays which is which,"stock"or"future"(33 MB);volare_realized_covariances_daily.parquet: the daily realized covariances of the 40 stocks (117 MB).
The files are licensed: Git ignores them, so they never end up on GitHub. The
datafolder already holds the public data of the first chapter, so everyone in the course works with the same numbers. When you are done, it looks like this:data/ ├── cpi_monthly.parquet consumer price index ├── crsp_monthly.parquet Absalon ├── factors_ff3_monthly.parquet Fama-French three factors ├── industries_ff_monthly.parquet ten industry portfolios ├── macro_predictors.parquet Welch and Goyal predictors ├── volare_realized_covariances_daily.parquet Absalon └── volare_realized_measures_daily.parquet AbsalonAlmost all exercises from now on start by reading data from these files. This is all it takes, from a fresh R or Python session:
import polars as pl
crsp_monthly = pl.read_parquet("data/crsp_monthly.parquet")- Plot the number of stocks in the CRSP sample over time, by listing exchange (NYSE, NYSE American, formerly AMEX, and Nasdaq).
Solutions: The book chapter WRDS, CRSP, and Compustat solves the figure in R and Python.