# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize and examine changes in the underlying trend in the downside risk of your portfolio in terms of kurtosis.
Choose your stocks.
from 2012-12-31 to present
symbols <-c("INTL", "NVDA", "MSFT", "AMD")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2018-12-31",
to = "2023-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
asset_returns_tbl
## # A tibble: 192 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AMD 2019-01-31 0.279
## 2 AMD 2019-02-28 -0.0367
## 3 AMD 2019-03-29 0.0812
## 4 AMD 2019-04-30 0.0794
## 5 AMD 2019-05-31 -0.00799
## 6 AMD 2019-06-28 0.103
## 7 AMD 2019-07-31 0.00263
## 8 AMD 2019-08-30 0.0323
## 9 AMD 2019-09-30 -0.0814
## 10 AMD 2019-10-31 0.157
## # ℹ 182 more rows
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMD" "INTL" "MSFT" "NVDA"
# weights
weights <- c(0.25, 0.25, 0.25, 0.25)
weights
## [1] 0.25 0.25 0.25 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
## symbols weights
## <chr> <dbl>
## 1 AMD 0.25
## 2 INTL 0.25
## 3 MSFT 0.25
## 4 NVDA 0.25
# ?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months",
col_rename = "returns")
portfolio_returns_tbl
## # A tibble: 60 × 2
## date returns
## <date> <dbl>
## 1 2019-01-31 0.0953
## 2 2019-02-28 0.0273
## 3 2019-03-29 0.0711
## 4 2019-04-30 0.0473
## 5 2019-05-31 -0.0869
## 6 2019-06-28 0.0937
## 7 2019-07-31 0.0117
## 8 2019-08-30 0.0103
## 9 2019-09-30 -0.00865
## 10 2019-10-31 0.0830
## # ℹ 50 more rows
portfolio_returns_tbl %>%
ggplot(aes(x = returns)) +
geom_histogram()
# Assing a value for window
window = 24
# Transform data: calculate 24 month rolling kurtosis
rolling_kurt_tbl <- portfolio_returns_tbl %>%
tq_mutate(select. = returns,
mutate_fun = rollapply,
width = window,
FUN = kurtosis,
col_rename = "kurt") %>%
na.omit() %>%
select(-returns)
# Plot
rolling_kurt_tbl %>%
ggplot(aes(x = date, y = kurt)) +
geom_line(color = "cornflowerblue") +
# Formatting
scale_y_continuous(breaks = seq(-1, 4, 0.5)) +
scale_x_date(breaks = scales::pretty_breaks(n = 7)) +
theme(plot.title = element_text(hjust = 0.5)) +
# Labeling
labs(x = NULL,
y = "Kurtosis",
title = paste0("My Portfolio Risk Has Become More Stable Over Time")) +
annotate(geom = "text", x = as.Date("2018-12-31", "2023-12-01"), y = 1,
size = 2, color = "red",
label = str_glue(""))
Has the downside risk of your portfolio increased or decreased over time? Explain using the plot you created. You may also refer to the skewness of the returns distribution you plotted in the previous assignment.
The kurtosis graph of my portfolio indicates changes in downside risk over time. Initially, the negative kurtosis suggests lower risk with fewer extreme returns. But around mid-2022, a peak in kurtosis indicates an increased risk in my portfolio due to the potential for more extreme outcomes. After this peak, the kurtosis decreases and remains negative, signaling a reduction in downside risk as the distribution of returns shows lighter tails. Therefore, my portfolio’s downside risk increased briefly in 2022 but has since declined, suggesting a more stable risk profile recently.