## ----setup, include = FALSE--------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = FALSE ) ## ----load-packages------------------------------------------------------------ # library(EventStudy) # library(dplyr) # library(ggplot2) ## ----volume-data-------------------------------------------------------------- # firm_tbl <- tibble( # symbol = rep("FIRM_A", 300), # date = format(seq(as.Date("2014-01-01"), by = "day", length.out = 300), # "%d.%m.%Y"), # adjusted = 100 * cumprod(1 + rnorm(300, 0.0003, 0.015)), # firm_volume = pmax(0, rnorm(300, mean = 1e6, sd = 2e5)) # ) ## ----volume-setup------------------------------------------------------------- # set.seed(42) # # # Firm data with volume # n <- 300 # firm_tbl <- tibble( # symbol = "FIRM_A", # date = format(seq(as.Date("2014-06-01"), by = "day", # length.out = n), "%d.%m.%Y"), # adjusted = 100 * cumprod(1 + rnorm(n, 0.0003, 0.015)), # firm_volume = pmax(0, rnorm(n, mean = 1e6, sd = 2e5)) # ) # # # Index data # index_tbl <- tibble( # symbol = "INDEX_1", # date = firm_tbl$date, # adjusted = 1000 * cumprod(1 + rnorm(n, 0.0002, 0.012)) # ) # # # Request table # request_tbl <- tibble( # event_id = 1L, # firm_symbol = "FIRM_A", # index_symbol = "INDEX_1", # event_date = firm_tbl$date[200], # group = "Earnings", # event_window_start = -10L, # event_window_end = 10L, # shift_estimation_window = -11L, # estimation_window_length = 150L # ) ## ----volume-run--------------------------------------------------------------- # task <- EventStudyTask$new(firm_tbl, index_tbl, request_tbl) # # params <- ParameterSet$new( # return_calculation = SimpleReturn$new(), # return_model = VolumeModel$new(log_transform = TRUE), # single_event_statistics = SingleEventStatisticsSet$new(), # multi_event_statistics = MultiEventStatisticsSet$new() # ) # params$study_type <- "volume" # # task <- task |> # prepare_event_study(params) |> # fit_model(params) |> # calculate_statistics(params) ## ----volume-no-log------------------------------------------------------------ # vm <- VolumeModel$new(log_transform = FALSE) ## ----volume-results----------------------------------------------------------- # # Single-event results # task$get_ar(event_id = 1) # task$get_car(event_id = 1) # # # Tidy output # tidy.EventStudyTask(task, type = "ar") ## ----volume-plot, fig.width=7, fig.height=4----------------------------------- # plot_event_study(task, type = "ar", event_id = 1) + # labs(title = "Abnormal Volume Around Earnings Announcement", # y = "Abnormal Log-Volume") ## ----volatility-setup--------------------------------------------------------- # set.seed(123) # # n <- 300 # firm_tbl <- tibble( # symbol = "FIRM_A", # date = format(seq(as.Date("2014-06-01"), by = "day", # length.out = n), "%d.%m.%Y"), # adjusted = 100 * cumprod(1 + rnorm(n, 0.0003, 0.015)) # ) # # index_tbl <- tibble( # symbol = "INDEX_1", # date = firm_tbl$date, # adjusted = 1000 * cumprod(1 + rnorm(n, 0.0002, 0.012)) # ) # # request_tbl <- tibble( # event_id = 1L, # firm_symbol = "FIRM_A", # index_symbol = "INDEX_1", # event_date = firm_tbl$date[200], # group = "Earnings", # event_window_start = -10L, # event_window_end = 10L, # shift_estimation_window = -11L, # estimation_window_length = 150L # ) ## ----volatility-run----------------------------------------------------------- # task <- EventStudyTask$new(firm_tbl, index_tbl, request_tbl) # # params <- ParameterSet$new( # return_calculation = SimpleReturn$new(), # return_model = VolatilityModel$new(), # single_event_statistics = SingleEventStatisticsSet$new(), # multi_event_statistics = MultiEventStatisticsSet$new() # ) # params$study_type <- "volatility" # # task <- task |> # prepare_event_study(params) |> # fit_model(params) |> # calculate_statistics(params) ## ----volatility-results------------------------------------------------------- # task$get_ar(event_id = 1) # task$get_car(event_id = 1) # # tidy.EventStudyTask(task, type = "ar") ## ----volatility-plot, fig.width=7, fig.height=4------------------------------- # plot_event_study(task, type = "ar", event_id = 1) + # labs(title = "Abnormal Volatility Around Earnings Announcement", # y = "Abnormal Volatility (Variance Ratio - 1)") ## ----combined-study----------------------------------------------------------- # set.seed(42) # # n <- 300 # firm_tbl <- tibble( # symbol = "FIRM_A", # date = format(seq(as.Date("2014-06-01"), by = "day", # length.out = n), "%d.%m.%Y"), # adjusted = 100 * cumprod(1 + rnorm(n, 0.0003, 0.015)), # firm_volume = pmax(0, rnorm(n, mean = 1e6, sd = 2e5)) # ) # # index_tbl <- tibble( # symbol = "INDEX_1", # date = firm_tbl$date, # adjusted = 1000 * cumprod(1 + rnorm(n, 0.0002, 0.012)) # ) # # request_tbl <- tibble( # event_id = 1L, firm_symbol = "FIRM_A", index_symbol = "INDEX_1", # event_date = firm_tbl$date[200], group = "Earnings", # event_window_start = -10L, event_window_end = 10L, # shift_estimation_window = -11L, estimation_window_length = 150L # ) # # # --- Price study --- # task_price <- EventStudyTask$new(firm_tbl, index_tbl, request_tbl) # params_price <- ParameterSet$new(return_model = MarketModel$new()) # task_price <- run_event_study(task_price, params_price) # # # --- Volume study --- # task_vol <- EventStudyTask$new(firm_tbl, index_tbl, request_tbl) # params_vol <- ParameterSet$new(return_model = VolumeModel$new()) # params_vol$study_type <- "volume" # task_vol <- task_vol |> # prepare_event_study(params_vol) |> # fit_model(params_vol) |> # calculate_statistics(params_vol) # # # --- Volatility study --- # task_volat <- EventStudyTask$new(firm_tbl, index_tbl, request_tbl) # params_volat <- ParameterSet$new(return_model = VolatilityModel$new()) # params_volat$study_type <- "volatility" # task_volat <- task_volat |> # prepare_event_study(params_volat) |> # fit_model(params_volat) |> # calculate_statistics(params_volat) ## ----combined-plot, fig.width=7, fig.height=8--------------------------------- # # Extract tidy ARs from each study # ar_price <- tidy.EventStudyTask(task_price, type = "ar") %>% # mutate(study = "Price (AR)") # ar_vol <- tidy.EventStudyTask(task_vol, type = "ar") %>% # mutate(study = "Volume (Abnormal Log-Volume)") # ar_volat <- tidy.EventStudyTask(task_volat, type = "ar") %>% # mutate(study = "Volatility (Variance Ratio - 1)") # # combined <- bind_rows(ar_price, ar_vol, ar_volat) # # ggplot(combined, aes(x = relative_index, y = abnormal_returns)) + # geom_hline(yintercept = 0, linetype = "dashed", colour = "grey40") + # geom_vline(xintercept = 0, linetype = "dotted", colour = "red", alpha = 0.6) + # geom_col(fill = "steelblue", alpha = 0.7) + # facet_wrap(~ study, ncol = 1, scales = "free_y") + # labs( # title = "Price, Volume, and Volatility Event Study", # x = "Relative Time to Event", # y = "Abnormal Measure" # ) + # theme_minimal() ## ----diagnostics-------------------------------------------------------------- # # Volume model diagnostics # model_diagnostics(task_vol) # plot_diagnostics(task_vol, event_id = 1) # # # Volatility model diagnostics # model_diagnostics(task_volat) # plot_diagnostics(task_volat, event_id = 1) ## ----export------------------------------------------------------------------- # export_results(task_vol, "volume_results.csv") # export_results(task_vol, "volume_results.xlsx") # export_results(task_volat, "volatility_results.tex", which = c("model", "ar")) .