# Load packages
# Core
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
##
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
##
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
##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
symbol <- c("BIG", "TSLA", "AMZN", "WM", "PLUG")
prices <- tq_get(x = symbol,
get = "stock.prices",
from = "2012-12-31",
to = "2017-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 date returns
## "asset" "date" "returns"
symbols <- asset_returns_tbl %>% distinct(symbol) %>% pull()
symbols
## [1] "AMZN" "BIG" "PLUG" "TSLA" "WM"
weight <- c(0.2,0.2,0.2,0.2,0.2)
weight
## [1] 0.2 0.2 0.2 0.2 0.2
w_tbl <- tibble(symbols, weight)
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = symbol,
returns_col = monthly.returns,
weights = w_tbl,
rebalance_on = "months",
col_rename = "returns")
## Warning: `spread_()` was deprecated in tidyr 1.2.0.
## Please use `spread()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
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 2.46
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("Rolling " , window, " Month Kurtosis")) +
annotate(geom = "text",
x = as.Date("2016-07-01"),
y = 3,
size = 5,
color = "red",
label = str_glue("The risk is moderate and kurtosis shifts between -.7 and 1.2"))
#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. It decreased from 2015 to 2016 (from .5 to -.7) and then
went on to increase from 2016 to 2018 (-.7 to 1.2)