# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

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

1 Import stock prices

symbols <- c("NXPI", "IFX.DE", "AMD", "ON")

prices <- tq_get(
  x = symbols,
  get = "stock.prices",
  from = "2020-01-01",
  to = "2025-01-01"
)

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
  group_by(symbol) %>%
    
  tq_transmute(
    select       = adjusted,
    mutate_fun   = periodReturn,
    period       = "monthly",
    type         = "arithmetic",
    col_rename   = "returns") %>%
    
  slice(-1) %>%
    
  ungroup() %>%
    
  rename(asset = symbol)

3 Assign a weight to each asset (change the weigting scheme)

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull(asset)
symbols
## [1] "AMD"    "IFX.DE" "NXPI"   "ON"
# 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 IFX.DE     0.25
## 3 NXPI       0.25
## 4 ON         0.25

4 Build a 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: 63 × 2
##    date       returns
##    <date>       <dbl>
##  1 2020-02-28 -0.0895
##  2 2020-03-31 -0.222 
##  3 2020-04-30  0.226 
##  4 2020-05-29  0.0330
##  5 2020-06-30  0.119 
##  6 2020-07-31  0.138 
##  7 2020-08-31  0.0941
##  8 2020-09-30 -0.0114
##  9 2020-10-30  0.0372
## 10 2020-11-30  0.196 
## # ℹ 53 more rows

5 Compute kurtosis

portfolio_kurt_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
  
  tq_performance(
    Ra = returns,
    performance_fun = table.Stats
  ) %>%
  
  select(Kurtosis)

portfolio_kurt_tidyquant_builtin_percent
## # A tibble: 1 × 1
##   Kurtosis
##      <dbl>
## 1   -0.309
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <-
  mean(portfolio_returns_tbl$returns)

portfolio_mean_tidyquant_builtin_percent
## [1] 0.01989704

6 Plot: Rolling kurtosis

# Assign a value for window
window <- 24

# Calculate 24-month rolling kurtosis
rolling_kurt_tbl <- portfolio_returns_tbl %>%
    
    tq_mutate(
        select      = returns,
        mutate_fun  = rollapply,
        width       = window,
        FUN         = PerformanceAnalytics::kurtosis,
        align       = "right",
        col_rename  = "kurt"
    ) %>%
    
    na.omit() %>%
    select(-returns)

# Plot
rolling_kurt_tbl %>%
    
    ggplot(aes(x = date, y = kurt)) +
    geom_line(color = "cornflowerblue") +
    
    geom_hline(yintercept = 0, linetype = "dotted") +
    
    # Formatting
    scale_y_continuous(breaks = scales::pretty_breaks(n = 7)) +
    scale_x_date(date_breaks = "6 months", date_labels = "%b %Y") +
    theme(
        plot.title = element_text(hjust = 0.5),
        axis.text.x = element_text(angle = 45, hjust = 1)
    ) +
    
    # Labeling
    labs(
        x = NULL,
        y = "Excess Kurtosis",
        title = paste0("Rolling ", window, "-Month Portfolio Kurtosis"),
        subtitle = "NXPI, IFX.DE, AMD, and ON"
    )

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.

My portfolio’s downside risk generally decreased over time based on the rolling 24-month kurtosis plot. Kurtosis peaked around 1.8 near the beginning of 2022, then declined below zero toward the end of 2022 and reached approximately −1.3 in early 2024. It increased toward the end of 2024 but remained negative. This suggests that extreme returns became less pronounced in the later periods. However, kurtosis measures extreme returns in both directions, so the plot suggests lower overall tail risk rather than proving that the risk of losses decreased.