estimate_betas: Estimate Rolling Betas

View source: R/estimate_betas.R

estimate_betasR Documentation

Estimate Rolling Betas

Description

Estimates rolling betas for a given linear model using a fast, vectorized approach. Instead of fitting one regression per stock and estimation window, the function collapses the data to additive cumulants (the entries of the moment matrices X'X and X'y) per stock and period, aggregates these cumulants over rolling calendar windows in a single pass with slider::slide_index_sum(), and recovers the coefficients via the closed-form OLS solution \hat\beta = (X'X)^{-1} X'y. This produces estimates that are numerically identical to a per-window regression, but fast enough that no per-stock nesting or parallelization is required.

Usage

estimate_betas(data, model, lookback, min_obs = NULL, data_options = NULL)

Arguments

data

A data frame containing the data with a date identifier (defaults to date), a stock identifier (defaults to permno), and other variables used in the model.

model

A character string describing the model to be estimated (e.g., "ret_excess ~ mkt_excess + hml + smb").

lookback

A Period object specifying the number of months, days, hours, minutes, or seconds to look back when estimating the rolling model.

min_obs

An integer specifying the minimum number of observations required to estimate the model. Defaults to 80% of lookback. Windows with fewer observations are dropped from the output.

data_options

A list of class tidyfinance_data_options (created via data_options()) specifying column name mappings. The id is used to specify the entity (i.e., firm), and the date element is used to specify the date column. Uses data_options() default if NULL: "id" = "permno" and "date" = "date".

Value

A data frame with the estimated betas for each entity and period. It contains the entity and date identifiers, an intercept column (if the model includes one), and one ⁠beta_<variable>⁠ column per regressor.

See Also

Other estimation functions: estimate_fama_macbeth(), estimate_model()

Examples

# Estimate monthly betas using monthly return data
set.seed(1234)
data_monthly <- tibble::tibble(
  date = rep(seq.Date(from = as.Date("2020-01-01"),
                      to = as.Date("2020-12-01"), by = "month"), each = 50),
  permno = rep(1:50, times = 12),
  ret_excess = rnorm(600, 0, 0.1),
  mkt_excess = rnorm(600, 0, 0.1),
  smb = rnorm(600, 0, 0.1),
  hml = rnorm(600, 0, 0.1),
)

estimate_betas(data_monthly,  "ret_excess ~ mkt_excess", months(3))
estimate_betas(
  data_monthly,
  "ret_excess ~ mkt_excess + smb + hml",
  months(6)
)

data_monthly |>
  dplyr::rename(id = permno) |>
  estimate_betas("ret_excess ~ mkt_excess", months(3),
                 data_options = data_options(id = "id"))

# Estimate monthly betas using daily return data
data_daily <- tibble::tibble(
  date = rep(seq.Date(from = as.Date("2020-01-01"),
                      to = as.Date("2020-12-31"), by = "day"), each = 50),
  permno = rep(1:50, times = 366),
  ret_excess = rnorm(18300, 0, 0.02),
  mkt_excess = rnorm(18300, 0, 0.02),
  smb = rnorm(18300, 0, 0.02),
  hml = rnorm(18300, 0, 0.02),
)

data_daily <- data_daily |>
  dplyr::mutate(date = lubridate::floor_date(date, "month"))

estimate_betas(
  data_daily,
  "ret_excess ~ mkt_excess",
  lubridate::days(90)
)


tidyfinance documentation built on July 3, 2026, 1:09 a.m.