# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize and compare skewness of your portfolio and its assets.
Choose your stocks.
from 2012-12-31 to 2017-12-31
symbols <- c("MSFT", "DPZ", "AAPL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2020-12-31",
to = "2025-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" "DPZ" "MSFT"
#weights
weights <- c(0.34, 0.33, 0.33)
weights
## [1] 0.34 0.33 0.33
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.34
## 2 DPZ 0.33
## 3 MSFT 0.33
# ?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: 54 × 2
## date returns
## <date> <dbl>
## 1 2021-01-29 0.000869
## 2 2021-02-26 -0.0492
## 3 2021-03-31 0.0278
## 4 2021-04-30 0.0928
## 5 2021-05-28 -0.0166
## 6 2021-06-30 0.0890
## 7 2021-07-30 0.0773
## 8 2021-08-31 0.0284
## 9 2021-09-30 -0.0725
## 10 2021-10-29 0.0812
## # ℹ 44 more rows
portfolio_skew_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Skewness)
portfolio_skew_tidyquant_builtin_percent
## # A tibble: 1 × 1
## Skewness
## <dbl>
## 1 -0.338
# Calculate sd of portfolio returns
sd_portfolio <- sd(portfolio_returns_tbl$returns)
mean_portfolio <- mean(portfolio_returns_tbl$returns)
portfolio_returns_tbl %>%
# Add a new variable
mutate(extreme_neg = ifelse(returns < mean_portfolio - 2 *sd_portfolio,
"ext_neg",
"not_ext_neg")) %>%
ggplot(aes(x = returns, fill = extreme_neg)) +
geom_histogram(binwidth = 0.003) +
scale_x_continuous(breaks = seq(-0.06,0.06,0.02)) +
labs(x = "monthly returns")
# Data transformation: calculate skewness
asset_skewness_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
summarise(skew = skewness(returns)) %>%
ungroup() %>%
# Add portfolio skewness
add_row(tibble(asset = "Portfolio",
skew = skewness(portfolio_returns_tbl$returns)))
asset_skewness_tbl
## # A tibble: 4 × 2
## asset skew
## <chr> <dbl>
## 1 AAPL 0.0722
## 2 DPZ -0.368
## 3 MSFT 0.155
## 4 Portfolio -0.338
# Plot skewness
asset_skewness_tbl %>%
ggplot(aes(x = asset, y = skew, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset),
data = asset_skewness_tbl %>%
filter(asset == "Portfolio")) +
labs(y = "skewness")
rolling_skew_tbl <- portfolio_returns_tbl %>%
tq_mutate(select = returns,
mutate_fun = rollapply,
width = 24,
FUN = skewness,
col_rename = "Skew") %>%
select(-returns) %>%
na.omit()
# Plot
rolling_skew_tbl %>%
ggplot(aes(x = date, y = Skew)) +
geom_line(color = "cornflowerblue") +
# Formatting
scale_y_continuous(limits = c(-1,1), breaks = seq(-1,1,0.2)) +
theme(plot.title = element_text(hjust = 0.5)) +
# Labelling
labs(y = "Skewness",
x = NULL,
title = "Rolling 24-Month Skewness") +
annotate(geom = "text",
x = as.Date("2020-12-01"), y = 0.8,
color = "red", size = 5,
label = str_glue("The 24 month rolling skewness is negative for about half of the lifetime, eventually though the overall skewness is positive"))
Is any asset in your portfolio more likely to return extreme positive returns than your portfolio collectively? Discuss in terms of skewness. You may also refer to the distribution of returns you plotted in Code along 4.
Yes, AAPL and MSFT, are more likely to skew positively than the portfolio.To a pretty drastic degree aswell, as the portfolio sits at about -0.3 and the MSFT and AAPL are in the positives.