datascience+

R tutorial

Stock market crashes in R: inflation turned an 8-year recovery into 14

Published
· today
Length
6 min
Views
26
By this author
37 articles

The stocks that American households own fell 53% between the end of 1972 and September 1974, the deepest drop since the quarterly record starts in 1952. In dollars their price was back at the 1972 level after 7.7 years. After inflation it took 14.0. The 2007-2009 crash was nearly as deep (47%), but inflation barely changed its recovery: 5.5 years against 5.8. I wanted to see what else differed between the big drops, using only data that US government agencies publish.

Stock prices from the Federal Reserve

The Federal Reserve Board’s Financial Accounts (release Z.1) report the market value of the stocks households and nonprofits hold, and separately the gain or loss from price changes each quarter. The price index for consumer spending (PCE), real GDP and after-tax corporate profits come from the U.S. Bureau of Economic Analysis (BEA). This runs on R 4.6.1 with readr 2.2.0 and dplyr 1.2.1:

library(tidyverse)
options(timeout = 300)                            # the BEA file is 36 MB

zip <- tempfile(fileext = ".zip")
download.file("https://www.federalreserve.gov/releases/z1/current/z1_csv_files.zip",
              zip, mode = "wb", quiet = TRUE)
fed <- read_csv(unz(zip, "csv/S1M_i_q.csv"), na = c("ND", "")) |>
  transmute(quarter = yq(date),
            value = LM153064105.Q,                # market value, end of quarter
            gain  = FR153064105.Q)                # change due to prices alone

txt <- tempfile(fileext = ".txt")
download.file("https://apps.bea.gov/national/Release/TXT/NipaDataQ.txt", txt, quiet = TRUE)
bea <- read_csv(txt, skip = 1, col_names = c("code", "quarter", "value"), col_types = "ccc") |>
  filter(code %in% c("DPCERG", "A191RX", "A551RC")) |>
  mutate(quarter = yq(quarter), value = parse_number(value)) |>
  pivot_wider(names_from = code, values_from = value) |>
  rename(prices = DPCERG, gdp = A191RX, profits = A551RC)

Dividing the gain by the value one quarter earlier gives a price return, without the money households put in or took out. Chaining those returns gives a price index:

stocks <- fed |>
  mutate(return = gain / lag(value)) |>
  filter(quarter >= "1952-01-01") |>              # annual data before that
  inner_join(bea, by = "quarter") |>
  mutate(dollars = cumprod(1 + replace(return, 1, 0)),
         real    = dollars / prices)

How do I calculate drawdowns in R?

Divide the series by its running maximum and subtract one: x / cummax(x) - 1. It is 0 at every new high and negative below it.

price <- c(100, 120, 90, 60, 110, 125)
round(price / cummax(price) - 1, 2)
## [1]  0.00  0.00 -0.25 -0.50 -0.08  0.00

Each zero starts a new spell, so cumsum(drawdown == 0) numbers the spells, and the lowest point inside each one is the crash. I keep those of 15% or more.

crashes <- stocks |>
  mutate(drawdown = dollars / cummax(dollars) - 1,
         spell    = cumsum(drawdown == 0)) |>
  summarise(peak = first(quarter), trough = quarter[which.min(drawdown)],
            dollars = min(drawdown), .by = spell) |>
  filter(dollars <= -0.15)

What happened to GDP, profits and prices in each crash?

For GDP and profits I take the worst fall from a previous high between the peak and one quarter after the trough. Profits are the BEA’s after-tax measure with inventory and depreciation adjustments. That matters in 1973-74, when inflation swelled unadjusted profits through the value of inventories.

at    <- \(q, col) stocks[[col]][match(q, stocks$quarter)]
worst <- \(from, to, col) {
  x <- stocks[[col]][between(stocks$quarter, from, to %m+% months(3))]
  min(x / cummax(x) - 1)
}

factors <- crashes |>
  mutate(real      = at(trough, "real") / at(peak, "real") - 1,
         inflation = at(trough, "prices") / at(peak, "prices") - 1,
         gdp       = map2_dbl(peak, trough, \(a, b) worst(a, b, "gdp")),
         profits   = map2_dbl(peak, trough, \(a, b) worst(a, b, "profits")))
label <- \(q) paste0(year(q), " Q", quarter(q))

factors |>
  transmute(Peak = label(peak), Trough = label(trough),
            `In dollars` = dollars, `After inflation` = real, Inflation = inflation,
            `Real GDP` = gdp, Profits = profits) |>
  mutate(across(where(is.numeric), \(x) scales::percent(x, accuracy = 0.1))) |>
  knitr::kable()
