## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4
)

## -----------------------------------------------------------------------------
library(PiChange)
library(ggplot2)

data("wti_oil")

event_dates <- as.Date(c("2003-01-01", "2005-08-26", "2006-12-23", "2008-07-01"))
penalty <- construct_penalty(
  time = wti_oil$date,
  centers = event_dates,
  width = 26 * 5,
  method = "mbic",
  family = "zag"
)
fit <- pi_change(wti_oil$abs_price_change, penalty, min_seg_len = 13 * 5)

change_indices <- changepoints(fit)
wti_oil[change_indices, c("date", "price", "abs_price_change")]

## ----fig.height=6-------------------------------------------------------------
plot(
  fit,
  ylab = "Absolute daily price change",
  title = "WTI oil price volatility with PI-Change estimates"
)

