---
title: "Replication file for RDP 2021-04"
subtitle: "Monetary Policy Surprises"
output: 
  html_document:
    toc: true
    number_sections: true
    toc_float: 
      collapsed: false
    theme: united
    highlight: tango
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = F, message = F, fig.align = 'center', fig.dim = c(6, 5.6))
```

```{r}
library(lubridate)
library(tidyverse)
purrr::walk(list.files(here::here("R"), full.names = T), source)
```

This file will show how the monetary policy surprises were constructed using a _synthetic_ data series. 

The output from using the actual data can be found in _./data/mp-surprises/mp-surprises.csv_ and _./data/mp-surprises/mp-surprises_all.csv_

# Import (fake) OIS data

First import all (fake) OIS data. This contains OIS data for all available time points.

```{r}
ois_all <- readr::read_csv(here::here("data", "fake-data", "ois-all.csv"))
ois_all
```

From this object we need to extract the data around monetary policy events/announcements. To do this we must first import a series of monetary policy events/announcements.

```{r}
mp_dates <- readr::read_csv(here::here("data", "mp-surprises", "mp-dates.csv" ))

mp_board_dates <- mp_dates %>% filter(type == "board_meeting")
mp_board_dates
```

# Extract relevant data

Using the user defined function `data_window_extract` I feed through the `ois_all` object and ask it to extract the data around the `mp_board_dates$date_time`. The window is given through the arguments `pre` (minutes before), `post` (minutes after) and `open_window` (minutes after open if the monetary policy event falls between close and open)

```{r}
ois_mp <- data_window_extract(ois_all, mp_board_dates$date_time, pre = 30, post = 90, open_window = 60)

ois_mp
```

# Transform data 

Now we have the necessary data to create the measure of monetary policy surprises. First, for each tenor, I'll calculate the change in the OIS contract around a monetary policy announcement. 


```{r}
ois_surprise <- ois_mp %>% 
    select(-value) %>% # remove `value` column which contains actual date-time of extracted data point
    pivot_longer( starts_with("ois") ,  names_to = "tenor") %>%
    pivot_wider( names_from = name, values_from = value) %>%
    mutate(mp_change = post - pre) %>%  # calculate change in OIS around monetary policy announcement
    select(-pre, - post) %>% 
    pivot_wider(names_from = tenor, values_from = mp_change) 
ois_surprise
```


# Create monetary policy surprises

To create the measure of monetary policy surprises first take the `ois_surprise` data and perform the principal component analysis.

```{r}
pca_data <- ois_surprise %>% 
  select(-ois_1w, -ois_18m, -ois_2y, -ois_3y) # remove tenors not used in the analysis.

pca_data <- pca_data[ rowSums(is.na(pca_data)) == 0 , ] # remove row if it contains an NA

pca <- prcomp(pca_data %>% select(-date_time), center = T, scale. = T ) # perform pca

pca_surprise <-  bind_cols( decision_date_time = pca_data$date_time, 
                            decision_date = lubridate::date(pca_data$date_time),
                            as_tibble(pca$x)  ) # put in nice dataframe

```

As the factors from PCA are scaleless, I scale the PCA surprises to the change in the 1-month OIS. This allows the surprises to be interpreted as a 100 basis point change in the 1-month OIS. 


```{r}

mp_surprises_all <- left_join(pca_surprise, ois_surprise, by = c("decision_date_time" = "date_time" )) %>% 
                     mp_surprise_scale(., pc = "PC1", scale = "ois_1m") %>%  # scale PC1 by ois_1m
                      relocate(decision_date_time, decision_date, pc1_scaled)
mp_surprises_all
```

You can plot the synthetic monetary policy surprises as in Figure 2 in the RDP as follows:

```{r figure-2}

p <- ggplot(mp_surprises_all, aes(x = decision_date, y = pc1_scaled)) +
  geom_col() +
  rba_theme() +
  rba_syc() +
  ylab("%") +
  rba_ylim(-3, 3) +
  labs(title = "Figure 2: Monetary Policy Surprises", subtitle = "Factor 1, scaled to one-month OIS",
       caption = "Sources: RBA")  +
  rba_panel_text_size(title = 2, subtitle = 1.2) +
  scale_fill_rba()

rba_xticks_date(p) # add more x-ticks

```

# All monetary policy events

The same process can be done for to create the monetary policy surprises using all monetary policy communication dates. Simply expand the object `mp_board_dates` to include other dates outside the monetary policy announcement.

# Equity and earnings changes around monetary policy events

The construction of the files in _./data/equities/_ and _./data/earnings/_ are constructed in a similar manner to how the object `ois_mp` is constructed. I will therefore not replicate this process. 

# Session Information

```{r session-info}
sessionInfo()
```