Peak Trough In dollars After inflation Inflation Real GDP Profits
1952 Q2 1953 Q3 -15.2% -16.8% 1.9% -2.1% -20.0%
1961 Q4 1962 Q2 -24.9% -25.5% 0.8% 0.0% 0.0%
1965 Q4 1966 Q3 -15.9% -17.9% 2.4% 0.0% -3.1%
1968 Q4 1970 Q2 -36.8% -41.0% 7.1% -0.6% -18.9%
1972 Q4 1974 Q3 -53.0% -59.7% 16.6% -1.9% -18.5%
1981 Q1 1982 Q2 -23.5% -28.7% 7.3% -2.6% -8.3%
1983 Q2 1984 Q2 -15.9% -19.2% 4.1% 0.0% 0.0%
1987 Q3 1987 Q4 -17.3% -18.0% 0.9% 0.0% 0.0%
1989 Q4 1990 Q3 -18.5% -21.4% 3.7% -0.9% -10.0%
2000 Q1 2002 Q3 -40.0% -42.6% 4.5% -0.4% -4.8%
2007 Q3 2009 Q1 -47.2% -48.0% 1.6% -4.0% -25.9%
2019 Q4 2020 Q1 -24.2% -24.5% 0.3% -9.1% -17.8%
2021 Q4 2022 Q3 -24.7% -28.3% 5.0% -0.3% -3.3%

The four drops deeper than 35% all came with falling real GDP, by as little as 0.4% (2000-02) and as much as 4.0% (2007-09). In four others GDP never fell, and those stopped at 25% or less. The pandemic quarter breaks the pattern in the other direction, with the largest fall in GDP in the table (9.1%) and a 24% drop in stocks.

Inflation is what sets 1972-74 apart. Consumer prices rose 17% while stocks were falling, which turns a 53% loss into 60%. In 2007-09 they rose 2%.

How long did the recovery take?

I count the years from the peak to the first quarter at or above the old level, once in dollars and once after inflation, for the four deepest drops.

years_back <- function(peak, col) {
  back <- stocks$quarter[stocks$quarter > peak & stocks[[col]] >= at(peak, col)]
  interval(peak, min(back)) / years(1)
}

recovery <- crashes |>
  filter(dollars < -0.35) |>
  mutate(dollars = map_dbl(peak, \(p) years_back(p, "dollars")),
         real    = map_dbl(peak, \(p) years_back(p, "real")))
recovery |>
  transmute(Peak = label(peak), `Years, in dollars` = round(dollars, 1),
            `Years, after inflation` = round(real, 1)) |>
  knitr::kable()
Peak Years, in dollars Years, after inflation
1968 Q4 3.7 18.0
1972 Q4 7.7 14.0
2000 Q1 5.5 6.0
2007 Q3 5.5 5.8

The 1968 row needs care. After inflation, stocks at the end of 1972 were 0.3% short of their 1968 level, so the 18.0 years there hang on that gap. A slightly different price index would split that spell in two. The 1972 figure does not depend on it.

dsp_colors <- c("#0066CC", "#E8862D", "#159A6C", "#7D5BD6",
                "#D64580", "#2AA9B8", "#C9A227")
dsp_theme <- theme_minimal(base_size = 13) +
  theme(plot.background    = element_rect(fill = "#ECECEF", color = NA),
        panel.background   = element_rect(fill = "#ECECEF", color = NA),
        panel.grid.minor   = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(color = "grey78"),
        axis.ticks         = element_blank(),
        plot.title         = element_text(face = "bold"),
        strip.text         = element_text(face = "bold", hjust = 0))

stocks |>
  select(quarter, `In dollars` = dollars, `After inflation` = real) |>
  pivot_longer(-quarter) |>
  mutate(drawdown = value / cummax(value) - 1, .by = name) |>
  ggplot(aes(quarter, drawdown, fill = fct_inorder(name))) +
  geom_area(show.legend = FALSE) +
  facet_wrap(~ fct_inorder(name), ncol = 1) +
  scale_fill_manual(values = dsp_colors[1:2]) +
  scale_x_date(breaks = as.Date(paste0(seq(1950, 2020, 10), "-01-01")), date_labels = "%Y") +
  scale_y_continuous(labels = scales::label_percent()) +
  labs(x = NULL, y = "Below the previous high") +
  dsp_theme
How long did the recovery take?: image from “Stock market crashes in R: inflation turned an 8-year recovery into 14”

These are the stocks households hold, including shares in closely held companies, so the numbers differ from any published stock index. They are measured at quarter ends, which misses the lowest days, and they leave out dividends, which would shorten every wait. This is an analysis of past data, not financial advice.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Read here often? Set us as a preferred source on Google