# 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("PLTR", "IBM", "BLK", "TSM", "SLB")

prices <- tq_get(x   = symbols, 
                 get = "stock.prices",
                 fro = "2012-12-31",
                 to  = "2024-12-31")

2 Convert prices to returns (monthly)

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"))

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

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "BLK"  "IBM"  "PLTR" "SLB"  "TSM"
weights <- c(0.4, 0.2, 0.2, 0.1, 0.1)
weights
## [1] 0.4 0.2 0.2 0.1 0.1
w_tbl <- tibble(symbols, weights)

4 Build a portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
    tq_portfolio(assets_col = asset, 
                 returns_col = returns,
                 weights = w_tbl, 
                 rebalance_on = "months")

5 Compute kurtosis

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

portfolio_kurtosis_tidyquant_builtin_percent
## # A tibble: 1 × 1
##   Kurtosis
##      <dbl>
## 1     4.46

6 Plot: Rolling kurtosis

mean_kurtosis_tbl <- asset_returns_tbl %>%
    
    group_by(asset) %>%
    summarise(mean = mean(returns),
              kurt = kurtosis(returns)) %>%
    ungroup() %>%
    
    add_row(portfolio_returns_tbl %>%
    summarise(mean = mean(portfolio.returns),
              kurt = kurtosis(portfolio.returns)) %>%
        mutate(asset = "Portfolio"))

mean_kurtosis_tbl %>%
    
    ggplot(aes(x = kurt, y = mean)) +
    geom_point() +
    ggrepel::geom_label_repel(aes(label = asset, color = asset)) +
    
    theme(legend.position = "none") +
    scale_y_continuous(labels = scales::percent_format(accuracy = 0.1)) +
    
    labs(x = "Kurt",
         y = "Expected Returns")

window = 24

rolling_kurtosis_tbl <- portfolio_returns_tbl %>%
    
    tq_mutate(select = portfolio.returns,
              mutate_fun = rollapply,
              width      = window,
              FUN        = kurtosis,
              col_rename = "Kurt") %>%
    
    na.omit() %>%
    select(-portfolio.returns)


rolling_kurtosis_tbl %>%
    
    ggplot(aes(x = date, y = Kurt)) +
    geom_line(color = "cornflowerblue") +
    
    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)) +
    
    labs(x = NULL,
         y  = "Kurtosis",
         title = paste0("Rolling ", window, "Month Kurtosis")) 

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 downside risk increased exponentially at the start of 2019. It quickly fell as we approached 2020. Once again, it began to sky rocket at the end of 2020 and start of 2021. This time the downside risk stayed above 4 for two years. At the end of 2023, it went down to previous levels that had not been seen since 2015.