# 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("TSLA", "GOOG","MSFT", "AAPL")
prices <- tq_get(x = symbols,
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"))
# symbols
symbols <- asset_returns_tbl %>%
distinct(asset) %>%
pull()
symbols
## [1] "AAPL" "GOOG" "MSFT" "TSLA"
# 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 AAPL 0.25
## 2 GOOG 0.25
## 3 MSFT 0.25
## 4 TSLA 0.25
#?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months")
portfolio_returns_tbl
## # A tibble: 60 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-01-31 0.00997
## 2 2013-02-28 -0.00509
## 3 2013-03-28 0.0267
## 4 2013-04-30 0.134
## 5 2013-05-31 0.183
## 6 2013-06-28 -0.00804
## 7 2013-07-31 0.0707
## 8 2013-08-30 0.0795
## 9 2013-09-30 0.0358
## 10 2013-10-31 0.0317
## # … with 50 more rows
portfolio_kurt_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = table.Stats) %>%
select(Kurtosis)
portfolio_kurt_tidyquant_builtin_percent
## # A tibble: 1 × 1
## Kurtosis
## <dbl>
## 1 0.342
# Assign a value for window
window = 24
# Transform data: calculate 24 month rolling kurtosis
rolling_kurt_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_kurt_tbl
## # A tibble: 37 × 2
## date kurt
## <date> <dbl>
## 1 2014-12-31 0.722
## 2 2015-01-30 0.499
## 3 2015-02-27 0.448
## 4 2015-03-31 0.190
## 5 2015-04-30 0.503
## 6 2015-05-29 -0.984
## 7 2015-06-30 -1.01
## 8 2015-07-31 -0.933
## 9 2015-08-31 -0.962
## 10 2015-09-30 -0.965
## # … with 27 more rows
# 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("Downside risk plummetted
before July 2015, and has bagn a slow upward trend by 2018"))
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 of my portfolio has remained below -0.5 since 2015. It currently is slowly increasing, but not at a concerning rate. My portfolio is well versed with both more conservative investments such as GOOG and MSFT, and more risky stocks such as TSLA and AAPL. Investors should feel comfortable with a portfolio such as mine, as the downside risk has been steady since its rapid decline in 2015. The portfolio results have been trending between -0.5 and -1.0 which shows that there is less extreme outliers compared to normal distribution. As spoken about in Apply 7 assignment, TSLA seems to bring in more extreme positive returns compared to that of my portfolio. TSLA may report more extreme returns as its skewness is around .94. It is important to note, that goes both negatively and positively with extreme returns. Risk taking investors should see TSLA as a more risky investment because of their extreme rates of return. GOOG on the other hand, sees skewness below .8 around .78 compared to the portfolio skewness of around .87 . A conservative investor should see GOOG, and MSFT as a more preferred stocks than TSLA, and AAPL; especially when considering more extreme returns to that of the collective portfolio. If one is looking for a higher risk, higher reward stock, TSLA is the stock to choose. With a portfolio that shows fewer outliers with low kurtosis, and a moderately skewed portfolio bodes well for my portfolios risk.